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