Skip to content

Commit

Permalink
Merge pull request #1724 from eclipse-ditto/bugfix/cloudevents-policy…
Browse files Browse the repository at this point in the history
…-commands

fix using "/cloudevents" endpoint for other entities than things
  • Loading branch information
thjaeckle authored Aug 23, 2023
2 parents d17d1b7 + 9982347 commit 480e0c9
Showing 1 changed file with 35 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@

import javax.annotation.Nullable;

import org.eclipse.ditto.gateway.service.endpoints.routes.RouteBaseProperties;
import org.eclipse.ditto.gateway.service.util.config.endpoints.CloudEventsConfig;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.base.model.acks.AcknowledgementRequest;
import org.eclipse.ditto.base.model.acks.DittoAcknowledgementLabel;
import org.eclipse.ditto.base.model.exceptions.CloudEventMissingPayloadException;
Expand All @@ -32,21 +29,26 @@
import org.eclipse.ditto.base.model.headers.DittoHeaderDefinition;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.protocol.adapter.DittoProtocolAdapter;
import org.eclipse.ditto.protocol.JsonifiableAdaptable;
import org.eclipse.ditto.protocol.ProtocolFactory;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.base.model.signals.commands.CommandNotSupportedException;
import org.eclipse.ditto.gateway.service.endpoints.actors.AbstractHttpRequestActor;
import org.eclipse.ditto.gateway.service.endpoints.routes.AbstractRoute;
import org.eclipse.ditto.gateway.service.endpoints.routes.RouteBaseProperties;
import org.eclipse.ditto.gateway.service.util.config.endpoints.CloudEventsConfig;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.akka.logging.ThreadSafeDittoLogger;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.base.model.signals.commands.CommandNotSupportedException;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.protocol.JsonifiableAdaptable;
import org.eclipse.ditto.protocol.ProtocolFactory;
import org.eclipse.ditto.protocol.adapter.DittoProtocolAdapter;
import org.eclipse.ditto.things.model.signals.commands.ThingCommand;

import akka.actor.Status;
import akka.http.javadsl.model.ContentType;
import akka.http.javadsl.model.ContentTypes;
import akka.http.javadsl.model.HttpRequest;
import akka.http.javadsl.model.HttpResponse;
import akka.http.javadsl.model.StatusCodes;
import akka.http.javadsl.server.RequestContext;
import akka.http.javadsl.server.Route;
import akka.stream.javadsl.Sink;
Expand Down Expand Up @@ -141,7 +143,15 @@ private Route acceptCloudEvent(final RequestContext ctx, final DittoHeaders ditt
AbstractHttpRequestActor.COMPLETE_MESSAGE))
);

return completeWithFuture(httpResponseFuture);
return completeWithFuture(httpResponseFuture.thenApply(response -> {
if (response.status().isSuccess()) {
// as the /cloudevents is only intended to accept Ditto commands and apply them, replace the
// actual successful response with just 202 (accepted) and no additional body/headers
return HttpResponse.create().withStatus(StatusCodes.ACCEPTED);
} else {
return response;
}
}));
});
}

Expand Down Expand Up @@ -236,15 +246,24 @@ private Optional<Signal<?>> jsonToDittoSignal(@Nullable final CloudEventData dat
LOGGER.withCorrelationId(dittoHeaders)
.debug("CloudEvent payload JSON: {}", jsonObject);

final DittoHeaders adjustedHeaders = dittoHeaders.toBuilder()
.responseRequired(false)
.acknowledgementRequest(AcknowledgementRequest.of(DittoAcknowledgementLabel.TWIN_PERSISTED))
.build();

final JsonifiableAdaptable jsonifiableAdaptable = ProtocolFactory.jsonifiableAdaptableFromJson(jsonObject);
final Signal<?> signal = PROTOCOL_ADAPTER.fromAdaptable(jsonifiableAdaptable);
final Signal<?> signalWithAdjustedHeaders = signal.setDittoHeaders(
signal.getDittoHeaders().toBuilder().putHeaders(adjustedHeaders).build());

final Signal<?> signalWithAdjustedHeaders;
if (signal instanceof ThingCommand) {
final DittoHeaders adjustedHeaders = dittoHeaders.toBuilder()
.responseRequired(false)
.acknowledgementRequest(AcknowledgementRequest.of(DittoAcknowledgementLabel.TWIN_PERSISTED))
.build();
signalWithAdjustedHeaders = signal.setDittoHeaders(
signal.getDittoHeaders().toBuilder().putHeaders(adjustedHeaders).build()
);
} else {
signalWithAdjustedHeaders = signal.setDittoHeaders(
signal.getDittoHeaders().toBuilder().putHeaders(dittoHeaders).build()
);
}

return Optional.of(signalWithAdjustedHeaders);
}

Expand Down

0 comments on commit 480e0c9

Please sign in to comment.