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.HttpException;
34 import org.apache.http.HttpRequest;
35 import org.apache.http.HttpResponse;
36 import org.apache.http.annotation.ThreadingBehavior;
37 import org.apache.http.annotation.Contract;
38 import org.apache.http.nio.ContentDecoder;
39 import org.apache.http.nio.ContentEncoder;
40 import org.apache.http.nio.NHttpClientConnection;
41 import org.apache.http.nio.NHttpClientHandler;
42 import org.apache.http.nio.entity.BufferingNHttpEntity;
43 import org.apache.http.nio.entity.ConsumingNHttpEntity;
44 import org.apache.http.nio.util.ByteBufferAllocator;
45 import org.apache.http.nio.util.HeapByteBufferAllocator;
46 import org.apache.http.params.HttpParams;
47 import org.apache.http.protocol.HttpContext;
48 import org.apache.http.protocol.HttpProcessor;
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 @Deprecated
71 @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
72 public class BufferingHttpClientHandler implements NHttpClientHandler {
73
74 private final AsyncNHttpClientHandler asyncHandler;
75
76 public BufferingHttpClientHandler(
77 final HttpProcessor httpProcessor,
78 final HttpRequestExecutionHandler execHandler,
79 final ConnectionReuseStrategy connStrategy,
80 final ByteBufferAllocator allocator,
81 final HttpParams params) {
82 this.asyncHandler = new AsyncNHttpClientHandler(
83 httpProcessor,
84 new ExecutionHandlerAdaptor(execHandler),
85 connStrategy,
86 allocator,
87 params);
88 }
89
90 public BufferingHttpClientHandler(
91 final HttpProcessor httpProcessor,
92 final HttpRequestExecutionHandler execHandler,
93 final ConnectionReuseStrategy connStrategy,
94 final HttpParams params) {
95 this(httpProcessor, execHandler, connStrategy,
96 HeapByteBufferAllocator.INSTANCE, params);
97 }
98
99 public void setEventListener(final EventListener eventListener) {
100 this.asyncHandler.setEventListener(eventListener);
101 }
102
103 @Override
104 public void connected(final NHttpClientConnection conn, final Object attachment) {
105 this.asyncHandler.connected(conn, attachment);
106 }
107
108 @Override
109 public void closed(final NHttpClientConnection conn) {
110 this.asyncHandler.closed(conn);
111 }
112
113 @Override
114 public void requestReady(final NHttpClientConnection conn) {
115 this.asyncHandler.requestReady(conn);
116 }
117
118 @Override
119 public void inputReady(final NHttpClientConnection conn, final ContentDecoder decoder) {
120 this.asyncHandler.inputReady(conn, decoder);
121 }
122
123 @Override
124 public void outputReady(final NHttpClientConnection conn, final ContentEncoder encoder) {
125 this.asyncHandler.outputReady(conn, encoder);
126 }
127
128 @Override
129 public void responseReceived(final NHttpClientConnection conn) {
130 this.asyncHandler.responseReceived(conn);
131 }
132
133 @Override
134 public void exception(final NHttpClientConnection conn, final HttpException httpex) {
135 this.asyncHandler.exception(conn, httpex);
136 }
137
138 @Override
139 public void exception(final NHttpClientConnection conn, final IOException ioex) {
140 this.asyncHandler.exception(conn, ioex);
141 }
142
143 @Override
144 public void timeout(final NHttpClientConnection conn) {
145 this.asyncHandler.timeout(conn);
146 }
147
148 static class ExecutionHandlerAdaptor implements NHttpRequestExecutionHandler {
149
150 private final HttpRequestExecutionHandler execHandler;
151
152 public ExecutionHandlerAdaptor(final HttpRequestExecutionHandler execHandler) {
153 super();
154 this.execHandler = execHandler;
155 }
156
157 @Override
158 public void initalizeContext(final HttpContext context, final Object attachment) {
159 this.execHandler.initalizeContext(context, attachment);
160 }
161 @Override
162 public void finalizeContext(final HttpContext context) {
163 this.execHandler.finalizeContext(context);
164 }
165
166 @Override
167 public HttpRequest submitRequest(final HttpContext context) {
168 return this.execHandler.submitRequest(context);
169 }
170
171 @Override
172 public ConsumingNHttpEntity responseEntity(
173 final HttpResponse response,
174 final HttpContext context) throws IOException {
175 return new BufferingNHttpEntity(
176 response.getEntity(),
177 HeapByteBufferAllocator.INSTANCE);
178 }
179
180 @Override
181 public void handleResponse(
182 final HttpResponse response,
183 final HttpContext context) throws IOException {
184 this.execHandler.handleResponse(response, context);
185 }
186
187 }
188
189 }