Skip to content

Package: AbstractMqttTransport

AbstractMqttTransport

nameinstructionbranchcomplexitylinemethod
AbstractMqttTransport(GatewayConfiguration)
M: 23 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
createConnectOptions(String)
M: 49 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
plainUrl(String)
M: 20 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 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;
14:
15: import java.net.URISyntaxException;
16: import java.util.Collections;
17: import java.util.HashMap;
18: import java.util.Map;
19:
20: import org.apache.http.client.utils.URIBuilder;
21: import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
22:
23: public abstract class AbstractMqttTransport implements Transport {
24:
25: protected final Map<String, String> topicContext;
26:
27: public AbstractMqttTransport(final GatewayConfiguration configuration) {
28: final Map<String, String> topicContext = new HashMap<>();
29: topicContext.put("account-name", configuration.getAccountName());
30: topicContext.put("client-id", configuration.getClientId());
31:
32: this.topicContext = Collections.unmodifiableMap(topicContext);
33:
34: }
35:
36: protected MqttConnectOptions createConnectOptions(final String brokerUrl) {
37: try {
38: final URIBuilder u = new URIBuilder(brokerUrl);
39:
40: final MqttConnectOptions result = new MqttConnectOptions();
41: result.setAutomaticReconnect(true);
42:
43: final String ui = u.getUserInfo();
44:• if (ui != null && !ui.isEmpty()) {
45: final String[] toks = ui.split("\\:", 2);
46:• if (toks.length == 2) {
47: result.setUserName(toks[0]);
48: result.setPassword(toks[1].toCharArray());
49: }
50: }
51:
52: return result;
53: } catch (final URISyntaxException e) {
54: throw new RuntimeException("Failed to create MQTT options", e);
55:
56: }
57: }
58:
59: protected static String plainUrl(final String brokerUrl) {
60: try {
61: final URIBuilder u = new URIBuilder(brokerUrl);
62: u.setUserInfo(null);
63: return u.build().toString();
64: } catch (final URISyntaxException e) {
65: throw new RuntimeException("Failed to clean up broker URL", e);
66: }
67: }
68:
69: }