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