Java 类org.eclipse.paho.client.mqttv3.internal.security.SSLSocketFactoryFactory 实例源码

项目:joynr    文件:MqttPahoClient.java   
private MqttConnectOptions getConnectOptions() {
    MqttConnectOptions options = new MqttConnectOptions();
    options.setAutomaticReconnect(false);
    options.setConnectionTimeout(connectionTimeoutSec);
    options.setKeepAliveInterval(keepAliveTimerSec);
    options.setMaxInflight(maxMsgsInflight);
    options.setCleanSession(cleanSession);

    if (isSecureConnection) {
        // Set global SSL properties for all Joynr SSL clients
        Properties sslClientProperties = new Properties();
        sslClientProperties.setProperty(SSLSocketFactoryFactory.KEYSTORETYPE, "JKS");
        sslClientProperties.setProperty(SSLSocketFactoryFactory.KEYSTORE, keyStorePath);
        sslClientProperties.setProperty(SSLSocketFactoryFactory.KEYSTOREPWD, keyStorePWD);
        sslClientProperties.setProperty(SSLSocketFactoryFactory.TRUSTSTORETYPE, "JKS");
        sslClientProperties.setProperty(SSLSocketFactoryFactory.TRUSTSTORE, trustStorePath);
        sslClientProperties.setProperty(SSLSocketFactoryFactory.TRUSTSTOREPWD, trustStorePWD);
        options.setSSLProperties(sslClientProperties);
    }

    return options;
}
项目:joynr    文件:MqttPahoClientTest.java   
@Test
public void mqttClientTLSTestWithDisabledMessageSizeCheck() throws URISyntaxException {
    final int initialMessageSize = 100;
    final boolean isSecureConnection = true;
    properties.put(MqttModule.PROPERTY_KEY_MQTT_MAX_MESSAGE_SIZE_BYTES, "0");

    final String keyStorePath = getResourcePath("clientkeystore.jks");
    final String trustStorePath = getResourcePath("catruststore.jks");
    properties.put(SSLSocketFactoryFactory.SYSKEYSTORE, keyStorePath);
    properties.put(SSLSocketFactoryFactory.SYSTRUSTSTORE, trustStorePath);
    properties.put(SSLSocketFactoryFactory.SYSKEYSTOREPWD, KEYSTORE_PASSWORD);
    properties.put(SSLSocketFactoryFactory.SYSTRUSTSTOREPWD, KEYSTORE_PASSWORD);

    createJoynrMqttClient(isSecureConnection);

    byte[] shortSerializedMessage = new byte[initialMessageSize];
    joynrMqttClientPublishAndVerifyReceivedMessage(shortSerializedMessage);

    byte[] largeSerializedMessage = new byte[initialMessageSize + 1];
    joynrMqttClientPublishAndVerifyReceivedMessage(largeSerializedMessage);
}
项目:joynr    文件:MqttPahoClientTest.java   
@Test
public void mqttClientTLSCreationFailsIfKeystorePasswordIsWrongOrMissing() throws URISyntaxException {
    final String wrongPassword = "wrongPassword";

    final String keyStorePath = getResourcePath("clientkeystore.jks");
    final String trustStorePath = getResourcePath("catruststore.jks");
    properties.put(SSLSocketFactoryFactory.SYSKEYSTORE, keyStorePath);
    properties.put(SSLSocketFactoryFactory.SYSTRUSTSTORE, trustStorePath);

    // test missing keystore password
    properties.remove(SSLSocketFactoryFactory.SYSKEYSTOREPWD);
    properties.put(SSLSocketFactoryFactory.SYSTRUSTSTOREPWD, KEYSTORE_PASSWORD);

    testCreateMqttClientFailsWithJoynrIllegalArgumentException();

    // test wrong keystore password
    properties.put(SSLSocketFactoryFactory.SYSKEYSTOREPWD, wrongPassword);
    testCreateMqttClientFailsWithJoynrIllegalArgumentException();
}
项目:joynr    文件:MqttPahoClientTest.java   
@Test
public void mqttClientTLSCreationFailsIfTrustorePasswordIsWrongOrMissing() throws URISyntaxException {
    final String wrongPassword = "wrongPassword";

    final String keyStorePath = getResourcePath("clientkeystore.jks");
    final String trustStorePath = getResourcePath("catruststore.jks");
    properties.put(SSLSocketFactoryFactory.SYSKEYSTORE, keyStorePath);
    properties.put(SSLSocketFactoryFactory.SYSTRUSTSTORE, trustStorePath);

    // test missing truststore password
    properties.put(SSLSocketFactoryFactory.SYSKEYSTOREPWD, KEYSTORE_PASSWORD);
    properties.remove(SSLSocketFactoryFactory.SYSTRUSTSTOREPWD);

    testCreateMqttClientFailsWithJoynrIllegalArgumentException();

    // test wrong truststore password
    properties.put(SSLSocketFactoryFactory.SYSTRUSTSTOREPWD, wrongPassword);
    testCreateMqttClientFailsWithJoynrIllegalArgumentException();
}
项目:joynr    文件:MqttPahoClientTest.java   
@Test
public void mqttClientTLSCreationFailsIfKeystorePathIsWrongOrMissing() throws URISyntaxException {
    final String wrongKeyStorePath = getResourcePath("clientkeystore.jks") + "42";

    final String trustStorePath = getResourcePath("catruststore.jks");
    properties.put(SSLSocketFactoryFactory.SYSTRUSTSTORE, trustStorePath);
    properties.put(SSLSocketFactoryFactory.SYSKEYSTOREPWD, KEYSTORE_PASSWORD);
    properties.put(SSLSocketFactoryFactory.SYSTRUSTSTOREPWD, KEYSTORE_PASSWORD);

    // test missing keystore path
    properties.remove(SSLSocketFactoryFactory.SYSKEYSTORE);

    testCreateMqttClientFailsWithJoynrIllegalArgumentException();

    // test wrong keystore path
    properties.put(SSLSocketFactoryFactory.SYSKEYSTORE, wrongKeyStorePath);
    testCreateMqttClientFailsWithJoynrIllegalArgumentException();
}
项目:joynr    文件:MqttPahoClientTest.java   
@Test
public void mqttClientTLSCreationFailsIfTrustorePathIsWrongOrMissing() throws URISyntaxException {
    final String wrongTrustStorePath = getResourcePath("catruststore.jks") + "42";

    final String keyStorePath = getResourcePath("clientkeystore.jks");
    properties.put(SSLSocketFactoryFactory.SYSKEYSTORE, keyStorePath);
    properties.put(SSLSocketFactoryFactory.SYSKEYSTOREPWD, KEYSTORE_PASSWORD);
    properties.put(SSLSocketFactoryFactory.SYSTRUSTSTOREPWD, KEYSTORE_PASSWORD);

    // test missing truststore path
    properties.remove(SSLSocketFactoryFactory.SYSTRUSTSTORE);

    testCreateMqttClientFailsWithJoynrIllegalArgumentException();

    // test wrong truststore path
    properties.put(SSLSocketFactoryFactory.SYSTRUSTSTORE, wrongTrustStorePath);
    testCreateMqttClientFailsWithJoynrIllegalArgumentException();
}
项目:iot-edge-greengrass    文件:MqttBrokerMonitor.java   
public void connect() {
    try {
        client = new MqttAsyncClient((configuration.isSsl() ? "ssl" : "tcp") + "://" + configuration.getHost() + ":" + configuration.getPort(),
                getClientId(), new MemoryPersistence());
        client.setCallback(this);
        clientOptions = new MqttConnectOptions();
        clientOptions.setCleanSession(true);
        if (configuration.isSsl() && !StringUtils.isEmpty(configuration.getTruststore())) {
            Properties sslProperties = new Properties();
            sslProperties.put(SSLSocketFactoryFactory.TRUSTSTORE, configuration.getTruststore());
            sslProperties.put(SSLSocketFactoryFactory.TRUSTSTOREPWD, configuration.getTruststorePassword());
            sslProperties.put(SSLSocketFactoryFactory.TRUSTSTORETYPE, "JKS");
            sslProperties.put(SSLSocketFactoryFactory.CLIENTAUTH, false);
            clientOptions.setSSLProperties(sslProperties);
        }
        configuration.getCredentials().configure(clientOptions);
        checkConnection();
        if (configuration.getAttributeUpdates() != null) {
            configuration.getAttributeUpdates().forEach(mapping ->
                    gateway.subscribe(new AttributesUpdateSubscription(mapping.getDeviceNameFilter(), this))
            );
        }
        if (configuration.getServerSideRpc() != null) {
            configuration.getServerSideRpc().forEach(mapping ->
                    gateway.subscribe(new RpcCommandSubscription(mapping.getDeviceNameFilter(), this))
            );
        }
    } catch (MqttException e) {
        log.error("[{}:{}] MQTT broker connection failed!", configuration.getHost(), configuration.getPort(), e);
        throw new RuntimeException("MQTT broker connection failed!", e);
    }
}
项目:thingsboard-gateway    文件:MqttBrokerMonitor.java   
public void connect() {
    try {
        client = new MqttAsyncClient((configuration.isSsl() ? "ssl" : "tcp") + "://" + configuration.getHost() + ":" + configuration.getPort(),
                getClientId(), new MemoryPersistence());
        client.setCallback(this);
        clientOptions = new MqttConnectOptions();
        clientOptions.setCleanSession(true);
        if (configuration.isSsl() && !StringUtils.isEmpty(configuration.getTruststore())) {
            Properties sslProperties = new Properties();
            sslProperties.put(SSLSocketFactoryFactory.TRUSTSTORE, configuration.getTruststore());
            sslProperties.put(SSLSocketFactoryFactory.TRUSTSTOREPWD, configuration.getTruststorePassword());
            sslProperties.put(SSLSocketFactoryFactory.TRUSTSTORETYPE, "JKS");
            sslProperties.put(SSLSocketFactoryFactory.CLIENTAUTH, false);
            clientOptions.setSSLProperties(sslProperties);
        }
        configuration.getCredentials().configure(clientOptions);
        checkConnection();
        if (configuration.getAttributeUpdates() != null) {
            configuration.getAttributeUpdates().forEach(mapping ->
                    gateway.subscribe(new AttributesUpdateSubscription(mapping.getDeviceNameFilter(), this))
            );
        }
        if (configuration.getServerSideRpc() != null) {
            configuration.getServerSideRpc().forEach(mapping ->
                    gateway.subscribe(new RpcCommandSubscription(mapping.getDeviceNameFilter(), this))
            );
        }
    } catch (MqttException e) {
        log.error("[{}:{}] MQTT broker connection failed!", configuration.getHost(), configuration.getPort(), e);
        throw new RuntimeException("MQTT broker connection failed!", e);
    }
}
项目:iotgateway    文件:MqttBrokerMonitor.java   
public void connect() {
  try {
    client = new MqttAsyncClient(
        (configuration.isSsl() ? "ssl" : "tcp") + "://" + configuration.getHost() + ":" + configuration.getPort(),
        getClientId(), new MemoryPersistence());
    client.setCallback(this);
    clientOptions = new MqttConnectOptions();
    clientOptions.setCleanSession(true);
    SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
    sslContext.init(null, null, null);
    clientOptions.setSocketFactory(sslContext.getSocketFactory());

    if (configuration.isSsl() && !StringUtils.isEmpty(configuration.getTruststore())) {
      Properties sslProperties = new Properties();
      sslProperties.put(SSLSocketFactoryFactory.TRUSTSTORE, configuration.getTruststore());
      sslProperties.put(SSLSocketFactoryFactory.TRUSTSTOREPWD, configuration.getTruststorePassword());
      sslProperties.put(SSLSocketFactoryFactory.TRUSTSTORETYPE, "JKS");
      sslProperties.put(SSLSocketFactoryFactory.CLIENTAUTH, false);
      clientOptions.setSSLProperties(sslProperties);
    }
    configuration.getCredentials().configure(clientOptions);
    checkConnection();
    if (configuration.getAttributeUpdates() != null) {
      configuration.getAttributeUpdates().forEach(
          mapping -> gateway.subscribe(new AttributesUpdateSubscription(mapping.getDeviceNameFilter(), this)));
    }
    if (configuration.getServerSideRpc() != null) {
      configuration.getServerSideRpc()
          .forEach(mapping -> gateway.subscribe(new RpcCommandSubscription(mapping.getDeviceNameFilter(), this)));
    }
  } catch (MqttException | NoSuchAlgorithmException | KeyManagementException e) {
    log.error("[{}:{}] MQTT broker connection failed!", configuration.getHost(), configuration.getPort(), e);
    throw new RuntimeException("MQTT broker connection failed!", e);
  }
}
项目:iot-edge-greengrass    文件:WiotpMqttBrokerMonitor.java   
public void connect() {
        try {
            client = new MqttAsyncClient((configuration.isSsl() ? "ssl" : "tcp") + "://" + configuration.getHost() + ":" + configuration.getPort(),
                    getClientId(), new MemoryPersistence());
            client.setCallback(this);
            clientOptions = new MqttConnectOptions();
//            clientOptions.setUserName("a-u88ncg-1qo8utyxwr");
//            clientOptions.setPassword("KB?fB1sG-1GFb?wLvx".toCharArray());
            clientOptions.setCleanSession(true);
            SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
            sslContext.init(null, null, null);
            clientOptions.setSocketFactory(sslContext.getSocketFactory());

            if (configuration.isSsl() && !StringUtils.isEmpty(configuration.getTruststore())) {
                Properties sslProperties = new Properties();
                sslProperties.put(SSLSocketFactoryFactory.TRUSTSTORE, configuration.getTruststore());
                sslProperties.put(SSLSocketFactoryFactory.TRUSTSTOREPWD, configuration.getTruststorePassword());
                sslProperties.put(SSLSocketFactoryFactory.TRUSTSTORETYPE, "JKS");
                sslProperties.put(SSLSocketFactoryFactory.CLIENTAUTH, false);
                clientOptions.setSSLProperties(sslProperties);
            }
            configuration.getCredentials().configure(clientOptions);

//          Properties props = new Properties();
//          props.put("id", "appId001");
//          props.put("Organization-ID", "u88ncg");
//          props.put("Authentication-Method", "apikey");
//          props.put("API-Key", "a-u88ncg-1qo8utyxwr");
//          props.put("Authentication-Token", "KB?fB1sG-1GFb?wLvx");
//          props.put("Device-Type", "DC_SensorType");
//          props.put("Device-ID", "my-device-001");
//          props.put("Shared-Subscription", "false");
//          props.put("Clean-Session", "true");
//          
//          myClient = new ApplicationClient(props);

            client.connect(clientOptions).waitForCompletion(1000 * 60);
            if (client.isConnected()) {
              System.out.println("Successfully connected " + "to the IBM Watson IoT Platform");
            }

            checkConnection();
            if (configuration.getAttributeUpdates() != null) {
                configuration.getAttributeUpdates().forEach(mapping ->
                        gateway.subscribe(new AttributesUpdateSubscription(mapping.getDeviceNameFilter(), this))
                );
            }
            if (configuration.getServerSideRpc() != null) {
                configuration.getServerSideRpc().forEach(mapping ->
                        gateway.subscribe(new RpcCommandSubscription(mapping.getDeviceNameFilter(), this))
                );
            }
        } catch (Exception e) {
            log.error("[{}:{}] MQTT broker connection failed!", configuration.getHost(), configuration.getPort(), e);
            throw new RuntimeException("MQTT broker connection failed!", e);
        }
    }
