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.HttpEntityEnclosingRequest;
34  import org.apache.http.HttpException;
35  import org.apache.http.HttpRequest;
36  import org.apache.http.HttpResponse;
37  import org.apache.http.HttpResponseFactory;
38  import org.apache.http.annotation.ThreadingBehavior;
39  import org.apache.http.annotation.Contract;
40  import org.apache.http.nio.ContentDecoder;
41  import org.apache.http.nio.ContentEncoder;
42  import org.apache.http.nio.NHttpServerConnection;
43  import org.apache.http.nio.NHttpServiceHandler;
44  import org.apache.http.nio.entity.BufferingNHttpEntity;
45  import org.apache.http.nio.entity.ConsumingNHttpEntity;
46  import org.apache.http.nio.util.ByteBufferAllocator;
47  import org.apache.http.nio.util.HeapByteBufferAllocator;
48  import org.apache.http.params.HttpParams;
49  import org.apache.http.protocol.HttpContext;
50  import org.apache.http.protocol.HttpExpectationVerifier;
51  import org.apache.http.protocol.HttpProcessor;
52  import org.apache.http.protocol.HttpRequestHandler;
53  import org.apache.http.protocol.HttpRequestHandlerResolver;
54  
55  /**
56   * Service protocol handler implementations that provide compatibility with
57   * the blocking I/O by storing the full content of HTTP messages in memory.
58   * The {@link HttpRequestHandler#handle(HttpRequest, HttpResponse, HttpContext)}
59   * method will fire only when the entire message content has been read into
60   * an in-memory buffer. Please note that request processing take place the
61   * main I/O thread and therefore individual HTTP request handlers should not
62   * block indefinitely.
63   * <p>
64   * When using this protocol handler {@link org.apache.http.HttpEntity}'s content
65   * can be generated / consumed using standard {@link java.io.InputStream}/
66   * {@link java.io.OutputStream} classes.
67   * <p>
68   * IMPORTANT: This protocol handler should be used only when dealing with HTTP
69   * messages that are known to be limited in length.
70   *
71   * @since 4.0
72   *
73   * @deprecated (4.2) use {@link HttpAsyncService}
74   */
75  @Deprecated
76  @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
77  public class BufferingHttpServiceHandler implements NHttpServiceHandler {
78  
79      private final AsyncNHttpServiceHandler asyncHandler;
80  
81      private HttpRequestHandlerResolver handlerResolver;
82  
83      public BufferingHttpServiceHandler(
84              final HttpProcessor httpProcessor,
85              final HttpResponseFactory responseFactory,
86              final ConnectionReuseStrategy connStrategy,
87              final ByteBufferAllocator allocator,
88              final HttpParams params) {
89          super();
90          this.asyncHandler = new AsyncNHttpServiceHandler(
91                  httpProcessor,
92                  responseFactory,
93                  connStrategy,
94                  allocator,
95                  params);
96          this.asyncHandler.setHandlerResolver(new RequestHandlerResolverAdaptor());
97      }
98  
99      public BufferingHttpServiceHandler(
100             final HttpProcessor httpProcessor,
101             final HttpResponseFactory responseFactory,
102             final ConnectionReuseStrategy connStrategy,
103             final HttpParams params) {
104         this(httpProcessor, responseFactory, connStrategy,
105                 HeapByteBufferAllocator.INSTANCE, params);
106     }
107 
108     public void setEventListener(final EventListener eventListener) {
109         this.asyncHandler.setEventListener(eventListener);
110     }
111 
112     public void setExpectationVerifier(final HttpExpectationVerifier expectationVerifier) {
113         this.asyncHandler.setExpectationVerifier(expectationVerifier);
114     }
115 
116     public void setHandlerResolver(final HttpRequestHandlerResolver handlerResolver) {
117         this.handlerResolver = handlerResolver;
118     }
119 
120     @Override
121     public void connected(final NHttpServerConnection conn) {
122         this.asyncHandler.connected(conn);
123     }
124 
125     @Override
126     public void closed(final NHttpServerConnection conn) {
127         this.asyncHandler.closed(conn);
128     }
129 
130     @Override
131     public void requestReceived(final NHttpServerConnection conn) {
132         this.asyncHandler.requestReceived(conn);
133     }
134 
135     @Override
136     public void inputReady(final NHttpServerConnection conn, final ContentDecoder decoder) {
137         this.asyncHandler.inputReady(conn, decoder);
138     }
139 
140     @Override
141     public void responseReady(final NHttpServerConnection conn) {
142         this.asyncHandler.responseReady(conn);
143     }
144 
145     @Override
146     public void outputReady(final NHttpServerConnection conn, final ContentEncoder encoder) {
147         this.asyncHandler.outputReady(conn, encoder);
148     }
149 
150     @Override
151     public void exception(final NHttpServerConnection conn, final HttpException httpex) {
152         this.asyncHandler.exception(conn, httpex);
153     }
154 
155     @Override
156     public void exception(final NHttpServerConnection conn, final IOException ioex) {
157         this.asyncHandler.exception(conn, ioex);
158     }
159 
160     @Override
161     public void timeout(final NHttpServerConnection conn) {
162         this.asyncHandler.timeout(conn);
163     }
164 
165     class RequestHandlerResolverAdaptor implements NHttpRequestHandlerResolver {
166 
167         @Override
168         public NHttpRequestHandler lookup(final String requestURI) {
169             final HttpRequestHandler handler = handlerResolver.lookup(requestURI);
170             if (handler != null) {
171                 return new RequestHandlerAdaptor(handler);
172             } else {
173                 return null;
174             }
175         }
176 
177     }
178 
179     static class RequestHandlerAdaptor extends SimpleNHttpRequestHandler {
180 
181         private final HttpRequestHandler requestHandler;
182 
183         public RequestHandlerAdaptor(final HttpRequestHandler requestHandler) {
184             super();
185             this.requestHandler = requestHandler;
186         }
187 
188         @Override
189         public ConsumingNHttpEntity entityRequest(
190                 final HttpEntityEnclosingRequest request,
191                 final HttpContext context) throws HttpException, IOException {
192             return new BufferingNHttpEntity(
193                     request.getEntity(),
194                     HeapByteBufferAllocator.INSTANCE);
195         }
196 
197         @Override
198         public void handle(
199                 final HttpRequest request,
200                 final HttpResponse response,
201                 final HttpContext context) throws HttpException, IOException {
202             this.requestHandler.handle(request, response, context);
203         }
204 
205     }
206 
207 }