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  package org.apache.http.impl.nio;
28  
29  import org.apache.http.HttpResponseFactory;
30  import org.apache.http.annotation.Immutable;
31  import org.apache.http.impl.DefaultHttpResponseFactory;
32  import org.apache.http.nio.NHttpClientConnection;
33  import org.apache.http.nio.NHttpConnectionFactory;
34  import org.apache.http.nio.reactor.IOSession;
35  import org.apache.http.nio.util.ByteBufferAllocator;
36  import org.apache.http.nio.util.HeapByteBufferAllocator;
37  import org.apache.http.params.HttpConnectionParams;
38  import org.apache.http.params.HttpParams;
39  
40  /**
41   * Factory for plain (non-encrypted), non-blocking {@link NHttpClientConnection}s.
42   * <p>
43   * The following parameters can be used to customize the behavior of this
44   * class:
45   * <ul>
46   *  <li>{@link org.apache.http.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li>
47   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SO_TIMEOUT}</li>
48   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SOCKET_BUFFER_SIZE}</li>
49   *  <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_HEADER_COUNT}</li>
50   *  <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li>
51   * </ul>
52   *
53   * @since 4.2
54   */
55  @Immutable
56  public class DefaultNHttpClientConnectionFactory
57      implements NHttpConnectionFactory<DefaultNHttpClientConnection> {
58  
59      private final HttpResponseFactory responseFactory;
60      private final ByteBufferAllocator allocator;
61      private final HttpParams params;
62  
63      public DefaultNHttpClientConnectionFactory(
64              final HttpResponseFactory responseFactory,
65              final ByteBufferAllocator allocator,
66              final HttpParams params) {
67          super();
68          if (responseFactory == null) {
69              throw new IllegalArgumentException("HTTP response factory may not be null");
70          }
71          if (allocator == null) {
72              throw new IllegalArgumentException("Byte buffer allocator may not be null");
73          }
74          if (params == null) {
75              throw new IllegalArgumentException("HTTP parameters may not be null");
76          }
77          this.responseFactory = responseFactory;
78          this.allocator = allocator;
79          this.params = params;
80      }
81  
82      public DefaultNHttpClientConnectionFactory(final HttpParams params) {
83          this(new DefaultHttpResponseFactory(), new HeapByteBufferAllocator(), params);
84      }
85  
86      protected DefaultNHttpClientConnection createConnection(
87              final IOSession session,
88              final HttpResponseFactory responseFactory,
89              final ByteBufferAllocator allocator,
90              final HttpParams params) {
91          return new DefaultNHttpClientConnection(session, responseFactory, allocator, params);
92      }
93  
94      public DefaultNHttpClientConnection createConnection(final IOSession session) {
95          DefaultNHttpClientConnection conn = createConnection(session, this.responseFactory, this.allocator, this.params);
96          int timeout = HttpConnectionParams.getSoTimeout(this.params);
97          conn.setSocketTimeout(timeout);
98          return conn;
99      }
100 
101 }