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     public void connected(final NHttpClientConnection conn, final Object attachment) {
104         this.asyncHandler.connected(conn, attachment);
105     }
106 
107     public void closed(final NHttpClientConnection conn) {
108         this.asyncHandler.closed(conn);
109     }
110 
111     public void requestReady(final NHttpClientConnection conn) {
112         this.asyncHandler.requestReady(conn);
113     }
114 
115     public void inputReady(final NHttpClientConnection conn, final ContentDecoder decoder) {
116         this.asyncHandler.inputReady(conn, decoder);
117     }
118 
119     public void outputReady(final NHttpClientConnection conn, final ContentEncoder encoder) {
120         this.asyncHandler.outputReady(conn, encoder);
121     }
122 
123     public void responseReceived(final NHttpClientConnection conn) {
124         this.asyncHandler.responseReceived(conn);
125     }
126 
127     public void exception(final NHttpClientConnection conn, final HttpException httpex) {
128         this.asyncHandler.exception(conn, httpex);
129     }
130 
131     public void exception(final NHttpClientConnection conn, final IOException ioex) {
132         this.asyncHandler.exception(conn, ioex);
133     }
134 
135     public void timeout(final NHttpClientConnection conn) {
136         this.asyncHandler.timeout(conn);
137     }
138 
139     static class ExecutionHandlerAdaptor implements NHttpRequestExecutionHandler {
140 
141         private final HttpRequestExecutionHandler execHandler;
142 
143         public ExecutionHandlerAdaptor(final HttpRequestExecutionHandler execHandler) {
144             super();
145             this.execHandler = execHandler;
146         }
147 
148         public void initalizeContext(final HttpContext context, final Object attachment) {
149             this.execHandler.initalizeContext(context, attachment);
150         }
151         public void finalizeContext(final HttpContext context) {
152             this.execHandler.finalizeContext(context);
153         }
154 
155         public HttpRequest submitRequest(final HttpContext context) {
156             return this.execHandler.submitRequest(context);
157         }
158 
159         public ConsumingNHttpEntity responseEntity(
160                 final HttpResponse response,
161                 final HttpContext context) throws IOException {
162             return new BufferingNHttpEntity(
163                     response.getEntity(),
164                     HeapByteBufferAllocator.INSTANCE);
165         }
166 
167         public void handleResponse(
168                 final HttpResponse response,
169                 final HttpContext context) throws IOException {
170             this.execHandler.handleResponse(response, context);
171         }
172 
173     }
174 
175 }