/
AbstractMappingResultHandler.java
165 lines (139 loc) · 5.72 KB
/
AbstractMappingResultHandler.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
/*
* 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;
import static org.eclipse.ditto.model.base.common.ConditionChecker.checkNotNull;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.eclipse.ditto.model.base.exceptions.DittoRuntimeException;
import org.eclipse.ditto.services.connectivity.messaging.monitoring.ConnectionMonitor;
import org.eclipse.ditto.services.models.connectivity.MappedInboundExternalMessage;
import org.eclipse.ditto.signals.base.Signal;
import akka.stream.javadsl.Source;
/**
* {@link MappingResultHandler} for inbound messages. This handler forwards to the given handlers. Additionally it
* calls the {@link MappingResultHandler#onException(Exception)} method for exceptions thrown in handlers and
* increases the according counters for mapped, dropped failed messages.
*
* @param <R> type of results.
*/
final class InboundMappingResultHandler<R> implements MappingResultHandler<MappedInboundExternalMessage, R> {
private final Function<MappedInboundExternalMessage, R> onMessageMapped;
private final Runnable onMessageDropped;
private final Consumer<Exception> onException;
private final R emptyResult;
private final BinaryOperator<R> combineResults;
private final ConnectionMonitor inboundMapped;
private final ConnectionMonitor inboundDropped;
private final ConnectionMonitor.InfoProvider infoProvider;
private InboundMappingResultHandler(final Builder<R> builder) {
onMessageMapped = checkNotNull(builder.onMessageMapped, "onMessageMapped");
onMessageDropped = checkNotNull(builder.onMessageDropped, "onMessageDropped");
onException = checkNotNull(builder.onException, "onException");
inboundMapped = checkNotNull(builder.inboundMapped, "inboundMapped");
inboundDropped = checkNotNull(builder.inboundDropped, "inboundDropped");
infoProvider = checkNotNull(builder.infoProvider, "infoProvider");
emptyResult = checkNotNull(builder.emptyResult, "emptyResult");
combineResults = checkNotNull(builder.combineResults, "combineResults");
}
static <S> Builder<Source<S, ?>> newSourceBuilder() {
return new Builder<Source<S, ?>>()
.emptyResult(Source.empty())
.combineResults(Source::concat);
}
@Override
public R onMessageMapped(final MappedInboundExternalMessage inboundExternalMessage) {
try {
inboundMapped.success(infoProvider);
return onMessageMapped.apply(inboundExternalMessage);
} catch (final Exception e) {
return onException(e);
}
}
@Override
public R onMessageDropped() {
try {
inboundDropped.success(infoProvider);
onMessageDropped.run();
return emptyResult;
} catch (Exception e) {
return onException(e);
}
}
@Override
public R onException(final Exception exception) {
if (exception instanceof DittoRuntimeException) {
inboundMapped.failure(((DittoRuntimeException) exception));
} else {
inboundMapped.exception(exception);
}
onException.accept(exception);
return emptyResult;
}
@Override
public R combineResults(final R left, final R right) {
return combineResults.apply(left, right);
}
@Override
public R emptyResult() {
return emptyResult;
}
static final class Builder<R> {
private ConnectionMonitor inboundMapped;
private ConnectionMonitor inboundDropped;
private ConnectionMonitor.InfoProvider infoProvider;
private Function<MappedInboundExternalMessage, R> onMessageMapped;
private Runnable onMessageDropped;
private Consumer<Exception> onException;
private R emptyResult;
private BinaryOperator<R> combineResults;
private Builder() {}
InboundMappingResultHandler<R> build() {
return new InboundMappingResultHandler<>(this);
}
Builder<R> inboundMapped(final ConnectionMonitor inboundMapped) {
this.inboundMapped = inboundMapped;
return this;
}
Builder<R> inboundDropped(final ConnectionMonitor inboundDropped) {
this.inboundDropped = inboundDropped;
return this;
}
Builder<R> infoProvider(final ConnectionMonitor.InfoProvider infoProvider) {
this.infoProvider = infoProvider;
return this;
}
Builder<R> onMessageMapped(
final Function<MappedInboundExternalMessage, R> onMessageMapped) {
this.onMessageMapped = onMessageMapped;
return this;
}
Builder<R> onMessageDropped(final Runnable onMessageDropped) {
this.onMessageDropped = onMessageDropped;
return this;
}
Builder<R> onException(final Consumer<Exception> onException) {
this.onException = onException;
return this;
}
Builder<R> emptyResult(final R emptyResult) {
this.emptyResult = emptyResult;
return this;
}
Builder<R> combineResults(final BinaryOperator<R> combineResults) {
this.combineResults = combineResults;
return this;
}
}
}