Skip to content

Commit

Permalink
fix using "/cloudevents" endpoint for other entities than things
Browse files Browse the repository at this point in the history
* e.g. policy commands could not be created with the "POST /cloudevents" endpoint as "response-required" was always set to `false` in combination with setting a "requested ACK" (which is only available for thing commands)

Signed-off-by: Thomas Jäckle <thomas.jaeckle@beyonnex.io>
  • Loading branch information
thjaeckle committed Aug 23, 2023
1 parent dcfafe1 commit 9982347
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 9982347

Please sign in to comment.