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 import java.io.InputStream;
32 import java.io.OutputStream;
33
34 import org.apache.http.ConnectionReuseStrategy;
35 import org.apache.http.HttpEntity;
36 import org.apache.http.HttpEntityEnclosingRequest;
37 import org.apache.http.HttpException;
38 import org.apache.http.HttpRequest;
39 import org.apache.http.HttpResponse;
40 import org.apache.http.HttpResponseFactory;
41 import org.apache.http.annotation.Immutable;
42 import org.apache.http.nio.ContentDecoder;
43 import org.apache.http.nio.ContentEncoder;
44 import org.apache.http.nio.NHttpServerConnection;
45 import org.apache.http.nio.NHttpServiceHandler;
46 import org.apache.http.nio.entity.BufferingNHttpEntity;
47 import org.apache.http.nio.entity.ConsumingNHttpEntity;
48 import org.apache.http.nio.util.ByteBufferAllocator;
49 import org.apache.http.nio.util.HeapByteBufferAllocator;
50 import org.apache.http.params.HttpParams;
51 import org.apache.http.protocol.HttpContext;
52 import org.apache.http.protocol.HttpExpectationVerifier;
53 import org.apache.http.protocol.HttpProcessor;
54 import org.apache.http.protocol.HttpRequestHandler;
55 import org.apache.http.protocol.HttpRequestHandlerResolver;
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 @Deprecated
78 @Immutable
79 public class BufferingHttpServiceHandler implements NHttpServiceHandler {
80
81 private final AsyncNHttpServiceHandler asyncHandler;
82
83 private HttpRequestHandlerResolver handlerResolver;
84
85 public BufferingHttpServiceHandler(
86 final HttpProcessor httpProcessor,
87 final HttpResponseFactory responseFactory,
88 final ConnectionReuseStrategy connStrategy,
89 final ByteBufferAllocator allocator,
90 final HttpParams params) {
91 super();
92 this.asyncHandler = new AsyncNHttpServiceHandler(
93 httpProcessor,
94 responseFactory,
95 connStrategy,
96 allocator,
97 params);
98 this.asyncHandler.setHandlerResolver(new RequestHandlerResolverAdaptor());
99 }
100
101 public BufferingHttpServiceHandler(
102 final HttpProcessor httpProcessor,
103 final HttpResponseFactory responseFactory,
104 final ConnectionReuseStrategy connStrategy,
105 final HttpParams params) {
106 this(httpProcessor, responseFactory, connStrategy,
107 HeapByteBufferAllocator.INSTANCE, params);
108 }
109
110 public void setEventListener(final EventListener eventListener) {
111 this.asyncHandler.setEventListener(eventListener);
112 }
113
114 public void setExpectationVerifier(final HttpExpectationVerifier expectationVerifier) {
115 this.asyncHandler.setExpectationVerifier(expectationVerifier);
116 }
117
118 public void setHandlerResolver(final HttpRequestHandlerResolver handlerResolver) {
119 this.handlerResolver = handlerResolver;
120 }
121
122 public void connected(final NHttpServerConnection conn) {
123 this.asyncHandler.connected(conn);
124 }
125
126 public void closed(final NHttpServerConnection conn) {
127 this.asyncHandler.closed(conn);
128 }
129
130 public void requestReceived(final NHttpServerConnection conn) {
131 this.asyncHandler.requestReceived(conn);
132 }
133
134 public void inputReady(final NHttpServerConnection conn, final ContentDecoder decoder) {
135 this.asyncHandler.inputReady(conn, decoder);
136 }
137
138 public void responseReady(final NHttpServerConnection conn) {
139 this.asyncHandler.responseReady(conn);
140 }
141
142 public void outputReady(final NHttpServerConnection conn, final ContentEncoder encoder) {
143 this.asyncHandler.outputReady(conn, encoder);
144 }
145
146 public void exception(final NHttpServerConnection conn, final HttpException httpex) {
147 this.asyncHandler.exception(conn, httpex);
148 }
149
150 public void exception(final NHttpServerConnection conn, final IOException ioex) {
151 this.asyncHandler.exception(conn, ioex);
152 }
153
154 public void timeout(final NHttpServerConnection conn) {
155 this.asyncHandler.timeout(conn);
156 }
157
158 class RequestHandlerResolverAdaptor implements NHttpRequestHandlerResolver {
159
160 public NHttpRequestHandler lookup(final String requestURI) {
161 final HttpRequestHandler handler = handlerResolver.lookup(requestURI);
162 if (handler != null) {
163 return new RequestHandlerAdaptor(handler);
164 } else {
165 return null;
166 }
167 }
168
169 }
170
171 static class RequestHandlerAdaptor extends SimpleNHttpRequestHandler {
172
173 private final HttpRequestHandler requestHandler;
174
175 public RequestHandlerAdaptor(final HttpRequestHandler requestHandler) {
176 super();
177 this.requestHandler = requestHandler;
178 }
179
180 public ConsumingNHttpEntity entityRequest(
181 final HttpEntityEnclosingRequest request,
182 final HttpContext context) throws HttpException, IOException {
183 return new BufferingNHttpEntity(
184 request.getEntity(),
185 HeapByteBufferAllocator.INSTANCE);
186 }
187
188 @Override
189 public void handle(
190 final HttpRequest request,
191 final HttpResponse response,
192 final HttpContext context) throws HttpException, IOException {
193 this.requestHandler.handle(request, response, context);
194 }
195
196 }
197
198 }