Skip to content

Commit

Permalink
Switch over signal type instead of using instanceof checks
Browse files Browse the repository at this point in the history
Signed-off-by: Yannic Klem <yannic.klem@bosch.io>
  • Loading branch information
Yannic92 committed Sep 11, 2020
1 parent 6100ee6 commit 8f623a0
Showing 1 changed file with 20 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ private CompletionStage<CommandResponseOrAcknowledgement> toCommandResponseOrAck
return getResponseBody(response, maxResponseSize, materializer).thenApply(body -> {
@Nullable final CommandResponse<?> commandResponse;
if (isMessageCommand) {
commandResponse = toMessageCommandResponse((MessageCommand<?, ?> ) signal, dittoHeaders,
commandResponse = toMessageCommandResponse((MessageCommand<?, ?>) signal, dittoHeaders,
body, statusCode, response.entity().getContentType(), response.getHeaders());
} else {
commandResponse = null;
Expand All @@ -319,28 +319,29 @@ private CommandResponse<?> toMessageCommandResponse(final MessageCommand<?, ?> m
final akka.http.javadsl.model.ContentType contentType,
final Iterable<HttpHeader> headers) {

final MessageHeadersBuilder responseMessageBuilder = messageCommand.getMessage().getHeaders().toBuilder();
final MessageHeaders messageHeaders = responseMessageBuilder.statusCode(status)
.contentType(contentType.toString())
.putHeaders(StreamSupport.stream(headers.spliterator(), false)
.collect(Collectors.toMap(HttpHeader::name, HttpHeader::value))
)
.build();
final MessageHeadersBuilder responseMessageBuilder = messageCommand.getMessage().getHeaders().toBuilder();
final MessageHeaders messageHeaders = responseMessageBuilder.statusCode(status)
.contentType(contentType.toString())
.putHeaders(StreamSupport.stream(headers.spliterator(), false)
.collect(Collectors.toMap(HttpHeader::name, HttpHeader::value))
)
.build();
final Message<Object> message = Message.newBuilder(messageHeaders)
.payload(jsonValue)
.build();

if (messageCommand instanceof SendClaimMessage) {
return SendClaimMessageResponse.of(messageCommand.getThingEntityId(), message, status, dittoHeaders);
} else if (messageCommand instanceof SendThingMessage) {
return SendThingMessageResponse.of(messageCommand.getThingEntityId(), message, status, dittoHeaders);
} else if (messageCommand instanceof SendFeatureMessage) {
final SendFeatureMessage<?> sendFeatureMessage = (SendFeatureMessage<?>) messageCommand;
return SendFeatureMessageResponse.of(messageCommand.getThingEntityId(),
sendFeatureMessage.getFeatureId(), message, status, dittoHeaders);
} else {
throw new IllegalArgumentException("Unknown MessageCommand <" + messageCommand.getClass().getSimpleName() +
">");
switch (messageCommand.getType()) {
case SendClaimMessage.TYPE:
return SendClaimMessageResponse.of(messageCommand.getThingEntityId(), message, status, dittoHeaders);
case SendThingMessage.TYPE:
return SendThingMessageResponse.of(messageCommand.getThingEntityId(), message, status, dittoHeaders);
case SendFeatureMessage.TYPE:
final SendFeatureMessage<?> sendFeatureMessage = (SendFeatureMessage<?>) messageCommand;
return SendFeatureMessageResponse.of(messageCommand.getThingEntityId(),
sendFeatureMessage.getFeatureId(), message, status, dittoHeaders);
default:
throw new IllegalArgumentException(
"Unknown MessageCommand <" + messageCommand.getClass().getSimpleName() + ">");
}
}

Expand Down

0 comments on commit 8f623a0

Please sign in to comment.