Skip to content

Package: Request$2

Request$2

nameinstructionbranchcomplexitylinemethod
send(KuraPayloadProto.KuraPayload.Builder)
M: 18 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
{...}
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2017, 2022 Red Hat Inc and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Red Hat Inc - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.kura.simulator.app;
14:
15: import org.apache.commons.lang3.exception.ExceptionUtils;
16: import org.eclipse.kapua.kura.simulator.payload.Message;
17: import org.eclipse.kapua.kura.simulator.payload.Metrics;
18: import org.eclipse.kapua.kura.simulator.proto.KuraPayloadProto;
19: import org.eclipse.kapua.kura.simulator.proto.KuraPayloadProto.KuraPayload;
20: import org.eclipse.kapua.kura.simulator.proto.KuraPayloadProto.KuraPayload.Builder;
21: import org.eclipse.kapua.kura.simulator.proto.KuraPayloadProto.KuraPayload.KuraMetricOrBuilder;
22: import org.eclipse.kapua.kura.simulator.topic.Topic;
23:
24: import java.util.Collections;
25: import java.util.HashMap;
26: import java.util.Map;
27: import java.util.Objects;
28:
29: public class Request {
30:
31: private static final String NL = System.lineSeparator();
32:
33: private final ApplicationContext applicationContext;
34: private final Message message;
35: private final Map<String, Object> metrics;
36: private final String requestId;
37: private final String requesterClientId;
38:
39: public Request(final ApplicationContext applicationContext, final Message message,
40: final Map<String, Object> metrics, final String requestId, final String requesterClientId) {
41:
42: Objects.requireNonNull(applicationContext);
43: Objects.requireNonNull(message);
44: Objects.requireNonNull(metrics);
45: Objects.requireNonNull(requestId);
46: Objects.requireNonNull(requesterClientId);
47:
48: this.applicationContext = applicationContext;
49: this.message = message;
50: this.metrics = metrics;
51: this.requestId = requestId;
52: this.requesterClientId = requesterClientId;
53: }
54:
55: public ApplicationContext getApplicationContext() {
56: return this.applicationContext;
57: }
58:
59: public Message getMessage() {
60: return this.message;
61: }
62:
63: public Map<String, Object> getMetrics() {
64: return Collections.unmodifiableMap(this.metrics);
65: }
66:
67: public String getRequesterClientId() {
68: return this.requesterClientId;
69: }
70:
71: public String getRequestId() {
72: return this.requestId;
73: }
74:
75: public String renderTopic(final int index) {
76: return this.message.getTopic().render(index, index + 1);
77: }
78:
79: public String renderTopic(final int fromIndex, final int toIndex) {
80: return this.message.getTopic().render(fromIndex, toIndex);
81: }
82:
83: public static Request parse(final ApplicationContext context, final Message message) throws Exception {
84:
85: final KuraPayload payload = KuraPayloadProto.KuraPayload.parseFrom(message.getPayload());
86:
87: final Map<String, Object> metrics = Metrics.extractMetrics(payload);
88:
89: final String requestId = Metrics.getAsString(metrics, Metrics.KEY_REQUEST_ID);
90: if (requestId == null) {
91: throw new IllegalArgumentException("Request ID (" + Metrics.KEY_REQUEST_ID + ") missing in message");
92: }
93:
94: final String requesterClientId = Metrics.getAsString(metrics, Metrics.KEY_REQUESTER_CLIENT_ID);
95: if (requesterClientId == null) {
96: throw new IllegalArgumentException(
97: "Requester Client ID (" + Metrics.KEY_REQUESTER_CLIENT_ID + ") missing in message");
98: }
99:
100: return new Request(context, message, metrics, requestId, requesterClientId);
101: }
102:
103: /**
104: * Get a success reply sender
105: * <p>
106: * <strong>Note:</strong> A reply will only be send when one of the
107: * {@code send} methods of the result was invoked.
108: * </p>
109: *
110: * @return a new sender, never returns {@code null}
111: */
112: public Sender replySuccess() {
113: return reply(200);
114: }
115:
116: /**
117: * Get a error reply sender
118: * <p>
119: * <strong>Note:</strong> A reply will only be send when one of the
120: * {@code send} methods of the result was invoked.
121: * </p>
122: *
123: * @return a new sender, never returns {@code null}
124: */
125: public Sender replyError() {
126: return reply(500);
127: }
128:
129: /**
130: * Get a reply sender
131: * <p>
132: * <strong>Note:</strong> A reply will only be send when one of the
133: * {@code send} methods of the result was invoked.
134: * </p>
135: *
136: * @return a new sender, never returns {@code null}
137: */
138: public Sender reply(final int responseCode) {
139: return new Sender() {
140:
141: @Override
142: public void send(final KuraPayload.Builder payload) {
143:
144: // check for existing response code metric
145:
146: for (final KuraMetricOrBuilder metric : payload.getMetricOrBuilderList()) {
147: if (metric.getName().equals(Metrics.KEY_RESPONSE_CODE)) {
148: throw new IllegalArgumentException(
149: String.format("Metrics must not already contain '%s'", Metrics.KEY_RESPONSE_CODE));
150: }
151: }
152:
153: // add response code
154:
155: Metrics.addMetric(payload, Metrics.KEY_RESPONSE_CODE, responseCode);
156:
157: Request.this.applicationContext
158: .sender(Topic.reply(Request.this.requesterClientId, Request.this.requestId))
159: .send(payload);
160: }
161: };
162: }
163:
164: /**
165: * Get a notification sender
166: * <p>
167: * <strong>Note:</strong> A reply will only be send when one of the
168: * {@code send} methods of the result was invoked.
169: * </p>
170: *
171: * @return a new sender, never returns {@code null}
172: */
173: public Sender notification(final String resource) {
174: return new Sender() {
175:
176: @Override
177: public void send(final Builder payload) {
178: Request.this.applicationContext
179: .sender(Topic.notify(Request.this.requesterClientId, resource))
180: .send(payload);
181: }
182: };
183: }
184:
185: /**
186: * Send an error reply
187: */
188: public void replyError(final Throwable error) {
189: final Map<String, Object> metrics = new HashMap<>();
190: if (error != null) {
191: metrics.put(Metrics.KEY_RESPONSE_EXCEPTION_MESSAGE, ExceptionUtils.getRootCauseMessage(error));
192: metrics.put(Metrics.KEY_RESPONSE_EXCEPTION_STACKTRACE, ExceptionUtils.getStackTrace(error));
193: }
194: reply(500).send(metrics);
195: }
196:
197: /**
198: * Send a "not found" reply
199: */
200: public void replyNotFound() {
201: reply(404).send(Collections.emptyMap());
202: }
203:
204: @Override
205: public String toString() {
206: final StringBuilder sb = new StringBuilder();
207:
208: sb.append("[Request - ").append(this.message.getTopic());
209:
210: if (!this.metrics.isEmpty()) {
211: sb.append(NL);
212: }
213:
214: for (final Map.Entry<String, Object> entry : this.metrics.entrySet()) {
215: final Object value = entry.getValue();
216:
217: sb.append("\t");
218: sb.append(entry.getKey()).append(" => ");
219: if (value != null) {
220: sb.append(value.getClass().getSimpleName()).append(" : ").append(value);
221: } else {
222: sb.append("null");
223: }
224: sb.append(NL);
225: }
226: sb.append("]");
227:
228: return sb.toString();
229: }
230: }