Search in sources :

Example 6 with HttpConnection

use of org.apache.http.HttpConnection in project robovm by robovm.

the class IdleConnectionHandler method closeIdleConnections.

/**
     * Closes connections that have been idle for at least the given amount of time.
     * 
     * @param idleTime the minimum idle time, in milliseconds, for connections to be closed
     */
//@@@ add TimeUnit argument here?
public void closeIdleConnections(long idleTime) {
    // the latest time for which connections will be closed
    long idleTimeout = System.currentTimeMillis() - idleTime;
    if (log.isDebugEnabled()) {
        log.debug("Checking for connections, idleTimeout: " + idleTimeout);
    }
    Iterator<HttpConnection> connectionIter = connectionToTimes.keySet().iterator();
    while (connectionIter.hasNext()) {
        HttpConnection conn = connectionIter.next();
        TimeValues times = connectionToTimes.get(conn);
        Long connectionTime = times.timeAdded;
        if (connectionTime.longValue() <= idleTimeout) {
            if (log.isDebugEnabled()) {
                log.debug("Closing connection, connection time: " + connectionTime);
            }
            connectionIter.remove();
            try {
                conn.close();
            } catch (IOException ex) {
                log.debug("I/O error closing connection", ex);
            }
        }
    }
}
Also used : HttpConnection(org.apache.http.HttpConnection) IOException(java.io.IOException)

Example 7 with HttpConnection

use of org.apache.http.HttpConnection in project robovm by robovm.

the class DefaultConnectionReuseStrategy method keepAlive.

// see interface ConnectionReuseStrategy
public boolean keepAlive(final HttpResponse response, final HttpContext context) {
    if (response == null) {
        throw new IllegalArgumentException("HTTP response may not be null.");
    }
    if (context == null) {
        throw new IllegalArgumentException("HTTP context may not be null.");
    }
    HttpConnection conn = (HttpConnection) context.getAttribute(ExecutionContext.HTTP_CONNECTION);
    if (conn != null && !conn.isOpen())
        return false;
    // do NOT check for stale connection, that is an expensive operation
    // Check for a self-terminating entity. If the end of the entity will
    // be indicated by closing the connection, there is no keep-alive.
    HttpEntity entity = response.getEntity();
    ProtocolVersion ver = response.getStatusLine().getProtocolVersion();
    if (entity != null) {
        if (entity.getContentLength() < 0) {
            if (!entity.isChunked() || ver.lessEquals(HttpVersion.HTTP_1_0)) {
                // encoded, the connection cannot be reused
                return false;
            }
        }
    }
    // Check for the "Connection" header. If that is absent, check for
    // the "Proxy-Connection" header. The latter is an unspecified and
    // broken but unfortunately common extension of HTTP.
    HeaderIterator hit = response.headerIterator(HTTP.CONN_DIRECTIVE);
    if (!hit.hasNext())
        hit = response.headerIterator("Proxy-Connection");
    if (hit.hasNext()) {
        try {
            TokenIterator ti = createTokenIterator(hit);
            boolean keepalive = false;
            while (ti.hasNext()) {
                final String token = ti.nextToken();
                if (HTTP.CONN_CLOSE.equalsIgnoreCase(token)) {
                    return false;
                } else if (HTTP.CONN_KEEP_ALIVE.equalsIgnoreCase(token)) {
                    // continue the loop, there may be a "close" afterwards
                    keepalive = true;
                }
            }
            if (keepalive)
                return true;
        // neither "close" nor "keep-alive", use default policy
        } catch (ParseException px) {
            // we don't have logging in HttpCore, so the exception is lost
            return false;
        }
    }
    // default since HTTP/1.1 is persistent, before it was non-persistent
    return !ver.lessEquals(HttpVersion.HTTP_1_0);
}
Also used : HttpEntity(org.apache.http.HttpEntity) TokenIterator(org.apache.http.TokenIterator) BasicTokenIterator(org.apache.http.message.BasicTokenIterator) HttpConnection(org.apache.http.HttpConnection) HeaderIterator(org.apache.http.HeaderIterator) ParseException(org.apache.http.ParseException) ProtocolVersion(org.apache.http.ProtocolVersion)

Example 8 with HttpConnection

use of org.apache.http.HttpConnection in project XobotOS by xamarin.

the class IdleConnectionHandler method closeExpiredConnections.

public void closeExpiredConnections() {
    long now = System.currentTimeMillis();
    if (log.isDebugEnabled()) {
        log.debug("Checking for expired connections, now: " + now);
    }
    Iterator<HttpConnection> connectionIter = connectionToTimes.keySet().iterator();
    while (connectionIter.hasNext()) {
        HttpConnection conn = connectionIter.next();
        TimeValues times = connectionToTimes.get(conn);
        if (times.timeExpires <= now) {
            if (log.isDebugEnabled()) {
                log.debug("Closing connection, expired @: " + times.timeExpires);
            }
            connectionIter.remove();
            try {
                conn.close();
            } catch (IOException ex) {
                log.debug("I/O error closing connection", ex);
            }
        }
    }
}
Also used : HttpConnection(org.apache.http.HttpConnection) IOException(java.io.IOException)

