001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-nio/src/main/java/org/apache/http/nio/protocol/NHttpHandlerBase.java $
003: * $Revision: 580738 $
004: * $Date: 2007-09-30 17:32:01 +0200 (Sun, 30 Sep 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.nio.protocol;
033:
034: import java.io.IOException;
035:
036: import org.apache.http.ConnectionReuseStrategy;
037: import org.apache.http.HttpConnection;
038: import org.apache.http.HttpRequest;
039: import org.apache.http.HttpResponse;
040: import org.apache.http.HttpStatus;
041: import org.apache.http.nio.NHttpConnection;
042: import org.apache.http.nio.util.ByteBufferAllocator;
043: import org.apache.http.params.HttpParams;
044: import org.apache.http.protocol.HttpProcessor;
045:
046: public abstract class NHttpHandlerBase {
047:
048: protected static final String CONN_STATE = "http.nio.conn-state";
049:
050: protected final HttpProcessor httpProcessor;
051: protected final ConnectionReuseStrategy connStrategy;
052: protected final ByteBufferAllocator allocator;
053: protected final HttpParams params;
054:
055: protected EventListener eventListener;
056:
057: public NHttpHandlerBase(final HttpProcessor httpProcessor,
058: final ConnectionReuseStrategy connStrategy,
059: final ByteBufferAllocator allocator, final HttpParams params) {
060: super ();
061: if (httpProcessor == null) {
062: throw new IllegalArgumentException(
063: "HTTP processor may not be null.");
064: }
065: if (connStrategy == null) {
066: throw new IllegalArgumentException(
067: "Connection reuse strategy may not be null");
068: }
069: if (allocator == null) {
070: throw new IllegalArgumentException(
071: "ByteBuffer allocator may not be null");
072: }
073: if (params == null) {
074: throw new IllegalArgumentException(
075: "HTTP parameters may not be null");
076: }
077: this .httpProcessor = httpProcessor;
078: this .connStrategy = connStrategy;
079: this .allocator = allocator;
080: this .params = params;
081: }
082:
083: public HttpParams getParams() {
084: return this .params;
085: }
086:
087: public void setEventListener(final EventListener eventListener) {
088: this .eventListener = eventListener;
089: }
090:
091: protected void closeConnection(final HttpConnection conn,
092: final Throwable cause) {
093: try {
094: // Try to close it nicely
095: conn.close();
096: } catch (IOException ex) {
097: try {
098: // Just shut the damn thing down
099: conn.shutdown();
100: } catch (IOException ignore) {
101: }
102: }
103: }
104:
105: protected void shutdownConnection(final HttpConnection conn,
106: final Throwable cause) {
107: try {
108: conn.shutdown();
109: } catch (IOException ignore) {
110: }
111: }
112:
113: protected void handleTimeout(final NHttpConnection conn) {
114: try {
115: if (conn.getStatus() == NHttpConnection.ACTIVE) {
116: conn.close();
117: if (conn.getStatus() == NHttpConnection.CLOSING) {
118: // Give the connection some grace time to
119: // close itself nicely
120: conn.setSocketTimeout(250);
121: }
122: if (this .eventListener != null) {
123: this .eventListener.connectionTimeout(conn);
124: }
125: } else {
126: conn.shutdown();
127: }
128: } catch (IOException ignore) {
129: }
130: }
131:
132: protected boolean canResponseHaveBody(final HttpRequest request,
133: final HttpResponse response) {
134:
135: if (request != null
136: && "HEAD".equalsIgnoreCase(request.getRequestLine()
137: .getMethod())) {
138: return false;
139: }
140:
141: int status = response.getStatusLine().getStatusCode();
142: return status >= HttpStatus.SC_OK
143: && status != HttpStatus.SC_NO_CONTENT
144: && status != HttpStatus.SC_NOT_MODIFIED
145: && status != HttpStatus.SC_RESET_CONTENT;
146: }
147:
148: }
|