/** * 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 */ 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, idle timeout: " + idleTimeout); } for (Entry<HttpConnection, TimeValues> entry : connectionToTimes.entrySet()) { HttpConnection conn = entry.getKey(); TimeValues times = entry.getValue(); long connectionTime = times.timeAdded; if (connectionTime <= idleTimeout) { if (log.isDebugEnabled()) { log.debug("Closing idle connection, connection time: " + connectionTime); } try { conn.close(); } catch (IOException ex) { log.debug("I/O error closing connection", ex); } } } }
public void closeExpiredConnections() { long now = System.currentTimeMillis(); if (log.isDebugEnabled()) { log.debug("Checking for expired connections, now: " + now); } for (Entry<HttpConnection, TimeValues> entry : connectionToTimes.entrySet()) { HttpConnection conn = entry.getKey(); TimeValues times = entry.getValue(); if(times.timeExpires <= now) { if (log.isDebugEnabled()) { log.debug("Closing connection, expired @: " + times.timeExpires); } try { conn.close(); } catch (IOException ex) { log.debug("I/O error closing connection", ex); } } } }
public Object getUserToken(final HttpContext context) { final HttpClientContext clientContext = HttpClientContext.adapt(context); Principal userPrincipal = null; final AuthStateHC4 targetAuthState = clientContext.getTargetAuthState(); if (targetAuthState != null) { userPrincipal = getAuthPrincipal(targetAuthState); if (userPrincipal == null) { final AuthStateHC4 proxyAuthState = clientContext.getProxyAuthState(); userPrincipal = getAuthPrincipal(proxyAuthState); } } if (userPrincipal == null) { final HttpConnection conn = clientContext.getConnection(); if (conn.isOpen() && conn instanceof ManagedHttpClientConnection) { final SSLSession sslsession = ((ManagedHttpClientConnection) conn).getSSLSession(); if (sslsession != null) { userPrincipal = sslsession.getLocalPrincipal(); } } } return userPrincipal; }
/** * 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 */ public void closeIdleConnections(final long idleTime) { // the latest time for which connections will be closed final long idleTimeout = System.currentTimeMillis() - idleTime; if (log.isDebugEnabled()) { log.debug("Checking for connections, idle timeout: " + idleTimeout); } for (final Entry<HttpConnection, TimeValues> entry : connectionToTimes.entrySet()) { final HttpConnection conn = entry.getKey(); final TimeValues times = entry.getValue(); final long connectionTime = times.timeAdded; if (connectionTime <= idleTimeout) { if (log.isDebugEnabled()) { log.debug("Closing idle connection, connection time: " + connectionTime); } try { conn.close(); } catch (final IOException ex) { log.debug("I/O error closing connection", ex); } } } }
public void closeExpiredConnections() { final long now = System.currentTimeMillis(); if (log.isDebugEnabled()) { log.debug("Checking for expired connections, now: " + now); } for (final Entry<HttpConnection, TimeValues> entry : connectionToTimes.entrySet()) { final HttpConnection conn = entry.getKey(); final TimeValues times = entry.getValue(); if(times.timeExpires <= now) { if (log.isDebugEnabled()) { log.debug("Closing connection, expired @: " + times.timeExpires); } try { conn.close(); } catch (final IOException ex) { log.debug("I/O error closing connection", ex); } } } }
@Override public Object getUserToken(final HttpContext context) { final HttpClientContext clientContext = HttpClientContext.adapt(context); Principal userPrincipal = null; final AuthState targetAuthState = clientContext.getTargetAuthState(); if (targetAuthState != null) { userPrincipal = getAuthPrincipal(targetAuthState); if (userPrincipal == null) { final AuthState proxyAuthState = clientContext.getProxyAuthState(); userPrincipal = getAuthPrincipal(proxyAuthState); } } if (userPrincipal == null) { final HttpConnection conn = clientContext.getConnection(); if (conn.isOpen() && conn instanceof ManagedHttpClientConnection) { final SSLSession sslsession = ((ManagedHttpClientConnection) conn).getSSLSession(); if (sslsession != null) { userPrincipal = sslsession.getLocalPrincipal(); } } } return userPrincipal; }
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); } try { conn.close(); } catch (IOException ex) { log.debug("I/O error closing connection", ex); } } } }
/** * Registers the given connection with this handler. The connection will be held until * {@link #remove} or {@link #closeIdleConnections} is called. * * @param connection the connection to add * * @see #remove */ public void add(HttpConnection connection, long validDuration, TimeUnit unit) { long timeAdded = System.currentTimeMillis(); if (log.isDebugEnabled()) { log.debug("Adding connection at: " + timeAdded); } connectionToTimes.put(connection, new TimeValues(timeAdded, validDuration, unit)); }
/** * Removes the given connection from the list of connections to be closed when idle. * This will return true if the connection is still valid, and false * if the connection should be considered expired and not used. * * @param connection * @return True if the connection is still valid. */ public boolean remove(HttpConnection connection) { TimeValues times = connectionToTimes.remove(connection); if(times == null) { log.warn("Removing a connection that never existed!"); return true; } else { return System.currentTimeMillis() <= times.timeExpires; } }
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { Args.notNull(request, "HTTP request"); final HttpCoreContext corecontext = HttpCoreContext.adapt(context); final ProtocolVersion ver = request.getRequestLine().getProtocolVersion(); final String method = request.getRequestLine().getMethod(); if (method.equalsIgnoreCase("CONNECT") && ver.lessEquals(HttpVersion.HTTP_1_0)) { return; } if (!request.containsHeader(HTTP.TARGET_HOST)) { HttpHost targethost = corecontext.getTargetHost(); if (targethost == null) { final HttpConnection conn = corecontext.getConnection(); if (conn instanceof HttpInetConnection) { // Populate the context with a default HTTP host based on the // inet address of the target host final InetAddress address = ((HttpInetConnection) conn).getRemoteAddress(); final int port = ((HttpInetConnection) conn).getRemotePort(); if (address != null) { targethost = new HttpHost(address.getHostName(), port); } } if (targethost == null) { if (ver.lessEquals(HttpVersion.HTTP_1_0)) { return; } else { throw new ProtocolException("Target host missing"); } } } request.addHeader(HTTP.TARGET_HOST, targethost.toHostString()); } }
/** * Registers the given connection with this handler. The connection will be held until * {@link #remove} or {@link #closeIdleConnections} is called. * * @param connection the connection to add * * @see #remove */ public void add(final HttpConnection connection, final long validDuration, final TimeUnit unit) { final long timeAdded = System.currentTimeMillis(); if (log.isDebugEnabled()) { log.debug("Adding connection at: " + timeAdded); } connectionToTimes.put(connection, new TimeValues(timeAdded, validDuration, unit)); }
/** * Removes the given connection from the list of connections to be closed when idle. * This will return true if the connection is still valid, and false * if the connection should be considered expired and not used. * * @param connection * @return True if the connection is still valid. */ public boolean remove(final HttpConnection connection) { final TimeValues times = connectionToTimes.remove(connection); if(times == null) { log.warn("Removing a connection that never existed!"); return true; } else { return System.currentTimeMillis() <= times.timeExpires; } }
public void abort() { request.abort(); // try to close the connection? is this necessary? unclear based on preliminary debugging of HttpClient, but // it doesn't seem to hurt to try HttpConnection conn = (HttpConnection) ctx.getAttribute("http.connection"); if (conn != null) { try { conn.close(); } catch (IOException e) { // this is fine, we're shutting it down anyway } } }
/** * 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 */ 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, idle timeout: " + 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 <= idleTimeout) { if (log.isDebugEnabled()) { log.debug("Closing idle connection, connection time: " + connectionTime); } try { conn.close(); } catch (IOException ex) { log.debug("I/O error closing connection", ex); } } } }
@Override public void process(HttpResponse response, HttpContext context) throws HttpException, IOException { HttpConnection conn = (HttpConnection) context.getAttribute(ExecutionContext.HTTP_CONNECTION); HttpConnectionMetrics metrics = conn.getMetrics(); context.setAttribute(CONTEXT_METRICS, metrics); }
@Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { HttpConnection conn = (HttpConnection) context.getAttribute(ExecutionContext.HTTP_CONNECTION); HttpConnectionMetrics metrics = conn.getMetrics(); metrics.reset(); }
public IdleConnectionHandler() { super(); connectionToTimes = new HashMap<HttpConnection,TimeValues>(); }
private void shutdownConnection(final HttpConnection conn) { try { conn.shutdown(); } catch (IOException ignore) { } }
private void closeConnection(final HttpConnection conn) { try { conn.shutdown(); } catch (IOException ignore) { } }
public <T extends HttpConnection> T getConnection(final Class<T> clazz) { return getAttribute(HTTP_CONNECTION, clazz); }
public HttpConnection getConnection() { return getAttribute(HTTP_CONNECTION, HttpConnection.class); }
@Override public void process(HttpResponse response, HttpContext context) throws HttpException, IOException { HttpConnectionMetrics metrics = ((HttpConnection) context.getAttribute(HttpCoreContext.HTTP_CONNECTION)).getMetrics(); context.setAttribute(CONTEXT_METRICS, metrics); }
@Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { HttpConnectionMetrics metrics = ((HttpConnection) context.getAttribute(HttpCoreContext.HTTP_CONNECTION)).getMetrics(); metrics.reset(); }
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { if (request == null) { throw new IllegalArgumentException("HTTP request may not be null"); } if (context == null) { throw new IllegalArgumentException("HTTP context may not be null"); } ProtocolVersion ver = request.getRequestLine().getProtocolVersion(); String method = request.getRequestLine().getMethod(); if (method.equalsIgnoreCase("CONNECT") && ver.lessEquals(HttpVersion.HTTP_1_0)) { return; } if (!request.containsHeader(HTTP.TARGET_HOST)) { HttpHost targethost = (HttpHost) context .getAttribute(ExecutionContext.HTTP_TARGET_HOST); if (targethost == null) { HttpConnection conn = (HttpConnection) context .getAttribute(ExecutionContext.HTTP_CONNECTION); if (conn instanceof HttpInetConnection) { // Populate the context with a default HTTP host based on the // inet address of the target host InetAddress address = ((HttpInetConnection) conn).getRemoteAddress(); int port = ((HttpInetConnection) conn).getRemotePort(); if (address != null) { targethost = new HttpHost(address.getHostName(), port); } } if (targethost == null) { if (ver.lessEquals(HttpVersion.HTTP_1_0)) { return; } else { throw new ProtocolException("Target host missing"); } } } request.addHeader(HTTP.TARGET_HOST, targethost.toHostString()); } }