Example 9 with HttpConnection

use of org.apache.http.HttpConnection in project XobotOS by xamarin.

the class IdleConnectionHandler method closeIdleConnections.

/**
     * Closes connections that have been idle for at least the given amount of time.
     * 
     * @param idleTime the minimum idle time, in milliseconds, for connections to be closed
     */
//@@@ add TimeUnit argument here?
public void closeIdleConnections(long idleTime) {
    // the latest time for which connections will be closed
    long idleTimeout = System.currentTimeMillis() - idleTime;
    if (log.isDebugEnabled()) {
        log.debug("Checking for connections, idleTimeout: " + idleTimeout);
    }
    Iterator<HttpConnection> connectionIter = connectionToTimes.keySet().iterator();
    while (connectionIter.hasNext()) {
        HttpConnection conn = connectionIter.next();
        TimeValues times = connectionToTimes.get(conn);
        Long connectionTime = times.timeAdded;
        if (connectionTime.longValue() <= idleTimeout) {
            if (log.isDebugEnabled()) {
                log.debug("Closing connection, connection time: " + connectionTime);
            }
            connectionIter.remove();
            try {
                conn.close();
            } catch (IOException ex) {
                log.debug("I/O error closing connection", ex);
            }
        }
    }
}
Also used : HttpConnection(org.apache.http.HttpConnection) IOException(java.io.IOException)

Example 10 with HttpConnection

use of org.apache.http.HttpConnection in project XobotOS by xamarin.

the class DefaultConnectionReuseStrategy method keepAlive.

// see interface ConnectionReuseStrategy
public boolean keepAlive(final HttpResponse response, final HttpContext context) {
    if (response == null) {
        throw new IllegalArgumentException("HTTP response may not be null.");
    }
    if (context == null) {
        throw new IllegalArgumentException("HTTP context may not be null.");
    }
    HttpConnection conn = (HttpConnection) context.getAttribute(ExecutionContext.HTTP_CONNECTION);
    if (conn != null && !conn.isOpen())
        return false;
    // do NOT check for stale connection, that is an expensive operation
    // Check for a self-terminating entity. If the end of the entity will
    // be indicated by closing the connection, there is no keep-alive.
    HttpEntity entity = response.getEntity();
    ProtocolVersion ver = response.getStatusLine().getProtocolVersion();
    if (entity != null) {
        if (entity.getContentLength() < 0) {
            if (!entity.isChunked() || ver.lessEquals(HttpVersion.HTTP_1_0)) {
                // encoded, the connection cannot be reused
                return false;
            }
        }
    }
    // Check for the "Connection" header. If that is absent, check for
    // the "Proxy-Connection" header. The latter is an unspecified and
    // broken but unfortunately common extension of HTTP.
    HeaderIterator hit = response.headerIterator(HTTP.CONN_DIRECTIVE);
    if (!hit.hasNext())
        hit = response.headerIterator("Proxy-Connection");
    if (hit.hasNext()) {
        try {
            TokenIterator ti = createTokenIterator(hit);
            boolean keepalive = false;
            while (ti.hasNext()) {
                final String token = ti.nextToken();
                if (HTTP.CONN_CLOSE.equalsIgnoreCase(token)) {
                    return false;
                } else if (HTTP.CONN_KEEP_ALIVE.equalsIgnoreCase(token)) {
                    // continue the loop, there may be a "close" afterwards
                    keepalive = true;
                }
            }
            if (keepalive)
                return true;
        // neither "close" nor "keep-alive", use default policy
        } catch (ParseException px) {
            // we don't have logging in HttpCore, so the exception is lost
            return false;
        }
    }
    // default since HTTP/1.1 is persistent, before it was non-persistent
    return !ver.lessEquals(HttpVersion.HTTP_1_0);
}
Also used : HttpEntity(org.apache.http.HttpEntity) TokenIterator(org.apache.http.TokenIterator) BasicTokenIterator(org.apache.http.message.BasicTokenIterator) HttpConnection(org.apache.http.HttpConnection) HeaderIterator(org.apache.http.HeaderIterator) ParseException(org.apache.http.ParseException) ProtocolVersion(org.apache.http.ProtocolVersion)

Aggregations

HttpConnection (org.apache.http.HttpConnection)12 IOException (java.io.IOException)6 ProtocolVersion (org.apache.http.ProtocolVersion)6 InetAddress (java.net.InetAddress)3 HeaderIterator (org.apache.http.HeaderIterator)3 HttpEntity (org.apache.http.HttpEntity)3 HttpHost (org.apache.http.HttpHost)3 HttpInetConnection (org.apache.http.HttpInetConnection)3 ParseException (org.apache.http.ParseException)3 ProtocolException (org.apache.http.ProtocolException)3 TokenIterator (org.apache.http.TokenIterator)3 BasicTokenIterator (org.apache.http.message.BasicTokenIterator)3