From 39a0d0a6207b9219227c2048c88fa7d8017d7f6e Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Mon, 22 Feb 2010 18:45:55 +0000 Subject: [PATCH] HTTPCLIENT-915: mechanism to attatch user define attributes to connections git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@915013 13f79535-47bb-0310-9956-ffa450edef68 --- .../impl/conn/AbstractClientConnAdapter.java | 32 ++++++++++++++++++- .../impl/conn/DefaultClientConnection.java | 21 +++++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java b/httpclient/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java index 46d435420..15cb37952 100644 --- a/httpclient/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java +++ b/httpclient/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java @@ -43,6 +43,7 @@ import org.apache.http.HttpConnectionMetrics; import org.apache.http.conn.OperatedClientConnection; import org.apache.http.conn.ManagedClientConnection; import org.apache.http.conn.ClientConnectionManager; +import org.apache.http.protocol.HttpContext; /** * Abstract adapter from {@link OperatedClientConnection operated} to @@ -65,7 +66,8 @@ import org.apache.http.conn.ClientConnectionManager; * * @since 4.0 */ -public abstract class AbstractClientConnAdapter implements ManagedClientConnection { +public abstract class AbstractClientConnAdapter + implements ManagedClientConnection, HttpContext { /** * The connection manager, if any. @@ -322,4 +324,32 @@ public abstract class AbstractClientConnAdapter implements ManagedClientConnecti } } + public synchronized Object getAttribute(final String id) { + OperatedClientConnection conn = getWrappedConnection(); + assertValid(conn); + if (conn instanceof HttpContext) { + return ((HttpContext) conn).getAttribute(id); + } else { + return null; + } + } + + public synchronized Object removeAttribute(final String id) { + OperatedClientConnection conn = getWrappedConnection(); + assertValid(conn); + if (conn instanceof HttpContext) { + return ((HttpContext) conn).removeAttribute(id); + } else { + return null; + } + } + + public synchronized void setAttribute(final String id, final Object obj) { + OperatedClientConnection conn = getWrappedConnection(); + assertValid(conn); + if (conn instanceof HttpContext) { + ((HttpContext) conn).setAttribute(id, obj); + } + } + } diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnection.java b/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnection.java index a8a3c598e..72731138b 100644 --- a/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnection.java +++ b/httpclient/src/main/java/org/apache/http/impl/conn/DefaultClientConnection.java @@ -29,6 +29,8 @@ package org.apache.http.impl.conn; import java.io.IOException; import java.net.Socket; +import java.util.HashMap; +import java.util.Map; import org.apache.http.annotation.NotThreadSafe; @@ -41,6 +43,7 @@ import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; import org.apache.http.HttpResponseFactory; import org.apache.http.params.HttpParams; +import org.apache.http.protocol.HttpContext; import org.apache.http.impl.SocketHttpClientConnection; import org.apache.http.io.HttpMessageParser; import org.apache.http.io.SessionInputBuffer; @@ -65,7 +68,7 @@ import org.apache.http.conn.OperatedClientConnection; */ @NotThreadSafe // connSecure, targetHost public class DefaultClientConnection extends SocketHttpClientConnection - implements OperatedClientConnection { + implements OperatedClientConnection, HttpContext { private final Log log = LogFactory.getLog(getClass()); private final Log headerLog = LogFactory.getLog("org.apache.http.headers"); @@ -83,8 +86,12 @@ public class DefaultClientConnection extends SocketHttpClientConnection /** True if this connection was shutdown. */ private volatile boolean shutdown; + /** connection specific attributes */ + private final Map attributes; + public DefaultClientConnection() { super(); + this.attributes = new HashMap(); } public final HttpHost getTargetHost() { @@ -253,4 +260,16 @@ public class DefaultClientConnection extends SocketHttpClientConnection } } + public Object getAttribute(final String id) { + return this.attributes.get(id); + } + + public Object removeAttribute(final String id) { + return this.attributes.remove(id); + } + + public void setAttribute(final String id, final Object obj) { + this.attributes.put(id, obj); + } + }