use of javax.net.SocketFactory in project camel by apache.
the class FtpInitialConnectTimeoutTest method createSocketFactory.
private SocketFactory createSocketFactory() throws IOException {
SocketFactory socketFactory = mock(SocketFactory.class);
when(socketFactory.createSocket()).thenAnswer(new SocketAnswer());
return socketFactory;
}
use of javax.net.SocketFactory in project Smack by igniterealtime.
the class XMPPTCPConnection method connectUsingConfiguration.
private void connectUsingConfiguration() throws ConnectionException, IOException {
List<HostAddress> failedAddresses = populateHostAddresses();
SocketFactory socketFactory = config.getSocketFactory();
ProxyInfo proxyInfo = config.getProxyInfo();
int timeout = config.getConnectTimeout();
if (socketFactory == null) {
socketFactory = SocketFactory.getDefault();
}
for (HostAddress hostAddress : hostAddresses) {
Iterator<InetAddress> inetAddresses = null;
String host = hostAddress.getFQDN();
int port = hostAddress.getPort();
if (proxyInfo == null) {
inetAddresses = hostAddress.getInetAddresses().iterator();
assert (inetAddresses.hasNext());
innerloop: while (inetAddresses.hasNext()) {
// Create a *new* Socket before every connection attempt, i.e. connect() call, since Sockets are not
// re-usable after a failed connection attempt. See also SMACK-724.
socket = socketFactory.createSocket();
final InetAddress inetAddress = inetAddresses.next();
final String inetAddressAndPort = inetAddress + " at port " + port;
LOGGER.finer("Trying to establish TCP connection to " + inetAddressAndPort);
try {
socket.connect(new InetSocketAddress(inetAddress, port), timeout);
} catch (Exception e) {
hostAddress.setException(inetAddress, e);
if (inetAddresses.hasNext()) {
continue innerloop;
} else {
break innerloop;
}
}
LOGGER.finer("Established TCP connection to " + inetAddressAndPort);
// We found a host to connect to, return here
this.host = host;
this.port = port;
return;
}
failedAddresses.add(hostAddress);
} else {
socket = socketFactory.createSocket();
StringUtils.requireNotNullOrEmpty(host, "Host of HostAddress " + hostAddress + " must not be null when using a Proxy");
final String hostAndPort = host + " at port " + port;
LOGGER.finer("Trying to establish TCP connection via Proxy to " + hostAndPort);
try {
proxyInfo.getProxySocketConnection().connect(socket, host, port, timeout);
} catch (IOException e) {
hostAddress.setException(e);
continue;
}
LOGGER.finer("Established TCP connection to " + hostAndPort);
// We found a host to connect to, return here
this.host = host;
this.port = port;
return;
}
}
// HostAddresses in the exception
throw ConnectionException.from(failedAddresses);
}
use of javax.net.SocketFactory in project rest.li by linkedin.
the class TrustingSocketFactory method connectSocket.
public Socket connectSocket(int connectTimeout, Socket sock, HttpHost host, InetSocketAddress remoteAddress, InetSocketAddress localAddress, HttpContext context) throws IOException {
SocketFactory socketfactory = getSSLContext().getSocketFactory();
if (connectTimeout == 0) {
return socketfactory.createSocket(host.getHostName(), host.getPort(), localAddress.getAddress(), localAddress.getPort());
} else {
Socket socket = socketfactory.createSocket();
SocketAddress localaddr = new InetSocketAddress(localAddress.getAddress(), localAddress.getPort());
SocketAddress remoteaddr = new InetSocketAddress(remoteAddress.getAddress(), remoteAddress.getPort());
socket.bind(localaddr);
socket.connect(remoteaddr, connectTimeout);
return socket;
}
}
use of javax.net.SocketFactory in project java-apns by notnoop.
the class MockingUtils method mockSocketFactory.
static SocketFactory mockSocketFactory(OutputStream out, InputStream in) {
try {
Socket socket = mock(Socket.class);
when(socket.getOutputStream()).thenReturn(out);
when(socket.getInputStream()).thenReturn(in);
SocketFactory factory = mock(SocketFactory.class);
when(factory.createSocket()).thenReturn(socket);
when(factory.createSocket(anyString(), anyInt())).thenReturn(socket);
return factory;
} catch (Exception e) {
e.printStackTrace();
throw new AssertionError("Cannot be here!");
}
}
use of javax.net.SocketFactory in project java-apns by notnoop.
the class ApnsConnectionTest method errorOnce.
@Test
public void errorOnce() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
SocketFactory factory = mockClosedThenOpenSocket(baos, null, false, 1);
packetSentRegardless(factory, baos);
}
Aggregations