Skip to content

Commit

Permalink
extract RequestProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
ndr-brt committed Jul 31, 2023
1 parent 0d9c9df commit 08425b3
Show file tree
Hide file tree
Showing 50 changed files with 482 additions and 1,172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@

package org.eclipse.edc.protocol.dsp.catalog.dispatcher;

import org.eclipse.edc.jsonld.spi.JsonLd;
import org.eclipse.edc.protocol.dsp.catalog.dispatcher.delegate.CatalogRequestHttpDelegate;
import org.eclipse.edc.catalog.spi.CatalogRequestMessage;
import org.eclipse.edc.catalog.spi.DatasetRequestMessage;
import org.eclipse.edc.protocol.dsp.catalog.dispatcher.delegate.CatalogRequestHttpRawDelegate;
import org.eclipse.edc.protocol.dsp.catalog.dispatcher.delegate.DatasetRequestHttpRawDelegate;
import org.eclipse.edc.protocol.dsp.dispatcher.GetDspHttpRequestFactory;
import org.eclipse.edc.protocol.dsp.dispatcher.PostDspHttpRequestFactory;
import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpRemoteMessageDispatcher;
import org.eclipse.edc.protocol.dsp.spi.serialization.JsonLdRemoteMessageSerializer;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.types.TypeManager;
import org.eclipse.edc.transform.spi.TypeTransformerRegistry;

import static org.eclipse.edc.spi.CoreConstants.JSON_LD;
import static org.eclipse.edc.protocol.dsp.catalog.dispatcher.CatalogApiPaths.BASE_PATH;
import static org.eclipse.edc.protocol.dsp.catalog.dispatcher.CatalogApiPaths.CATALOG_REQUEST;
import static org.eclipse.edc.protocol.dsp.catalog.dispatcher.CatalogApiPaths.DATASET_REQUEST;

