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