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
This commit is contained in:
Oleg Kalnichevski 2010-02-22 18:45:55 +00:00
parent 40f2e9ba92
commit 39a0d0a620
2 changed files with 51 additions and 2 deletions

View File

@ -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);
}
}
}

View File

@ -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<String, Object> attributes;
public DefaultClientConnection() {
super();
this.attributes = new HashMap<String, Object>();
}
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);
}
}