Skip to content

Commit

Permalink
Refactoring: avoid redundancy in message response
Browse files Browse the repository at this point in the history
  • Loading branch information
ununhexium committed Jun 14, 2024
1 parent e315337 commit 908a6db
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import de.sovity.edc.extension.custommessages.api.MessageHandlerRegistry;
import de.sovity.edc.extension.custommessages.api.SovityMessenger;
import de.sovity.edc.extension.custommessages.controller.CustomMessageReceiverController;
import de.sovity.edc.extension.custommessages.impl.JsonObjectFromGenericSovityMessage;
import de.sovity.edc.extension.custommessages.impl.JsonObjectFromSovityMessageRequest;
import de.sovity.edc.extension.custommessages.impl.JsonObjectFromSovityMessageResponse;
import de.sovity.edc.extension.custommessages.impl.MessageEmitter;
import de.sovity.edc.extension.custommessages.impl.MessageHandlerRegistryImpl;
import de.sovity.edc.extension.custommessages.impl.MessageReceiver;
Expand Down Expand Up @@ -65,7 +66,6 @@ public void initialize(ServiceExtensionContext context) {
context.registerService(MessageHandlerRegistry.class, handlers);
setupSovityCustomEmitter(context, objectMapper);
setupSovityCustomReceiver(objectMapper, handlers);
typeTransformerRegistry.register(new JsonObjectFromGenericSovityMessage());
}

private void setupSovityCustomReceiver(ObjectMapper objectMapper, MessageHandlerRegistry handlers) {
Expand All @@ -78,6 +78,8 @@ private void setupSovityCustomReceiver(ObjectMapper objectMapper, MessageHandler
handlers);

webService.registerResource(dspApiConfiguration.getContextAlias(), receiver);

typeTransformerRegistry.register(new JsonObjectFromSovityMessageResponse());
}

private void setupSovityCustomEmitter(ServiceExtensionContext context, ObjectMapper objectMapper) {
Expand All @@ -86,6 +88,8 @@ private void setupSovityCustomEmitter(ServiceExtensionContext context, ObjectMap

dspHttpRemoteMessageDispatcher.registerMessage(SovityMessageRequest.class, factory, delegate);

typeTransformerRegistry.register(new JsonObjectFromSovityMessageRequest());

val postOffice = new SovityMessengerImpl(registry, objectMapper);
context.registerService(SovityMessenger.class, postOffice);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import de.sovity.edc.extension.custommessages.api.MessageHandlerRegistry;
import de.sovity.edc.extension.custommessages.api.SovityMessage;
import de.sovity.edc.extension.custommessages.api.SovityMessageApi;
import de.sovity.edc.extension.custommessages.impl.SovityMessageRequest;
import de.sovity.edc.extension.custommessages.impl.SovityMessageResponse;
import de.sovity.edc.utils.JsonUtils;
import de.sovity.edc.utils.jsonld.JsonLdUtils;
import de.sovity.edc.utils.jsonld.vocab.Prop;
Expand All @@ -32,8 +32,6 @@
import java.io.StringReader;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.UUID;

@RequiredArgsConstructor
Expand Down Expand Up @@ -85,7 +83,7 @@ public Response post(
var errorCode = UUID.randomUUID();
monitor.warning(String.format("Error transforming negotiation, error id %s: %s", errorCode, failure.getFailureDetail()));
return DspErrorResponse
.type(Prop.SovityMessageExt.MESSAGE)
.type(Prop.SovityMessageExt.REQUEST)
.internalServerError();
});
}
Expand All @@ -98,21 +96,20 @@ private static String extractMessageType(JsonObject compacted) {
return messageType;
}

