use of de.fraunhofer.iais.eis.Message 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.Message in project DataSpaceConnector by eclipse-dataspaceconnector.
the class MultipartContractAgreementSender method getResponseContent.
@Override
protected MultipartMessageProcessedResponse getResponseContent(IdsMultipartParts parts) throws Exception {
Message header = getObjectMapper().readValue(parts.getHeader(), Message.class);
String payload = null;
if (parts.getPayload() != null) {
payload = new String(parts.getPayload().readAllBytes());
}
return MultipartMessageProcessedResponse.Builder.newInstance().header(header).payload(payload).build();
}
use of de.fraunhofer.iais.eis.Message in project DataSpaceConnector by eclipse-dataspaceconnector.
the class DescriptionHandlerTest method testHandleRequestOfTypeDescriptionRequestMessage.
@Test
void testHandleRequestOfTypeDescriptionRequestMessage() {
DescriptionRequestMessage requestHeader = mock(DescriptionRequestMessage.class);
Message responseHeader = mock(Message.class);
MultipartResponse response = MultipartResponse.Builder.newInstance().header(responseHeader).build();
when(requestHeader.getRequestedElement()).thenReturn(null);
MultipartRequest multipartRequest = MultipartRequest.Builder.newInstance().header(requestHeader).build();
var claimToken = ClaimToken.Builder.newInstance().build();
when(connectorDescriptionRequestHandler.handle(requestHeader, claimToken, multipartRequest.getPayload())).thenReturn(response);
var result = descriptionHandler.handleRequest(multipartRequest, claimToken);
assertThat(result).isNotNull();
assertThat(result).extracting(MultipartResponse::getHeader).isEqualTo(responseHeader);
verify(connectorDescriptionRequestHandler).handle(requestHeader, claimToken, multipartRequest.getPayload());
}
use of de.fraunhofer.iais.eis.Message 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.Message in project DataSpaceConnector by eclipse-dataspaceconnector.
the class MultipartArtifactRequestSender method getResponseContent.
@Override
protected MultipartRequestInProcessResponse getResponseContent(IdsMultipartParts parts) throws Exception {
Message header = getObjectMapper().readValue(parts.getHeader(), Message.class);
String payload = null;
if (parts.getPayload() != null) {
payload = new String(parts.getPayload().readAllBytes());
}
if (header instanceof RequestInProcessMessage) {
// TODO Update TransferProcess State Machine
} else {
// TODO Update TransferProcess State Machine
}
return MultipartRequestInProcessResponse.Builder.newInstance().header(header).payload(payload).build();
}
Aggregations