package org.apache.http.impl.conn;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import org.apache.http.HttpConnectionMetrics;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.ManagedClientConnection;
import org.apache.http.conn.OperatedClientConnection;
import org.apache.http.protocol.HttpContext;
@Deprecated
public abstract class AbstractClientConnAdapter implements ManagedClientConnection, HttpContext {
private final ClientConnectionManager connManager;
private volatile OperatedClientConnection wrappedConnection;
private volatile boolean markedReusable;
private volatile boolean released;
private volatile long duration;
protected AbstractClientConnAdapter(final ClientConnectionManager mgr,
final OperatedClientConnection conn) {
super();
connManager = mgr;
wrappedConnection = conn;
markedReusable = false;
released = false;
duration = Long.MAX_VALUE;
}
protected synchronized void detach() {
wrappedConnection = null;
duration = Long.MAX_VALUE;
}
protected OperatedClientConnection getWrappedConnection() {
return wrappedConnection;
}
protected ClientConnectionManager getManager() {
return connManager;
}
@Deprecated
protected final void assertNotAborted() throws InterruptedIOException {
if (isReleased()) {
throw new InterruptedIOException("Connection has been shut down");
}
}
protected boolean isReleased() {
return released;
}
protected final void assertValid(
final OperatedClientConnection wrappedConn) throws ConnectionShutdownException {
if (isReleased() || wrappedConn == null) {
throw new ConnectionShutdownException();
}
}
@Override
public boolean isOpen() {
final OperatedClientConnection conn = getWrappedConnection();
if (conn == null) {
return false;
}
return conn.isOpen();
}
@Override
public boolean isStale() {
if (isReleased()) {
return true;
}
final OperatedClientConnection conn = getWrappedConnection();
if (conn == null) {
return true;
}
return conn.isStale();
}
@Override
public void setSocketTimeout(final int timeout) {
final OperatedClientConnection conn = getWrappedConnection();
assertValid(conn);
conn.setSocketTimeout(timeout);
}
@Override
public int getSocketTimeout() {
final OperatedClientConnection conn = getWrappedConnection();
assertValid(conn);
return conn.getSocketTimeout();
}
@Override
public HttpConnectionMetrics getMetrics() {
final OperatedClientConnection conn = getWrappedConnection();
assertValid(conn);
return conn.getMetrics();
}
@Override
public void flush() throws IOException {
final OperatedClientConnection conn = getWrappedConnection();
assertValid(conn);
conn.flush();
}
@Override
public boolean isResponseAvailable(final int timeout) throws IOException {
final OperatedClientConnection conn = getWrappedConnection();
assertValid(conn);
return conn.isResponseAvailable(timeout);
}
@Override
public void receiveResponseEntity(final HttpResponse response)
throws HttpException, IOException {
final OperatedClientConnection conn = getWrappedConnection();
assertValid(conn);
unmarkReusable();
conn.receiveResponseEntity(response);
}
@Override
public HttpResponse ()
throws HttpException, IOException {
final OperatedClientConnection conn = getWrappedConnection();
assertValid(conn);
unmarkReusable();
return conn.receiveResponseHeader();
}
@Override
public void sendRequestEntity(final HttpEntityEnclosingRequest request)
throws HttpException, IOException {
final OperatedClientConnection conn = getWrappedConnection();
assertValid(conn);
unmarkReusable();
conn.sendRequestEntity(request);
}
@Override
public void (final HttpRequest request)
throws HttpException, IOException {
final OperatedClientConnection conn = getWrappedConnection();
assertValid(conn);
unmarkReusable();
conn.sendRequestHeader(request);
}
@Override
public InetAddress getLocalAddress() {
final OperatedClientConnection conn = getWrappedConnection();
assertValid(conn);
return conn.getLocalAddress();
}
@Override
public int getLocalPort() {
final OperatedClientConnection conn = getWrappedConnection();
assertValid(conn);
return conn.getLocalPort();
}
@Override
public InetAddress getRemoteAddress() {
final OperatedClientConnection conn = getWrappedConnection();
assertValid(conn);
return conn.getRemoteAddress();
}
@Override
public int getRemotePort() {
final OperatedClientConnection conn = getWrappedConnection();
assertValid(conn);
return conn.getRemotePort();
}
@Override
public boolean isSecure() {
final OperatedClientConnection conn = getWrappedConnection();
assertValid(conn);
return conn.isSecure();
}
@Override
public void bind(final Socket socket) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public Socket getSocket() {
final OperatedClientConnection conn = getWrappedConnection();
assertValid(conn);
if (!isOpen()) {
return null;
}
return conn.getSocket();
}
@Override
public SSLSession getSSLSession() {
final OperatedClientConnection conn = getWrappedConnection();
assertValid(conn);
if (!isOpen()) {
return null;
}
SSLSession result = null;
final Socket sock = conn.getSocket();
if (sock instanceof SSLSocket) {
result = ((SSLSocket)sock).getSession();
}
return result;
}
@Override
public void markReusable() {
markedReusable = true;
}
@Override
public void unmarkReusable() {
markedReusable = false;
}
@Override
public boolean isMarkedReusable() {
return markedReusable;
}
@Override
public void setIdleDuration(final long duration, final TimeUnit unit) {
if(duration > 0) {
this.duration = unit.toMillis(duration);
} else {
this.duration = -1;
}
}
@Override
public synchronized void releaseConnection() {
if (released) {
return;
}
released = true;
connManager.releaseConnection(this, duration, TimeUnit.MILLISECONDS);
}
@Override
public synchronized void abortConnection() {
if (released) {
return;
}
released = true;
unmarkReusable();
try {
shutdown();
} catch (final IOException ignore) {
}
connManager.releaseConnection(this, duration, TimeUnit.MILLISECONDS);
}
@Override
public Object getAttribute(final String id) {
final OperatedClientConnection conn = getWrappedConnection();
assertValid(conn);
if (conn instanceof HttpContext) {
return ((HttpContext) conn).getAttribute(id);
} else {
return null;
}
}
@Override
public Object removeAttribute(final String id) {
final OperatedClientConnection conn = getWrappedConnection();
assertValid(conn);
if (conn instanceof HttpContext) {
return ((HttpContext) conn).removeAttribute(id);
} else {
return null;
}
}
@Override
public void setAttribute(final String id, final Object obj) {
final OperatedClientConnection conn = getWrappedConnection();
assertValid(conn);
if (conn instanceof HttpContext) {
((HttpContext) conn).setAttribute(id, obj);
}
}
}