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.HttpException;
34  import org.apache.http.HttpRequest;
35  import org.apache.http.HttpResponse;
36  import org.apache.http.annotation.ThreadingBehavior;
37  import org.apache.http.annotation.Contract;
38  import org.apache.http.nio.ContentDecoder;
39  import org.apache.http.nio.ContentEncoder;
40  import org.apache.http.nio.NHttpClientConnection;
41  import org.apache.http.nio.NHttpClientHandler;
42  import org.apache.http.nio.entity.BufferingNHttpEntity;
43  import org.apache.http.nio.entity.ConsumingNHttpEntity;
44  import org.apache.http.nio.util.ByteBufferAllocator;
45  import org.apache.http.nio.util.HeapByteBufferAllocator;
46  import org.apache.http.params.HttpParams;
47  import org.apache.http.protocol.HttpContext;
48  import org.apache.http.protocol.HttpProcessor;
49  
50  /**
51   * Client protocol handler implementation that provides compatibility with the
52   * blocking I/O by storing the full content of HTTP messages in memory.
53   * The {@link HttpRequestExecutionHandler#handleResponse(HttpResponse, HttpContext)}
54   * method will fire only when the entire message content has been read into a
55   * in-memory buffer. Please note that request execution / response processing
56   * take place the main I/O thread and therefore
57   * {@link HttpRequestExecutionHandler} methods should not block indefinitely.
58   * <p>
59   * When using this protocol handler {@link org.apache.http.HttpEntity}'s content
60   * can be generated / consumed using standard {@link java.io.InputStream}/
61   * {@link java.io.OutputStream} classes.
62   * <p>
63   * IMPORTANT: This protocol handler should be used only when dealing with HTTP
64   * messages that are known to be limited in length.
65   *
66   * @since 4.0
67   *
68   * @deprecated (4.2) use {@link HttpAsyncRequestExecutor} and {@link HttpAsyncRequester}
69   */
70  @Deprecated
71  @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
72  public class BufferingHttpClientHandler implements NHttpClientHandler {
73  
74      private final AsyncNHttpClientHandler asyncHandler;
75  
76      public BufferingHttpClientHandler(
77              final HttpProcessor httpProcessor,
78              final HttpRequestExecutionHandler execHandler,
79              final ConnectionReuseStrategy connStrategy,
80              final ByteBufferAllocator allocator,
81              final HttpParams params) {
82          this.asyncHandler = new AsyncNHttpClientHandler(
83                  httpProcessor,
84                  new ExecutionHandlerAdaptor(execHandler),
85                  connStrategy,
86                  allocator,
87                  params);
88      }
89  
90      public BufferingHttpClientHandler(
91              final HttpProcessor httpProcessor,
92              final HttpRequestExecutionHandler execHandler,
93              final ConnectionReuseStrategy connStrategy,
94              final HttpParams params) {
95          this(httpProcessor, execHandler, connStrategy,
96                  HeapByteBufferAllocator.INSTANCE, params);
97      }
98  
99      public void setEventListener(final EventListener eventListener) {
100         this.asyncHandler.setEventListener(eventListener);
101     }
102 
103     @Override
104     public void connected(final NHttpClientConnection conn, final Object attachment) {
105         this.asyncHandler.connected(conn, attachment);
106     }
107 
108     @Override
109     public void closed(final NHttpClientConnection conn) {
110         this.asyncHandler.closed(conn);
111     }
112 
113     @Override
114     public void requestReady(final NHttpClientConnection conn) {
115         this.asyncHandler.requestReady(conn);
116     }
117 
118     @Override
119     public void inputReady(final NHttpClientConnection conn, final ContentDecoder decoder) {
120         this.asyncHandler.inputReady(conn, decoder);
121     }
122 
123     @Override
124     public void outputReady(final NHttpClientConnection conn, final ContentEncoder encoder) {
125         this.asyncHandler.outputReady(conn, encoder);
126     }
127 
128     @Override
129     public void responseReceived(final NHttpClientConnection conn) {
130         this.asyncHandler.responseReceived(conn);
131     }
132 
133     @Override
134     public void exception(final NHttpClientConnection conn, final HttpException httpex) {
135         this.asyncHandler.exception(conn, httpex);
136     }
137 
138     @Override
139     public void exception(final NHttpClientConnection conn, final IOException ioex) {
140         this.asyncHandler.exception(conn, ioex);
141     }
142 
143     @Override
144     public void timeout(final NHttpClientConnection conn) {
145         this.asyncHandler.timeout(conn);
146     }
147 
148     static class ExecutionHandlerAdaptor implements NHttpRequestExecutionHandler {
149 
150         private final HttpRequestExecutionHandler execHandler;
151 
152         public ExecutionHandlerAdaptor(final HttpRequestExecutionHandler execHandler) {
153             super();
154             this.execHandler = execHandler;
155         }
156 
157         @Override
158         public void initalizeContext(final HttpContext context, final Object attachment) {
159             this.execHandler.initalizeContext(context, attachment);
160         }
161         @Override
162         public void finalizeContext(final HttpContext context) {
163             this.execHandler.finalizeContext(context);
164         }
165 
166         @Override
167         public HttpRequest submitRequest(final HttpContext context) {
168             return this.execHandler.submitRequest(context);
169         }
170 
171         @Override
172         public ConsumingNHttpEntity responseEntity(
173                 final HttpResponse response,
174                 final HttpContext context) throws IOException {
175             return new BufferingNHttpEntity(
176                     response.getEntity(),
177                     HeapByteBufferAllocator.INSTANCE);
178         }
179 
180         @Override
181         public void handleResponse(
182                 final HttpResponse response,
183                 final HttpContext context) throws IOException {
184             this.execHandler.handleResponse(response, context);
185         }
186 
187     }
188 
189 }