Search in sources :

Example 1 with Message

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);
    }
}
Also used : QueryMessage(de.fraunhofer.iais.eis.QueryMessage) ParticipantRequestMessage(de.fraunhofer.iais.eis.ParticipantRequestMessage) ContractRequestMessage(de.fraunhofer.iais.eis.ContractRequestMessage) ParticipantRequestMessage(de.fraunhofer.iais.eis.ParticipantRequestMessage) ContractRequestMessage(de.fraunhofer.iais.eis.ContractRequestMessage) ParticipantUpdateMessage(de.fraunhofer.iais.eis.ParticipantUpdateMessage) ArtifactResponseMessage(de.fraunhofer.iais.eis.ArtifactResponseMessage) ParticipantUnavailableMessage(de.fraunhofer.iais.eis.ParticipantUnavailableMessage) ResourceUpdateMessage(de.fraunhofer.iais.eis.ResourceUpdateMessage) ArtifactRequestMessage(de.fraunhofer.iais.eis.ArtifactRequestMessage) DescriptionRequestMessage(de.fraunhofer.iais.eis.DescriptionRequestMessage) RejectionMessage(de.fraunhofer.iais.eis.RejectionMessage) ContractRejectionMessage(de.fraunhofer.iais.eis.ContractRejectionMessage) ContractOfferMessage(de.fraunhofer.iais.eis.ContractOfferMessage) QueryMessage(de.fraunhofer.iais.eis.QueryMessage) Message(de.fraunhofer.iais.eis.Message) ContractResponseMessage(de.fraunhofer.iais.eis.ContractResponseMessage) ContractAgreementMessage(de.fraunhofer.iais.eis.ContractAgreementMessage) MessageProcessedNotificationMessage(de.fraunhofer.iais.eis.MessageProcessedNotificationMessage) ConnectorUpdateMessage(de.fraunhofer.iais.eis.ConnectorUpdateMessage) ConnectorUnavailableMessage(de.fraunhofer.iais.eis.ConnectorUnavailableMessage) ResultMessage(de.fraunhofer.iais.eis.ResultMessage) DescriptionResponseMessage(de.fraunhofer.iais.eis.DescriptionResponseMessage) ResourceUnavailableMessage(de.fraunhofer.iais.eis.ResourceUnavailableMessage) ResourceUpdateMessage(de.fraunhofer.iais.eis.ResourceUpdateMessage) ConnectorUnavailableMessage(de.fraunhofer.iais.eis.ConnectorUnavailableMessage) RejectionMessage(de.fraunhofer.iais.eis.RejectionMessage) ContractRejectionMessage(de.fraunhofer.iais.eis.ContractRejectionMessage) ParticipantUpdateMessage(de.fraunhofer.iais.eis.ParticipantUpdateMessage) ContractAgreementMessage(de.fraunhofer.iais.eis.ContractAgreementMessage) ArtifactRequestMessage(de.fraunhofer.iais.eis.ArtifactRequestMessage) ArtifactResponseMessage(de.fraunhofer.iais.eis.ArtifactResponseMessage) ConnectorUpdateMessage(de.fraunhofer.iais.eis.ConnectorUpdateMessage) ContractOfferMessage(de.fraunhofer.iais.eis.ContractOfferMessage) DescriptionRequestMessage(de.fraunhofer.iais.eis.DescriptionRequestMessage) DescriptionResponseMessage(de.fraunhofer.iais.eis.DescriptionResponseMessage) IOException(java.io.IOException) DeserializeException(ids.messaging.common.DeserializeException) ParticipantUnavailableMessage(de.fraunhofer.iais.eis.ParticipantUnavailableMessage) ResultMessage(de.fraunhofer.iais.eis.ResultMessage) MessageProcessedNotificationMessage(de.fraunhofer.iais.eis.MessageProcessedNotificationMessage) ResourceUnavailableMessage(de.fraunhofer.iais.eis.ResourceUnavailableMessage) ContractResponseMessage(de.fraunhofer.iais.eis.ContractResponseMessage)

