List of usage examples for org.apache.http.conn OperatedClientConnection isOpen
boolean isOpen();
From source file:com.msopentech.thali.utilities.universal.HttpKeySocksProxyClientConnOperator.java
@Override public void openConnection(final OperatedClientConnection conn, final HttpHost target, final InetAddress local, final HttpContext context, final HttpParams params) throws IOException { Socket socket = null;//from w ww . ja v a 2s . c om Socket sslSocket = null; try { if (conn == null || target == null || params == null) { throw new IllegalArgumentException("Required argument may not be null"); } if (conn.isOpen()) { throw new IllegalStateException("Connection must not be open"); } // The original NetCipher code uses a SchemeSocketFactory class that isn't supported by the version // of Apache that ships standard with Android. It also doesn't support the layered socket factory // interface either. We work around this later on but for now we just get our HttpKeySSLSocketFactory Scheme scheme = schemeRegistry.getScheme(target.getSchemeName()); HttpKeySSLSocketFactory httpKeySSLSocketFactory = (HttpKeySSLSocketFactory) scheme.getSocketFactory(); int port = scheme.resolvePort(target.getPort()); String host = target.getHostName(); // Perform explicit SOCKS4a connection request. SOCKS4a supports remote host name resolution // (i.e., Tor resolves the hostname, which may be an onion address). // The Android (Apache Harmony) Socket class appears to support only SOCKS4 and throws an // exception on an address created using INetAddress.createUnresolved() -- so the typical // technique for using Java SOCKS4a/5 doesn't appear to work on Android: // https://android.googlesource.com/platform/libcore/+/master/luni/src/main/java/java/net/PlainSocketImpl.java // See also: http://www.mit.edu/~foley/TinFoil/src/tinfoil/TorLib.java, for a similar implementation // From http://en.wikipedia.org/wiki/SOCKS#SOCKS4a: // // field 1: SOCKS version number, 1 byte, must be 0x04 for this version // field 2: command code, 1 byte: // 0x01 = establish a TCP/IP stream connection // 0x02 = establish a TCP/IP port binding // field 3: network byte order port number, 2 bytes // field 4: deliberate invalid IP address, 4 bytes, first three must be 0x00 and the last one must not be 0x00 // field 5: the user ID string, variable length, terminated with a null (0x00) // field 6: the domain name of the host we want to contact, variable length, terminated with a null (0x00) socket = new Socket(); conn.opening(socket, target); socket.setSoTimeout(READ_TIMEOUT_MILLISECONDS); socket.connect(proxy.address(), CONNECT_TIMEOUT_MILLISECONDS); DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream()); outputStream.write((byte) 0x04); outputStream.write((byte) 0x01); outputStream.writeShort((short) port); outputStream.writeInt(0x01); outputStream.write((byte) 0x00); outputStream.write(host.getBytes()); outputStream.write((byte) 0x00); DataInputStream inputStream = new DataInputStream(socket.getInputStream()); if (inputStream.readByte() != (byte) 0x00 || inputStream.readByte() != (byte) 0x5a) { throw new IOException("SOCKS4a connect failed"); } inputStream.readShort(); inputStream.readInt(); // In the NetCipher code we cast to SchemeLayeredSocketFactory and call createLayeredSocket which amongst // other things takes 'params' as an argument. But none of this is supported in Android. When I looked in // Java at what createLayeredSocket was actually doing it was just calling createSocket with exactly the // arguments used below (it ignored params completely). So we should be good. sslSocket = ((HttpKeySSLSocketFactory) httpKeySSLSocketFactory).createSocket(socket, host, port, true); conn.opening(sslSocket, target); sslSocket.setSoTimeout(READ_TIMEOUT_MILLISECONDS); prepareSocket(sslSocket, context, params); conn.openCompleted(httpKeySSLSocketFactory.isSecure(sslSocket), params); // TODO: clarify which connection throws java.net.SocketTimeoutException? } catch (IOException e) { try { if (sslSocket != null) { sslSocket.close(); } if (socket != null) { socket.close(); } } catch (IOException ioe) { } throw e; } }
From source file:com.qiniu.android.http.ClientConnectionOperator.java
public void updateSecureConnection(OperatedClientConnection conn, HttpHost target, HttpContext context, HttpParams params) throws IOException { if (conn == null) { throw new IllegalArgumentException("Connection must not be null."); }/* w w w . ja va 2 s . c om*/ if (target == null) { throw new IllegalArgumentException("Target host must not be null."); } if (params == null) { throw new IllegalArgumentException("Parameters must not be null."); } if (!conn.isOpen()) { throw new IllegalArgumentException("Connection must be open."); } final Scheme schm = schemeRegistry.getScheme(target.getSchemeName()); if (!(schm.getSocketFactory() instanceof LayeredSocketFactory)) { throw new IllegalArgumentException( "Target scheme (" + schm.getName() + ") must have layered socket factory."); } final LayeredSocketFactory lsf = (LayeredSocketFactory) schm.getSocketFactory(); final Socket sock; try { sock = lsf.createSocket(conn.getSocket(), target.getHostName(), target.getPort(), true); } catch (ConnectException ex) { throw new HttpHostConnectException(target, ex); } prepareSocket(sock, context, params); conn.update(sock, target, lsf.isSecure(sock), params); }
From source file:com.android.mms.service.http.NetworkAwareClientConnectionOperator.java
/** * This method is mostly copied from the overridden one in parent. The only change * is how we resolve host name.// ww w . ja v a2 s .c o m */ @Override public void openConnection(OperatedClientConnection conn, HttpHost target, InetAddress local, HttpContext context, HttpParams params) throws IOException { if (conn == null) { throw new IllegalArgumentException("Connection must not be null."); } if (target == null) { throw new IllegalArgumentException("Target host must not be null."); } // local address may be null //@@@ is context allowed to be null? if (params == null) { throw new IllegalArgumentException("Parameters must not be null."); } if (conn.isOpen()) { throw new IllegalArgumentException("Connection must not be open."); } final Scheme schm = schemeRegistry.getScheme(target.getSchemeName()); final SocketFactory sf = schm.getSocketFactory(); final SocketFactory plain_sf; final LayeredSocketFactory layered_sf; if (sf instanceof LayeredSocketFactory) { plain_sf = staticPlainSocketFactory; layered_sf = (LayeredSocketFactory) sf; } else { plain_sf = sf; layered_sf = null; } // CHANGE FOR MmsService ArrayList<InetAddress> addresses = resolveHostName(target.getHostName()); for (int i = 0; i < addresses.size(); ++i) { Log.d(TAG, "NetworkAwareClientConnectionOperator: connecting " + addresses.get(i)); Socket sock = plain_sf.createSocket(); conn.opening(sock, target); try { Socket connsock = plain_sf.connectSocket(sock, addresses.get(i).getHostAddress(), schm.resolvePort(target.getPort()), local, 0, params); if (sock != connsock) { sock = connsock; conn.opening(sock, target); } /* * prepareSocket is called on the just connected * socket before the creation of the layered socket to * ensure that desired socket options such as * TCP_NODELAY, SO_RCVTIMEO, SO_LINGER will be set * before any I/O is performed on the socket. This * happens in the common case as * SSLSocketFactory.createSocket performs hostname * verification which requires that SSL handshaking be * performed. */ prepareSocket(sock, context, params); if (layered_sf != null) { Socket layeredsock = layered_sf.createSocket(sock, target.getHostName(), schm.resolvePort(target.getPort()), true); if (layeredsock != sock) { conn.opening(layeredsock, target); } conn.openCompleted(sf.isSecure(layeredsock), params); } else { conn.openCompleted(sf.isSecure(sock), params); } break; // BEGIN android-changed // catch SocketException to cover any kind of connect failure } catch (SocketException ex) { if (i == addresses.size() - 1) { ConnectException cause = ex instanceof ConnectException ? (ConnectException) ex : (ConnectException) new ConnectException(ex.getMessage()).initCause(ex); throw new HttpHostConnectException(target, cause); } // END android-changed } catch (ConnectTimeoutException ex) { if (i == addresses.size() - 1) { throw ex; } } } }
From source file:com.qiniu.android.http.ClientConnectionOperator.java
public void openConnection(final OperatedClientConnection conn, final HttpHost target, final InetAddress local, final HttpContext context, final HttpParams params) throws IOException { if (conn == null) { throw new IllegalArgumentException("Connection must not be null."); }/*w ww .jav a 2s.co m*/ if (target == null) { throw new IllegalArgumentException("Target host must not be null."); } // local address may be null //@@@ is context allowed to be null? if (params == null) { throw new IllegalArgumentException("Parameters must not be null."); } if (conn.isOpen()) { throw new IllegalArgumentException("Connection must not be open."); } final Scheme schm = schemeRegistry.getScheme(target.getSchemeName()); final SocketFactory sf = schm.getSocketFactory(); String host = target.getHostName(); String[] ips; if (validIP(host)) { ips = new String[] { host }; } else { ips = systemResolv(host); if (ips == null || ips.length == 0) { throw new UnknownHostException("no ip for " + host); } } final int port = schm.resolvePort(target.getPort()); for (int i = 0; i < ips.length; i++) { final String ip = ips[i]; final boolean last = i == ips.length - 1; Socket sock = sf.createSocket(); conn.opening(sock, target); try { Socket connsock = sf.connectSocket(sock, ip, port, local, 0, params); if (sock != connsock) { sock = connsock; conn.opening(sock, target); } prepareSocket(sock, context, params); conn.openCompleted(sf.isSecure(sock), params); AsyncHttpClientMod.ip.set(ip); return; } catch (ConnectException ex) { if (last) { throw new HttpHostConnectException(target, ex); } } } }
From source file:org.apache.http.impl.conn.DefaultClientConnectionOperator.java
public void openConnection(final OperatedClientConnection conn, final HttpHost target, final InetAddress local, final HttpContext context, final HttpParams params) throws IOException { Args.notNull(conn, "Connection"); Args.notNull(target, "Target host"); Args.notNull(params, "HTTP parameters"); Asserts.check(!conn.isOpen(), "Connection must not be open"); final SchemeRegistry registry = getSchemeRegistry(context); final Scheme schm = registry.getScheme(target.getSchemeName()); final SchemeSocketFactory sf = schm.getSchemeSocketFactory(); final InetAddress[] addresses = resolveHostname(target.getHostName()); final int port = schm.resolvePort(target.getPort()); for (int i = 0; i < addresses.length; i++) { final InetAddress address = addresses[i]; final boolean last = i == addresses.length - 1; Socket sock = sf.createSocket(params); conn.opening(sock, target);/*from w ww . ja v a 2s. c om*/ final InetSocketAddress remoteAddress = new HttpInetSocketAddress(target, address, port); InetSocketAddress localAddress = null; if (local != null) { localAddress = new InetSocketAddress(local, 0); } if (this.log.isDebugEnabled()) { this.log.debug("Connecting to " + remoteAddress); } try { final Socket connsock = sf.connectSocket(sock, remoteAddress, localAddress, params); if (sock != connsock) { sock = connsock; conn.opening(sock, target); } prepareSocket(sock, context, params); conn.openCompleted(sf.isSecure(sock), params); return; } catch (final ConnectException ex) { if (last) { throw ex; } } catch (final ConnectTimeoutException ex) { if (last) { throw ex; } } if (this.log.isDebugEnabled()) { this.log.debug("Connect to " + remoteAddress + " timed out. " + "Connection will be retried using another IP address"); } } }
From source file:org.apache.http.impl.conn.DefaultClientConnectionOperator.java
public void updateSecureConnection(final OperatedClientConnection conn, final HttpHost target, final HttpContext context, final HttpParams params) throws IOException { Args.notNull(conn, "Connection"); Args.notNull(target, "Target host"); Args.notNull(params, "Parameters"); Asserts.check(conn.isOpen(), "Connection must be open"); final SchemeRegistry registry = getSchemeRegistry(context); final Scheme schm = registry.getScheme(target.getSchemeName()); Asserts.check(schm.getSchemeSocketFactory() instanceof SchemeLayeredSocketFactory, "Socket factory must implement SchemeLayeredSocketFactory"); final SchemeLayeredSocketFactory lsf = (SchemeLayeredSocketFactory) schm.getSchemeSocketFactory(); final Socket sock = lsf.createLayeredSocket(conn.getSocket(), target.getHostName(), schm.resolvePort(target.getPort()), params); prepareSocket(sock, context, params); conn.update(sock, target, lsf.isSecure(sock), params); }
From source file:org.apache.http.impl.conn.HttpPoolEntry.java
@Override public boolean isClosed() { final OperatedClientConnection conn = getConnection(); return !conn.isOpen(); }