@Test(expected = MqttException.class) public void checkClientAuthenticationFail() throws Exception { LOG.info("*** checkClientAuthenticationFail ***"); SSLSocketFactory ssf = configureSSLSocketFactory("unsignedclientkeystore.jks"); MqttConnectOptions options = new MqttConnectOptions(); options.setSocketFactory(ssf); // actual a "Broken pipe" is thrown, this is not very specific. try { m_client.connect(options); } catch (MqttException e) { e.printStackTrace(); throw e; } }
@Test public void checkSupportSSL() throws Exception { LOG.info("*** checkSupportSSL ***"); SSLSocketFactory ssf = configureSSLSocketFactory(); MqttConnectOptions options = new MqttConnectOptions(); options.setSocketFactory(ssf); m_client.connect(options); m_client.subscribe("/topic", 0); m_client.disconnect(); }
@Test public void checkClientAuthentication() throws Exception { LOG.info("*** checkClientAuthentication ***"); SSLSocketFactory ssf = configureSSLSocketFactory("signedclientkeystore.jks"); MqttConnectOptions options = new MqttConnectOptions(); options.setSocketFactory(ssf); m_client.connect(options); m_client.subscribe("/topic", 0); m_client.disconnect(); }
@Test public void checkSupportSSLForMultipleClient() throws Exception { LOG.info("*** checkSupportSSLForMultipleClient ***"); SSLSocketFactory ssf = configureSSLSocketFactory(); MqttConnectOptions options = new MqttConnectOptions(); options.setSocketFactory(ssf); m_client.connect(options); m_client.subscribe("/topic", 0); MqttClient secondClient = new MqttClient("ssl://localhost:8883", "secondTestClient", new MemoryPersistence()); MqttConnectOptions secondClientOptions = new MqttConnectOptions(); secondClientOptions.setSocketFactory(ssf); secondClient.connect(secondClientOptions); secondClient.publish("/topic", new MqttMessage("message".getBytes(UTF_8))); secondClient.disconnect(); m_client.disconnect(); }
/** * Set the socketFactory. * @param socketFactory the socketFactory to set. * @deprecated use {@link #setConnectionOptions(MqttConnectOptions)} instead. */ @Deprecated public void setSocketFactory(SocketFactory socketFactory) { this.options.setSocketFactory(socketFactory); }
public MqttClient(String clientId, SSLSocketFactory sslSocketFactory) { this(clientId); connectOptions.setSocketFactory(sslSocketFactory); }
options.setSocketFactory(clientSocketFactory);
factory.setPersistence(persistence); final SocketFactory socketFactory = SocketFactory.getDefault(); connectOptions.setSocketFactory(socketFactory); final Properties props = new Properties(); connectOptions.setSSLProperties(props);
factory.setPersistence(persistence); final SocketFactory socketFactory = SocketFactory.getDefault(); connectOptions.setSocketFactory(socketFactory); final Properties props = new Properties(); connectOptions.setSSLProperties(props);
options.setSocketFactory(clientSocketFactory);
factory.setPersistence(persistence); final SocketFactory socketFactory = SocketFactory.getDefault(); connectOptions.setSocketFactory(socketFactory); final Properties props = new Properties(); connectOptions.setSSLProperties(props);
options.setSocketFactory(mqttClient.getSSLSocketFactory(this.getContext().getAssets().open("test.bks"),keyStorePwd));
options.setSocketFactory(mqttClient.getSSLSocketFactory(this.getContext().getAssets().open("test.bks"), keyStorePwd));
options.setSocketFactory(mqttClient.getSSLSocketFactory(this.getContext().getAssets().open("test.bks"),keyStorePwd));
options.setSocketFactory(mqttClient.getSSLSocketFactory(this.getContext().getAssets().open("test.bks"), keyStorePwd));
/** * Generates the connection options for the mqtt broker connection. * * @param userName the user name for the mqtt broker connection. * @param userPassword the user password for the mqtt broker connection. */ private void updateConnectionOptions(String userName, String userPassword, SSLContext sslContext) { this.connectionOptions.setKeepAliveInterval(KEEP_ALIVE_INTERVAL); this.connectionOptions.setCleanSession(SET_CLEAN_SESSION); this.connectionOptions.setMqttVersion(MQTT_VERSION); this.connectionOptions.setUserName(userName); this.connectionOptions.setSocketFactory(sslContext.getSocketFactory()); if (userPassword != null && !userPassword.isEmpty()) { this.connectionOptions.setPassword(userPassword.toCharArray()); } }
/** * Generates the connection options for the mqtt broker connection. * * @param userName the user name for the mqtt broker connection. * @param userPassword the user password for the mqtt broker connection. */ private void updateConnectionOptions(String userName, String userPassword, SSLContext iotHubSSLContext) { this.connectionOptions.setKeepAliveInterval(KEEP_ALIVE_INTERVAL); this.connectionOptions.setCleanSession(SET_CLEAN_SESSION); this.connectionOptions.setMqttVersion(MQTT_VERSION); this.connectionOptions.setUserName(userName); this.connectionOptions.setSocketFactory(iotHubSSLContext.getSocketFactory()); if (userPassword != null && !userPassword.isEmpty()) { this.connectionOptions.setPassword(userPassword.toCharArray()); } }
private static MqttClient connect(String clientId, String userName, String password) throws MqttException { MemoryPersistence persistence = new MemoryPersistence(); MqttConnectOptions connOpts = new MqttConnectOptions(); connOpts.setCleanSession(true); connOpts.setUserName(userName); connOpts.setPassword(password.toCharArray()); connOpts.setConnectionTimeout(10); connOpts.setKeepAliveInterval(20); SSLSocketFactory socketFactory = SecureSocketSslContextFactory.getClientContext().getSocketFactory(); connOpts.setSocketFactory(socketFactory); connOpts.setWill("/test","haha".getBytes(),0,false); // String[] uris = {"tcp://10.100.124.206:1883","tcp://10.100.124.207:1883"}; // connOpts.setServerURIs(uris); //起到负载均衡和高可用的作用 MqttClient mqttClient = new MqttClient(broker, clientId, persistence); mqttClient.setCallback(new PushCallback("test")); mqttClient.connect(connOpts); return mqttClient; }
private static MqttClient connect(String clientId, String userName, String password) throws MqttException { MemoryPersistence persistence = new MemoryPersistence(); MqttConnectOptions connOpts = new MqttConnectOptions(); connOpts.setCleanSession(false); connOpts.setUserName(userName); connOpts.setPassword(password.toCharArray()); connOpts.setConnectionTimeout(10); connOpts.setKeepAliveInterval(20); SSLSocketFactory socketFactory = SecureSocketSslContextFactory.getClientContext().getSocketFactory(); connOpts.setSocketFactory(socketFactory); // String[] uris = {"tcp://10.100.124.206:1883","tcp://10.100.124.207:1883"}; // connOpts.setServerURIs(uris); //起到负载均衡和高可用的作用 MqttClient mqttClient = new MqttClient(broker, clientId, persistence); mqttClient.setCallback(new PushCallback("test")); mqttClient.connect(connOpts); return mqttClient; }
@Test public void connection(TestContext context) { this.async = context.async(); try { MemoryPersistence persistence = new MemoryPersistence(); MqttClient client = new MqttClient(String.format("ssl://%s:%d", MQTT_SERVER_HOST, MQTT_SERVER_TLS_PORT), "12345", persistence); MqttConnectOptions options = new MqttConnectOptions(); options.setSocketFactory(this.getSocketFactory("/tls/client-truststore-root-ca.jks", null)); client.connect(options); client.publish(MQTT_TOPIC, MQTT_MESSAGE.getBytes(), 0, false); this.async.await(); client.disconnect(); context.assertTrue(true); } catch (MqttException e) { e.printStackTrace(); context.assertTrue(false); } catch (Exception e1) { e1.printStackTrace(); context.assertTrue(false); } }