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.nio.NHttpConnection;
37  import org.apache.http.nio.util.ByteBufferAllocator;
38  import org.apache.http.params.HttpParams;
39  import org.apache.http.protocol.HttpProcessor;
40  import org.apache.http.util.Args;
41  
42  /**
43   * @since 4.0
44   *
45   * @deprecated (4.2) do not use
46   */
47  @Deprecated
48  public abstract class NHttpHandlerBase {
49  
50      protected static final String CONN_STATE = "http.nio.conn-state";
51  
52      protected final HttpProcessor httpProcessor;
53      protected final ConnectionReuseStrategy connStrategy;
54      protected final ByteBufferAllocator allocator;
55      protected final HttpParams params;
56  
57      protected EventListener eventListener;
58  
59      public NHttpHandlerBase(
60              final HttpProcessor httpProcessor,
61              final ConnectionReuseStrategy connStrategy,
62              final ByteBufferAllocator allocator,
63              final HttpParams params) {
64          super();
65          Args.notNull(httpProcessor, "HTTP processor");
66          Args.notNull(connStrategy, "Connection reuse strategy");
67          Args.notNull(allocator, "ByteBuffer allocator");
68          Args.notNull(params, "HTTP parameters");
69          this.httpProcessor = httpProcessor;
70          this.connStrategy = connStrategy;
71          this.allocator = allocator;
72          this.params = params;
73      }
74  
75      public HttpParams getParams() {
76          return this.params;
77      }
78  
79      public void setEventListener(final EventListener eventListener) {
80          this.eventListener = eventListener;
81      }
82  
83      protected void closeConnection(final NHttpConnection conn, final Throwable cause) {
84          try {
85              // Try to close it nicely
86              conn.close();
87          } catch (final IOException ex) {
88              try {
89                  // Just shut the damn thing down
90                  conn.shutdown();
91              } catch (final IOException ignore) {
92              }
93          }
94      }
95  
96      protected void shutdownConnection(final NHttpConnection conn, final Throwable cause) {
97          try {
98              conn.shutdown();
99          } catch (final IOException ignore) {
100         }
101     }
102 
103     protected void handleTimeout(final NHttpConnection conn) {
104         try {
105             if (conn.getStatus() == NHttpConnection.ACTIVE) {
106                 conn.close();
107                 if (conn.getStatus() == NHttpConnection.CLOSING) {
108                     // Give the connection some grace time to
109                     // close itself nicely
110                     conn.setSocketTimeout(250);
111                 }
112                 if (this.eventListener != null) {
113                     this.eventListener.connectionTimeout(conn);
114                 }
115             } else {
116                 conn.shutdown();
117             }
118         } catch (final IOException ignore) {
119         }
120     }
121 
122     protected boolean canResponseHaveBody(
123             final HttpRequest request, final HttpResponse response) {
124 
125         if (request != null && "HEAD".equalsIgnoreCase(request.getRequestLine().getMethod())) {
126             return false;
127         }
128 
129         final int status = response.getStatusLine().getStatusCode();
130         return status >= HttpStatus.SC_OK
131             && status != HttpStatus.SC_NO_CONTENT
132             && status != HttpStatus.SC_NOT_MODIFIED
133             && status != HttpStatus.SC_RESET_CONTENT;
134     }
135 
136 }