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     public void connected(final NHttpServerConnection conn) {
121         this.asyncHandler.connected(conn);
122     }
123 
124     public void closed(final NHttpServerConnection conn) {
125         this.asyncHandler.closed(conn);
126     }
127 
128     public void requestReceived(final NHttpServerConnection conn) {
129         this.asyncHandler.requestReceived(conn);
130     }
131 
132     public void inputReady(final NHttpServerConnection conn, final ContentDecoder decoder) {
133         this.asyncHandler.inputReady(conn, decoder);
134     }
135 
136     public void responseReady(final NHttpServerConnection conn) {
137         this.asyncHandler.responseReady(conn);
138     }
139 
140     public void outputReady(final NHttpServerConnection conn, final ContentEncoder encoder) {
141         this.asyncHandler.outputReady(conn, encoder);
142     }
143 
144     public void exception(final NHttpServerConnection conn, final HttpException httpex) {
145         this.asyncHandler.exception(conn, httpex);
146     }
147 
148     public void exception(final NHttpServerConnection conn, final IOException ioex) {
149         this.asyncHandler.exception(conn, ioex);
150     }
151 
152     public void timeout(final NHttpServerConnection conn) {
153         this.asyncHandler.timeout(conn);
154     }
155 
156     class RequestHandlerResolverAdaptor implements NHttpRequestHandlerResolver {
157 
158         public NHttpRequestHandler lookup(final String requestURI) {
159             final HttpRequestHandler handler = handlerResolver.lookup(requestURI);
160             if (handler != null) {
161                 return new RequestHandlerAdaptor(handler);
162             } else {
163                 return null;
164             }
165         }
166 
167     }
168 
169     static class RequestHandlerAdaptor extends SimpleNHttpRequestHandler {
170 
171         private final HttpRequestHandler requestHandler;
172 
173         public RequestHandlerAdaptor(final HttpRequestHandler requestHandler) {
174             super();
175             this.requestHandler = requestHandler;
176         }
177 
178         public ConsumingNHttpEntity entityRequest(
179                 final HttpEntityEnclosingRequest request,
180                 final HttpContext context) throws HttpException, IOException {
181             return new BufferingNHttpEntity(
182                     request.getEntity(),
183                     HeapByteBufferAllocator.INSTANCE);
184         }
185 
186         @Override
187         public void handle(
188                 final HttpRequest request,
189                 final HttpResponse response,
190                 final HttpContext context) throws HttpException, IOException {
191             this.requestHandler.handle(request, response, context);
192         }
193 
194     }
195 
196 }