Search in sources :

Example 1 with ContractRequestMessage

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);
    }
}
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 ContractRequestMessage

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();
}
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 3 with ContractRequestMessage

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());
}
Also used : ContractRequestMessage(de.fraunhofer.iais.eis.ContractRequestMessage) ContractRequestMessageDesc(io.dataspaceconnector.model.message.ContractRequestMessageDesc) DynamicAttributeTokenBuilder(de.fraunhofer.iais.eis.DynamicAttributeTokenBuilder) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with ContractRequestMessage

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

Aggregations

ContractRequestMessage (de.fraunhofer.iais.eis.ContractRequestMessage)4 IOException (java.io.IOException)3 ContractRequest (de.fraunhofer.iais.eis.ContractRequest)2 Message (de.fraunhofer.iais.eis.Message)2 ContractOffer (org.eclipse.dataspaceconnector.spi.types.domain.contract.offer.ContractOffer)2 Nullable (org.jetbrains.annotations.Nullable)2 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 ContractAgreementMessage (de.fraunhofer.iais.eis.ContractAgreementMessage)1 ContractOfferMessage (de.fraunhofer.iais.eis.ContractOfferMessage)1 ContractRejectionMessage (de.fraunhofer.iais.eis.ContractRejectionMessage)1 ContractResponseMessage (de.fraunhofer.iais.eis.ContractResponseMessage)1 DescriptionRequestMessage (de.fraunhofer.iais.eis.DescriptionRequestMessage)1 DescriptionResponseMessage (de.fraunhofer.iais.eis.DescriptionResponseMessage)1 DynamicAttributeTokenBuilder (de.fraunhofer.iais.eis.DynamicAttributeTokenBuilder)1 MessageProcessedNotificationMessage (de.fraunhofer.iais.eis.MessageProcessedNotificationMessage)1 ParticipantRequestMessage (de.fraunhofer.iais.eis.ParticipantRequestMessage)1 ParticipantUnavailableMessage (de.fraunhofer.iais.eis.ParticipantUnavailableMessage)1