} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Transport failed to start.", e);
throw e;
}
final TCPNIOConnectorHandler connectorHandler = new TCPNIOConnectorHandler(sharedTransport ? transport : privateTransport) {
};
connectorHandler.setSyncConnectTimeout(timeoutMs, TimeUnit.MILLISECONDS);
GrizzlyFuture<Connection> connectionGrizzlyFuture;
final TCPNIOTransport finalPrivateTransport = privateTransport;
final ClientEngine.TimeoutHandler timeoutHandler = sharedTransport ? null : new ClientEngine.TimeoutHandler() {
@Override
public void handleTimeout() {
closeTransport(finalPrivateTransport);
}
};
final SocketAddress connectAddress;
switch (proxy.type()) {
case DIRECT:
connectAddress = socketAddress;
LOGGER.log(Level.CONFIG, String.format("Connecting to '%s' (no proxy).", uri));
break;
default:
LOGGER.log(Level.CONFIG, String.format("Connecting to '%s' via proxy '%s'.", uri, proxy));
// default ProxySelector always returns proxies with unresolved addresses.
SocketAddress address = proxy.address();
if (address instanceof InetSocketAddress) {
InetSocketAddress inetSocketAddress = (InetSocketAddress) address;
if (inetSocketAddress.isUnresolved()) {
// resolve the address.
address = new InetSocketAddress(inetSocketAddress.getHostName(), inetSocketAddress.getPort());
}
}
connectAddress = address;
break;
}
connectorHandler.setProcessor(createFilterChain(engine, null, clientSSLEngineConfigurator, !(proxy.type() == Proxy.Type.DIRECT), uri, timeoutHandler, sharedTransport, sharedTransportTimeout, proxyHeaders, grizzlyConnector));
connectionGrizzlyFuture = connectorHandler.connect(connectAddress);
try {
final Connection connection = connectionGrizzlyFuture.get(timeoutMs, TimeUnit.MILLISECONDS);
LOGGER.log(Level.CONFIG, String.format("Connected to '%s'.", connection.getPeerAddress()));