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