use of de.fraunhofer.iais.eis.ContractRequestMessage in project IDS-Messaging-Services by International-Data-Spaces-Association.
the class MultipartResponseConverter method convertResponse.
/**
* Converts a Response into a corresponding MessageAndPayload Object.
*
* @param responseMap Response in a Map.
* @return MessageAndPayload containing the corresponding Message and the payload parsed
* in Infomodel classes.
* @throws UnknownResponseException If message or payload cannot be parsed.
* @throws DeserializeException If problems occur deseralizing the message.
*/
public MessageAndPayload<?, ?> convertResponse(final Map<String, String> responseMap) throws UnknownResponseException, DeserializeException {
// The return param
MessageAndPayload<?, ?> messageAndPayload = null;
// The response "message", set in try catch
Message responseHeader;
final var responsePayload = getResponsePayload(responseMap);
try {
// Can throw IOException if Header can not be deserialized
responseHeader = getResponseHeader(responseMap);
} catch (IOException ioException) {
if (log.isErrorEnabled()) {
log.error("Error deserializing Header! [code=(IMSMEE0023)," + " exception=({})]", ioException.getMessage());
}
throw new DeserializeException(ioException);
}
try {
if (responseHeader instanceof DescriptionRequestMessage) {
messageAndPayload = getDescriptionRequestMessage(responseHeader);
} else if (responseHeader instanceof DescriptionResponseMessage) {
messageAndPayload = getDescriptionResponseMessage(responseHeader, responsePayload);
} else if (responseHeader instanceof MessageProcessedNotificationMessage) {
messageAndPayload = getMessageProcessedNotificationMessage(responseHeader);
} else if (responseHeader instanceof RejectionMessage) {
messageAndPayload = getRejectionMessage(responseHeader, responsePayload);
} else if (responseHeader instanceof ArtifactRequestMessage) {
messageAndPayload = getArtifactRequestMessage(responseHeader);
} else if (responseHeader instanceof ArtifactResponseMessage) {
messageAndPayload = getArtifactResponseMessage(responseHeader, responsePayload);
} else if (responseHeader instanceof ConnectorUpdateMessage) {
messageAndPayload = getConnectorUpdateMessage(responseHeader, responsePayload);
} else if (responseHeader instanceof ConnectorUnavailableMessage) {
messageAndPayload = getConnectorUnavailableMessage(responseHeader);
} else if (responseHeader instanceof QueryMessage) {
messageAndPayload = getQueryMessage(responseHeader, responsePayload);
} else if (responseHeader instanceof ResultMessage) {
messageAndPayload = getResultMessage(responseHeader, responsePayload);
} else if (responseHeader instanceof ResourceUpdateMessage) {
messageAndPayload = getResourceUpdateMessage(responseHeader, responsePayload);
} else if (responseHeader instanceof ResourceUnavailableMessage) {
messageAndPayload = getResourceUnavailableMessage(responseHeader);
} else if (responseHeader instanceof ParticipantUpdateMessage) {
messageAndPayload = getParticipantUpdateMessage(responseHeader, responsePayload);
} else if (responseHeader instanceof ParticipantUnavailableMessage) {
messageAndPayload = getParticipantUnavailableMessage(responseHeader);
} else if (responseHeader instanceof ParticipantRequestMessage) {
messageAndPayload = getParticipantRequestMessage(responseHeader);
} else if (responseHeader instanceof ContractOfferMessage) {
messageAndPayload = getContractOfferMessage(responseHeader, responsePayload);
} else if (responseHeader instanceof ContractRequestMessage) {
messageAndPayload = getContractRequestMessage(responseHeader, responsePayload);
} else if (responseHeader instanceof ContractAgreementMessage) {
messageAndPayload = getContractAgreementMessage(responseHeader, responsePayload);
} else if (responseHeader instanceof ContractResponseMessage) {
messageAndPayload = getContractResponseMessage(responseHeader, responsePayload);
}
if (messageAndPayload != null) {
// Match found, returning messageAndPayload
return messageAndPayload;
} else {
// No match found, throw UnknownResponseException
if (log.isErrorEnabled()) {
log.error("Could not convert input header to suitable responseHeader" + " and payload type! [code=(IMSMEE0024)]");
}
throw new UnknownResponseException("Could not convert input header to suitable responseHeader and payload" + " type. Header: " + responseHeader.toRdf());
}
} catch (IOException ioException) {
// Deserializing Payload threw exception
throw new DeserializeException(ioException);
}
}
use of de.fraunhofer.iais.eis.ContractRequestMessage in project DataSpaceConnector by eclipse-dataspaceconnector.
the class ContractRequestHandler method handleRequest.
@Override
@Nullable
public MultipartResponse handleRequest(@NotNull MultipartRequest multipartRequest, @NotNull ClaimToken claimToken) {
Objects.requireNonNull(multipartRequest);
Objects.requireNonNull(claimToken);
var message = (ContractRequestMessage) multipartRequest.getHeader();
ContractRequest contractRequest;
try {
contractRequest = objectMapper.readValue(multipartRequest.getPayload(), ContractRequest.class);
} catch (IOException e) {
monitor.severe("ContractRequestHandler: Contract Request is invalid", e);
return createBadParametersErrorMultipartResponse(message);
}
var idsWebhookAddress = message.getProperties().get(IDS_WEBHOOK_ADDRESS_PROPERTY);
if (idsWebhookAddress == null || idsWebhookAddress.toString().isBlank()) {
var msg = "Ids webhook address is invalid";
monitor.debug(String.format("ContractRequestHandler: %s", msg));
return createBadParametersErrorMultipartResponse(message, msg);
}
// extract target from contract request
var permission = contractRequest.getPermission().stream().findFirst().orElse(null);
if (permission == null) {
monitor.debug("ContractRequestHandler: Contract Request is invalid");
return createBadParametersErrorMultipartResponse(message);
}
var target = permission.getTarget();
if (target == null || String.valueOf(target).isBlank()) {
monitor.debug("ContractRequestHandler: Contract Request is invalid");
return createBadParametersErrorMultipartResponse(message);
}
// search for matching asset
var assetId = IdsIdParser.parse(String.valueOf(target));
var asset = assetIndex.findById(assetId.getValue());
if (asset == null) {
var msg = "Target id is invalid";
monitor.debug(String.format("ContractRequestHandler: %s", msg));
return createBadParametersErrorMultipartResponse(message, msg);
}
// Create contract offer request
var input = ContractTransformerInput.Builder.newInstance().contract(contractRequest).asset(asset).build();
Result<ContractOffer> result = transformerRegistry.transform(input, ContractOffer.class);
if (result.failed()) {
monitor.debug(String.format("Could not transform contract request: [%s]", String.join(", ", result.getFailureMessages())));
return createBadParametersErrorMultipartResponse(message);
}
var contractOffer = result.getContent();
var requestObj = ContractOfferRequest.Builder.newInstance().protocol(Protocols.IDS_MULTIPART).connectorAddress(idsWebhookAddress.toString()).type(ContractOfferRequest.Type.INITIAL).connectorId(String.valueOf(message.getIssuerConnector())).correlationId(String.valueOf(message.getTransferContract())).contractOffer(contractOffer).build();
Message response;
try {
response = responseMessageFactory.createRequestInProcessMessage(message);
} catch (Exception e) {
if (e instanceof InvalidCorrelationMessageException) {
monitor.debug(String.format("Rejecting invalid IDS contract request message [Msg-ID: %s]", message.getId()), e);
} else {
monitor.severe(String.format("Exception while creating IDS RequestInProcessMessage to answer contract request [Msg-ID: %s]", message.getId()), e);
}
response = responseMessageFactory.createRejectionMessage(message, e);
}
if (response instanceof RequestInProcessMessage) {
// Start negotiation process
negotiationManager.requested(claimToken, requestObj);
}
return MultipartResponse.Builder.newInstance().header(response).build();
}
use of de.fraunhofer.iais.eis.ContractRequestMessage in project DataspaceConnector by International-Data-Spaces-Association.
the class ContractRequestServiceTest method buildMessage_validDesc_returnValidMessage.
@Test
public void buildMessage_validDesc_returnValidMessage() {
/* ARRANGE */
final var desc = new ContractRequestMessageDesc();
desc.setRecipient(URI.create("https://recipient"));
desc.setTransferContract(URI.create("https://transferContract"));
final var connectorId = URI.create("https://connector");
final var modelVersion = "4.0.0";
final var token = new DynamicAttributeTokenBuilder()._tokenFormat_(TokenFormat.OTHER)._tokenValue_("").build();
Mockito.when(connectorService.getConnectorId()).thenReturn(connectorId);
Mockito.when(connectorService.getOutboundModelVersion()).thenReturn(modelVersion);
Mockito.when(connectorService.getCurrentDat()).thenReturn(token);
/* ACT */
final var result = (ContractRequestMessage) requestService.buildMessage(desc);
/* ASSERT */
assertEquals(1, result.getRecipientConnector().size());
assertEquals(desc.getRecipient(), result.getRecipientConnector().get(0));
assertEquals(desc.getTransferContract(), result.getTransferContract());
assertEquals(connectorId, result.getIssuerConnector());
assertEquals(modelVersion, result.getModelVersion());
assertEquals(token, result.getSecurityToken());
}
use of de.fraunhofer.iais.eis.ContractRequestMessage in project DataSpaceConnector by eclipse-dataspaceconnector.
the class ContractRequestHandler method handleRequest.
@Override
@Nullable
public MultipartResponse handleRequest(@NotNull MultipartRequest multipartRequest, @NotNull Result<ClaimToken> verificationResult) {
Objects.requireNonNull(multipartRequest);
Objects.requireNonNull(verificationResult);
var message = (ContractRequestMessage) multipartRequest.getHeader();
ContractRequest contractRequest;
try {
contractRequest = objectMapper.readValue(multipartRequest.getPayload(), ContractRequest.class);
} catch (IOException e) {
monitor.severe("ContractRequestHandler: Contract Request is invalid", e);
return createBadParametersErrorMultipartResponse(message);
}
var idsWebhookAddress = message.getProperties().get(IDS_WEBHOOK_ADDRESS_PROPERTY);
if (idsWebhookAddress == null || idsWebhookAddress.toString().isBlank()) {
var msg = "Ids webhook address is invalid";
monitor.debug(String.format("ContractRequestHandler: %s", msg));
return createBadParametersErrorMultipartResponse(message, msg);
}
// extract target from contract request
var permission = contractRequest.getPermission().get(0);
if (permission == null) {
monitor.debug("ContractRequestHandler: Contract Request is invalid");
return createBadParametersErrorMultipartResponse(message);
}
var target = permission.getTarget();
if (target == null || String.valueOf(target).isBlank()) {
monitor.debug("ContractRequestHandler: Contract Request is invalid");
return createBadParametersErrorMultipartResponse(message);
}
// search for matching asset
var assetId = IdsIdParser.parse(String.valueOf(target));
var asset = assetIndex.findById(assetId.getValue());
if (asset == null) {
var msg = "Target id is invalid";
monitor.debug(String.format("ContractRequestHandler: %s", msg));
return createBadParametersErrorMultipartResponse(message, msg);
}
// Create contract offer request
var input = ContractTransformerInput.Builder.newInstance().contract(contractRequest).asset(asset).build();
Result<ContractOffer> result = transformerRegistry.transform(input, ContractOffer.class);
if (result.failed()) {
monitor.debug(String.format("Could not transform contract request: [%s]", String.join(", ", result.getFailureMessages())));
return createBadParametersErrorMultipartResponse(message);
}
var contractOffer = result.getContent();
var requestObj = ContractOfferRequest.Builder.newInstance().protocol(Protocols.IDS_MULTIPART).connectorAddress(idsWebhookAddress.toString()).type(ContractOfferRequest.Type.INITIAL).connectorId(String.valueOf(message.getIssuerConnector())).correlationId(String.valueOf(message.getTransferContract())).contractOffer(contractOffer).build();
// Start negotiation process
negotiationManager.requested(verificationResult.getContent(), requestObj);
return MultipartResponse.Builder.newInstance().header(ResponseMessageUtil.createRequestInProcessMessage(connectorId, message)).build();
}
Aggregations