Search in sources :

Example 1 with MessageCommandResponse

use of org.eclipse.ditto.messages.model.signals.commands.MessageCommandResponse in project ditto by eclipse.

the class AbstractHttpRequestActor method handleMessageResponseMessage.

private HttpResponse handleMessageResponseMessage(final MessageCommandResponse<?, ?> messageCommandResponse) {
    HttpResponse httpResponse;
    final Message<?> message = messageCommandResponse.getMessage();
    final Optional<?> optionalPayload = message.getPayload();
    final Optional<ByteBuffer> optionalRawPayload = message.getRawPayload();
    final var responseStatus = Optional.of(messageCommandResponse.getHttpStatus()).filter(httpStatus -> StatusCodes.lookup(httpStatus.getCode()).isPresent()).filter(httpStatus -> !HttpStatus.BAD_GATEWAY.equals(httpStatus));
    // if statusCode is != NO_CONTENT
    if (responseStatus.map(status -> !HttpStatus.NO_CONTENT.equals(status)).orElse(true)) {
        // this is on purpose not .map(ContentTypes:parse) as this would throw an exception:
        final Optional<akka.http.scaladsl.model.ContentType> optionalContentType = message.getContentType().map(ContentType$.MODULE$::parse).filter(Either::isRight).map(Either::toOption).map(Option::get);
        final boolean isBinary = optionalContentType.map(akka.http.scaladsl.model.ContentType::value).map(ContentType::of).filter(ContentType::isBinary).isPresent();
        httpResponse = createHttpResponse(responseStatus.orElse(HttpStatus.OK));
        if (optionalPayload.isPresent() && optionalContentType.isPresent() && !isBinary) {
            final akka.http.scaladsl.model.ContentType contentType = optionalContentType.get();
            final Object payload = optionalPayload.get();
            final ByteString responsePayload = ByteString.fromString(payload.toString());
            httpResponse = httpResponse.withEntity(HttpEntities.create(contentType, responsePayload));
        } else if (optionalRawPayload.isPresent() && optionalContentType.isPresent() && isBinary) {
            final akka.http.scaladsl.model.ContentType contentType = optionalContentType.get();
            final ByteBuffer rawPayload = optionalRawPayload.get();
            httpResponse = httpResponse.withEntity(HttpEntities.create(contentType, rawPayload.array()));
        } else if (optionalRawPayload.isPresent()) {
            final ByteBuffer rawPayload = optionalRawPayload.get();
            httpResponse = httpResponse.withEntity(HttpEntities.create(rawPayload.array()));
        }
    } else {
        // if payload was missing OR HTTP status was NO_CONTENT:
        optionalRawPayload.ifPresent(byteBuffer -> logger.withCorrelationId(messageCommandResponse).info("Response payload was set but response status code was also set to <{}>." + " Ignoring the response payload. Command=<{}>", responseStatus, messageCommandResponse));
        httpResponse = createHttpResponse(HttpStatus.NO_CONTENT);
    }
    return enhanceResponseWithExternalDittoHeaders(httpResponse, messageCommandResponse.getDittoHeaders());
}
Also used : Location(akka.http.javadsl.model.headers.Location) Command(org.eclipse.ditto.base.model.signals.commands.Command) Uri(akka.http.javadsl.model.Uri) Acknowledgement(org.eclipse.ditto.base.model.signals.acks.Acknowledgement) Signal(org.eclipse.ditto.base.model.signals.Signal) DittoHeaderDefinition(org.eclipse.ditto.base.model.headers.DittoHeaderDefinition) UnaryOperator(java.util.function.UnaryOperator) MessageCommandAckRequestSetter(org.eclipse.ditto.messages.model.signals.commands.acks.MessageCommandAckRequestSetter) ByteBuffer(java.nio.ByteBuffer) AddConnectionLogEntry(org.eclipse.ditto.connectivity.api.messaging.monitoring.logs.AddConnectionLogEntry) DittoRuntimeException(org.eclipse.ditto.base.model.exceptions.DittoRuntimeException) DittoHeaders(org.eclipse.ditto.base.model.headers.DittoHeaders) RawHeader(akka.http.javadsl.model.headers.RawHeader) ActorRef(akka.actor.ActorRef) Duration(java.time.Duration) Map(java.util.Map) Message(org.eclipse.ditto.messages.model.Message) CommandResponse(org.eclipse.ditto.base.model.signals.commands.CommandResponse) ContentType(org.eclipse.ditto.base.model.headers.contenttype.ContentType) JsonRuntimeException(org.eclipse.ditto.json.JsonRuntimeException) LogEntryFactory(org.eclipse.ditto.connectivity.api.messaging.monitoring.logs.LogEntryFactory) SignalInformationPoint(org.eclipse.ditto.internal.models.signal.SignalInformationPoint) Predicate(java.util.function.Predicate) ReceiveTimeout(akka.actor.ReceiveTimeout) ThingLiveCommandAckRequestSetter(org.eclipse.ditto.things.model.signals.commands.acks.ThingLiveCommandAckRequestSetter) GatewayServiceUnavailableException(org.eclipse.ditto.base.model.signals.commands.exceptions.GatewayServiceUnavailableException) MatchingValidationResult(org.eclipse.ditto.internal.models.signal.correlation.MatchingValidationResult) ContentType$(akka.http.scaladsl.model.ContentType$) DittoLoggerFactory(org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory) Collectors(java.util.stream.Collectors) HeaderTranslator(org.eclipse.ditto.protocol.HeaderTranslator) Base64(java.util.Base64) List(java.util.List) MessageCommandResponse(org.eclipse.ditto.messages.model.signals.commands.MessageCommandResponse) DefaultUserInformation(org.eclipse.ditto.gateway.service.endpoints.routes.whoami.DefaultUserInformation) WithDittoHeaders(org.eclipse.ditto.base.model.headers.WithDittoHeaders) CommandConfig(org.eclipse.ditto.gateway.service.util.config.endpoints.CommandConfig) Optional(java.util.Optional) GatewayCommandTimeoutException(org.eclipse.ditto.base.model.signals.commands.exceptions.GatewayCommandTimeoutException) DittoAcknowledgementLabel(org.eclipse.ditto.base.model.acks.DittoAcknowledgementLabel) WithOptionalEntity(org.eclipse.ditto.base.model.signals.WithOptionalEntity) AcknowledgementConfig(org.eclipse.ditto.internal.models.acks.config.AcknowledgementConfig) CompletableFuture(java.util.concurrent.CompletableFuture) HttpStatus(org.eclipse.ditto.base.model.common.HttpStatus) JsonValueSourceRef(org.eclipse.ditto.internal.utils.cluster.JsonValueSourceRef) StatusCodes(akka.http.javadsl.model.StatusCodes) Supplier(java.util.function.Supplier) WhoamiResponse(org.eclipse.ditto.gateway.service.endpoints.routes.whoami.WhoamiResponse) ErrorResponse(org.eclipse.ditto.base.model.signals.commands.ErrorResponse) ByteString(akka.util.ByteString) EntityId(org.eclipse.ditto.base.model.entity.id.EntityId) DittoJsonException(org.eclipse.ditto.base.model.exceptions.DittoJsonException) Either(scala.util.Either) JsonPointer(org.eclipse.ditto.json.JsonPointer) Nullable(javax.annotation.Nullable) AskTimeoutException(akka.pattern.AskTimeoutException) Whoami(org.eclipse.ditto.gateway.service.endpoints.routes.whoami.Whoami) WithEntity(org.eclipse.ditto.base.model.signals.commands.WithEntity) DittoDiagnosticLoggingAdapter(org.eclipse.ditto.internal.utils.akka.logging.DittoDiagnosticLoggingAdapter) HttpHeader(akka.http.javadsl.model.HttpHeader) HttpRequest(akka.http.javadsl.model.HttpRequest) ContentTypes(akka.http.javadsl.model.ContentTypes) HttpEntities(akka.http.javadsl.model.HttpEntities) EntityStreamSizeException(akka.http.scaladsl.model.EntityStreamSizeException) Option(scala.Option) HttpResponse(akka.http.javadsl.model.HttpResponse) AcknowledgementAggregatorActorStarter(org.eclipse.ditto.internal.models.acks.AcknowledgementAggregatorActorStarter) DevOpsCommand(org.eclipse.ditto.base.api.devops.signals.commands.DevOpsCommand) Consumer(java.util.function.Consumer) Acknowledgements(org.eclipse.ditto.base.model.signals.acks.Acknowledgements) HttpConfig(org.eclipse.ditto.gateway.service.util.config.endpoints.HttpConfig) ThingModifyCommandAckRequestSetter(org.eclipse.ditto.things.model.signals.commands.acks.ThingModifyCommandAckRequestSetter) Status(akka.actor.Status) AbstractActor(akka.actor.AbstractActor) ConnectionId(org.eclipse.ditto.connectivity.model.ConnectionId) ReceiveBuilder(akka.japi.pf.ReceiveBuilder) ContentType(org.eclipse.ditto.base.model.headers.contenttype.ContentType) ByteString(akka.util.ByteString) HttpResponse(akka.http.javadsl.model.HttpResponse) ByteBuffer(java.nio.ByteBuffer) ContentType$(akka.http.scaladsl.model.ContentType$) Either(scala.util.Either) Option(scala.Option)

Aggregations

AbstractActor (akka.actor.AbstractActor)1 ActorRef (akka.actor.ActorRef)1 ReceiveTimeout (akka.actor.ReceiveTimeout)1 Status (akka.actor.Status)1 ContentTypes (akka.http.javadsl.model.ContentTypes)1 HttpEntities (akka.http.javadsl.model.HttpEntities)1 HttpHeader (akka.http.javadsl.model.HttpHeader)1 HttpRequest (akka.http.javadsl.model.HttpRequest)1 HttpResponse (akka.http.javadsl.model.HttpResponse)1 StatusCodes (akka.http.javadsl.model.StatusCodes)1 Uri (akka.http.javadsl.model.Uri)1 Location (akka.http.javadsl.model.headers.Location)1 RawHeader (akka.http.javadsl.model.headers.RawHeader)1 ContentType$ (akka.http.scaladsl.model.ContentType$)1 EntityStreamSizeException (akka.http.scaladsl.model.EntityStreamSizeException)1 ReceiveBuilder (akka.japi.pf.ReceiveBuilder)1 AskTimeoutException (akka.pattern.AskTimeoutException)1 ByteString (akka.util.ByteString)1 ByteBuffer (java.nio.ByteBuffer)1 Duration (java.time.Duration)1