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.HttpRequestFactory;
31  import org.apache.http.HttpResponse;
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.DefaultHttpRequestFactory;
38  import org.apache.http.impl.nio.codecs.DefaultHttpRequestParserFactory;
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.NHttpServerConnection}s.
52   *
53   * @since 4.2
54   */
55  @SuppressWarnings("deprecation")
56  @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
57  public class DefaultNHttpServerConnectionFactory
58      implements NHttpConnectionFactory<DefaultNHttpServerConnection> {
59  
60      private final ContentLengthStrategy incomingContentStrategy;
61      private final ContentLengthStrategy outgoingContentStrategy;
62      private final NHttpMessageParserFactory<HttpRequest> requestParserFactory;
63      private final NHttpMessageWriterFactory<HttpResponse> responseWriterFactory;
64      private final ByteBufferAllocator allocator;
65      private final ConnectionConfig cconfig;
66  
67      /**
68       * @deprecated (4.3) use {@link
69       *   DefaultNHttpServerConnectionFactory#DefaultNHttpServerConnectionFactory(
70       *      ByteBufferAllocator, NHttpMessageParserFactory, NHttpMessageWriterFactory,
71       *      ConnectionConfig)}
72       */
73      @Deprecated
74      public DefaultNHttpServerConnectionFactory(
75              final HttpRequestFactory requestFactory,
76              final ByteBufferAllocator allocator,
77              final HttpParams params) {
78          super();
79          Args.notNull(requestFactory, "HTTP request factory");
80          Args.notNull(allocator, "Byte buffer allocator");
81          Args.notNull(params, "HTTP parameters");
82          this.incomingContentStrategy = null;
83          this.outgoingContentStrategy = null;
84          this.requestParserFactory = new DefaultHttpRequestParserFactory(null, requestFactory);
85          this.responseWriterFactory = null;
86          this.allocator = allocator;
87          this.cconfig = HttpParamConfig.getConnectionConfig(params);
88      }
89  
90      /**
91       * @deprecated (4.3) use {@link
92       *   DefaultNHttpServerConnectionFactory#DefaultNHttpServerConnectionFactory(ConnectionConfig)}
93       */
94      @Deprecated
95      public DefaultNHttpServerConnectionFactory(final HttpParams params) {
96          this(DefaultHttpRequestFactory.INSTANCE, HeapByteBufferAllocator.INSTANCE, params);
97      }
98  
99      /**
100      * @deprecated (4.3) no longer used.
101      */
102     @Deprecated
103     protected DefaultNHttpServerConnection createConnection(
104             final IOSession session,
105             final HttpRequestFactory requestFactory,
106             final ByteBufferAllocator allocator,
107             final HttpParams params) {
108         return new DefaultNHttpServerConnection(session, requestFactory, allocator, params);
109     }
110 
111     /**
112      * @since 4.3
113      */
114     public DefaultNHttpServerConnectionFactory(
115             final ContentLengthStrategy incomingContentStrategy,
116             final ContentLengthStrategy outgoingContentStrategy,
117             final NHttpMessageParserFactory<HttpRequest> requestParserFactory,
118             final NHttpMessageWriterFactory<HttpResponse> responseWriterFactory,
119             final ByteBufferAllocator allocator,
120             final ConnectionConfig cconfig) {
121         super();
122         this.incomingContentStrategy = incomingContentStrategy;
123         this.outgoingContentStrategy = outgoingContentStrategy;
124         this.requestParserFactory = requestParserFactory;
125         this.responseWriterFactory = responseWriterFactory;
126         this.allocator = allocator;
127         this.cconfig = cconfig != null ? cconfig : ConnectionConfig.DEFAULT;
128     }
129 
130     /**
131      * @since 4.3
132      */
133     public DefaultNHttpServerConnectionFactory(
134             final ByteBufferAllocator allocator,
135             final NHttpMessageParserFactory<HttpRequest> requestParserFactory,
136             final NHttpMessageWriterFactory<HttpResponse> responseWriterFactory,
137             final ConnectionConfig cconfig) {
138         this(null, null, requestParserFactory, responseWriterFactory, allocator, cconfig);
139     }
140 
141     /**
142      * @since 4.3
143      */
144     public DefaultNHttpServerConnectionFactory(final ConnectionConfig config) {
145         this(null, null, null, null, null, config);
146     }
147 
148     /**
149      * @since 4.3
150      */
151     public DefaultNHttpServerConnectionFactory() {
152         this(null, null, null, null, null, null);
153     }
154 
155     @Override
156     public DefaultNHttpServerConnection createConnection(final IOSession session) {
157         return new DefaultNHttpServerConnection(session,
158                 this.cconfig.getBufferSize(),
159                 this.cconfig.getFragmentSizeHint(),
160                 this.allocator,
161                 ConnSupport.createDecoder(this.cconfig),
162                 ConnSupport.createEncoder(this.cconfig),
163                 this.cconfig.getMessageConstraints(),
164                 this.incomingContentStrategy,
165                 this.outgoingContentStrategy,
166                 this.requestParserFactory,
167                 this.responseWriterFactory);
168     }
169 
170 }