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.HttpRequest;
30  import org.apache.http.HttpResponse;
31  import org.apache.http.HttpResponseFactory;
32  import org.apache.http.annotation.ThreadingBehavior;
33  import org.apache.http.annotation.Contract;
34  import org.apache.http.config.ConnectionConfig;
35  import org.apache.http.entity.ContentLengthStrategy;
36  import org.apache.http.impl.ConnSupport;
37  import org.apache.http.impl.DefaultHttpResponseFactory;
38  import org.apache.http.impl.nio.codecs.DefaultHttpResponseParserFactory;
39  import org.apache.http.nio.NHttpConnectionFactory;
40  import org.apache.http.nio.NHttpMessageParserFactory;
41  import org.apache.http.nio.NHttpMessageWriterFactory;
42  import org.apache.http.nio.reactor.IOSession;
43  import org.apache.http.nio.util.ByteBufferAllocator;
44  import org.apache.http.nio.util.HeapByteBufferAllocator;
45  import org.apache.http.params.HttpParamConfig;
46  import org.apache.http.params.HttpParams;
47  import org.apache.http.util.Args;
48  
49  /**
50   * Default factory for plain (non-encrypted), non-blocking
51   * {@link org.apache.http.nio.NHttpClientConnection}s.
52   *
53   * @since 4.2
54   */
55  @SuppressWarnings("deprecation")
56  @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
57  public class DefaultNHttpClientConnectionFactory
58      implements NHttpConnectionFactory<DefaultNHttpClientConnection> {
59  
60      public static final DefaultNHttpClientConnectionFactorytory.html#DefaultNHttpClientConnectionFactory">DefaultNHttpClientConnectionFactory INSTANCE = new DefaultNHttpClientConnectionFactory();
61  
62      private final ContentLengthStrategy incomingContentStrategy;
63      private final ContentLengthStrategy outgoingContentStrategy;
64      private final NHttpMessageParserFactory<HttpResponse> responseParserFactory;
65      private final NHttpMessageWriterFactory<HttpRequest> requestWriterFactory;
66      private final ByteBufferAllocator allocator;
67      private final ConnectionConfig cconfig;
68  
69      /**
70       * @deprecated (4.3) use {@link
71       *   DefaultNHttpClientConnectionFactory#DefaultNHttpClientConnectionFactory(
72       *      NHttpMessageParserFactory, NHttpMessageWriterFactory, ByteBufferAllocator,
73       *      ConnectionConfig)}
74       */
75      @Deprecated
76      public DefaultNHttpClientConnectionFactory(
77              final HttpResponseFactory responseFactory,
78              final ByteBufferAllocator allocator,
79              final HttpParams params) {
80          super();
81          Args.notNull(responseFactory, "HTTP response factory");
82          Args.notNull(allocator, "Byte buffer allocator");
83          Args.notNull(params, "HTTP parameters");
84          this.allocator = allocator;
85          this.incomingContentStrategy = null;
86          this.outgoingContentStrategy = null;
87          this.responseParserFactory = new DefaultHttpResponseParserFactory(null, responseFactory);
88          this.requestWriterFactory = null;
89          this.cconfig = HttpParamConfig.getConnectionConfig(params);
90      }
91  
92      /**
93       * @deprecated (4.3) use {@link
94       *   DefaultNHttpClientConnectionFactory#DefaultNHttpClientConnectionFactory(
95       *     ConnectionConfig)}
96       */
97      @Deprecated
98      public DefaultNHttpClientConnectionFactory(final HttpParams params) {
99          this(DefaultHttpResponseFactory.INSTANCE, HeapByteBufferAllocator.INSTANCE, params);
100     }
101 
102     /**
103      * @since 4.3
104      */
105     public DefaultNHttpClientConnectionFactory(
106             final ContentLengthStrategy incomingContentStrategy,
107             final ContentLengthStrategy outgoingContentStrategy,
108             final NHttpMessageParserFactory<HttpResponse> responseParserFactory,
109             final NHttpMessageWriterFactory<HttpRequest> requestWriterFactory,
110             final ByteBufferAllocator allocator,
111             final ConnectionConfig cconfig) {
112         super();
113         this.incomingContentStrategy = incomingContentStrategy;
114         this.outgoingContentStrategy = outgoingContentStrategy;
115         this.responseParserFactory = responseParserFactory;
116         this.requestWriterFactory = requestWriterFactory;
117         this.allocator = allocator;
118         this.cconfig = cconfig != null ? cconfig : ConnectionConfig.DEFAULT;
119     }
120 
121     /**
122      * @since 4.3
123      */
124     public DefaultNHttpClientConnectionFactory(
125             final NHttpMessageParserFactory<HttpResponse> responseParserFactory,
126             final NHttpMessageWriterFactory<HttpRequest> requestWriterFactory,
127             final ByteBufferAllocator allocator,
128             final ConnectionConfig cconfig) {
129         this(null, null, responseParserFactory, requestWriterFactory, allocator, cconfig);
130     }
131 
132     /**
133      * @since 4.3
134      */
135     public DefaultNHttpClientConnectionFactory(
136             final NHttpMessageParserFactory<HttpResponse> responseParserFactory,
137             final NHttpMessageWriterFactory<HttpRequest> requestWriterFactory,
138             final ConnectionConfig cconfig) {
139         this(null, null, responseParserFactory, requestWriterFactory, null, cconfig);
140     }
141 
142     /**
143      * @since 4.3
144      */
145     public DefaultNHttpClientConnectionFactory(final ConnectionConfig cconfig) {
146         this(null, null, null, null, null, cconfig);
147     }
148 
149     /**
150      * @since 4.3
151      */
152     public DefaultNHttpClientConnectionFactory() {
153         this(null, null, null, null, null, null);
154     }
155 
156     /**
157      * @deprecated (4.3) no longer used.
158      */
159     @Deprecated
160     protected DefaultNHttpClientConnection createConnection(
161             final IOSession session,
162             final HttpResponseFactory responseFactory,
163             final ByteBufferAllocator allocator,
164             final HttpParams params) {
165         return new DefaultNHttpClientConnection(session, responseFactory, allocator, params);
166     }
167 
168     @Override
169     public DefaultNHttpClientConnection createConnection(final IOSession session) {
170         return new DefaultNHttpClientConnection(
171                 session,
172                 this.cconfig.getBufferSize(),
173                 this.cconfig.getFragmentSizeHint(),
174                 this.allocator,
175                 ConnSupport.createDecoder(this.cconfig),
176                 ConnSupport.createEncoder(this.cconfig),
177                 this.cconfig.getMessageConstraints(),
178                 this.incomingContentStrategy,
179                 this.outgoingContentStrategy,
180                 this.requestWriterFactory,
181                 this.responseParserFactory);
182     }
183 
184 }