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 java.nio.charset.Charset;
30  import java.nio.charset.CharsetDecoder;
31  import java.nio.charset.CharsetEncoder;
32  import java.nio.charset.CodingErrorAction;
33  
34  import org.apache.http.HttpRequest;
35  import org.apache.http.HttpRequestFactory;
36  import org.apache.http.annotation.Immutable;
37  import org.apache.http.config.ConnectionConfig;
38  import org.apache.http.impl.DefaultHttpRequestFactory;
39  import org.apache.http.impl.nio.codecs.DefaultHttpRequestParserFactory;
40  import org.apache.http.nio.NHttpConnectionFactory;
41  import org.apache.http.nio.NHttpMessageParserFactory;
42  import org.apache.http.nio.NHttpServerConnection;
43  import org.apache.http.nio.reactor.IOSession;
44  import org.apache.http.nio.util.ByteBufferAllocator;
45  import org.apache.http.nio.util.HeapByteBufferAllocator;
46  import org.apache.http.params.HttpParamConfig;
47  import org.apache.http.params.HttpParams;
48  import org.apache.http.util.Args;
49  
50  /**
51   * Default factory for plain (non-encrypted), non-blocking {@link NHttpServerConnection}s.
52   *
53   * @since 4.2
54   */
55  @SuppressWarnings("deprecation")
56  @Immutable
57  public class DefaultNHttpServerConnectionFactory
58      implements NHttpConnectionFactory<DefaultNHttpServerConnection> {
59  
60      private final NHttpMessageParserFactory<HttpRequest> requestParserFactory;
61      private final ByteBufferAllocator allocator;
62      private final ConnectionConfig config;
63  
64      /**
65       * @deprecated (4.3) use {@link
66       *   DefaultNHttpServerConnectionFactory#DefaultNHttpServerConnectionFactory(
67       *      ByteBufferAllocator, NHttpMessageParserFactory, ConnectionConfig)}
68       */
69      @Deprecated
70      public DefaultNHttpServerConnectionFactory(
71              final HttpRequestFactory requestFactory,
72              final ByteBufferAllocator allocator,
73              final HttpParams params) {
74          super();
75          Args.notNull(requestFactory, "HTTP request factory");
76          Args.notNull(allocator, "Byte buffer allocator");
77          Args.notNull(params, "HTTP parameters");
78          this.requestParserFactory = new DefaultHttpRequestParserFactory(null, requestFactory);
79          this.allocator = allocator;
80          this.config = HttpParamConfig.getConnectionConfig(params);
81      }
82  
83      /**
84       * @deprecated (4.3) use {@link
85       *   DefaultNHttpServerConnectionFactory#DefaultNHttpServerConnectionFactory(ConnectionConfig)}
86       */
87      @Deprecated
88      public DefaultNHttpServerConnectionFactory(final HttpParams params) {
89          this(DefaultHttpRequestFactory.INSTANCE, HeapByteBufferAllocator.INSTANCE, params);
90      }
91  
92      /**
93       * @deprecated (4.3) no longer used.
94       */
95      @Deprecated
96      protected DefaultNHttpServerConnection createConnection(
97              final IOSession session,
98              final HttpRequestFactory requestFactory,
99              final ByteBufferAllocator allocator,
100             final HttpParams params) {
101         return new DefaultNHttpServerConnection(session, requestFactory, allocator, params);
102     }
103 
104     /**
105      * @since 4.3
106      */
107     public DefaultNHttpServerConnectionFactory(
108             final ByteBufferAllocator allocator,
109             final NHttpMessageParserFactory<HttpRequest> requestParserFactory,
110             final ConnectionConfig config) {
111         super();
112         this.allocator = allocator != null ? allocator : HeapByteBufferAllocator.INSTANCE;
113         this.requestParserFactory = requestParserFactory != null ? requestParserFactory :
114             DefaultHttpRequestParserFactory.INSTANCE;
115         this.config = config != null ? config : ConnectionConfig.DEFAULT;
116     }
117 
118     /**
119      * @since 4.3
120      */
121     public DefaultNHttpServerConnectionFactory(final ConnectionConfig config) {
122         this(null, null, config);
123     }
124 
125     public DefaultNHttpServerConnection createConnection(final IOSession session) {
126         CharsetDecoder chardecoder = null;
127         CharsetEncoder charencoder = null;
128         final Charset charset = this.config.getCharset();
129         final CodingErrorAction malformedInputAction = this.config.getMalformedInputAction() != null ?
130                 this.config.getMalformedInputAction() : CodingErrorAction.REPORT;
131         final CodingErrorAction unmappableInputAction = this.config.getUnmappableInputAction() != null ?
132                 this.config.getUnmappableInputAction() : CodingErrorAction.REPORT;
133         if (charset != null) {
134             chardecoder = charset.newDecoder();
135             chardecoder.onMalformedInput(malformedInputAction);
136             chardecoder.onUnmappableCharacter(unmappableInputAction);
137             charencoder = charset.newEncoder();
138             charencoder.onMalformedInput(malformedInputAction);
139             charencoder.onUnmappableCharacter(unmappableInputAction);
140         }
141         return new DefaultNHttpServerConnection(session,
142                 this.config.getBufferSize(),
143                 this.config.getFragmentSizeHint(),
144                 this.allocator,
145                 chardecoder, charencoder, this.config.getMessageConstraints(),
146                 null, null,
147                 this.requestParserFactory,
148                 null);
149     }
150 
151 }