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.impl;
29  
30  import java.io.IOException;
31  import java.io.OutputStream;
32  import java.net.Socket;
33  import java.net.SocketTimeoutException;
34  import java.nio.charset.CharsetDecoder;
35  import java.nio.charset.CharsetEncoder;
36  
37  import org.apache.http.HttpClientConnection;
38  import org.apache.http.HttpEntity;
39  import org.apache.http.HttpEntityEnclosingRequest;
40  import org.apache.http.HttpException;
41  import org.apache.http.HttpRequest;
42  import org.apache.http.HttpResponse;
43  import org.apache.http.HttpStatus;
44  import org.apache.http.annotation.NotThreadSafe;
45  import org.apache.http.config.MessageConstraints;
46  import org.apache.http.entity.ContentLengthStrategy;
47  import org.apache.http.impl.io.DefaultHttpRequestWriterFactory;
48  import org.apache.http.impl.io.DefaultHttpResponseParserFactory;
49  import org.apache.http.io.HttpMessageParser;
50  import org.apache.http.io.HttpMessageParserFactory;
51  import org.apache.http.io.HttpMessageWriter;
52  import org.apache.http.io.HttpMessageWriterFactory;
53  import org.apache.http.util.Args;
54  
55  /**
56   * Default implementation of {@link HttpClientConnection}.
57   *
58   * @since 4.3
59   */
60  @NotThreadSafe
61  public class DefaultBHttpClientConnection extends BHttpConnectionBase
62                                                     implements HttpClientConnection {
63  
64      private final HttpMessageParser<HttpResponse> responseParser;
65      private final HttpMessageWriter<HttpRequest> requestWriter;
66  
67      /**
68       * Creates new instance of DefaultBHttpClientConnection.
69       *
70       * @param buffersize buffer size. Must be a positive number.
71       * @param fragmentSizeHint fragment size hint.
72       * @param chardecoder decoder to be used for decoding HTTP protocol elements.
73       *   If <code>null</code> simple type cast will be used for byte to char conversion.
74       * @param charencoder encoder to be used for encoding HTTP protocol elements.
75       *   If <code>null</code> simple type cast will be used for char to byte conversion.
76       * @param constraints Message constraints. If <code>null</code>
77       *   {@link MessageConstraints#DEFAULT} will be used.
78       * @param incomingContentStrategy incoming content length strategy. If <code>null</code>
79       *   {@link org.apache.http.impl.entity.LaxContentLengthStrategy#INSTANCE} will be used.
80       * @param outgoingContentStrategy outgoing content length strategy. If <code>null</code>
81       *   {@link org.apache.http.impl.entity.StrictContentLengthStrategy#INSTANCE} will be used.
82       * @param requestWriterFactory request writer factory. If <code>null</code>
83       *   {@link DefaultHttpRequestWriterFactory#INSTANCE} will be used.
84       * @param responseParserFactory response parser factory. If <code>null</code>
85       *   {@link DefaultHttpResponseParserFactory#INSTANCE} will be used.
86       */
87      public DefaultBHttpClientConnection(
88              final int buffersize,
89              final int fragmentSizeHint,
90              final CharsetDecoder chardecoder,
91              final CharsetEncoder charencoder,
92              final MessageConstraints constraints,
93              final ContentLengthStrategy incomingContentStrategy,
94              final ContentLengthStrategy outgoingContentStrategy,
95              final HttpMessageWriterFactory<HttpRequest> requestWriterFactory,
96              final HttpMessageParserFactory<HttpResponse> responseParserFactory) {
97          super(buffersize, fragmentSizeHint, chardecoder, charencoder,
98                  constraints, incomingContentStrategy, outgoingContentStrategy);
99          this.requestWriter = (requestWriterFactory != null ? requestWriterFactory :
100             DefaultHttpRequestWriterFactory.INSTANCE).create(getSessionOutputBuffer());
101         this.responseParser = (responseParserFactory != null ? responseParserFactory :
102             DefaultHttpResponseParserFactory.INSTANCE).create(getSessionInputBuffer(), constraints);
103     }
104 
105     public DefaultBHttpClientConnection(
106             final int buffersize,
107             final CharsetDecoder chardecoder,
108             final CharsetEncoder charencoder,
109             final MessageConstraints constraints) {
110         this(buffersize, buffersize, chardecoder, charencoder, constraints, null, null, null, null);
111     }
112 
113     public DefaultBHttpClientConnection(final int buffersize) {
114         this(buffersize, buffersize, null, null, null, null, null, null, null);
115     }
116 
117     protected void onResponseReceived(final HttpResponse response) {
118     }
119 
120     protected void onRequestSubmitted(final HttpRequest request) {
121     }
122 
123     @Override
124     public void bind(final Socket socket) throws IOException {
125         super.bind(socket);
126     }
127 
128     @Override
129     public boolean isResponseAvailable(final int timeout) throws IOException {
130         ensureOpen();
131         try {
132             return awaitInput(timeout);
133         } catch (final SocketTimeoutException ex) {
134             return false;
135         }
136     }
137 
138     @Override
139     public void sendRequestHeader(final HttpRequest request)
140             throws HttpException, IOException {
141         Args.notNull(request, "HTTP request");
142         ensureOpen();
143         this.requestWriter.write(request);
144         onRequestSubmitted(request);
145         incrementRequestCount();
146     }
147 
148     @Override
149     public void sendRequestEntity(final HttpEntityEnclosingRequest request)
150             throws HttpException, IOException {
151         Args.notNull(request, "HTTP request");
152         ensureOpen();
153         final HttpEntity entity = request.getEntity();
154         if (entity == null) {
155             return;
156         }
157         final OutputStream outstream = prepareOutput(request);
158         entity.writeTo(outstream);
159         outstream.close();
160     }
161 
162     @Override
163     public HttpResponse receiveResponseHeader() throws HttpException, IOException {
164         ensureOpen();
165         final HttpResponse response = this.responseParser.parse();
166         onResponseReceived(response);
167         if (response.getStatusLine().getStatusCode() >= HttpStatus.SC_OK) {
168             incrementResponseCount();
169         }
170         return response;
171     }
172 
173     @Override
174     public void receiveResponseEntity(
175             final HttpResponse response) throws HttpException, IOException {
176         Args.notNull(response, "HTTP response");
177         ensureOpen();
178         final HttpEntity entity = prepareInput(response);
179         response.setEntity(entity);
180     }
181 
182     @Override
183     public void flush() throws IOException {
184         ensureOpen();
185         doFlush();
186     }
187 
188 }