private static void validateUriByPaho(final Connection connection, final DittoHeaders dittoHeaders) { try { MqttConnectOptions.validateURI(connection.getUri()); } catch (final IllegalArgumentException e) { final String location = String.format("Connection with ID ''%s''", connection.getId()); final String configName = Connection.JsonFields.URI.getPointer().toString(); final String description = "Hint: MQTT connection URI may not have trailing '/' or any other path component."; throw invalidValueForConfig(connection.getUri(), configName, location) .description(description) .dittoHeaders(dittoHeaders) .build(); } }
private static void validateUriByPaho(final Connection connection, final DittoHeaders dittoHeaders) { try { MqttConnectOptions.validateURI(connection.getUri()); } catch (final IllegalArgumentException e) { final String location = String.format("Connection with ID ''%s''", connection.getId()); final String configName = Connection.JsonFields.URI.getPointer().toString(); final String description = "Hint: MQTT connection URI may not have trailing '/' or any other path component."; throw invalidValueForConfig(connection.getUri(), configName, location) .description(description) .dittoHeaders(dittoHeaders) .build(); } }
MqttConnectionSettings createMqttConnectionSettings(final Connection connection, final DittoHeaders dittoHeaders) { final String uri = connection.getUri(); MqttConnectionSettings connectionSettings = MqttConnectionSettings .create(uri, connection.getId(), new MemoryPersistence()); connectionSettings = connectionSettings.withAutomaticReconnect(connection.isFailoverEnabled()); final Optional<String> possibleUsername = connection.getUsername(); final Optional<String> possiblePassword = connection.getPassword(); if (possibleUsername.isPresent() && possiblePassword.isPresent()) { connectionSettings = connectionSettings.withAuth(possibleUsername.get(), possiblePassword.get()); } if (isSecureConnection(connection)) { connectionSettings = applySSLSocketFactory(connection, connectionSettings, dittoHeaders); } return connectionSettings; }
/** * Check whether the URI scheme of the connection belongs to an accepted scheme. * * @param connection the connection to check. * @param dittoHeaders headers of the command that triggered the connection validation. * @param acceptedSchemes valid URI schemes for the connection type. * @param protocolName protocol name of the connection type. * @throws DittoRuntimeException if the URI scheme is not accepted. */ protected static void validateUriScheme(final Connection connection, final DittoHeaders dittoHeaders, final Collection<String> acceptedSchemes, final String protocolName) { if (!acceptedSchemes.contains(connection.getProtocol())) { final String message = MessageFormat.format("The URI scheme ''{0}'' is not valid for {1}.", connection.getProtocol(), protocolName); final String description = MessageFormat.format("Accepted URI schemes are: {0}", String.join(", ", acceptedSchemes)); throw ConnectionUriInvalidException.newBuilder(connection.getUri()) .message(message) .description(description) .dittoHeaders(dittoHeaders) .build(); } }
/** * Check whether the URI scheme of the connection belongs to an accepted scheme. * * @param connection the connection to check. * @param dittoHeaders headers of the command that triggered the connection validation. * @param acceptedSchemes valid URI schemes for the connection type. * @param protocolName protocol name of the connection type. * @throws DittoRuntimeException if the URI scheme is not accepted. */ protected static void validateUriScheme(final Connection connection, final DittoHeaders dittoHeaders, final Collection<String> acceptedSchemes, final String protocolName) { if (!acceptedSchemes.contains(connection.getProtocol())) { final String message = MessageFormat.format("The URI scheme ''{0}'' is not valid for {1}.", connection.getProtocol(), protocolName); final String description = MessageFormat.format("Accepted URI schemes are: {0}", String.join(", ", acceptedSchemes)); throw ConnectionUriInvalidException.newBuilder(connection.getUri()) .message(message) .description(description) .dittoHeaders(dittoHeaders) .build(); } }
MqttConnectionSettings createMqttConnectionSettings(final Connection connection, final DittoHeaders dittoHeaders) { final String uri = connection.getUri(); MqttConnectionSettings connectionSettings = MqttConnectionSettings .create(uri, connection.getId(), new MemoryPersistence()); connectionSettings = connectionSettings.withAutomaticReconnect(connection.isFailoverEnabled()); final Optional<String> possibleUsername = connection.getUsername(); final Optional<String> possiblePassword = connection.getPassword(); if (possibleUsername.isPresent() && possiblePassword.isPresent()) { connectionSettings = connectionSettings.withAuth(possibleUsername.get(), possiblePassword.get()); } if (isSecureConnection(connection)) { connectionSettings = applySSLSocketFactory(connection, connectionSettings, dittoHeaders); } return connectionSettings; }
@Test public void createMinimalConnectionConfigurationInstance() { final Connection connection = ConnectivityModelFactory.newConnectionBuilder(ID, TYPE, STATUS, URI) .sources(SOURCES) .targets(TARGETS) .build(); assertThat(connection.getId()).isEqualTo(ID); assertThat((Object) connection.getConnectionType()).isEqualTo(TYPE); assertThat(connection.getUri()).isEqualTo(URI); assertThat(connection.getSources()).isEqualTo(SOURCES); }
@Override public ConnectionFactory createConnectionFactory(final Connection connection, final ExceptionHandler exceptionHandler) { checkNotNull(connection, "Connection"); checkNotNull(exceptionHandler, "Exception Handler"); try { final ConnectionFactory connectionFactory = new CustomConnectionFactory(); if (SECURE_AMQP_SCHEME.equalsIgnoreCase(connection.getProtocol())) { if (connection.isValidateCertificates()) { final SSLContextCreator sslContextCreator = SSLContextCreator.fromConnection(connection, null); connectionFactory.useSslProtocol(sslContextCreator.withoutClientCertificate()); } else { // attention: this accepts all certificates whether they are valid or not connectionFactory.useSslProtocol(); } } connectionFactory.setUri(connection.getUri()); // this makes no difference as the used newmotion client always sets the AutomaticRecoveryEnabled to false: connectionFactory.setAutomaticRecoveryEnabled(connection.isFailoverEnabled()); connectionFactory.setExceptionHandler(exceptionHandler); configureConnectionFactory(connectionFactory, connection.getSpecificConfig()); return connectionFactory; } catch (final NoSuchAlgorithmException | KeyManagementException | URISyntaxException e) { LOGGER.warn(e.getMessage()); throw new IllegalStateException("Failed to create RabbitMQ connection factory.", e); } }
@Override public ConnectionFactory createConnectionFactory(final Connection connection, final ExceptionHandler exceptionHandler) { checkNotNull(connection, "Connection"); checkNotNull(exceptionHandler, "Exception Handler"); try { final ConnectionFactory connectionFactory = new CustomConnectionFactory(); if (SECURE_AMQP_SCHEME.equalsIgnoreCase(connection.getProtocol())) { if (connection.isValidateCertificates()) { final SSLContextCreator sslContextCreator = SSLContextCreator.fromConnection(connection, null); connectionFactory.useSslProtocol(sslContextCreator.withoutClientCertificate()); } else { // attention: this accepts all certificates whether they are valid or not connectionFactory.useSslProtocol(); } } connectionFactory.setUri(connection.getUri()); // this makes no difference as the used newmotion client always sets the AutomaticRecoveryEnabled to false: connectionFactory.setAutomaticRecoveryEnabled(connection.isFailoverEnabled()); connectionFactory.setExceptionHandler(exceptionHandler); configureConnectionFactory(connectionFactory, connection.getSpecificConfig()); return connectionFactory; } catch (final NoSuchAlgorithmException | KeyManagementException | URISyntaxException e) { LOGGER.warn(e.getMessage()); throw new IllegalStateException("Failed to create RabbitMQ connection factory.", e); } }
@Override protected FSMStateFunctionBuilder<BaseClientState, BaseClientData> inTestingState() { return super.inTestingState() .event(Status.Status.class, (e, d) -> !Objects.equals(getSender(), getSelf()), this::handleStatusReportFromChildren) .event(ClientConnected.class, BaseClientData.class, (event, data) -> { final String url = data.getConnection().getUri(); final String message = "mqtt connection to " + url + " established successfully"; completeTestConnectionFuture(new Status.Success(message), data); return stay(); }) .event(ConnectionFailure.class, BaseClientData.class, (event, data) -> { completeTestConnectionFuture(new Status.Failure(event.getFailure().cause()), data); return stay(); }); }
@Override protected FSMStateFunctionBuilder<BaseClientState, BaseClientData> inTestingState() { return super.inTestingState() .event(Status.Status.class, (e, d) -> !Objects.equals(getSender(), getSelf()), this::handleStatusReportFromChildren) .event(ClientConnected.class, BaseClientData.class, (event, data) -> { final String url = data.getConnection().getUri(); final String message = "mqtt connection to " + url + " established successfully"; completeTestConnectionFuture(new Status.Success(message), data); return stay(); }) .event(ConnectionFailure.class, BaseClientData.class, (event, data) -> { completeTestConnectionFuture(new Status.Failure(event.getFailure().cause()), data); return stay(); }); }
/** * Returns a new {@code ConnectionBuilder} object. * * @param connection the connection to use for initializing the builder. * @return new instance of {@code ImmutableConnectionBuilder}. * @throws NullPointerException if {@code connection} is {@code null}. */ public static ConnectionBuilder getBuilder(final Connection connection) { checkNotNull(connection, "Connection"); return new Builder(connection.getConnectionType()) .id(connection.getId()) .connectionStatus(connection.getConnectionStatus()) .credentials(connection.getCredentials().orElse(null)) .uri(connection.getUri()) .trustedCertificates(connection.getTrustedCertificates().orElse(null)) .failoverEnabled(connection.isFailoverEnabled()) .validateCertificate(connection.isValidateCertificates()) .processorPoolSize(connection.getProcessorPoolSize()) .sources(connection.getSources()) .targets(connection.getTargets()) .clientCount(connection.getClientCount()) .specificConfig(connection.getSpecificConfig()) .mappingContext(connection.getMappingContext().orElse(null)) .name(connection.getName().orElse(null)) .tags(connection.getTags()); }
/** * Returns a new {@code ConnectionBuilder} object. * * @param connection the connection to use for initializing the builder. * @return new instance of {@code ImmutableConnectionBuilder}. * @throws NullPointerException if {@code connection} is {@code null}. */ public static ConnectionBuilder getBuilder(final Connection connection) { checkNotNull(connection, "Connection"); return new Builder(connection.getConnectionType()) .id(connection.getId()) .connectionStatus(connection.getConnectionStatus()) .credentials(connection.getCredentials().orElse(null)) .uri(connection.getUri()) .trustedCertificates(connection.getTrustedCertificates().orElse(null)) .failoverEnabled(connection.isFailoverEnabled()) .validateCertificate(connection.isValidateCertificates()) .processorPoolSize(connection.getProcessorPoolSize()) .sources(connection.getSources()) .targets(connection.getTargets()) .clientCount(connection.getClientCount()) .specificConfig(connection.getSpecificConfig()) .mappingContext(connection.getMappingContext().orElse(null)) .name(connection.getName().orElse(null)) .tags(connection.getTags()); }