Commit 51302f212015f33e45c27c3d679b586d393ddc4f
Committed by
GitHub
Merge pull request #1123 from kingbom/help_refactor_code_solid_patterns
[WIP]Help refactor code solid patterns
Showing
1 changed file
with
77 additions
and
29 deletions
@@ -50,38 +50,86 @@ public class MqttSslClient { | @@ -50,38 +50,86 @@ public class MqttSslClient { | ||
50 | File ksFile = new File(ksUrl.toURI()); | 50 | File ksFile = new File(ksUrl.toURI()); |
51 | URL tsUrl = Resources.getResource(KEY_STORE_FILE); | 51 | URL tsUrl = Resources.getResource(KEY_STORE_FILE); |
52 | File tsFile = new File(tsUrl.toURI()); | 52 | File tsFile = new File(tsUrl.toURI()); |
53 | + char[] ksPwd = setKeyPassword(); | ||
54 | + | ||
55 | + KeyStore trustStore = setKeyStore(tsFile, ksPwd); | ||
56 | + | ||
57 | + TrustManagerFactory tmf = setTrustManagerFactory(trustStore); | ||
58 | + | ||
59 | + KeyStore ks = setKeyStore2(ksFile, ksPwd); | ||
60 | + | ||
61 | + KeyManagerFactory kmf = setKeyManagerFactory(ks); | ||
62 | + | ||
63 | + SSLContext sslContext = setSSLContext(kmf, tmf); | ||
64 | + | ||
65 | + MqttConnectOptions mqttConnectOptions = setMqttConnectOptions(sslContext); | ||
66 | + | ||
67 | + MqttAsyncClient mqttAsyncClient = setMqttAsyncClient(mqttConnectOptions); | ||
68 | + | ||
69 | + setMessage(mqttAsyncClient, "v1/devices/me/telemetry", "{\"key1\":\"value1\", \"key2\":true, \"key3\": 3.0, \"key4\": 4}"); | ||
53 | 70 | ||
54 | - TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); | ||
55 | - | ||
56 | - KeyStore trustStore = KeyStore.getInstance(JKS); | ||
57 | - char[] ksPwd = new char[]{0x63, 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x5F, 0x6B, 0x73, 0x5F, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6F, 0x72, 0x64}; | ||
58 | - trustStore.load(new FileInputStream(tsFile), ksPwd); | ||
59 | - tmf.init(trustStore); | ||
60 | - KeyStore ks = KeyStore.getInstance(JKS); | ||
61 | - | ||
62 | - ks.load(new FileInputStream(ksFile), ksPwd); | ||
63 | - KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); | ||
64 | - char[] clientPwd = new char[]{0x63, 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x5F, 0x6B, 0x65, 0x79, 0x5F, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6F, 0x72, 0x64}; | ||
65 | - kmf.init(ks, clientPwd); | ||
66 | - | ||
67 | - KeyManager[] km = kmf.getKeyManagers(); | ||
68 | - TrustManager[] tm = tmf.getTrustManagers(); | ||
69 | - SSLContext sslContext = SSLContext.getInstance(TLS); | ||
70 | - sslContext.init(km, tm, null); | ||
71 | - | ||
72 | - MqttConnectOptions options = new MqttConnectOptions(); | ||
73 | - options.setSocketFactory(sslContext.getSocketFactory()); | ||
74 | - MqttAsyncClient client = new MqttAsyncClient(MQTT_URL, CLIENT_ID); | ||
75 | - client.connect(options); | ||
76 | - Thread.sleep(3000); | ||
77 | - MqttMessage message = new MqttMessage(); | ||
78 | - message.setPayload("{\"key1\":\"value1\", \"key2\":true, \"key3\": 3.0, \"key4\": 4}".getBytes()); | ||
79 | - client.publish("v1/devices/me/telemetry", message); | ||
80 | - client.disconnect(); | ||
81 | - log.info("Disconnected"); | ||
82 | - System.exit(0); | ||
83 | } catch (Exception e) { | 71 | } catch (Exception e) { |
84 | log.error("Unexpected exception occurred in MqttSslClient", e); | 72 | log.error("Unexpected exception occurred in MqttSslClient", e); |
85 | } | 73 | } |
86 | } | 74 | } |
75 | + | ||
76 | + private static char[] setKeyPassword(){ | ||
77 | + return new char[]{0x63, 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x5F, 0x6B, 0x73, 0x5F, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6F, 0x72, 0x64}; | ||
78 | + } | ||
79 | + | ||
80 | + private static KeyStore setKeyStore(File tsFile, char[] ksPwd) throws Exception{ | ||
81 | + KeyStore trustStore = KeyStore.getInstance(JKS); | ||
82 | + trustStore.load(new FileInputStream(tsFile), ksPwd); | ||
83 | + return trustStore; | ||
84 | + } | ||
85 | + | ||
86 | + private static TrustManagerFactory setTrustManagerFactory(KeyStore trustStore) throws Exception { | ||
87 | + TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); | ||
88 | + tmf.init(trustStore); | ||
89 | + return tmf; | ||
90 | + } | ||
91 | + | ||
92 | + private static KeyStore setKeyStore2(File ksFile, char[] ksPwd)throws Exception{ | ||
93 | + KeyStore ks = KeyStore.getInstance(JKS); | ||
94 | + ks.load(new FileInputStream(ksFile), ksPwd); | ||
95 | + return ks; | ||
96 | + } | ||
97 | + | ||
98 | + private static KeyManagerFactory setKeyManagerFactory(KeyStore ks) throws Exception { | ||
99 | + KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); | ||
100 | + char[] clientPwd = new char[]{0x63, 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x5F, 0x6B, 0x65, 0x79, 0x5F, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6F, 0x72, 0x64}; | ||
101 | + kmf.init(ks, clientPwd); | ||
102 | + return kmf; | ||
103 | + } | ||
104 | + | ||
105 | + private static SSLContext setSSLContext(KeyManagerFactory kmf, TrustManagerFactory tmf) throws Exception { | ||
106 | + KeyManager[] km = kmf.getKeyManagers(); | ||
107 | + TrustManager[] tm = tmf.getTrustManagers(); | ||
108 | + SSLContext sslContext = SSLContext.getInstance(TLS); | ||
109 | + sslContext.init(km, tm, null); | ||
110 | + return sslContext; | ||
111 | + } | ||
112 | + | ||
113 | + private static MqttConnectOptions setMqttConnectOptions(SSLContext sslContext){ | ||
114 | + MqttConnectOptions options = new MqttConnectOptions(); | ||
115 | + options.setSocketFactory(sslContext.getSocketFactory()); | ||
116 | + return options; | ||
117 | + } | ||
118 | + | ||
119 | + private static MqttAsyncClient setMqttAsyncClient(MqttConnectOptions options) throws Exception{ | ||
120 | + MqttAsyncClient client = new MqttAsyncClient(MQTT_URL, CLIENT_ID); | ||
121 | + client.connect(options); | ||
122 | + Thread.sleep(3000); | ||
123 | + return client; | ||
124 | + } | ||
125 | + | ||
126 | + private static void setMessage(MqttAsyncClient client, String topic, String payload) throws Exception { | ||
127 | + MqttMessage message = new MqttMessage(); | ||
128 | + message.setPayload(payload.getBytes()); | ||
129 | + client.publish(topic, message); | ||
130 | + client.disconnect(); | ||
131 | + log.info("Disconnected"); | ||
132 | + System.exit(0); | ||
133 | + } | ||
134 | + | ||
87 | } | 135 | } |