private SovityMessageRequest processMessage(JsonObject compacted, MessageHandlerRegistry.Handler<Object, Object> handler) {
private SovityMessageResponse processMessage(JsonObject compacted, MessageHandlerRegistry.Handler<Object, Object> handler) {
try {

val bodyStr = compacted.getString(Prop.SovityMessageExt.BODY);
val parsed = mapper.readValue(bodyStr, handler.clazz());
val result = handler.handler().apply(parsed);
val resultBody = mapper.writeValueAsString(result);

val response = new SovityMessageRequest(
new URL("https://example.com"), // TODO: the return type doesn't need any URL
val response = new SovityMessageResponse(
buildOkHeader(handler.clazz()),
resultBody);

return response;
} catch (JsonProcessingException | MalformedURLException e) {
} catch (JsonProcessingException e) {
throw new EdcException("Failed to process the message", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;

public class JsonObjectFromGenericSovityMessage extends AbstractJsonLdTransformer<SovityMessageRequest, JsonObject> {
public class JsonObjectFromSovityMessageRequest extends AbstractJsonLdTransformer<SovityMessageRequest, JsonObject> {

public JsonObjectFromGenericSovityMessage() {
public JsonObjectFromSovityMessageRequest() {
super(SovityMessageRequest.class, JsonObject.class);
}

Expand All @@ -22,7 +22,7 @@ public JsonObjectFromGenericSovityMessage() {
@NotNull TransformerContext context) {

var builder = Json.createObjectBuilder();
builder.add(TYPE, Prop.SovityMessageExt.MESSAGE)
builder.add(TYPE, Prop.SovityMessageExt.REQUEST)
.add(Prop.SovityMessageExt.HEADER, message.header())
.add(Prop.SovityMessageExt.BODY, message.body());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package de.sovity.edc.extension.custommessages.impl;

import de.sovity.edc.utils.jsonld.vocab.Prop;
import jakarta.json.Json;
import jakarta.json.JsonObject;
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;

public class JsonObjectFromSovityMessageResponse extends AbstractJsonLdTransformer<SovityMessageResponse, JsonObject> {

public JsonObjectFromSovityMessageResponse() {
super(SovityMessageResponse.class, JsonObject.class);
}

@Override
public @Nullable JsonObject transform(
@NotNull SovityMessageResponse message,
@NotNull TransformerContext context) {

var builder = Json.createObjectBuilder();
builder.add(TYPE, Prop.SovityMessageExt.RESPONSE)
.add(Prop.SovityMessageExt.HEADER, message.header())
.add(Prop.SovityMessageExt.BODY, message.body());

return builder.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package de.sovity.edc.extension.custommessages.impl;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import de.sovity.edc.utils.jsonld.vocab.Prop;

import java.net.URL;

public record SovityMessageResponse(
@JsonProperty(Prop.SovityMessageExt.HEADER)
String header,

@JsonProperty(Prop.SovityMessageExt.BODY)
String body
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.sovity.edc.extension.custommessages.api.SovityMessage;
import de.sovity.edc.extension.custommessages.impl.JsonObjectFromGenericSovityMessage;
import de.sovity.edc.extension.custommessages.impl.JsonObjectFromSovityMessageRequest;
import de.sovity.edc.extension.custommessages.impl.JsonObjectFromSovityMessageResponse;
import de.sovity.edc.extension.custommessages.impl.MessageHandlerRegistryImpl;
import de.sovity.edc.extension.custommessages.impl.ObjectMapperFactory;
import de.sovity.edc.extension.custommessages.impl.SovityMessageRequest;
Expand Down Expand Up @@ -70,7 +71,8 @@ public String getType() {
@BeforeEach
public void beforeEach() {
transformers = new TypeTransformerRegistryImpl();
transformers.register(new JsonObjectFromGenericSovityMessage());
transformers.register(new JsonObjectFromSovityMessageRequest());
transformers.register(new JsonObjectFromSovityMessageResponse());

monitor = new ConsoleMonitor();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class MessageEmitterTest {
void emmitValidMessage_whenEmpty_shouldSucceed() throws IOException {
// arrange
TypeTransformerRegistry registry = new TypeTransformerRegistryImpl();
registry.register(new JsonObjectFromGenericSovityMessage());
registry.register(new JsonObjectFromSovityMessageRequest());
JsonLdRemoteMessageSerializer serializer = new JsonLdRemoteMessageSerializerImpl(
registry,
mapperFactory.createObjectMapper(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ public class HttpDatasourceHints {
@UtilityClass
public class SovityMessageExt {
public final String CTX = "https://semantic.sovity.io/message/generic/";
public final String MESSAGE = CTX + "message";
public final String REQUEST = CTX + "request";
public final String RESPONSE = CTX + "response";
public final String ERROR_MESSAGE = CTX + "errorMessage";
public final String HEADER = CTX + "header";
public final String BODY = CTX + "body";
Expand Down

0 comments on commit 908a6db

Please sign in to comment.