Skip to content

Commit

Permalink
Merge pull request #83 from ids-basecamp/feat/edc-create-process
Browse files Browse the repository at this point in the history
Feat/edc create process
  • Loading branch information
augustocmleal authored Jan 24, 2024
2 parents c24e7c4 + de43fd6 commit a04ca83
Show file tree
Hide file tree
Showing 20 changed files with 604 additions and 69 deletions.
10 changes: 10 additions & 0 deletions clearing-house-edc/core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,14 @@ tasks.jacocoTestReport {
xml.required = true
}
dependsOn(tasks.test)
classDirectories.setFrom(
files(classDirectories.files.map {
fileTree(it) {
exclude(
"**/dto/**",
"**/types/clearinghouse/*",
"**/types/ids/*")
}
})
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2023 truzzt GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* truzzt GmbH - Initial implementation
*
*/
package de.truzzt.clearinghouse.edc.app.delegate;

import de.truzzt.clearinghouse.edc.dto.*;
import de.truzzt.clearinghouse.edc.types.TypeManagerUtil;
import de.truzzt.clearinghouse.edc.types.clearinghouse.Context;
import de.truzzt.clearinghouse.edc.types.clearinghouse.Header;
import de.truzzt.clearinghouse.edc.types.clearinghouse.SecurityToken;
import de.truzzt.clearinghouse.edc.types.clearinghouse.TokenFormat;
import okhttp3.ResponseBody;

public class CreateProcessDelegate implements AppSenderDelegate<CreateProcessResponse> {

private final TypeManagerUtil typeManagerUtil;

public CreateProcessDelegate(TypeManagerUtil typeManagerUtil) {
this.typeManagerUtil = typeManagerUtil;
}

public String buildRequestUrl(String baseUrl, HandlerRequest handlerRequest) {
return baseUrl + "/process/" + handlerRequest.getPid();
}

public CreateProcessRequest buildRequestBody(HandlerRequest handlerRequest) {
var header = handlerRequest.getHeader();

var multipartContext = header.getContext();
var context = new Context(multipartContext.getIds(), multipartContext.getIdsc());

var multipartSecurityToken = header.getSecurityToken();
var multipartTokenFormat = multipartSecurityToken.getTokenFormat();
var securityToken = SecurityToken.Builder.newInstance().
type(multipartSecurityToken.getType()).
id(multipartSecurityToken.getId()).
tokenFormat(new TokenFormat(multipartTokenFormat.getId())).
tokenValue(multipartSecurityToken.getTokenValue()).
build();

var requestHeader = Header.Builder.newInstance()
.context(context)
.id(header.getId())
.type(header.getType())
.securityToken(securityToken)
.issuerConnector(header.getIssuerConnector())
.modelVersion(header.getModelVersion())
.issued(header.getIssued())
.senderAgent(header.getSenderAgent())
.build();

return new CreateProcessRequest(requestHeader, handlerRequest.getPayload());
}

@Override
public CreateProcessResponse parseResponseBody(ResponseBody responseBody) {
return typeManagerUtil.parse(responseBody.byteStream(), CreateProcessResponse.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2023 truzzt GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* truzzt GmbH - Initial implementation
*
*/
package de.truzzt.clearinghouse.edc.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import de.truzzt.clearinghouse.edc.types.clearinghouse.Header;
import org.jetbrains.annotations.NotNull;

public class CreateProcessRequest {

@JsonProperty("header")
@NotNull
private final Header header;

@JsonProperty("payload")
@NotNull
private final String payload;

public CreateProcessRequest(@NotNull Header header, @NotNull String payload) {
this.header = header;
this.payload = payload;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2023 truzzt GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* truzzt GmbH - Initial implementation
*
*/
package de.truzzt.clearinghouse.edc.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.jetbrains.annotations.NotNull;

public class CreateProcessResponse {

@JsonProperty("pid")
@NotNull
private String pid;

public String getPid() {
return pid;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import de.truzzt.clearinghouse.edc.dto.HandlerResponse;
import de.truzzt.clearinghouse.edc.types.TypeManagerUtil;
import org.eclipse.edc.protocol.ids.spi.types.IdsId;
import org.eclipse.edc.spi.monitor.Monitor;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.jetbrains.annotations.NotNull;

Expand All @@ -32,22 +31,17 @@

public class LogMessageHandler implements Handler {

private final Monitor monitor;
private final IdsId connectorId;
private final TypeManagerUtil typeManagerUtil;
private final AppSender appSender;
private final LoggingMessageDelegate senderDelegate;

private final ServiceExtensionContext context;

public LogMessageHandler(Monitor monitor,
IdsId connectorId,
public LogMessageHandler(IdsId connectorId,
TypeManagerUtil typeManagerUtil,
AppSender appSender,
ServiceExtensionContext context) {
this.monitor = monitor;
this.connectorId = connectorId;
this.typeManagerUtil = typeManagerUtil;
this.appSender = appSender;
this.context = context;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2023 truzzt GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* truzzt GmbH - Initial implementation
*
*/
package de.truzzt.clearinghouse.edc.handler;

import de.truzzt.clearinghouse.edc.app.AppSender;
import de.truzzt.clearinghouse.edc.app.delegate.CreateProcessDelegate;
import de.truzzt.clearinghouse.edc.dto.AppSenderRequest;
import de.truzzt.clearinghouse.edc.dto.HandlerRequest;
import de.truzzt.clearinghouse.edc.dto.HandlerResponse;
import de.truzzt.clearinghouse.edc.types.TypeManagerUtil;
import org.eclipse.edc.protocol.ids.spi.types.IdsId;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.jetbrains.annotations.NotNull;

import static de.truzzt.clearinghouse.edc.util.ResponseUtil.createMultipartResponse;
import static de.truzzt.clearinghouse.edc.util.ResponseUtil.messageProcessedNotification;
import static de.truzzt.clearinghouse.edc.util.SettingsConstants.APP_BASE_URL_DEFAULT_VALUE;
import static de.truzzt.clearinghouse.edc.util.SettingsConstants.APP_BASE_URL_SETTING;

public class RequestMessageHandler implements Handler {

private final IdsId connectorId;
private final AppSender appSender;
private final CreateProcessDelegate senderDelegate;

private final ServiceExtensionContext context;

public RequestMessageHandler(IdsId connectorId,
TypeManagerUtil typeManagerUtil,
AppSender appSender,
ServiceExtensionContext context) {
this.connectorId = connectorId;
this.appSender = appSender;
this.context = context;

this.senderDelegate = new CreateProcessDelegate(typeManagerUtil);
}

@Override
public boolean canHandle(@NotNull HandlerRequest handlerRequest) {
return handlerRequest.getHeader().getType().equals("ids:RequestMessage");
}

@Override
public @NotNull HandlerResponse handleRequest(@NotNull HandlerRequest handlerRequest) {
var baseUrl = context.getSetting(APP_BASE_URL_SETTING, APP_BASE_URL_DEFAULT_VALUE);
var header = handlerRequest.getHeader();

var url = senderDelegate.buildRequestUrl(baseUrl, handlerRequest);
var token = buildJWTToken(handlerRequest.getHeader().getSecurityToken(), context);
var body = senderDelegate.buildRequestBody(handlerRequest);

var request = AppSenderRequest.Builder.newInstance().url(url).token(token).body(body).build();

var response = appSender.send(request, senderDelegate);
return createMultipartResponse(messageProcessedNotification(header, connectorId), response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package de.truzzt.clearinghouse.edc.types.ids;

import com.fasterxml.jackson.annotation.JsonProperty;
import de.truzzt.clearinghouse.edc.types.ids.util.VocabUtil;
import de.truzzt.clearinghouse.edc.types.util.VocabUtil;
import org.jetbrains.annotations.NotNull;

import java.net.URI;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

public class TokenFormat {

public static final String JWT_TOKEN_FORMAT = "idsc:JWT";
public static final String JWT_TOKEN_FORMAT_IDS = "idsc:JWT";
public static final String JWT_TOKEN_FORMAT_DSP = "https://w3id.org/idsa/code/JWT";

@JsonProperty("@id")
@NotNull
Expand All @@ -30,4 +31,8 @@ public class TokenFormat {
public URI getId() {
return id;
}

public static boolean isValid(String id) {
return id.equals(JWT_TOKEN_FORMAT_IDS) || id.equals(JWT_TOKEN_FORMAT_DSP);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* truzzt GmbH - EDC extension implementation
*
*/
package de.truzzt.clearinghouse.edc.types.ids.util;
package de.truzzt.clearinghouse.edc.types.util;

import java.net.MalformedURLException;
import java.net.URI;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import okhttp3.ResponseBody;
import org.eclipse.edc.protocol.ids.spi.types.IdsId;
import org.eclipse.edc.spi.EdcException;
import org.eclipse.edc.spi.http.EdcHttpClient;
import org.eclipse.edc.spi.monitor.Monitor;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -28,8 +26,6 @@
import static org.mockito.Mockito.spy;

class LogMessageHandlerTest {
@Mock
private Monitor monitor;
@Mock
private IdsId connectorId;
@Mock
Expand All @@ -49,7 +45,7 @@ class LogMessageHandlerTest {
public void setUp() {
MockitoAnnotations.openMocks(this);
senderDelegate = spy(new LoggingMessageDelegate(typeManagerUtil));
logMessageHandler = spy(new LogMessageHandler(monitor, connectorId, typeManagerUtil, appSender, context));
logMessageHandler = spy(new LogMessageHandler(connectorId, typeManagerUtil, appSender, context));
}

@Test
Expand Down
Loading

0 comments on commit a04ca83

Please sign in to comment.