View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.nio.protocol;
29  
30  import java.io.IOException;
31  
32  import org.apache.http.ConnectionReuseStrategy;
33  import org.apache.http.HttpRequest;
34  import org.apache.http.HttpResponse;
35  import org.apache.http.HttpStatus;
36  import org.apache.http.annotation.Immutable;
37  import org.apache.http.nio.NHttpConnection;
38  import org.apache.http.nio.util.ByteBufferAllocator;
39  import org.apache.http.params.HttpParams;
40  import org.apache.http.protocol.HttpProcessor;
41  import org.apache.http.util.Args;
42  
43  /**
44   * @since 4.0
45   *
46   * @deprecated (4.2) do not use
47   */
48  @Deprecated
49  @Immutable // provided injected dependencies are immutable
50  public abstract class NHttpHandlerBase {
51  
52      protected static final String CONN_STATE = "http.nio.conn-state";
53  
54      protected final HttpProcessor httpProcessor;
55      protected final ConnectionReuseStrategy connStrategy;
56      protected final ByteBufferAllocator allocator;
57      protected final HttpParams params;
58  
59      protected EventListener eventListener;
60  
61      public NHttpHandlerBase(
62              final HttpProcessor httpProcessor,
63              final ConnectionReuseStrategy connStrategy,
64              final ByteBufferAllocator allocator,
65              final HttpParams params) {
66          super();
67          Args.notNull(httpProcessor, "HTTP processor");
68          Args.notNull(connStrategy, "Connection reuse strategy");
69          Args.notNull(allocator, "ByteBuffer allocator");
70          Args.notNull(params, "HTTP parameters");
71          this.httpProcessor = httpProcessor;
72          this.connStrategy = connStrategy;
73          this.allocator = allocator;
74          this.params = params;
75      }
76  
77      public HttpParams getParams() {
78          return this.params;
79      }
80  
81      public void setEventListener(final EventListener eventListener) {
82          this.eventListener = eventListener;
83      }
84  
85      protected void closeConnection(final NHttpConnection conn, final Throwable cause) {
86          try {
87              // Try to close it nicely
88              conn.close();
89          } catch (final IOException ex) {
90              try {
91                  // Just shut the damn thing down
92                  conn.shutdown();
93              } catch (final IOException ignore) {
94              }
95          }
96      }
97  
98      protected void shutdownConnection(final NHttpConnection conn, final Throwable cause) {
99          try {
100             conn.shutdown();
101         } catch (final IOException ignore) {
102         }
103     }
104 
105     protected void handleTimeout(final NHttpConnection conn) {
106         try {
107             if (conn.getStatus() == NHttpConnection.ACTIVE) {
108                 conn.close();
109                 if (conn.getStatus() == NHttpConnection.CLOSING) {
110                     // Give the connection some grace time to
111                     // close itself nicely
112                     conn.setSocketTimeout(250);
113                 }
114                 if (this.eventListener != null) {
115                     this.eventListener.connectionTimeout(conn);
116                 }
117             } else {
118                 conn.shutdown();
119             }
120         } catch (final IOException ignore) {
121         }
122     }
123 
124     protected boolean canResponseHaveBody(
125             final HttpRequest request, final HttpResponse response) {
126 
127         if (request != null && "HEAD".equalsIgnoreCase(request.getRequestLine().getMethod())) {
128             return false;
129         }
130 
131         final int status = response.getStatusLine().getStatusCode();
132         return status >= HttpStatus.SC_OK
133             && status != HttpStatus.SC_NO_CONTENT
134             && status != HttpStatus.SC_NOT_MODIFIED
135             && status != HttpStatus.SC_RESET_CONTENT;
136     }
137 
138 }