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.HttpResponse;
35  import org.apache.http.HttpResponseFactory;
36  import org.apache.http.annotation.Immutable;
37  import org.apache.http.config.ConnectionConfig;
38  import org.apache.http.impl.DefaultHttpResponseFactory;
39  import org.apache.http.impl.nio.codecs.DefaultHttpResponseParserFactory;
40  import org.apache.http.nio.NHttpClientConnection;
41  import org.apache.http.nio.NHttpConnectionFactory;
42  import org.apache.http.nio.NHttpMessageParserFactory;
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 NHttpClientConnection}s.
52   *
53   * @since 4.2
54   */
55  @SuppressWarnings("deprecation")
56  @Immutable
57  public class DefaultNHttpClientConnectionFactory
58      implements NHttpConnectionFactory<DefaultNHttpClientConnection> {
59  
60      private final NHttpMessageParserFactory<HttpResponse> responseParserFactory;
61      private final ByteBufferAllocator allocator;
62      private final ConnectionConfig config;
63  
64      /**
65       * @deprecated (4.3) use {@link
66       *   DefaultNHttpClientConnectionFactory#DefaultNHttpClientConnectionFactory(
67       *      NHttpMessageParserFactory, ByteBufferAllocator, ConnectionConfig)}
68       */
69      @Deprecated
70      public DefaultNHttpClientConnectionFactory(
71              final HttpResponseFactory responseFactory,
72              final ByteBufferAllocator allocator,
73              final HttpParams params) {
74          super();
75          Args.notNull(responseFactory, "HTTP response factory");
76          Args.notNull(allocator, "Byte buffer allocator");
77          Args.notNull(params, "HTTP parameters");
78          this.allocator = allocator;
79          this.responseParserFactory = new DefaultHttpResponseParserFactory(null, responseFactory);
80          this.config = HttpParamConfig.getConnectionConfig(params);
81      }
82  
83      /**
84       * @deprecated (4.3) use {@link
85       *   DefaultNHttpClientConnectionFactory#DefaultNHttpClientConnectionFactory(ConnectionConfig)}
86       */
87      @Deprecated
88      public DefaultNHttpClientConnectionFactory(final HttpParams params) {
89          this(DefaultHttpResponseFactory.INSTANCE, HeapByteBufferAllocator.INSTANCE, params);
90      }
91  
92      /**
93       * @since 4.3
94       */
95      public DefaultNHttpClientConnectionFactory(
96              final NHttpMessageParserFactory<HttpResponse> responseParserFactory,
97              final ByteBufferAllocator allocator,
98              final ConnectionConfig config) {
99          super();
100         this.allocator = allocator != null ? allocator : HeapByteBufferAllocator.INSTANCE;
101         this.responseParserFactory = responseParserFactory != null ? responseParserFactory :
102             DefaultHttpResponseParserFactory.INSTANCE;
103         this.config = config != null ? config : ConnectionConfig.DEFAULT;
104     }
105 
106     /**
107      * @since 4.3
108      */
109     public DefaultNHttpClientConnectionFactory(final ConnectionConfig config) {
110         this(null, null, config);
111     }
112 
113     /**
114      * @deprecated (4.3) no longer used.
115      */
116     @Deprecated
117     protected DefaultNHttpClientConnection createConnection(
118             final IOSession session,
119             final HttpResponseFactory responseFactory,
120             final ByteBufferAllocator allocator,
121             final HttpParams params) {
122         return new DefaultNHttpClientConnection(session, responseFactory, allocator, params);
123     }
124 
125     public DefaultNHttpClientConnection 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 DefaultNHttpClientConnection(
142                 session,
143                 this.config.getBufferSize(),
144                 this.config.getFragmentSizeHint(),
145                 this.allocator,
146                 chardecoder, charencoder, this.config.getMessageConstraints(),
147                 null, null, null,
148                 this.responseParserFactory);
149     }
150 
151 }