Skip to content

Package: AbstractTranslatorKapuaKura

AbstractTranslatorKapuaKura

nameinstructionbranchcomplexitylinemethod
AbstractTranslatorKapuaKura()
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%
getControlMessageClassifier()
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%
getJsonMapper()
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%
lambda$translate$0(KapuaMessage)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 26 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
translate(KapuaMessage)
M: 59 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2017, 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: * Red Hat Inc
13: *******************************************************************************/
14: package org.eclipse.kapua.translator.kapua.kura;
15:
16: import com.fasterxml.jackson.annotation.JsonInclude;
17: import com.fasterxml.jackson.databind.DeserializationFeature;
18: import com.fasterxml.jackson.databind.ObjectMapper;
19: import org.eclipse.kapua.commons.security.KapuaSecurityUtils;
20: import org.eclipse.kapua.commons.setting.system.SystemSetting;
21: import org.eclipse.kapua.locator.KapuaLocator;
22: import org.eclipse.kapua.message.KapuaChannel;
23: import org.eclipse.kapua.message.KapuaMessage;
24: import org.eclipse.kapua.message.KapuaPayload;
25: import org.eclipse.kapua.service.account.Account;
26: import org.eclipse.kapua.service.account.AccountService;
27: import org.eclipse.kapua.service.device.call.message.kura.app.request.KuraRequestChannel;
28: import org.eclipse.kapua.service.device.call.message.kura.app.request.KuraRequestMessage;
29: import org.eclipse.kapua.service.device.call.message.kura.app.request.KuraRequestPayload;
30: import org.eclipse.kapua.service.device.registry.Device;
31: import org.eclipse.kapua.service.device.registry.DeviceRegistryService;
32: import org.eclipse.kapua.translator.Translator;
33: import org.eclipse.kapua.translator.exception.InvalidChannelException;
34: import org.eclipse.kapua.translator.exception.InvalidMessageException;
35: import org.eclipse.kapua.translator.exception.InvalidPayloadException;
36: import org.eclipse.kapua.translator.exception.TranslateException;
37:
38: /**
39: * {@link Translator} abstract implementation from {@link KapuaMessage} to {@link KuraRequestMessage}
40: *
41: * @since 1.0.0
42: */
43: public abstract class AbstractTranslatorKapuaKura<FROM_C extends KapuaChannel, FROM_P extends KapuaPayload, FROM_M extends KapuaMessage<FROM_C, FROM_P>> extends Translator<FROM_M, KuraRequestMessage> {
44:
45: private static final String CONTROL_MESSAGE_CLASSIFIER = SystemSetting.getInstance().getMessageClassifier();
46:
47: private static final ObjectMapper JSON_MAPPER = new ObjectMapper()
48: .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
49: .enable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
50: .setSerializationInclusion(JsonInclude.Include.NON_NULL);
51:
52: private static final KapuaLocator LOCATOR = KapuaLocator.getInstance();
53:
54: private static final AccountService ACCOUNT_SERVICE = LOCATOR.getService(AccountService.class);
55: private static final DeviceRegistryService DEVICE_REGISTRY_SERVICE = LOCATOR.getService(DeviceRegistryService.class);
56:
57: @Override
58: public KuraRequestMessage translate(FROM_M kapuaMessage) throws TranslateException {
59: try {
60: Account account = KapuaSecurityUtils.doPrivileged(() -> ACCOUNT_SERVICE.find(kapuaMessage.getScopeId()));
61:
62: Device device = null;
63:• if (kapuaMessage.getDeviceId() != null) {
64: device = DEVICE_REGISTRY_SERVICE.find(kapuaMessage.getScopeId(), kapuaMessage.getDeviceId());
65: }
66:
67: KuraRequestChannel kuraRequestChannel = translateChannel(kapuaMessage.getChannel());
68: kuraRequestChannel.setScope(account.getName());
69:• kuraRequestChannel.setClientId(device != null ? device.getClientId() : kapuaMessage.getClientId());
70:
71: //
72: // Kura payload
73: KuraRequestPayload kuraPayload = translatePayload(kapuaMessage.getPayload());
74:
75: //
76: // Return Kura Message
77: return new KuraRequestMessage(kuraRequestChannel, kapuaMessage.getReceivedOn(), kuraPayload);
78: } catch (InvalidChannelException | InvalidPayloadException te) {
79: throw te;
80: } catch (Exception e) {
81: throw new InvalidMessageException(e, kapuaMessage);
82: }
83: }
84:
85: /**
86: * Gets the value from {@link SystemSetting#getMessageClassifier()}
87: *
88: * @return The value from {@link SystemSetting#getMessageClassifier()}
89: * @since 1.2.0
90: */
91: protected static String getControlMessageClassifier() {
92: return CONTROL_MESSAGE_CLASSIFIER;
93: }
94:
95: protected abstract KuraRequestChannel translateChannel(FROM_C kapuaChannel) throws InvalidChannelException;
96:
97: protected abstract KuraRequestPayload translatePayload(FROM_P kapuaPayload) throws InvalidPayloadException;
98:
99: /**
100: * Returns the {@link ObjectMapper} instance.
101: * <p>
102: * This can be also used to change the {@link ObjectMapper}'s configuration.
103: *
104: * @return The {@link ObjectMapper} instance.
105: * @since 1.5.0
106: */
107: protected ObjectMapper getJsonMapper() {
108: return JSON_MAPPER;
109: }
110:
111: }