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