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.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
44
45
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
86 conn.close();
87 } catch (final IOException ex) {
88 try {
89
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
109
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 }