package org.apache.http.nio.protocol;
import java.io.IOException;
import org.apache.http.ConnectionReuseStrategy;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.nio.NHttpConnection;
import org.apache.http.nio.util.ByteBufferAllocator;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpProcessor;
import org.apache.http.util.Args;
@Deprecated
public abstract class NHttpHandlerBase {
protected static final String CONN_STATE = "http.nio.conn-state";
protected final HttpProcessor httpProcessor;
protected final ConnectionReuseStrategy connStrategy;
protected final ByteBufferAllocator allocator;
protected final HttpParams params;
protected EventListener eventListener;
public NHttpHandlerBase(
final HttpProcessor httpProcessor,
final ConnectionReuseStrategy connStrategy,
final ByteBufferAllocator allocator,
final HttpParams params) {
super();
Args.notNull(httpProcessor, "HTTP processor");
Args.notNull(connStrategy, "Connection reuse strategy");
Args.notNull(allocator, "ByteBuffer allocator");
Args.notNull(params, "HTTP parameters");
this.httpProcessor = httpProcessor;
this.connStrategy = connStrategy;
this.allocator = allocator;
this.params = params;
}
public HttpParams getParams() {
return this.params;
}
public void setEventListener(final EventListener eventListener) {
this.eventListener = eventListener;
}
protected void closeConnection(final NHttpConnection conn, final Throwable cause) {
try {
conn.close();
} catch (final IOException ex) {
try {
conn.shutdown();
} catch (final IOException ignore) {
}
}
}
protected void shutdownConnection(final NHttpConnection conn, final Throwable cause) {
try {
conn.shutdown();
} catch (final IOException ignore) {
}
}
protected void handleTimeout(final NHttpConnection conn) {
try {
if (conn.getStatus() == NHttpConnection.ACTIVE) {
conn.close();
if (conn.getStatus() == NHttpConnection.CLOSING) {
conn.setSocketTimeout(250);
}
if (this.eventListener != null) {
this.eventListener.connectionTimeout(conn);
}
} else {
conn.shutdown();
}
} catch (final IOException ignore) {
}
}
protected boolean canResponseHaveBody(
final HttpRequest request, final HttpResponse response) {
if (request != null && "HEAD".equalsIgnoreCase(request.getRequestLine().getMethod())) {
return false;
}
final int status = response.getStatusLine().getStatusCode();
return status >= HttpStatus.SC_OK
&& status != HttpStatus.SC_NO_CONTENT
&& status != HttpStatus.SC_NOT_MODIFIED
&& status != HttpStatus.SC_RESET_CONTENT;
}
}