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  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   * Service protocol handler implementations that provide compatibility with
59   * the blocking I/O by storing the full content of HTTP messages in memory.
60   * The {@link HttpRequestHandler#handle(HttpRequest, HttpResponse, HttpContext)}
61   * method will fire only when the entire message content has been read into
62   * an in-memory buffer. Please note that request processing take place the
63   * main I/O thread and therefore individual HTTP request handlers should not
64   * block indefinitely.
65   * <p>
66   * When using this protocol handler {@link HttpEntity}'s content can be
67   * generated / consumed using standard {@link InputStream}/{@link OutputStream}
68   * classes.
69   * <p>
70   * IMPORTANT: This protocol handler should be used only when dealing with HTTP
71   * messages that are known to be limited in length.
72   *
73   * @since 4.0
74   *
75   * @deprecated (4.2) use {@link HttpAsyncService}
76   */
77  @Deprecated
78  @Immutable // provided injected dependencies are 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                 new HeapByteBufferAllocator(), 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(NHttpServerConnection conn) {
155         this.asyncHandler.timeout(conn);
156     }
157 
158     class RequestHandlerResolverAdaptor implements NHttpRequestHandlerResolver {
159 
160         public NHttpRequestHandler lookup(final String requestURI) {
161             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                     new HeapByteBufferAllocator());
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 }