Skip to content

Package: AbstractTranslatorJmsKura

AbstractTranslatorJmsKura

nameinstructionbranchcomplexitylinemethod
AbstractTranslatorJmsKura(Class)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getClassFrom()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getClassTo()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isRawPayloadToBody()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
translate(JmsMessage)
M: 23 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
translate(JmsPayload)
M: 31 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2020, 2022 Eurotech and/or its affiliates 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: * Eurotech - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.translator.jms.kura;
14:
15: import org.eclipse.kapua.message.internal.MessageException;
16: import org.eclipse.kapua.service.device.call.kura.Kura;
17: import org.eclipse.kapua.service.device.call.message.DeviceChannel;
18: import org.eclipse.kapua.service.device.call.message.DeviceMessage;
19: import org.eclipse.kapua.service.device.call.message.DevicePayload;
20: import org.eclipse.kapua.service.device.call.message.kura.KuraPayload;
21: import org.eclipse.kapua.translator.Translator;
22: import org.eclipse.kapua.translator.exception.InvalidChannelException;
23: import org.eclipse.kapua.translator.exception.InvalidMessageException;
24: import org.eclipse.kapua.translator.exception.InvalidPayloadException;
25: import org.eclipse.kapua.translator.exception.TranslateException;
26: import org.eclipse.kapua.transport.message.jms.JmsMessage;
27: import org.eclipse.kapua.transport.message.jms.JmsPayload;
28: import org.eclipse.kapua.transport.message.jms.JmsTopic;
29:
30: import java.util.Date;
31:
32: /**
33: * {@link Translator} {@code abstract} base implementation from {@link JmsMessage} to {@link DeviceMessage}.
34: *
35: * @param <C> The {@link DeviceChannel} type.
36: * @param <P> The {@link DevicePayload} type.
37: * @param <M> The {@link DeviceMessage} type.
38: * @since 2.0.0
39: */
40: public abstract class AbstractTranslatorJmsKura<C extends DeviceChannel, P extends DevicePayload, M extends DeviceMessage<C, P>>
41: extends Translator<JmsMessage, M> {
42:
43: /**
44: * The {@link DeviceMessage} type.
45: *
46: * @since 2.0.0
47: */
48: private final Class<M> messageClazz;
49:
50: /**
51: * Constructor.
52: * <p>
53: * Defines the {@link DeviceMessage} type.
54: *
55: * @param messageClazz The specific {@link DeviceMessage} type.
56: * @since 2.0.0
57: */
58: public AbstractTranslatorJmsKura(Class<M> messageClazz) {
59: this.messageClazz = messageClazz;
60: }
61:
62: @Override
63: public M translate(JmsMessage jmsMessage) throws TranslateException {
64: try {
65: return createMessage(translate(jmsMessage.getTopic()),
66: jmsMessage.getReceivedOn(),
67: translate(jmsMessage.getPayload()));
68: } catch (InvalidChannelException | InvalidPayloadException te) {
69: throw te;
70: } catch (Exception e) {
71: throw new InvalidMessageException(e, jmsMessage);
72: }
73: }
74:
75: /**
76: * Translates the given {@link JmsTopic}.
77: * <p>
78: * Checks that the {@link JmsTopic#getSplittedTopic()} has at least 3 tokens.
79: *
80: * @param jmsTopic The {@link JmsTopic} to translate
81: * @return The translated {@link DeviceChannel}
82: * @throws InvalidChannelException in case that @link JmsTopic#getSplittedTopic()} has less than 3 tokens.
83: * @since 2.0.0
84: */
85: public abstract C translate(JmsTopic jmsTopic) throws InvalidChannelException;
86:
87: /**
88: * Translates the given {@link JmsPayload}
89: *
90: * @param jmsPayload the {@link JmsPayload} to translate.
91: * @return The translated {@link DevicePayload}
92: * @throws InvalidPayloadException in case that {@link JmsPayload#getBody()} is not {@link Kura} protobuf encoded and {@link #isRawPayloadToBody()} is {@code false}.
93: * @since 1.2.0
94: */
95: public P translate(JmsPayload jmsPayload) throws InvalidPayloadException {
96: try {
97: P kuraPayload = createPayload();
98:
99:• if (jmsPayload.hasBody()) {
100:
101: try {
102: kuraPayload.readFromByteArray(jmsPayload.getBody());
103: } catch (MessageException me) {
104:• if (isRawPayloadToBody()) {
105: kuraPayload.setBody(jmsPayload.getBody());
106: } else {
107: throw me;
108: }
109: }
110: }
111:
112: return kuraPayload;
113: } catch (Exception e) {
114: throw new InvalidPayloadException(e, jmsPayload);
115: }
116: }
117:
118: /**
119: * Whether a non-protobuf encoded {@link JmsPayload#getBody()} must be set as {@link KuraPayload#getBody()}.
120: * If not an exception is thrown.
121: * Defaults to {@link Boolean#FALSE}.
122: *
123: * @return {@code true} if a non-protobuf encoded {@link JmsPayload#getBody()} must be set as {@link KuraPayload#getBody()}, {@code false} otherwise
124: * @since 1.2.0
125: */
126: public boolean isRawPayloadToBody() {
127: return Boolean.FALSE;
128: }
129:
130: /**
131: * Instantiates the specific {@link DeviceMessage} with the given parameters.
132: *
133: * @param deviceChannel The {@link DeviceChannel}.
134: * @param receivedOn The timestamp of the {@link JmsMessage#getReceivedOn()}
135: * @param devicePayload The {@link DevicePayload}.
136: * @return The newly created {@link DeviceMessage}
137: * @since 2.0.0
138: */
139: public abstract M createMessage(C deviceChannel, Date receivedOn, P devicePayload);
140:
141: /**
142: * Instantiates the specific {@link DeviceChannel} with the given parameters.
143: *
144: * @param messageClassifier The message classification.
145: * @param scopeName The scope namespace.
146: * @param clientId The clientId.
147: * @return The newly instantiated {@link DeviceChannel}.
148: * @since 2.0.0
149: */
150: public abstract C createChannel(String messageClassifier, String scopeName, String clientId);
151:
152: /**
153: * Instantiates the specific {@link DevicePayload}.
154: *
155: * @return The newly instantiated {@link DevicePayload}.
156: * @since 1.2.0
157: */
158: public abstract P createPayload();
159:
160: @Override
161: public Class<JmsMessage> getClassFrom() {
162: return JmsMessage.class;
163: }
164:
165: @Override
166: public Class<M> getClassTo() {
167: return messageClazz;
168: }
169: }
170:
171: