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