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.conn;
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  import java.util.concurrent.atomic.AtomicLong;
34  
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  import org.apache.http.HttpRequest;
38  import org.apache.http.HttpResponse;
39  import org.apache.http.config.ConnectionConfig;
40  import org.apache.http.impl.nio.codecs.DefaultHttpRequestWriterFactory;
41  import org.apache.http.impl.nio.codecs.DefaultHttpResponseParserFactory;
42  import org.apache.http.nio.NHttpMessageParserFactory;
43  import org.apache.http.nio.NHttpMessageWriterFactory;
44  import org.apache.http.nio.conn.ManagedNHttpClientConnection;
45  import org.apache.http.nio.conn.NHttpConnectionFactory;
46  import org.apache.http.nio.reactor.IOEventDispatch;
47  import org.apache.http.nio.reactor.IOSession;
48  import org.apache.http.nio.util.ByteBufferAllocator;
49  import org.apache.http.nio.util.HeapByteBufferAllocator;
50  
51  /**
52   * Default factory for {@link ManagedNHttpClientConnection} instances.
53   *
54   * @since 4.0
55   */
56  public class ManagedNHttpClientConnectionFactory implements NHttpConnectionFactory<ManagedNHttpClientConnection> {
57  
58      private final Log headerlog = LogFactory.getLog("org.apache.http.headers");
59      private final Log wirelog = LogFactory.getLog("org.apache.http.wire");
60      private final Log log = LogFactory.getLog(ManagedNHttpClientConnectionImpl.class);
61  
62      private static final AtomicLong COUNTER = new AtomicLong();
63  
64      public static final ManagedNHttpClientConnectionFactory INSTANCE = new ManagedNHttpClientConnectionFactory();
65  
66      private final ByteBufferAllocator allocator;
67      private final NHttpMessageWriterFactory<HttpRequest> requestWriterFactory;
68      private final NHttpMessageParserFactory<HttpResponse> responseParserFactory;
69  
70      public ManagedNHttpClientConnectionFactory(
71              final NHttpMessageWriterFactory<HttpRequest> requestWriterFactory,
72              final NHttpMessageParserFactory<HttpResponse> responseParserFactory,
73              final ByteBufferAllocator allocator) {
74          super();
75          this.requestWriterFactory = requestWriterFactory != null ? requestWriterFactory :
76              DefaultHttpRequestWriterFactory.INSTANCE;
77          this.responseParserFactory = responseParserFactory != null ? responseParserFactory :
78              DefaultHttpResponseParserFactory.INSTANCE;
79          this.allocator = allocator != null ? allocator : HeapByteBufferAllocator.INSTANCE;
80      }
81  
82      public ManagedNHttpClientConnectionFactory() {
83          this(null, null, null);
84      }
85  
86      @Override
87      public ManagedNHttpClientConnection create(
88              final IOSession iosession, final ConnectionConfig config) {
89          final String id = "http-outgoing-" + Long.toString(COUNTER.getAndIncrement());
90          CharsetDecoder chardecoder = null;
91          CharsetEncoder charencoder = null;
92          final Charset charset = config.getCharset();
93          final CodingErrorAction malformedInputAction = config.getMalformedInputAction() != null ?
94                  config.getMalformedInputAction() : CodingErrorAction.REPORT;
95          final CodingErrorAction unmappableInputAction = config.getUnmappableInputAction() != null ?
96                  config.getUnmappableInputAction() : CodingErrorAction.REPORT;
97          if (charset != null) {
98              chardecoder = charset.newDecoder();
99              chardecoder.onMalformedInput(malformedInputAction);
100             chardecoder.onUnmappableCharacter(unmappableInputAction);
101             charencoder = charset.newEncoder();
102             charencoder.onMalformedInput(malformedInputAction);
103             charencoder.onUnmappableCharacter(unmappableInputAction);
104         }
105         final ManagedNHttpClientConnection conn = new ManagedNHttpClientConnectionImpl(
106                 id,
107                 this.log,
108                 this.headerlog,
109                 this.wirelog,
110                 iosession,
111                 config.getBufferSize(),
112                 config.getFragmentSizeHint(),
113                 this.allocator,
114                 chardecoder,
115                 charencoder,
116                 config.getMessageConstraints(),
117                 null,
118                 null,
119                 this.requestWriterFactory,
120                 this.responseParserFactory);
121         iosession.setAttribute(IOEventDispatch.CONNECTION_KEY, conn);
122         return conn;
123     }
124 
125 }