}
}
HornetQClientLogger.LOGGER.debug("Remote destination: " + remoteDestination);
ChannelFuture future;
//port 0 does not work so only use local address if set
if (localPort != 0)
{
SocketAddress localDestination;
if (localAddress != null)
{
localDestination = new InetSocketAddress(localAddress, localPort);
}
else
{
localDestination = new InetSocketAddress(localPort);
}
future = bootstrap.connect(remoteDestination, localDestination);
}
else
{
future = bootstrap.connect(remoteDestination);
}
future.awaitUninterruptibly();
if (future.isSuccess())
{
final Channel ch = future.channel();
SslHandler sslHandler = ch.pipeline().get(SslHandler.class);
if (sslHandler != null)
{
Future<Channel> handshakeFuture = sslHandler.handshakeFuture();
if (handshakeFuture.awaitUninterruptibly(30000))
{
if (handshakeFuture.isSuccess())
{
ChannelPipeline channelPipeline = ch.pipeline();
HornetQChannelHandler channelHandler = channelPipeline.get(HornetQChannelHandler.class);
channelHandler.active = true;
}
else
{
ch.close().awaitUninterruptibly();
HornetQClientLogger.LOGGER.errorCreatingNettyConnection(handshakeFuture.cause());
return null;
}
}
else
{
//handshakeFuture.setFailure(new SSLException("Handshake was not completed in 30 seconds"));
ch.close().awaitUninterruptibly();
return null;
}
}
else
{
if (httpUpgradeEnabled)
{
// Send a HTTP GET + Upgrade request that will be handled by the http-upgrade handler.
try
{
//get this first incase it removes itself
HttpUpgradeHandler httpUpgradeHandler = (HttpUpgradeHandler) ch.pipeline().get("http-upgrade");
URI uri = new URI("http", null, host, port, null, null, null);
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath());
request.headers().set(HttpHeaders.Names.HOST, host);
request.headers().set(HttpHeaders.Names.UPGRADE, HORNETQ_REMOTING);
request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.UPGRADE);
final String endpoint = ConfigurationHelper.getStringProperty(TransportConstants.HTTP_UPGRADE_ENDPOINT_PROP_NAME,
null,
configuration);
if (endpoint != null)
{
request.headers().set(TransportConstants.HTTP_UPGRADE_ENDPOINT_PROP_NAME, endpoint);
}
// Get 16 bit nonce and base 64 encode it
byte[] nonce = randomBytes(16);
String key = base64(nonce);
request.headers().set(SEC_HORNETQ_REMOTING_KEY, key);
ch.attr(REMOTING_KEY).set(key);
HornetQClientLogger.LOGGER.debugf("Sending HTTP request %s", request);
// Send the HTTP request.
ch.writeAndFlush(request);
if (!httpUpgradeHandler.awaitHandshake())
{
return null;
}
}
catch (URISyntaxException e)
{
HornetQClientLogger.LOGGER.errorCreatingNettyConnection(e);
return null;
}
}
else
{
ChannelPipeline channelPipeline = ch.pipeline();
HornetQChannelHandler channelHandler = channelPipeline.get(HornetQChannelHandler.class);
channelHandler.active = true;
}
}
// No acceptor on a client connection
Listener connectionListener = new Listener();
NettyConnection conn = new NettyConnection(configuration, ch, connectionListener, !httpEnabled && batchDelay > 0, false);
connectionListener.connectionCreated(null, conn, HornetQClient.DEFAULT_CORE_PROTOCOL);
return conn;
}
else
{
Throwable t = future.cause();
if (t != null && !(t instanceof ConnectException))
{
HornetQClientLogger.LOGGER.errorCreatingNettyConnection(future.cause());
}
return null;
}
}