List of usage examples for org.apache.commons.httpclient ConnectTimeoutException ConnectTimeoutException
public ConnectTimeoutException(String paramString, Throwable paramThrowable)
From source file:br.com.ararati.operacoes.SocketFactory.java
@Override public Socket createSocket(String host, int port, InetAddress localAddress, int localPort, HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException { if (params == null) { throw new IllegalArgumentException("HttpConnectionParams: Parmetros no podem ser nulos."); }/*from ww w .java 2 s .co m*/ int timeout = params.getConnectionTimeout(); javax.net.SocketFactory socketfactory = getSSLContext().getSocketFactory(); if (timeout == 0) { return socketfactory.createSocket(host, port, localAddress, localPort); } Socket socket = socketfactory.createSocket(); SocketAddress localaddr = new InetSocketAddress(localAddress, localPort); SocketAddress remoteaddr = new InetSocketAddress(host, port); socket.bind(localaddr); try { socket.connect(remoteaddr, timeout); } catch (Exception e) { error(e.toString()); throw new ConnectTimeoutException("Possvel timeout de conexo", e); } return socket; }
From source file:com.gsf.dowload.nfe.HSProtocolSocketFactory.java
public Socket createSocket(String host, int port, InetAddress localAddress, int localPort, HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException { if (params == null) { throw new IllegalArgumentException("Parameters may not be null"); }//from ww w. ja v a2 s. c o m int timeout = params.getConnectionTimeout(); SocketFactory socketfactory = getSSLContext().getSocketFactory(); if (timeout == 0) { return socketfactory.createSocket(host, port, localAddress, localPort); } Socket socket = socketfactory.createSocket(); SocketAddress localaddr = new InetSocketAddress(localAddress, localPort); SocketAddress remoteaddr = new InetSocketAddress(host, port); socket.bind(localaddr); try { socket.connect(remoteaddr, timeout); } catch (Throwable t) { Logger.getLogger(HSProtocolSocketFactory.class.getName()).log(Level.SEVERE, null, t); throw new ConnectTimeoutException("Erro na conexao", t); } return socket; }
From source file:eu.unifiedviews.plugins.extractor.filesdownload.ReflectionSocketFactory.java
/** * This method attempts to execute Socket method available since Java 1.4 * using reflection. If the methods are not available or could not be executed <tt>null</tt> is returned * /*from ww w. j av a 2s.co m*/ * @param socketfactoryName * name of the socket factory class * @param host * the host name/IP * @param port * the port on the host * @param localAddress * the local host name/IP to bind the socket to * @param localPort * the port on the local machine * @param timeout * the timeout value to be used in milliseconds. If the socket cannot be * completed within the given time limit, it will be abandoned * @return a connected Socket * @throws IOException * if an I/O error occurs while creating the socket * @throws UnknownHostException * if the IP address of the host cannot be * determined * @throws ConnectTimeoutException * if socket cannot be connected within the * given time limit */ public static Socket createSocket(final Object socketfactory, final String host, final int port, final InetAddress localAddress, final int localPort, int timeout) throws IOException, UnknownHostException, ConnectTimeoutException { if (REFLECTION_FAILED) { //This is known to have failed before. Do not try it again return null; } // This code uses reflection to essentially do the following: // // SocketFactory socketFactory = Class.forName(socketfactoryName).getDefault(); // Socket socket = socketFactory.createSocket(); // SocketAddress localaddr = new InetSocketAddress(localAddress, localPort); // SocketAddress remoteaddr = new InetSocketAddress(host, port); // socket.bind(localaddr); // socket.connect(remoteaddr, timeout); // return socket; try { Class socketfactoryClass = socketfactory.getClass();//Class.forName(socketfactoryName); Method method = socketfactoryClass.getMethod("getDefault", new Class[] {}); // Object socketfactory = method.invoke(null, // new Object[] {}); method = socketfactoryClass.getMethod("createSocket", new Class[] {}); Socket socket = (Socket) method.invoke(socketfactory, new Object[] {}); if (INETSOCKETADDRESS_CONSTRUCTOR == null) { Class addressClass = Class.forName("java.net.InetSocketAddress"); INETSOCKETADDRESS_CONSTRUCTOR = addressClass .getConstructor(new Class[] { InetAddress.class, Integer.TYPE }); } Object remoteaddr = INETSOCKETADDRESS_CONSTRUCTOR .newInstance(new Object[] { InetAddress.getByName(host), new Integer(port) }); Object localaddr = INETSOCKETADDRESS_CONSTRUCTOR .newInstance(new Object[] { localAddress, new Integer(localPort) }); if (SOCKETCONNECT_METHOD == null) { SOCKETCONNECT_METHOD = Socket.class.getMethod("connect", new Class[] { Class.forName("java.net.SocketAddress"), Integer.TYPE }); } if (SOCKETBIND_METHOD == null) { SOCKETBIND_METHOD = Socket.class.getMethod("bind", new Class[] { Class.forName("java.net.SocketAddress") }); } SOCKETBIND_METHOD.invoke(socket, new Object[] { localaddr }); SOCKETCONNECT_METHOD.invoke(socket, new Object[] { remoteaddr, new Integer(timeout) }); return socket; } catch (InvocationTargetException e) { Throwable cause = e.getTargetException(); if (SOCKETTIMEOUTEXCEPTION_CLASS == null) { try { SOCKETTIMEOUTEXCEPTION_CLASS = Class.forName("java.net.SocketTimeoutException"); } catch (ClassNotFoundException ex) { // At this point this should never happen. Really. REFLECTION_FAILED = true; return null; } } if (SOCKETTIMEOUTEXCEPTION_CLASS.isInstance(cause)) { throw new ConnectTimeoutException( "The host did not accept the connection within timeout of " + timeout + " ms", cause); } if (cause instanceof IOException) { throw (IOException) cause; } return null; } catch (Exception e) { REFLECTION_FAILED = true; return null; } }
From source file:org.lockss.util.urlconn.LockssDefaultProtocolSocketFactory.java
/** * This is the only factory method that handles params, thus the only one * we need to override.//w ww . j a v a2s.c o m * @param host the host name/IP * @param port the port on the host * @param localAddress the local host name/IP to bind the socket to * @param localPort the local port to bing the socket to * @param params {@link HttpConnectionParams Http connection parameters} * * @return Socket a new socket * * @throws IOException if an I/O error occurs while creating the socket * @throws UnknownHostException if the IP address of the host cannot be * determined * @throws ConnectTimeoutException if socket cannot be connected within * the given time limit */ public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort, final HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException { if (params == null) { throw new IllegalArgumentException("Parameters may not be null"); } Socket sock = new Socket(); sock.bind(new InetSocketAddress(localAddress, localPort)); sock.setKeepAlive(params.getBooleanParameter(HttpClientUrlConnection.SO_KEEPALIVE, false)); int timeout = params.getConnectionTimeout(); if (timeout == 0) { sock.connect(new InetSocketAddress(host, port)); } else { try { sock.connect(new InetSocketAddress(host, port), timeout); } catch (SocketTimeoutException e) { // Reproduce httpclient behavior - distinguish connect timeout from // data timeout String msg = "The host did not accept the connection within timeout of " + timeout + " ms"; throw new ConnectTimeoutException(msg, e); } } return sock; }