Java tutorial
/* * ***** BEGIN LICENSE BLOCK ***** * Zimbra Collaboration Suite Server * Copyright (C) 2010, 2013, 2014, 2016 Synacor, Inc. * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software Foundation, * version 2 of the License. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * You should have received a copy of the GNU General Public License along with this program. * If not, see <https://www.gnu.org/licenses/>. * ***** END LICENSE BLOCK ***** */ package com.zimbra.common.net; import org.apache.commons.httpclient.params.HttpConnectionParams; import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; import javax.net.SocketFactory; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; class ProtocolSocketFactoryWrapper implements ProtocolSocketFactory { private final SocketFactory factory; ProtocolSocketFactoryWrapper(SocketFactory factory) { this.factory = factory; } @Override public Socket createSocket(String host, int port, InetAddress localAddress, int localPort) throws IOException { return factory.createSocket(host, port, localAddress, localPort); } @Override public Socket createSocket(String host, int port, InetAddress localAddress, int localPort, HttpConnectionParams params) throws IOException { int timeout = params != null ? params.getConnectionTimeout() : 0; if (timeout > 0) { Socket sock = factory.createSocket(); sock.bind(new InetSocketAddress(localAddress, localPort)); sock.connect(new InetSocketAddress(host, port), timeout); return sock; } else { return factory.createSocket(host, port, localAddress, localPort); } } @Override public Socket createSocket(String host, int port) throws IOException { return factory.createSocket(host, port); } }