/
ImmutableInfoProvider.java
190 lines (160 loc) · 6.92 KB
/
ImmutableInfoProvider.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
/*
* 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.services.connectivity.messaging.monitoring.logs;
import java.nio.ByteBuffer;
import java.time.Instant;
import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.model.base.headers.DittoHeaderDefinition;
import org.eclipse.ditto.services.connectivity.messaging.monitoring.ConnectionMonitor;
import org.eclipse.ditto.services.models.connectivity.ExternalMessage;
import org.eclipse.ditto.signals.base.Signal;
import org.eclipse.ditto.signals.base.WithThingId;
/**
* Immutable implementation fo {@link org.eclipse.ditto.services.connectivity.messaging.monitoring.ConnectionMonitor.InfoProvider}.
*/
@Immutable
public final class ImmutableInfoProvider implements ConnectionMonitor.InfoProvider {
private static final String FALLBACK_CORRELATION_ID = "<not-provided>";
private final String correlationId;
private final Instant timestamp;
@Nullable private final String thingId;
private final Map<String, String> headers;
// a supplier to postpone getting the payload until it is really needed
private final Supplier<String> payloadSupplier;
private ImmutableInfoProvider(final String correlationId, final Instant timestamp,
@Nullable final String thingId, final Map<String, String> headers, final Supplier<String> payloadSupplier) {
this.correlationId = correlationId;
this.timestamp = timestamp;
this.thingId = thingId;
this.headers = Collections.unmodifiableMap(new HashMap<>(headers));
this.payloadSupplier = payloadSupplier;
}
/**
* Creates a new info provider that uses {@code externalMessage} for getting a correlation ID.
* @param externalMessage the external message that might contain a correlation ID.
* @return an info provider with a correlation ID.
*/
public static ConnectionMonitor.InfoProvider forExternalMessage(final ExternalMessage externalMessage) {
final String correlationId = extractCorrelationId(externalMessage.getHeaders());
final Instant timestamp = Instant.now();
final Supplier<String> payloadSupplier = supplyPayloadFromExternalMessage(externalMessage);
return new ImmutableInfoProvider(correlationId, timestamp, null, externalMessage.getHeaders(), payloadSupplier);
}
private static Supplier<String> supplyPayloadFromExternalMessage(final ExternalMessage externalMessage) {
if (externalMessage.isTextMessage()) {
return () -> externalMessage.getTextPayload().orElse("<empty-text-payload>");
}
return () -> externalMessage.getBytePayload()
.filter(ByteBuffer::hasArray)
.map(ByteBuffer::array)
.map(Base64.getEncoder()::encodeToString)
.orElse("<empty-byte-payload>");
}
/**
* Creates a new info provider that uses {@code signal} for getting a correlation ID and thing ID.
* @param signal the signal that might contain a correlation ID and thing ID.
* @return an info provider with a correlation ID and maybe a thing ID.
*/
public static ConnectionMonitor.InfoProvider forSignal(final Signal<?> signal) {
final String correlationId = extractCorrelationId(signal.getDittoHeaders());
final Instant timestamp = Instant.now();
final String thingId = extractThingId(signal);
final Supplier<String> payloadSupplier = supplyPayloadFromSignal(signal);
return new ImmutableInfoProvider(correlationId, timestamp, thingId, signal.getDittoHeaders(), payloadSupplier);
}
private static Supplier<String> supplyPayloadFromSignal(final Signal<?> signal) {
return signal::toJsonString;
}
/**
* Creates a new info provider that uses {@code headers} for getting a correlation ID.
* @param headers the headers that might contain a correlation ID.
* @return an info provider with a correlation ID.
*/
public static ConnectionMonitor.InfoProvider forHeaders(final Map<String, String> headers) {
final String correlationId = extractCorrelationId(headers);
final Instant timestamp = Instant.now();
return new ImmutableInfoProvider(correlationId, timestamp, null, headers, supplyEmptyPayload());
}
private static String extractCorrelationId(final Map<String, String> headers) {
return headers.getOrDefault(DittoHeaderDefinition.CORRELATION_ID.getKey(), FALLBACK_CORRELATION_ID);
}
@Nullable
private static String extractThingId(final Signal<?> signal) {
if (signal instanceof WithThingId) {
return ((WithThingId) signal).getThingId();
}
return null;
}
public static ConnectionMonitor.InfoProvider empty() {
return new ImmutableInfoProvider(FALLBACK_CORRELATION_ID, Instant.now(), null, Collections.emptyMap(), supplyEmptyPayload());
}
private static Supplier<String> supplyEmptyPayload() {
return () -> null;
}
@Override
public String getCorrelationId() {
return correlationId;
}
@Override
public Instant getTimestamp() {
return timestamp;
}
@Nullable
@Override
public String getThingId() {
return thingId;
}
@Override
public Map<String, String> getHeaders() {
return headers;
}
@Override
public String getPayload() {
return payloadSupplier.get();
}
@Override
public boolean equals(@Nullable final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final ImmutableInfoProvider that = (ImmutableInfoProvider) o;
return Objects.equals(correlationId, that.correlationId) &&
Objects.equals(timestamp, that.timestamp) &&
Objects.equals(thingId, that.thingId) &&
Objects.equals(headers, that.headers) &&
Objects.equals(payloadSupplier, that.payloadSupplier);
}
@Override
public int hashCode() {
return Objects.hash(correlationId, timestamp, thingId, headers, payloadSupplier);
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
", correlationId=" + correlationId +
", timestamp=" + timestamp +
", thingId=" + thingId +
", headers=" + headers +
"]";
}
}