Search in sources :

Example 1 with RequestInProcessMessage

use of de.fraunhofer.iais.eis.RequestInProcessMessage 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 2 with RequestInProcessMessage

use of de.fraunhofer.iais.eis.RequestInProcessMessage 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)

Example 3 with RequestInProcessMessage

use of de.fraunhofer.iais.eis.RequestInProcessMessage in project DataSpaceConnector by eclipse-dataspaceconnector.

the class ContractOfferHandler method handleRequest.

@Override
@Nullable
public MultipartResponse handleRequest(@NotNull MultipartRequest multipartRequest, @NotNull ClaimToken claimToken) {
    Objects.requireNonNull(multipartRequest);
    Objects.requireNonNull(claimToken);
    var message = (ContractOfferMessage) multipartRequest.getHeader();
    ContractOffer contractOffer = null;
    try {
        contractOffer = objectMapper.readValue(multipartRequest.getPayload(), ContractOffer.class);
    } catch (IOException e) {
        monitor.severe("ContractOfferHandler: Contract Offer is invalid", e);
        return createBadParametersErrorMultipartResponse(message);
    }
    Message response;
    try {
        response = responseMessageFactory.createRequestInProcessMessage(message);
    } catch (Exception e) {
        if (e instanceof InvalidCorrelationMessageException) {
            monitor.debug(String.format("Rejecting invalid IDS contract offer message [Msg-ID: %s]", message.getId()), e);
        } else {
            monitor.severe(String.format("Exception while creating IDS RequestInProcessMessage to answer contract offer [Msg-ID: %s]", message.getId()), e);
        }
        response = responseMessageFactory.createRejectionMessage(message, e);
    }
    if (response instanceof RequestInProcessMessage) {
    // TODO similar implementation to ContractRequestHandler (only required if counter offers supported, not needed for M1)
    }
    return MultipartResponse.Builder.newInstance().header(response).build();
}
Also used : RequestInProcessMessage(de.fraunhofer.iais.eis.RequestInProcessMessage) ContractOfferMessage(de.fraunhofer.iais.eis.ContractOfferMessage) InvalidCorrelationMessageException(org.eclipse.dataspaceconnector.ids.api.multipart.message.ids.exceptions.InvalidCorrelationMessageException) ContractOfferMessage(de.fraunhofer.iais.eis.ContractOfferMessage) Message(de.fraunhofer.iais.eis.Message) RequestInProcessMessage(de.fraunhofer.iais.eis.RequestInProcessMessage) ContractOffer(de.fraunhofer.iais.eis.ContractOffer) IOException(java.io.IOException) InvalidCorrelationMessageException(org.eclipse.dataspaceconnector.ids.api.multipart.message.ids.exceptions.InvalidCorrelationMessageException) IOException(java.io.IOException) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Message (de.fraunhofer.iais.eis.Message)3 RequestInProcessMessage (de.fraunhofer.iais.eis.RequestInProcessMessage)3 IOException (java.io.IOException)2 InvalidCorrelationMessageException (org.eclipse.dataspaceconnector.ids.api.multipart.message.ids.exceptions.InvalidCorrelationMessageException)2 Nullable (org.jetbrains.annotations.Nullable)2 ContractOffer (de.fraunhofer.iais.eis.ContractOffer)1 ContractOfferMessage (de.fraunhofer.iais.eis.ContractOfferMessage)1 ContractRequest (de.fraunhofer.iais.eis.ContractRequest)1 ContractRequestMessage (de.fraunhofer.iais.eis.ContractRequestMessage)1 ContractOffer (org.eclipse.dataspaceconnector.spi.types.domain.contract.offer.ContractOffer)1