/
HttpPublisherActor.java
601 lines (532 loc) · 29.1 KB
/
HttpPublisherActor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/*
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.connectivity.service.messaging.httppush;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.function.BiFunction;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.acks.DittoAcknowledgementLabel;
import org.eclipse.ditto.base.model.common.HttpStatus;
import org.eclipse.ditto.base.model.common.HttpStatusCodeOutOfRangeException;
import org.eclipse.ditto.base.model.entity.id.EntityId;
import org.eclipse.ditto.base.model.entity.id.WithEntityId;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.headers.DittoHeadersBuilder;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.base.model.signals.acks.Acknowledgement;
import org.eclipse.ditto.base.model.signals.commands.CommandResponse;
import org.eclipse.ditto.connectivity.api.ExternalMessage;
import org.eclipse.ditto.connectivity.model.Connection;
import org.eclipse.ditto.connectivity.model.MessageSendingFailedException;
import org.eclipse.ditto.connectivity.model.Target;
import org.eclipse.ditto.connectivity.service.config.HttpPushConfig;
import org.eclipse.ditto.connectivity.service.messaging.BasePublisherActor;
import org.eclipse.ditto.connectivity.service.messaging.SendResult;
import org.eclipse.ditto.connectivity.service.messaging.internal.ConnectionFailure;
import org.eclipse.ditto.connectivity.service.messaging.internal.ImmutableConnectionFailure;
import org.eclipse.ditto.internal.utils.akka.controlflow.TimeMeasuringFlow;
import org.eclipse.ditto.internal.utils.akka.logging.ThreadSafeDittoLoggingAdapter;
import org.eclipse.ditto.internal.utils.metrics.DittoMetrics;
import org.eclipse.ditto.internal.utils.metrics.instruments.timer.PreparedTimer;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.messages.model.Message;
import org.eclipse.ditto.messages.model.MessageHeadersBuilder;
import org.eclipse.ditto.messages.model.signals.commands.MessageCommand;
import org.eclipse.ditto.messages.model.signals.commands.MessageCommandResponse;
import org.eclipse.ditto.messages.model.signals.commands.SendClaimMessage;
import org.eclipse.ditto.messages.model.signals.commands.SendClaimMessageResponse;
import org.eclipse.ditto.messages.model.signals.commands.SendFeatureMessage;
import org.eclipse.ditto.messages.model.signals.commands.SendFeatureMessageResponse;
import org.eclipse.ditto.messages.model.signals.commands.SendThingMessage;
import org.eclipse.ditto.messages.model.signals.commands.SendThingMessageResponse;
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.ThingId;
import akka.Done;
import akka.NotUsed;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.http.javadsl.model.HttpCharset;
import akka.http.javadsl.model.HttpEntities;
import akka.http.javadsl.model.HttpEntity;
import akka.http.javadsl.model.HttpHeader;
import akka.http.javadsl.model.HttpRequest;
import akka.http.javadsl.model.HttpResponse;
import akka.http.javadsl.model.Uri;
import akka.http.javadsl.model.headers.ContentType;
import akka.japi.Pair;
import akka.japi.pf.ReceiveBuilder;
import akka.stream.KillSwitch;
import akka.stream.KillSwitches;
import akka.stream.Materializer;
import akka.stream.OverflowStrategy;
import akka.stream.QueueOfferResult;
import akka.stream.UniqueKillSwitch;
import akka.stream.javadsl.Flow;
import akka.stream.javadsl.Keep;
import akka.stream.javadsl.Sink;
import akka.stream.javadsl.Source;
import akka.stream.javadsl.SourceQueue;
import akka.stream.javadsl.SourceQueueWithComplete;
import scala.util.Try;
/**
* Actor responsible for publishing messages to an HTTP endpoint.
*/
final class HttpPublisherActor extends BasePublisherActor<HttpPublishTarget> {
/**
* The name of this Actor in the ActorSystem.
*/
static final String ACTOR_NAME = "httpPublisherActor";
private static final long READ_BODY_TIMEOUT_MS = 10000L;
private static final DittoProtocolAdapter DITTO_PROTOCOL_ADAPTER = DittoProtocolAdapter.newInstance();
private static final String LIVE_RESPONSE_NOT_OF_EXPECTED_TYPE =
"Live response of type <%s> is not of expected type <%s>.";
private static final String TOO_MANY_IN_FLIGHT_MESSAGE_DESCRIPTION = "This can have the following reasons:\n" +
"a) The HTTP endpoint does not consume the messages fast enough.\n" +
"b) The client count and/or the parallelism of this connection is not configured high enough.";
private final HttpPushFactory factory;
private final Materializer materializer;
private final SourceQueue<Pair<HttpRequest, HttpPushContext>> sourceQueue;
private final KillSwitch killSwitch;
private final RequestSigning requestSigning;
@SuppressWarnings("unused")
private HttpPublisherActor(final Connection connection, final HttpPushFactory factory, final String clientId) {
super(connection, clientId);
this.factory = factory;
materializer = Materializer.createMaterializer(this::getContext);
final HttpPushConfig config = connectionConfig.getHttpPushConfig();
final Pair<Pair<SourceQueueWithComplete<Pair<HttpRequest, HttpPushContext>>, UniqueKillSwitch>,
CompletionStage<Done>> materialized =
Source.<Pair<HttpRequest, HttpPushContext>>queue(config.getMaxQueueSize(), OverflowStrategy.dropNew())
.viaMat(buildHttpRequestFlow(config), Keep.left())
.viaMat(KillSwitches.single(), Keep.both())
.toMat(Sink.foreach(HttpPublisherActor::processResponse), Keep.both())
.run(materializer);
sourceQueue = materialized.first().first();
killSwitch = materialized.first().second();
// Inform self of stream termination.
// If self is alive, the error should be escalated.
materialized.second()
.whenComplete((done, error) -> getSelf().tell(toConnectionFailure(done, error), ActorRef.noSender()));
requestSigning = connection.getCredentials()
.map(credentials -> credentials.accept(RequestSigningExtension.get(getContext().getSystem())))
.orElse(NoOpRequestSigning.INSTANCE);
}
static Props props(final Connection connection, final HttpPushFactory factory, final String clientId) {
return Props.create(HttpPublisherActor.class, connection, factory, clientId);
}
private Flow<Pair<HttpRequest, HttpPushContext>, Pair<Try<HttpResponse>, HttpPushContext>, ?>
buildHttpRequestFlow(final HttpPushConfig config) {
final Duration requestTimeout = config.getRequestTimeout();
final PreparedTimer timer = DittoMetrics.timer("http_publish_request_time")
// Set maximum duration higher than request timeout to avoid race conditions
.maximumDuration(requestTimeout.plus(Duration.ofSeconds(5)))
.tag("id", connection.getId().toString());
final Sink<Duration, CompletionStage<Done>> logRequestTimes = Sink.<Duration>foreach(duration ->
connectionLogger.success("HTTP request took <{0}> ms.", duration.toMillis())
);
final Flow<Pair<HttpRequest, HttpPushContext>, Pair<HttpRequest, HttpPushContext>, NotUsed> requestSigningFlow =
Flow.<Pair<HttpRequest, HttpPushContext>>create()
.flatMapConcat(pair -> requestSigning.sign(pair.first())
.map(signedRequest -> {
logger.debug("SignedRequest <{}>", signedRequest);
return Pair.create(signedRequest, pair.second());
}));
final var httpPushFlow = factory.<HttpPushContext>createFlow(getContext().getSystem(), logger, requestTimeout);
final var httpFlowIncludingRequestSigning = requestSigningFlow.via(httpPushFlow);
return TimeMeasuringFlow.measureTimeOf(httpFlowIncludingRequestSigning, timer, logRequestTimes);
}
@Override
public void postStop() throws Exception {
killSwitch.shutdown();
super.postStop();
}
@Override
protected void preEnhancement(final ReceiveBuilder receiveBuilder) {
// noop
}
@Override
protected void postEnhancement(final ReceiveBuilder receiveBuilder) {
receiveBuilder.match(ConnectionFailure.class, failure -> getContext().getParent().tell(failure, getSelf()));
}
@Override
protected HttpPublishTarget toPublishTarget(final String address) {
return HttpPublishTarget.of(address);
}
@Override
protected CompletionStage<SendResult> publishMessage(final Signal<?> signal,
@Nullable final Target autoAckTarget,
final HttpPublishTarget publishTarget,
final ExternalMessage message,
final int maxTotalMessageSize,
final int ackSizeQuota) {
final CompletableFuture<SendResult> resultFuture = new CompletableFuture<>();
final HttpRequest request = createRequest(publishTarget, message);
final HttpPushContext context = newContext(signal, autoAckTarget, request, message, maxTotalMessageSize,
ackSizeQuota, resultFuture);
sourceQueue.offer(Pair.create(request, context))
.handle(handleQueueOfferResult(message, resultFuture));
return resultFuture;
}
private HttpRequest createRequest(final HttpPublishTarget publishTarget, final ExternalMessage message) {
final Pair<Iterable<HttpHeader>, ContentType> headersPair = getHttpHeadersPair(message);
final HttpRequest requestWithoutEntity = factory.newRequest(publishTarget).addHeaders(headersPair.first());
final ContentType contentTypeHeader = headersPair.second();
if (contentTypeHeader != null) {
final HttpEntity.Strict httpEntity =
HttpEntities.create(contentTypeHeader.contentType(), getPayloadAsBytes(message));
return requestWithoutEntity.withEntity(httpEntity);
} else if (message.isTextMessage()) {
return requestWithoutEntity.withEntity(getTextPayload(message));
} else {
return requestWithoutEntity.withEntity(getBytePayload(message));
}
}
private static Pair<Iterable<HttpHeader>, ContentType> getHttpHeadersPair(final ExternalMessage message) {
final Collection<HttpHeader> headers = new ArrayList<>(message.getHeaders().size());
ContentType contentType = null;
for (final Map.Entry<String, String> entry : message.getHeaders().entrySet()) {
final HttpHeader httpHeader = HttpHeader.parse(entry.getKey(), entry.getValue());
if (httpHeader instanceof ContentType) {
contentType = (ContentType) httpHeader;
} else {
headers.add(httpHeader);
}
}
return Pair.create(headers, contentType);
}
// Async callback. Must be thread-safe.
private BiFunction<QueueOfferResult, Throwable, Void> handleQueueOfferResult(final ExternalMessage message,
final CompletableFuture<?> resultFuture) {
return (queueOfferResult, error) -> {
if (error != null) {
final String errorDescription = "Source queue failure";
logger.error(error, errorDescription);
resultFuture.completeExceptionally(error);
escalate(error, errorDescription);
} else if (Objects.equals(queueOfferResult, QueueOfferResult.dropped())) {
resultFuture.completeExceptionally(MessageSendingFailedException.newBuilder()
.message("Outgoing HTTP request aborted: There are too many in-flight requests.")
.description(TOO_MANY_IN_FLIGHT_MESSAGE_DESCRIPTION)
.dittoHeaders(message.getInternalHeaders())
.build());
}
return null;
};
}
// Async callback. Must be thread-safe.
private static void processResponse(final Pair<Try<HttpResponse>, HttpPushContext> responseWithContext) {
responseWithContext.second().onResponse(responseWithContext.first());
}
private HttpPushContext newContext(final Signal<?> signal,
@Nullable final Target autoAckTarget,
final HttpRequest request,
final ExternalMessage message,
final int maxTotalMessageSize,
final int ackSizeQuota,
final CompletableFuture<SendResult> resultFuture) {
return tryResponse -> {
final Uri requestUri = stripUserInfo(request.getUri());
final ThreadSafeDittoLoggingAdapter l;
if (logger.isDebugEnabled()) {
l = logger.withCorrelationId(message.getInternalHeaders());
} else {
l = logger;
}
if (tryResponse.isFailure()) {
final Throwable error = tryResponse.toEither().left().get();
final String errorDescription = MessageFormat.format("Failed to send HTTP request to <{0}>.",
requestUri);
l.debug("Failed to send message <{}> due to <{}>", message, error);
resultFuture.completeExceptionally(error);
escalate(error, errorDescription);
} else {
final HttpResponse response = tryResponse.toEither().right().get();
l.debug("Sent message <{}>. Got response <{} {}>", message, request,
response.status(), response.getHeaders());
toCommandResponseOrAcknowledgement(signal, autoAckTarget, response, maxTotalMessageSize, ackSizeQuota)
.thenAccept(resultFuture::complete)
.exceptionally(e -> {
resultFuture.completeExceptionally(e);
return null;
});
}
};
}
private CompletionStage<SendResult> toCommandResponseOrAcknowledgement(final Signal<?> signal,
@Nullable final Target autoAckTarget,
final HttpResponse response,
final int maxTotalMessageSize,
final int ackSizeQuota) {
final var autoAckLabel = getAcknowledgementLabel(autoAckTarget);
final var statusCode = response.status().intValue();
final HttpStatus httpStatus;
try {
httpStatus = HttpStatus.getInstance(statusCode);
} catch (final HttpStatusCodeOutOfRangeException e) {
response.discardEntityBytes(materializer);
final var error = MessageSendingFailedException.newBuilder()
.message(String.format("Remote server delivered unknown HTTP status code <%d>!", statusCode))
.cause(e)
.build();
return CompletableFuture.failedFuture(error);
}
final boolean isMessageCommand = signal instanceof MessageCommand;
final int maxResponseSize = isMessageCommand ? maxTotalMessageSize : ackSizeQuota;
return getResponseBody(response, maxResponseSize, materializer).thenApply(body -> {
@Nullable final CommandResponse<?> result;
final DittoHeaders dittoHeaders = setDittoHeaders(signal.getDittoHeaders(), response);
final Optional<ThingId> thingIdOptional = WithEntityId.getEntityIdOfType(ThingId.class, signal);
if (autoAckLabel.isPresent() && thingIdOptional.isPresent()) {
final ThingId thingId = thingIdOptional.get();
if (DittoAcknowledgementLabel.LIVE_RESPONSE.equals(autoAckLabel.get())) {
// Live-Response is declared as issued ack => parse live response from response
if (isMessageCommand) {
result =
toMessageCommandResponse((MessageCommand<?, ?>) signal, dittoHeaders, body, httpStatus);
} else {
result = null;
}
} else {
// There is an issued ack declared but its not live-response => handle response as acknowledgement.
result = Acknowledgement.of(autoAckLabel.get(), thingId, httpStatus, dittoHeaders, body);
}
} else {
// No Acks declared as issued acks => Handle response either as live response or as acknowledgement
// or as fallback build a response for local diagnostics.
final boolean isDittoProtocolMessage = dittoHeaders.getDittoContentType()
.filter(org.eclipse.ditto.base.model.headers.contenttype.ContentType::isDittoProtocol)
.isPresent();
if (isDittoProtocolMessage && body.isObject()) {
final CommandResponse<?> parsedResponse = toCommandResponse(body.asObject());
if (parsedResponse instanceof Acknowledgement) {
result = parsedResponse;
} else if (parsedResponse instanceof MessageCommandResponse) {
result = parsedResponse;
} else {
result = null;
}
} else {
result = null;
}
}
if (result instanceof MessageCommandResponse && isMessageCommand) {
// Do only return command response for live commands with a correct response.
validateLiveResponse(result, (MessageCommand<?, ?>) signal);
}
if (result == null) {
connectionLogger.success(
"No CommandResponse created from HTTP response with status <{0}> and body <{1}>.",
response.status(), body);
} else {
connectionLogger.success(
"CommandResponse <{0}> created from HTTP response with Status <{1}> and body <{2}>.",
result, response.status(), body);
}
return new SendResult(result, dittoHeaders);
});
}
private void validateLiveResponse(final CommandResponse<?> commandResponse,
final MessageCommand<?, ?> messageCommand) {
final ThingId messageThingId = messageCommand.getEntityId();
if (!(commandResponse instanceof WithEntityId)) {
final String message = String.format(
"Live response does not target the correct thing. Expected thing ID <%s>, but no ID found",
messageThingId);
handleInvalidResponse(message, commandResponse);
}
final EntityId responseThingId = ((WithEntityId) commandResponse).getEntityId();
if (!responseThingId.equals(messageThingId)) {
final String message = String.format(
"Live response does not target the correct thing. Expected thing ID <%s>, but was <%s>.",
messageThingId, responseThingId);
handleInvalidResponse(message, commandResponse);
}
final String messageCorrelationId = messageCommand.getDittoHeaders().getCorrelationId().orElse(null);
final String responseCorrelationId = commandResponse.getDittoHeaders().getCorrelationId().orElse(null);
if (!Objects.equals(messageCorrelationId, responseCorrelationId)) {
final String message = String.format(
"Correlation ID of response <%s> does not match correlation ID of message command <%s>. ",
responseCorrelationId, messageCorrelationId
);
handleInvalidResponse(message, commandResponse);
}
switch (messageCommand.getType()) {
case SendClaimMessage.TYPE:
if (!SendClaimMessageResponse.TYPE.equalsIgnoreCase(commandResponse.getType())) {
final String message = String.format(LIVE_RESPONSE_NOT_OF_EXPECTED_TYPE, commandResponse.getType(),
SendClaimMessageResponse.TYPE);
handleInvalidResponse(message, commandResponse);
}
break;
case SendThingMessage.TYPE:
if (!SendThingMessageResponse.TYPE.equalsIgnoreCase(commandResponse.getType())) {
final String message = String.format(LIVE_RESPONSE_NOT_OF_EXPECTED_TYPE, commandResponse.getType(),
SendThingMessageResponse.TYPE);
handleInvalidResponse(message, commandResponse);
}
break;
case SendFeatureMessage.TYPE:
if (!SendFeatureMessageResponse.TYPE.equalsIgnoreCase(commandResponse.getType())) {
final String message = String.format(LIVE_RESPONSE_NOT_OF_EXPECTED_TYPE, commandResponse.getType(),
SendFeatureMessageResponse.TYPE);
handleInvalidResponse(message, commandResponse);
}
final String messageFeatureId = ((SendFeatureMessage<?>) messageCommand).getFeatureId();
final String responseFeatureId = ((SendFeatureMessageResponse<?>) commandResponse).getFeatureId();
if (!messageFeatureId.equalsIgnoreCase(responseFeatureId)) {
final String message = String.format("Live response does not target the correct feature. " +
"Expected feature ID <%s>, but was <%s>.",
messageThingId, responseThingId);
handleInvalidResponse(message, commandResponse);
}
break;
default:
handleInvalidResponse("Initial message command type <{}> is unknown.", commandResponse);
}
}
private void handleInvalidResponse(final String message, final CommandResponse<?> commandResponse) {
final var exception = MessageSendingFailedException.newBuilder()
.httpStatus(HttpStatus.BAD_REQUEST)
.description(message)
.build();
connectionLogger.failure(commandResponse, exception);
throw exception;
}
@Nullable
private MessageCommandResponse<?, ?> toMessageCommandResponse(final MessageCommand<?, ?> messageCommand,
final DittoHeaders dittoHeaders,
final JsonValue jsonValue,
final HttpStatus status) {
final boolean isDittoProtocolMessage = dittoHeaders.getDittoContentType()
.filter(org.eclipse.ditto.base.model.headers.contenttype.ContentType::isDittoProtocol)
.isPresent();
if (isDittoProtocolMessage && jsonValue.isObject()) {
final CommandResponse<?> commandResponse = toCommandResponse(jsonValue.asObject());
if (commandResponse == null) {
return null;
} else if (commandResponse instanceof MessageCommandResponse) {
return (MessageCommandResponse<?, ?>) commandResponse;
} else {
connectionLogger.failure("Expected <{0}> to be of type <{1}> but was of type <{2}>.",
commandResponse, MessageCommandResponse.class.getSimpleName(),
commandResponse.getClass().getSimpleName());
return null;
}
} else {
final var commandMessage = messageCommand.getMessage();
final var messageHeaders = MessageHeadersBuilder.of(commandMessage.getHeaders())
.httpStatus(status)
.putHeaders(dittoHeaders)
.build();
final var message = Message.newBuilder(messageHeaders)
.payload(jsonValue)
.build();
switch (messageCommand.getType()) {
case SendClaimMessage.TYPE:
return SendClaimMessageResponse.of(messageCommand.getEntityId(), message, status,
dittoHeaders);
case SendThingMessage.TYPE:
return SendThingMessageResponse.of(messageCommand.getEntityId(), message, status,
dittoHeaders);
case SendFeatureMessage.TYPE:
final SendFeatureMessage<?> sendFeatureMessage = (SendFeatureMessage<?>) messageCommand;
return SendFeatureMessageResponse.of(messageCommand.getEntityId(),
sendFeatureMessage.getFeatureId(), message, status, dittoHeaders);
default:
connectionLogger.failure("Initial message command type <{0}> is unknown.",
messageCommand.getType());
return null;
}
}
}
@Nullable
private CommandResponse<?> toCommandResponse(final JsonObject jsonObject) {
final JsonifiableAdaptable jsonifiableAdaptable = ProtocolFactory.jsonifiableAdaptableFromJson(jsonObject);
final Signal<?> signal = DITTO_PROTOCOL_ADAPTER.fromAdaptable(jsonifiableAdaptable);
if (signal instanceof CommandResponse) {
return (CommandResponse<?>) signal;
} else {
connectionLogger.exception("Expected <{}> to be of type <{}> but was of type <{}>.",
jsonObject, CommandResponse.class.getSimpleName(), signal.getClass().getSimpleName());
return null;
}
}
private ConnectionFailure toConnectionFailure(@Nullable final Done done, @Nullable final Throwable error) {
return new ImmutableConnectionFailure(getSelf(), error, "HttpPublisherActor stream terminated");
}
private static DittoHeaders setDittoHeaders(final DittoHeaders dittoHeaders, final HttpResponse response) {
// Special handling of content-type because it needs to be extracted from the entity instead of the headers.
final DittoHeadersBuilder<?, ?> dittoHeadersBuilder =
dittoHeaders.toBuilder().contentType(response.entity().getContentType().toString());
response.getHeaders().forEach(header -> dittoHeadersBuilder.putHeader(header.name(), header.value()));
return dittoHeadersBuilder.build();
}
private static byte[] getPayloadAsBytes(final ExternalMessage message) {
return message.isTextMessage()
? getTextPayload(message).getBytes()
: getBytePayload(message);
}
private static String getTextPayload(final ExternalMessage message) {
return message.getTextPayload().orElse("");
}
private static byte[] getBytePayload(final ExternalMessage message) {
return message.getBytePayload().map(ByteBuffer::array).orElse(new byte[0]);
}
private static CompletionStage<JsonValue> getResponseBody(final HttpResponse response, final int maxBytes,
final Materializer materializer) {
return response.entity()
.withSizeLimit(maxBytes)
.toStrict(READ_BODY_TIMEOUT_MS, materializer)
.thenApply(strictEntity -> {
final akka.http.javadsl.model.ContentType contentType = strictEntity.getContentType();
final Charset charset = contentType.getCharsetOption()
.map(HttpCharset::nioCharset)
.orElse(StandardCharsets.UTF_8);
final byte[] bytes = strictEntity.getData().toArray();
final org.eclipse.ditto.base.model.headers.contenttype.ContentType dittoContentType =
org.eclipse.ditto.base.model.headers.contenttype.ContentType.of(contentType.toString());
if (dittoContentType.isJson()) {
final String bodyString = new String(bytes, charset);
try {
return JsonFactory.readFrom(bodyString);
} catch (final Exception e) {
return JsonValue.of(bodyString);
}
} else if (dittoContentType.isBinary()) {
final String base64bytes = Base64.getEncoder().encodeToString(bytes);
return JsonFactory.newValue(base64bytes);
} else {
// add text payload as JSON string
return JsonFactory.newValue(new String(bytes, charset));
}
});
}
private static Uri stripUserInfo(final Uri requestUri) {
return requestUri.userInfo("");
}
}