RabbitMqUtils { public static void configureFactory( ConnectionFactory factory, Map<String,String> configuration ) throws IOException { final Logger logger = Logger.getLogger( RabbitMqUtils.class.getName()); logger.fine( "Configuring a connection factory for RabbitMQ." ); String messageServerIp = configuration.get( RABBITMQ_SERVER_IP ); if( messageServerIp != null ) { Map.Entry<String,Integer> entry = Utils.findUrlAndPort( messageServerIp ); factory.setHost( entry.getKey()); if( entry.getValue() > 0 ) factory.setPort( entry.getValue()); } factory.setUsername( configuration.get( RABBITMQ_SERVER_USERNAME )); factory.setPassword( configuration.get( RABBITMQ_SERVER_PASSWORD )); factory.setConnectionTimeout( 5000 ); factory.setAutomaticRecoveryEnabled( true ); factory.setNetworkRecoveryInterval( 10000 ); factory.setTopologyRecoveryEnabled( true ); if( Boolean.parseBoolean( configuration.get( RABBITMQ_USE_SSL ))) { logger.fine( "Connection factory for RabbitMQ: SSL is used." ); InputStream clientIS = null; InputStream storeIS = null; try { clientIS = new FileInputStream( configuration.get( RABBITMQ_SSL_KEY_STORE_PATH )); storeIS = new FileInputStream( configuration.get( RABBITMQ_SSL_TRUST_STORE_PATH )); char[] keyStorePassphrase = configuration.get( RABBITMQ_SSL_KEY_STORE_PASSPHRASE ).toCharArray(); KeyStore ks = KeyStore.getInstance( getValue( configuration, RABBITMQ_SSL_KEY_STORE_TYPE, DEFAULT_SSL_KEY_STORE_TYPE )); ks.load( clientIS, keyStorePassphrase ); String value = getValue( configuration, RABBITMQ_SSL_KEY_MNGR_FACTORY, DEFAULT_SSL_MNGR_FACTORY ); KeyManagerFactory kmf = KeyManagerFactory.getInstance( value ); kmf.init( ks, keyStorePassphrase ); char[] trustStorePassphrase = configuration.get( RABBITMQ_SSL_TRUST_STORE_PASSPHRASE ).toCharArray(); KeyStore tks = KeyStore.getInstance( getValue( configuration, RABBITMQ_SSL_TRUST_STORE_TYPE, DEFAULT_SSL_TRUST_STORE_TYPE )); tks.load( storeIS, trustStorePassphrase ); value = getValue( configuration, RABBITMQ_SSL_TRUST_MNGR_FACTORY, DEFAULT_SSL_MNGR_FACTORY ); TrustManagerFactory tmf = TrustManagerFactory.getInstance( value ); tmf.init( tks ); SSLContext c = SSLContext.getInstance( getValue( configuration, RABBITMQ_SSL_PROTOCOL, DEFAULT_SSL_PROTOCOL )); c.init( kmf.getKeyManagers(), tmf.getTrustManagers(), null ); factory.useSslProtocol( c ); } catch( GeneralSecurityException e ) { throw new IOException( "SSL configuration for the RabbitMQ factory failed.", e ); } finally { Utils.closeQuietly( storeIS ); Utils.closeQuietly( clientIS ); } } } private  RabbitMqUtils(); static void configureFactory( ConnectionFactory factory, Map<String,String> configuration ); static void closeConnection( Channel channel ); static void declareApplicationExchanges( String domain, String applicationName, Channel channel ); static void declareGlobalExchanges( String domain, Channel channel ); static String buildExchangeNameForAgent( String domain, String applicationName ); static String buildExchangeNameForTheDm( String domain ); static String buildExchangeNameForInterApp( String domain ); static String buildExchangeName( MessagingContext ctx );  }
@Test public void testConfigureFactory() throws Exception { String address = "http: int port = 18547; String username = "toto"; String password= "123456789"; ConnectionFactory factory = new ConnectionFactory(); Assert.assertNotSame( address, factory.getHost()); Assert.assertNotSame( port, factory.getPort()); Map<String,String> configuration = new HashMap<> (); configuration.put( RABBITMQ_SERVER_IP, "http: configuration.put( RABBITMQ_SERVER_USERNAME, username ); configuration.put( RABBITMQ_SERVER_PASSWORD, password ); RabbitMqUtils.configureFactory( factory, configuration ); Assert.assertEquals( address, factory.getHost()); Assert.assertEquals( port, factory.getPort()); Assert.assertEquals( username, factory.getUsername()); Assert.assertEquals( password, factory.getPassword()); }
@Test public void testConfigureFactory_nullIp() throws Exception { ConnectionFactory factory = new ConnectionFactory(); String username = "toto"; String password= "123456789"; Map<String,String> configuration = new HashMap<> (); configuration.put( RABBITMQ_SERVER_IP, null ); configuration.put( RABBITMQ_SERVER_USERNAME, username ); configuration.put( RABBITMQ_SERVER_PASSWORD, password ); RabbitMqUtils.configureFactory( factory, configuration ); Assert.assertEquals( username, factory.getUsername()); Assert.assertEquals( password, factory.getPassword()); }
@Test public void testConfigureFactory_withoutSslEnabled() throws Exception { int port = 18547; ConnectionFactory factory = Mockito.mock( ConnectionFactory.class ); Map<String,String> configuration = new HashMap<> (); configuration.put( RABBITMQ_SERVER_IP, "http: configuration.put( RABBITMQ_SERVER_USERNAME, "toto" ); configuration.put( RABBITMQ_SERVER_PASSWORD, "123456789" ); configuration.put( RABBITMQ_SSL_KEY_STORE_PATH, this.folder.newFile().getAbsolutePath()); configuration.put( RABBITMQ_SSL_KEY_STORE_PASSPHRASE, "key store" ); configuration.put( RABBITMQ_SSL_TRUST_STORE_PATH, this.folder.newFile().getAbsolutePath()); configuration.put( RABBITMQ_SSL_TRUST_STORE_PASSPHRASE, "trust store" ); configuration.put( RABBITMQ_SSL_AS_USER_DATA, "true" ); RabbitMqUtils.configureFactory( factory, configuration ); Mockito.verify( factory ).setUsername( "toto" ); Mockito.verify( factory ).setPassword( "123456789" ); Mockito.verify( factory ).setHost( "http: Mockito.verify( factory ).setPort( port ); Mockito.verify( factory ).setConnectionTimeout( Mockito.anyInt()); Mockito.verify( factory ).setAutomaticRecoveryEnabled( true ); Mockito.verify( factory ).setNetworkRecoveryInterval( Mockito.anyInt()); Mockito.verify( factory ).setTopologyRecoveryEnabled( true ); Mockito.verifyZeroInteractions( factory ); }
@Test public void testConfigureFactory_withSslEnabled() throws Exception { int port = 18547; ConnectionFactory factory = Mockito.mock( ConnectionFactory.class ); String keyPassPhrase = "key store"; File keyStore = generateKeyStore( keyPassPhrase, RabbitMqConstants.DEFAULT_SSL_KEY_STORE_TYPE ); String trustPassPhrase = "trust store"; File trustStore = generateKeyStore( trustPassPhrase, RabbitMqConstants.DEFAULT_SSL_TRUST_STORE_TYPE ); Map<String,String> configuration = new HashMap<> (); configuration.put( RABBITMQ_SERVER_IP, "http: configuration.put( RABBITMQ_SERVER_USERNAME, "toto" ); configuration.put( RABBITMQ_SERVER_PASSWORD, "123456789" ); configuration.put( RABBITMQ_SSL_KEY_STORE_PATH, keyStore.getAbsolutePath()); configuration.put( RABBITMQ_SSL_KEY_STORE_PASSPHRASE, keyPassPhrase ); configuration.put( RABBITMQ_SSL_TRUST_STORE_PATH, trustStore.getAbsolutePath()); configuration.put( RABBITMQ_SSL_TRUST_STORE_PASSPHRASE, trustPassPhrase ); configuration.put( RABBITMQ_SSL_AS_USER_DATA, "true" ); configuration.put( RABBITMQ_USE_SSL, "true" ); RabbitMqUtils.configureFactory( factory, configuration ); Mockito.verify( factory ).setUsername( "toto" ); Mockito.verify( factory ).setPassword( "123456789" ); Mockito.verify( factory ).setHost( "http: Mockito.verify( factory ).setPort( port ); Mockito.verify( factory ).setConnectionTimeout( Mockito.anyInt()); Mockito.verify( factory ).setAutomaticRecoveryEnabled( true ); Mockito.verify( factory ).setNetworkRecoveryInterval( Mockito.anyInt()); Mockito.verify( factory ).setTopologyRecoveryEnabled( true ); Mockito.verify( factory ).useSslProtocol( Mockito.any( SSLContext.class )); Mockito.verifyZeroInteractions( factory ); }
@Test( expected = IOException.class ) public void testConfigureFactory_withSslEnabled_ioException() throws Exception { ConnectionFactory factory = Mockito.mock( ConnectionFactory.class ); String trustPassPhrase = "trust store"; File trustStore = generateKeyStore( trustPassPhrase, RabbitMqConstants.DEFAULT_SSL_TRUST_STORE_TYPE ); Map<String,String> configuration = new HashMap<> (); configuration.put( RABBITMQ_SERVER_IP, "http: configuration.put( RABBITMQ_SERVER_USERNAME, "toto" ); configuration.put( RABBITMQ_SERVER_PASSWORD, "123456789" ); configuration.put( RABBITMQ_SSL_KEY_STORE_PATH, "does not exist" ); configuration.put( RABBITMQ_SSL_KEY_STORE_PASSPHRASE, "key store" ); configuration.put( RABBITMQ_SSL_TRUST_STORE_PATH, trustStore.getAbsolutePath()); configuration.put( RABBITMQ_SSL_TRUST_STORE_PASSPHRASE, trustPassPhrase ); configuration.put( RABBITMQ_SSL_AS_USER_DATA, "true" ); configuration.put( RABBITMQ_USE_SSL, "true" ); RabbitMqUtils.configureFactory( factory, configuration ); }