Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SDK to Connector V5 #4

Merged
merged 17 commits into from
Sep 13, 2024
Merged
69 changes: 18 additions & 51 deletions src/main/java/eu/enmeshed/EnmeshedOnboardingService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package eu.enmeshed;

import static java.util.List.of;
import static java.util.Objects.isNull;

import eu.enmeshed.client.EnmeshedClient;
import eu.enmeshed.model.AttributeWrapper;
import eu.enmeshed.model.ContentWrapper;
Expand All @@ -16,9 +13,7 @@
import eu.enmeshed.model.relationshipTemplates.RelationshipTemplateContent;
import eu.enmeshed.model.relationshipTemplates.RelationshipTemplateCreation;
import eu.enmeshed.model.relationships.Relationship;
import eu.enmeshed.model.relationships.RelationshipChange;
import eu.enmeshed.model.relationships.RelationshipChangeRequestContent;
import eu.enmeshed.model.relationships.RelationshipCreationChangeRequestContent;
import eu.enmeshed.model.relationships.RelationshipCreationContent;
import eu.enmeshed.model.requestItems.CreateAttributeRequestItem;
import eu.enmeshed.model.requestItems.ReadAttributeRequestItem;
import eu.enmeshed.model.requestItems.RequestItem;
Expand All @@ -33,18 +28,17 @@
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class EnmeshedOnboardingService {

private static final Long QR_CODE_VALIDITY_MINUTES_DEFAULT = 60L;
private static final Integer QR_CODE_NUMBER_OF_ALLOCATIONS = 1;

Expand All @@ -65,11 +59,10 @@ public EnmeshedOnboardingService(
String connectorDisplayName,
List<Class<? extends AttributeValue>> requiredAttributes,
List<Class<? extends AttributeValue>> optionalAttributes) {

this.enmeshedClient = enmeshedClient;
this.requiredAttributes = requiredAttributes;
this.optionalAttributes = optionalAttributes;
this.createAttributes = of();
this.createAttributes = List.of();

// Get IdentifyInfo
identityInfo = enmeshedClient.getIdentityInfo().getResult();
Expand All @@ -84,7 +77,6 @@ public EnmeshedOnboardingService(
List<Class<? extends AttributeValue>> requiredAttributes,
List<Class<? extends AttributeValue>> optionalAttributes,
List<Class<? extends RequestItem>> createAttributes) {

this.enmeshedClient = enmeshedClient;
this.requiredAttributes = requiredAttributes;
this.optionalAttributes = optionalAttributes;
Expand All @@ -98,13 +90,8 @@ public EnmeshedOnboardingService(
}

private AttributeWrapper setupConnectorDisplayName(String connectorDisplayName) {

ResultWrapper<List<AttributeWrapper>> foundAttributes =
enmeshedClient.searchAttributes(
IdentityAttribute.class.getSimpleName(),
identityInfo.getAddress(),
DisplayName.class.getSimpleName());

enmeshedClient.searchAttributes(DisplayName.class.getSimpleName());
Optional<AttributeWrapper> displayNameAttribute =
foundAttributes.getResult().stream()
.filter(attribute -> attribute.getContent().getValue() instanceof DisplayName)
Expand All @@ -130,7 +117,6 @@ private AttributeWrapper setupConnectorDisplayName(String connectorDisplayName)

// Attribute not found - Create it!
IdentityAttribute identityAttribute = new IdentityAttribute();
identityAttribute.setOwner(identityInfo.getAddress());
identityAttribute.setValue(DisplayName.builder().value(connectorDisplayName).build());

AttributeWrapper createdDisplayNameAttribute =
Expand All @@ -152,14 +138,12 @@ public RegistrationData generateQrCodeForRegistrationAsJpg(
String displayTextSharedAttributes,
String displayTextCreateAttributes,
Long qrCodeValidityMinutes) {

RelationshipTemplate relationshipTemplate =
createOnboardingRelationshipTemplate(
displayTextRequestedAttributes,
displayTextSharedAttributes,
displayTextCreateAttributes,
qrCodeValidityMinutes);

Response qrCodeResponse =
enmeshedClient.getQrCodeForRelationshipTemplate(relationshipTemplate.getId());

Expand Down Expand Up @@ -216,56 +200,37 @@ public RegistrationResult checkRegistrationState(
}

Relationship relationship = relationships.get(0);
RelationshipChange relationshipChange = relationship.getChanges().get(0);

RelationshipChangeRequestContent relationshipChangeRequestContent =
relationshipChange.getRequest().getContent();

RelationshipCreationChangeRequestContent relationshipCreationChangeRequestContent =
(RelationshipCreationChangeRequestContent) relationshipChangeRequestContent;

RelationshipCreationContent creationContent = relationship.getCreationContent();
Map<Class<? extends AttributeValue>, AttributeValue> attributes =
getSharedSimpleAttributesFromResponseItems(
relationshipCreationChangeRequestContent.getResponse().getItems());
getSharedSimpleAttributesFromResponseItems(creationContent.getResponse().getItems());

if (relationshipChange.getStatus() == RelationshipChange.Status.PENDING) {
if (relationship.getStatus() == Relationship.Status.PENDING) {
boolean decision = acceptanceDecider.test(attributes);

if (decision) {
enmeshedClient.acceptRelationshipChange(
relationship.getId(),
relationshipChange.getId(),
ContentWrapper.containing(Collections.emptyMap()));
enmeshedClient.acceptRelationship(relationship.getId());
} else {
enmeshedClient.rejectRelationshipChange(
relationship.getId(),
relationshipChange.getId(),
ContentWrapper.containing(Collections.emptyMap()));
enmeshedClient.rejectRelationship(relationship.getId());
}

return checkRegistrationState(relationshipTemplateId, registrationResult -> decision);
} else if (relationshipChange.getStatus() == RelationshipChange.Status.ACCEPTED) {

} else if (creationContent.getResponse().getResult()
jkoenig134 marked this conversation as resolved.
Show resolved Hide resolved
== eu.enmeshed.model.Response.Result.ACCEPTED) {
// Request was accepted by User and us - Get the send Attributes and return them
return new RegistrationResult(
attributes,
relationships.get(0).getPeerIdentity().getAddress(),
relationshipChange.getId(),
relationship.getId(),
true);

} else if (relationshipChange.getStatus() == RelationshipChange.Status.REJECTED) {

} else if (creationContent.getResponse().getResult()
== eu.enmeshed.model.Response.Result.REJECTED) {
// Request was accepted by User and us - Get the send Attributes and return them
return new RegistrationResult(
attributes,
relationships.get(0).getPeerIdentity().getAddress(),
relationshipChange.getId(),
relationship.getId(),
false);
} else {

// Unknown Relationship State - This shouldn't happen.
return null;
}
}
Expand Down Expand Up @@ -310,7 +275,8 @@ private RelationshipTemplate createOnboardingRelationshipTemplate(
RequestItemGroup.builder()
.title(displayTextSharedAttributes)
.mustBeAccepted(true)
jkoenig134 marked this conversation as resolved.
Show resolved Hide resolved
.items(of(ShareAttributeRequestItem.fromWrapper(connectorDisplayNameAttribute, true)))
.items(
List.of(ShareAttributeRequestItem.fromWrapper(connectorDisplayNameAttribute, true)))
.build();

RequestItemGroup requestedAttributesGroup =
Expand Down Expand Up @@ -351,7 +317,9 @@ private RelationshipTemplate createOnboardingRelationshipTemplate(
.build();

Long qrCodeValidityTime =
isNull(qrCodeValidityMinutes) ? QR_CODE_VALIDITY_MINUTES_DEFAULT : qrCodeValidityMinutes;
Objects.isNull(qrCodeValidityMinutes)
? QR_CODE_VALIDITY_MINUTES_DEFAULT
: qrCodeValidityMinutes;

RelationshipTemplateCreation relationShipTemplateCreation =
RelationshipTemplateCreation.builder()
Expand All @@ -369,7 +337,6 @@ public record RegistrationData(
public record RegistrationResult(
Map<Class<? extends AttributeValue>, AttributeValue> attributes,
String enmeshedAddress,
String relationshipChangeId,
String relationshipId,
boolean accepted) {}
}
35 changes: 15 additions & 20 deletions src/main/java/eu/enmeshed/client/EnmeshedClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import java.util.List;

public interface EnmeshedClient {

ObjectMapper objectMapper =
new ObjectMapper()
.registerModule(new JavaTimeModule())
Expand All @@ -47,13 +46,11 @@ public interface EnmeshedClient {
.setDateFormat(new StdDateFormat().withColonInTimeZone(true));

static EnmeshedClient configure(String url, String apiKey) {

return configure(url, apiKey, new Request.Options(), Logger.Level.NONE);
}

static EnmeshedClient configure(
String url, String apiKey, Request.Options options, Logger.Level loggerLevel) {

return Feign.builder()
.decoder(new JacksonDecoder(objectMapper))
.encoder(new FormEncoder(new JacksonEncoder(objectMapper)))
Expand All @@ -77,16 +74,17 @@ static EnmeshedClient configure(
/*
Attributes
*/
@RequestLine("GET /api/v2/Attributes?content.@type={0}&content.owner={1}&content.value.@type={2}")
jkoenig134 marked this conversation as resolved.
Show resolved Hide resolved
ResultWrapper<List<AttributeWrapper>> searchAttributes(
@Param("0") String contentType,
@Param("1") String contentOwner,
@Param("2") String contentValueType);
@RequestLine("GET /api/v2/Attributes?content.value.@type={2}")
ResultWrapper<List<AttributeWrapper>> searchAttributes(@Param("2") String contentValueType);

@RequestLine("POST /api/v2/Attributes")
@Headers("Content-Type: application/json")
@Headers({"Content-Type: application/json"})
ResultWrapper<AttributeWrapper> createAttribute(ContentWrapper<Attribute> attribute);

@RequestLine("GET /api/v2/Attributes/{id}")
@Headers("Content-Type: application/json")
ResultWrapper<AttributeWrapper> getAttributeById(@Param("id") String attributeId);

/*
Relationship Templates
*/
Expand All @@ -111,19 +109,16 @@ ResultWrapper<QrCode> createRelationshipQrCode(
ResultWrapper<List<Relationship>> searchRelationships(
@Param("0") String templateId, @Param("1") String peer, @Param("2") String status);

@RequestLine("PUT /api/v2/Relationships/{0}/Changes/{1}/Accept")
@RequestLine("GET /api/v2/Relationships/{id}")
ResultWrapper<Relationship> getRelationshipById(@Param("id") String id);

@RequestLine("PUT /api/v2/Relationships/{id}/Accept")
@Headers("Content-Type: application/json")
ResultWrapper<Relationship> acceptRelationshipChange(
@Param("0") String relationshipId,
@Param("1") String changeId,
ContentWrapper<Object> dummyBody);
ResultWrapper<Relationship> acceptRelationship(@Param("id") String id);

@RequestLine("PUT /api/v2/Relationships/{0}/Changes/{1}/Reject")
@RequestLine("PUT /api/v2/Relationships/{0}/Reject")
@Headers("Content-Type: application/json")
ResultWrapper<Relationship> rejectRelationshipChange(
@Param("0") String relationshipId,
@Param("1") String changeId,
ContentWrapper<Object> dummyBody);
ResultWrapper<Relationship> rejectRelationship(@Param("id") String id);

/*
Messages
Expand Down Expand Up @@ -165,7 +160,7 @@ ResultWrapper<LocalRequest> acceptIncomingRequestById(
*/
@Retryable
@RequestLine("POST /api/v2/Files/Own")
@Headers({"Content-Type: multipart/form-data", "accept: application/json"})
@Headers({"Content-Type: multipart/form-data"})
ResultWrapper<FileMetaData> uploadNewOwnFile(FileUploadRequest fileUploadRequest);

@Retryable
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/eu/enmeshed/model/AttributeWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@
@Setter
@SuperBuilder
public class AttributeWrapper extends ContentWrapper<Attribute> {

private String id;

private ZonedDateTime createdAt;

private AttributeShareInfo shareInfo;

private String succeededBy;

private String succeeds;
private Boolean isDefault;
}
13 changes: 8 additions & 5 deletions src/main/java/eu/enmeshed/model/attributes/Attribute.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package eu.enmeshed.model.attributes;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import eu.enmeshed.model.attributes.values.AttributeValue;
Expand All @@ -21,15 +24,15 @@
@Getter
@Setter
@SuperBuilder
public abstract class Attribute {
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Attribute {
@JsonProperty("@type")
private String type;

private String owner;

private List<String> tags;

private ZonedDateTime validFrom;

private ZonedDateTime validTo;

private AttributeValue value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
@Setter
@SuperBuilder
public abstract class SimpleStringAttributeValue extends AttributeValue {

private String value;

@Override
public String toString() {
return getValue();
}
}
21 changes: 21 additions & 0 deletions src/main/java/eu/enmeshed/model/messaging/Notification.java
jkoenig134 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package eu.enmeshed.model.messaging;

import com.fasterxml.jackson.annotation.JsonProperty;
import eu.enmeshed.model.notificationItem.NotificationItem;
import java.util.List;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;

@NoArgsConstructor
@Getter
@Setter
@SuperBuilder
public class Notification extends MessageContent {
jkoenig134 marked this conversation as resolved.
Show resolved Hide resolved
@JsonProperty("@type")
private String type;

private String id;
private List<NotificationItem> items;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package eu.enmeshed.model.relationships;
package eu.enmeshed.model.notificationItem;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -10,9 +11,13 @@

@Data
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "@type")
@JsonSubTypes({@JsonSubTypes.Type(RelationshipCreationChangeRequestContent.class)})
@JsonSubTypes({@JsonSubTypes.Type(PeerSharedAttributeSucceededNotificationItem.class)})
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@SuperBuilder
public abstract class RelationshipChangeRequestContent {}
public abstract class NotificationItem {
private String predecessorId;
jkoenig134 marked this conversation as resolved.
Show resolved Hide resolved
private String successorId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package eu.enmeshed.model.notificationItem;

import eu.enmeshed.model.attributes.IdentityAttribute;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;

@NoArgsConstructor
@Getter
@Setter
@SuperBuilder
public class PeerSharedAttributeSucceededNotificationItem extends NotificationItem {
private IdentityAttribute successorContent;
jkoenig134 marked this conversation as resolved.
Show resolved Hide resolved
}
Loading