/**
* Creates and registers the HTTP dispatcher delegate for sending a catalog request as defined in
Expand All @@ -42,12 +44,6 @@ public class DspCatalogHttpDispatcherExtension implements ServiceExtension {
private DspHttpRemoteMessageDispatcher messageDispatcher;
@Inject
private JsonLdRemoteMessageSerializer remoteMessageSerializer;
@Inject
private TypeManager typeManager;
@Inject
private TypeTransformerRegistry transformerRegistry;
@Inject
private JsonLd jsonLdService;

@Override
public String name() {
Expand All @@ -56,10 +52,16 @@ public String name() {

@Override
public void initialize(ServiceExtensionContext context) {
var mapper = typeManager.getMapper(JSON_LD);
messageDispatcher.registerDelegate(new CatalogRequestHttpDelegate(remoteMessageSerializer, mapper, transformerRegistry, jsonLdService));
messageDispatcher.registerDelegate(new CatalogRequestHttpRawDelegate(remoteMessageSerializer));
messageDispatcher.registerDelegate(new DatasetRequestHttpRawDelegate(remoteMessageSerializer));
messageDispatcher.registerMessage(
CatalogRequestMessage.class,
new PostDspHttpRequestFactory<>(remoteMessageSerializer, m -> BASE_PATH + CATALOG_REQUEST),
new CatalogRequestHttpRawDelegate()
);
messageDispatcher.registerMessage(
DatasetRequestMessage.class,
new GetDspHttpRequestFactory<>(m -> BASE_PATH + DATASET_REQUEST + "/" + m.getDatasetId()),
new DatasetRequestHttpRawDelegate()
);
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,22 @@
package org.eclipse.edc.protocol.dsp.catalog.dispatcher.delegate;

import jakarta.json.JsonObject;
import okhttp3.Request;
import okhttp3.Response;
import org.eclipse.edc.catalog.spi.CatalogRequestMessage;
import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate;
import org.eclipse.edc.protocol.dsp.spi.serialization.JsonLdRemoteMessageSerializer;
import org.eclipse.edc.spi.EdcException;

import java.io.IOException;
import java.util.function.Function;

import static org.eclipse.edc.protocol.dsp.catalog.dispatcher.CatalogApiPaths.BASE_PATH;
import static org.eclipse.edc.protocol.dsp.catalog.dispatcher.CatalogApiPaths.CATALOG_REQUEST;

/**
* Delegate for dispatching catalog requests as defined in the
* <a href="https://docs.internationaldataspaces.org/dataspace-protocol/catalog/catalog.binding.https">dataspace protocol specification</a>
*/
public class CatalogRequestHttpRawDelegate extends DspHttpDispatcherDelegate<CatalogRequestMessage, byte[]> {

public CatalogRequestHttpRawDelegate(JsonLdRemoteMessageSerializer serializer) {
super(serializer);
}

@Override
public Class<CatalogRequestMessage> getMessageType() {
return CatalogRequestMessage.class;
}

/**
* Sends a catalog request. The request body is constructed as defined in the dataspace protocol
* implementation. The request is sent to the remote component using the path from the
* specification.
*
* @param message the message
* @return the built okhttp request
*/
@Override
public Request buildRequest(CatalogRequestMessage message) {
return buildPostRequest(message, BASE_PATH + CATALOG_REQUEST);
public CatalogRequestHttpRawDelegate() {
super();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,51 +15,22 @@
package org.eclipse.edc.protocol.dsp.catalog.dispatcher.delegate;

import jakarta.json.JsonObject;
import okhttp3.HttpUrl;
import okhttp3.Request;
import okhttp3.Response;
import org.eclipse.edc.catalog.spi.DatasetRequestMessage;
import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate;
import org.eclipse.edc.protocol.dsp.spi.serialization.JsonLdRemoteMessageSerializer;
import org.eclipse.edc.spi.EdcException;

import java.io.IOException;
import java.util.function.Function;

import static org.eclipse.edc.protocol.dsp.catalog.dispatcher.CatalogApiPaths.BASE_PATH;
import static org.eclipse.edc.protocol.dsp.catalog.dispatcher.CatalogApiPaths.DATASET_REQUEST;

/**
* Delegate for dispatching catalog requests as defined in the
* <a href="https://docs.internationaldataspaces.org/dataspace-protocol/catalog/catalog.binding.https">dataspace protocol specification</a>
*/
public class DatasetRequestHttpRawDelegate extends DspHttpDispatcherDelegate<DatasetRequestMessage, byte[]> {

public DatasetRequestHttpRawDelegate(JsonLdRemoteMessageSerializer serializer) {
super(serializer);
}

@Override
public Class<DatasetRequestMessage> getMessageType() {
return DatasetRequestMessage.class;
}

/**
* Sends a catalog request. The request body is constructed as defined in the dataspace protocol
* implementation. The request is sent to the remote component using the path from the
* specification.
*
* @param message the message
* @return the built okhttp request
*/
@Override
public Request buildRequest(DatasetRequestMessage message) {
var url = HttpUrl.get(message.getCounterPartyAddress() + BASE_PATH + DATASET_REQUEST + "/" + message.getDatasetId());

return new Request.Builder()
.url(url)
.get()
.build();
public DatasetRequestHttpRawDelegate() {
super();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@
import org.eclipse.edc.catalog.spi.CatalogRequestMessage;
import org.eclipse.edc.protocol.dsp.spi.dispatcher.DspHttpDispatcherDelegate;
import org.eclipse.edc.protocol.dsp.spi.testfixtures.dispatcher.DspHttpDispatcherDelegateTestBase;
import org.eclipse.edc.spi.query.QuerySpec;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.edc.protocol.dsp.catalog.dispatcher.CatalogApiPaths.BASE_PATH;
import static org.eclipse.edc.protocol.dsp.catalog.dispatcher.CatalogApiPaths.CATALOG_REQUEST;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -37,22 +34,7 @@ class CatalogRequestHttpRawDelegateTest extends DspHttpDispatcherDelegateTestBas

@BeforeEach
void setUp() {
delegate = new CatalogRequestHttpRawDelegate(serializer);
}

@Test
void getMessageType_returnCatalogRequest() {
assertThat(delegate.getMessageType()).isEqualTo(CatalogRequestMessage.class);
}

@Test
void buildRequest_returnRequest() throws IOException {
testBuildRequest_shouldReturnRequest(message(), BASE_PATH + CATALOG_REQUEST);
}

@Test
void buildRequest_serializationFails_throwException() {
testBuildRequest_shouldThrowException_whenSerializationFails(message());
delegate = new CatalogRequestHttpRawDelegate();
}

@Test
Expand All @@ -79,11 +61,4 @@ void parseResponse_responseBodyNull_throwException() {
return delegate;
}

private CatalogRequestMessage message() {
return CatalogRequestMessage.Builder.newInstance()
.counterPartyAddress("http://connector")
.protocol("protocol")
.querySpec(QuerySpec.max())
.build();
}
}
Loading

0 comments on commit 08425b3

Please sign in to comment.