项目:iotgateway    文件:WiotpMqttBrokerMonitor.java   
public void connect() {
        try {
            client = new MqttAsyncClient((configuration.isSsl() ? "ssl" : "tcp") + "://" + configuration.getHost() + ":" + configuration.getPort(),
                    getClientId(), new MemoryPersistence());
            client.setCallback(this);
            clientOptions = new MqttConnectOptions();
//            clientOptions.setUserName("a-u88ncg-1qo8utyxwr");
//            clientOptions.setPassword("KB?fB1sG-1GFb?wLvx".toCharArray());
            clientOptions.setCleanSession(true);
            SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
            sslContext.init(null, null, null);
            clientOptions.setSocketFactory(sslContext.getSocketFactory());

            if (configuration.isSsl() && !StringUtils.isEmpty(configuration.getTruststore())) {
                Properties sslProperties = new Properties();
                sslProperties.put(SSLSocketFactoryFactory.TRUSTSTORE, configuration.getTruststore());
                sslProperties.put(SSLSocketFactoryFactory.TRUSTSTOREPWD, configuration.getTruststorePassword());
                sslProperties.put(SSLSocketFactoryFactory.TRUSTSTORETYPE, "JKS");
                sslProperties.put(SSLSocketFactoryFactory.CLIENTAUTH, false);
                clientOptions.setSSLProperties(sslProperties);
            }
            configuration.getCredentials().configure(clientOptions);

//          Properties props = new Properties();
//          props.put("id", "appId001");
//          props.put("Organization-ID", "u88ncg");
//          props.put("Authentication-Method", "apikey");
//          props.put("API-Key", "a-u88ncg-1qo8utyxwr");
//          props.put("Authentication-Token", "KB?fB1sG-1GFb?wLvx");
//          props.put("Device-Type", "DC_SensorType");
//          props.put("Device-ID", "my-device-001");
//          props.put("Shared-Subscription", "false");
//          props.put("Clean-Session", "true");
//          
//          myClient = new ApplicationClient(props);

            client.connect(clientOptions).waitForCompletion(1000 * 60);
            if (client.isConnected()) {
              System.out.println("Successfully connected " + "to the IBM Watson IoT Platform");
            }

            checkConnection();
            if (configuration.getAttributeUpdates() != null) {
                configuration.getAttributeUpdates().forEach(mapping ->
                        gateway.subscribe(new AttributesUpdateSubscription(mapping.getDeviceNameFilter(), this))
                );
            }
            if (configuration.getServerSideRpc() != null) {
                configuration.getServerSideRpc().forEach(mapping ->
                        gateway.subscribe(new RpcCommandSubscription(mapping.getDeviceNameFilter(), this))
                );
            }
        } catch (Exception e) {
            log.error("[{}:{}] MQTT broker connection failed!", configuration.getHost(), configuration.getPort(), e);
            throw new RuntimeException("MQTT broker connection failed!", e);
        }
    }