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