FileDocCategorySizeDatePackage
HttpConnection.javaAPI DocAndroid 1.5 API9670Wed May 06 22:41:04 BST 2009org.apache.harmony.luni.internal.net.www.protocol.http

HttpConnection

public class HttpConnection extends Object
An HttpConnection represents a persistent http or https connection and contains various utility methods to access that connection.

Fields Summary
private boolean
usingSecureSocket
private Socket
socket
private SSLSocket
sslSocket
private InputStream
inputStream
private OutputStream
outputStream
private InputStream
sslInputStream
private OutputStream
sslOutputStream
private HttpConfiguration
config
private static final Class
SOCKET_TIMEOUT_CLASS
Constructors Summary
public HttpConnection(HttpConfiguration config, int connectTimeout)


           
        this.config = config;
        String hostName = config.getHostName();
        int hostPort = config.getHostPort();
        Proxy proxy = config.getProxy();
        if(proxy == null || proxy.type() == Proxy.Type.HTTP) {
            socket = new Socket();
        } else {
            socket = new Socket(proxy);
        }
        socket.connect(new InetSocketAddress(hostName, hostPort), connectTimeout);
    
Methods Summary
private static java.lang.ClassSocketTimeoutExceptionClass()

        try {
            return Class.forName("java.net.SocketTimeoutException");
        } catch (ClassNotFoundException e) {
            return null;
        }
    
public voidcloseSocketAndStreams()

        if(usingSecureSocket) {
            if (null != sslOutputStream) {
                OutputStream temp = sslOutputStream;
                sslOutputStream = null;
                try {
                    temp.close();
                } catch (Exception ex) {
                    // ignored
                }
            }

            if (null != sslInputStream) {
                InputStream temp = sslInputStream;
                sslInputStream = null;
                try {
                    temp.close();
                } catch (Exception ex) {
                    // ignored
                }
            }

            if (null != sslSocket) {
                Socket temp = sslSocket;
                sslSocket = null;
                try {
                    temp.close();
                } catch (Exception ex) {
                    // ignored
                }
            }
        }
        if (null != outputStream) {
            OutputStream temp = outputStream;
            outputStream = null;
            try {
                temp.close();
            } catch (Exception ex) {
                // ignored
            }
        }

        if (null != inputStream) {
            InputStream temp = inputStream;
            inputStream = null;
            try {
                temp.close();
            } catch (Exception ex) {
                // ignored
            }
        }

        if (null != socket) {
            Socket temp = socket;
            socket = null;
            try {
                temp.close();
            } catch (Exception ex) {
                // ignored
            }
        }
    
public HttpConfigurationgetHttpConfiguration()

        return config;
    
public java.io.InputStreamgetInputStream()

        if(usingSecureSocket) {
            if (sslInputStream == null) {
                sslInputStream = sslSocket.getInputStream();
            }
            return sslInputStream;
        } else if(inputStream == null) {
            inputStream = socket.getInputStream();
        }
        return inputStream;
    
public java.io.OutputStreamgetOutputStream()

        if(usingSecureSocket) {
            if (sslOutputStream == null) {
                sslOutputStream = sslSocket.getOutputStream();
            }
            return sslOutputStream;
        } else if(outputStream == null) {
            outputStream = socket.getOutputStream();
        }
        return outputStream;
    
public javax.net.ssl.SSLSocketgetSecureSocket(javax.net.ssl.SSLSocketFactory sslSocketFactory, javax.net.ssl.HostnameVerifier hostnameVerifier)

        if(!usingSecureSocket) {
            String hostName = config.getHostName();
            int port = config.getHostPort();
            // create the wrapper over connected socket
            sslSocket = (SSLSocket) sslSocketFactory.createSocket(socket,
                    hostName, port, true);
            sslSocket.setUseClientMode(true);
            sslSocket.startHandshake();
            if (!hostnameVerifier.verify(hostName, sslSocket.getSession())) {
                throw new IOException(Messages.getString("luni.02", hostName)); //$NON-NLS-1$
            }
            usingSecureSocket = true;
        }
        return sslSocket;
    
java.net.SocketgetSocket()

        return socket;
    
protected booleanisEligibleForRecycling()
Returns whether this connection is eligible to be recycled. This is like {@link #isStale} except that it doesn't try to actually perform any I/O.

return
true if the connection is eligible to be recycled

        return ! (socket.isClosed() || socket.isInputShutdown()
                || socket.isOutputShutdown());
    
public static booleanisSocketTimeoutException(java.io.InterruptedIOException e)

    
    /*
     * This method has been copied from the Apache Jakarta Commons HttpClient project
     * http://svn.apache.org/repos/asf/jakarta/commons/proper/httpclient/trunk/HttpClient/src/java/org/apache/commons/httpclient/HttpConnection.java r480424
     */
          
        if (SOCKET_TIMEOUT_CLASS != null) {
            return SOCKET_TIMEOUT_CLASS.isInstance(e);
        } else {
            return true;
        }
    
protected booleanisStale()

        boolean isStale = true;
        // BEGIN android-note
        // The following line was expanded to check for input/output shutdown.
        // END android-note
        if (! (socket.isClosed() || socket.isInputShutdown()
                        || socket.isOutputShutdown())) {
            // the socket is open, but could still have been closed from the other end
            isStale = false;
            try {
                if (inputStream.available() <= 0) {
                    int soTimeout = socket.getSoTimeout();
                    try {
                        socket.setSoTimeout(1);
                        inputStream.mark(1);
                        int byteRead = inputStream.read();
                        if (byteRead == -1) {
                            // again - if the socket is reporting all data read,
                            // probably stale
                            isStale = true;
                        } else {
                            inputStream.reset();
                        }
                    } finally {
                        socket.setSoTimeout(soTimeout);
                    }
                }
            } catch (InterruptedIOException e) {
                if (!isSocketTimeoutException(e)) {
                    throw e;
                }
                // aha - the connection is NOT stale - continue on!
            } catch (IOException e) {
                // oops - the connection is stale, the read or soTimeout failed.
                isStale = true;
            }
        }

        return isStale;
    
public voidsetSoTimeout(int readTimeout)

        socket.setSoTimeout(readTimeout);