1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
45
46
47
48 @Deprecated
49 @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
88 conn.close();
89 } catch (final IOException ex) {
90 try {
91
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
111
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 }