Example 2 with Message

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();
}
Also used : Message(de.fraunhofer.iais.eis.Message)

Example 3 with Message

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());
}
Also used : DescriptionRequestMessage(de.fraunhofer.iais.eis.DescriptionRequestMessage) DescriptionRequestMessage(de.fraunhofer.iais.eis.DescriptionRequestMessage) Message(de.fraunhofer.iais.eis.Message) MultipartResponse(org.eclipse.dataspaceconnector.ids.api.multipart.message.MultipartResponse) MultipartRequest(org.eclipse.dataspaceconnector.ids.api.multipart.message.MultipartRequest) Test(org.junit.jupiter.api.Test)

Example 4 with Message

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();
}
Also used : RequestInProcessMessage(de.fraunhofer.iais.eis.RequestInProcessMessage) ContractRequestMessage(de.fraunhofer.iais.eis.ContractRequestMessage) InvalidCorrelationMessageException(org.eclipse.dataspaceconnector.ids.api.multipart.message.ids.exceptions.InvalidCorrelationMessageException) ContractRequestMessage(de.fraunhofer.iais.eis.ContractRequestMessage) RequestInProcessMessage(de.fraunhofer.iais.eis.RequestInProcessMessage) Message(de.fraunhofer.iais.eis.Message) ContractOffer(org.eclipse.dataspaceconnector.spi.types.domain.contract.offer.ContractOffer) ContractRequest(de.fraunhofer.iais.eis.ContractRequest) IOException(java.io.IOException) InvalidCorrelationMessageException(org.eclipse.dataspaceconnector.ids.api.multipart.message.ids.exceptions.InvalidCorrelationMessageException) IOException(java.io.IOException) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with Message

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();
}
Also used : RequestInProcessMessage(de.fraunhofer.iais.eis.RequestInProcessMessage) Message(de.fraunhofer.iais.eis.Message) RequestInProcessMessage(de.fraunhofer.iais.eis.RequestInProcessMessage)

Aggregations

Message (de.fraunhofer.iais.eis.Message)10 IOException (java.io.IOException)4 ContractOfferMessage (de.fraunhofer.iais.eis.ContractOfferMessage)3 ContractRequestMessage (de.fraunhofer.iais.eis.ContractRequestMessage)3 DescriptionRequestMessage (de.fraunhofer.iais.eis.DescriptionRequestMessage)3 RequestInProcessMessage (de.fraunhofer.iais.eis.RequestInProcessMessage)3 ContractAgreementMessage (de.fraunhofer.iais.eis.ContractAgreementMessage)2 ContractRejectionMessage (de.fraunhofer.iais.eis.ContractRejectionMessage)2 MultipartResponse (org.eclipse.dataspaceconnector.ids.api.multipart.message.MultipartResponse)2 InvalidCorrelationMessageException (org.eclipse.dataspaceconnector.ids.api.multipart.message.ids.exceptions.InvalidCorrelationMessageException)2 Nullable (org.jetbrains.annotations.Nullable)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ArtifactRequestMessage (de.fraunhofer.iais.eis.ArtifactRequestMessage)1 ArtifactResponseMessage (de.fraunhofer.iais.eis.ArtifactResponseMessage)1 ConnectorUnavailableMessage (de.fraunhofer.iais.eis.ConnectorUnavailableMessage)1 ConnectorUpdateMessage (de.fraunhofer.iais.eis.ConnectorUpdateMessage)1 ContractOffer (de.fraunhofer.iais.eis.ContractOffer)1 ContractRequest (de.fraunhofer.iais.eis.ContractRequest)1 ContractResponseMessage (de.fraunhofer.iais.eis.ContractResponseMessage)1 DescriptionResponseMessage (de.fraunhofer.iais.eis.DescriptionResponseMessage)1