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.HttpResponse;
38  import org.apache.http.HttpResponseFactory;
39  import org.apache.http.config.ConnectionConfig;
40  import org.apache.http.impl.DefaultHttpResponseFactory;
41  import org.apache.http.impl.nio.codecs.DefaultHttpResponseParserFactory;
42  import org.apache.http.message.BasicLineParser;
43  import org.apache.http.nio.NHttpMessageParserFactory;
44  import org.apache.http.nio.conn.ClientAsyncConnection;
45  import org.apache.http.nio.conn.ClientAsyncConnectionFactory;
46  import org.apache.http.nio.conn.ManagedNHttpClientConnection;
47  import org.apache.http.nio.conn.NHttpConnectionFactory;
48  import org.apache.http.nio.reactor.IOEventDispatch;
49  import org.apache.http.nio.reactor.IOSession;
50  import org.apache.http.nio.util.ByteBufferAllocator;
51  import org.apache.http.nio.util.HeapByteBufferAllocator;
52  import org.apache.http.params.HttpParams;
53  
54  @Deprecated
55  public class DefaultClientAsyncConnectionFactory
56      implements ClientAsyncConnectionFactory, 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      public static final DefaultClientAsyncConnectionFactory INSTANCE = new DefaultClientAsyncConnectionFactory(null, null);
63  
64      private static AtomicLong COUNTER = new AtomicLong();
65  
66      private final HttpResponseFactory responseFactory;
67      private final NHttpMessageParserFactory<HttpResponse> responseParserFactory;
68      private final ByteBufferAllocator allocator;
69  
70      public DefaultClientAsyncConnectionFactory(
71              final NHttpMessageParserFactory<HttpResponse> responseParserFactory,
72              final ByteBufferAllocator allocator) {
73          super();
74          this.responseFactory = createHttpResponseFactory();
75          this.responseParserFactory = responseParserFactory != null ? responseParserFactory :
76              DefaultHttpResponseParserFactory.INSTANCE;
77          this.allocator = allocator != null ? allocator : HeapByteBufferAllocator.INSTANCE;
78      }
79  
80      public DefaultClientAsyncConnectionFactory() {
81          super();
82          this.responseFactory = createHttpResponseFactory();
83          this.responseParserFactory = new DefaultHttpResponseParserFactory(
84              BasicLineParser.INSTANCE, this.responseFactory);
85          this.allocator = createByteBufferAllocator();
86      }
87  
88      @Override
89      @Deprecated
90      public ClientAsyncConnection create(
91              final String id,
92              final IOSession iosession,
93              final HttpParams params) {
94          return new DefaultClientAsyncConnection(
95                  id, iosession, this.responseFactory, this.allocator, params);
96      }
97  
98      @Deprecated
99      protected ByteBufferAllocator createByteBufferAllocator() {
100         return HeapByteBufferAllocator.INSTANCE;
101     }
102 
103     @Deprecated
104     protected HttpResponseFactory createHttpResponseFactory() {
105         return DefaultHttpResponseFactory.INSTANCE;
106     }
107 
108     @Override
109     public ManagedNHttpClientConnection create(
110             final IOSession iosession, final ConnectionConfig config) {
111         final String id = "http-outgoing-" + Long.toString(COUNTER.getAndIncrement());
112         CharsetDecoder chardecoder = null;
113         CharsetEncoder charencoder = null;
114         final Charset charset = config.getCharset();
115         final CodingErrorAction malformedInputAction = config.getMalformedInputAction() != null ?
116                 config.getMalformedInputAction() : CodingErrorAction.REPORT;
117         final CodingErrorAction unmappableInputAction = config.getUnmappableInputAction() != null ?
118                 config.getUnmappableInputAction() : CodingErrorAction.REPORT;
119         if (charset != null) {
120             chardecoder = charset.newDecoder();
121             chardecoder.onMalformedInput(malformedInputAction);
122             chardecoder.onUnmappableCharacter(unmappableInputAction);
123             charencoder = charset.newEncoder();
124             charencoder.onMalformedInput(malformedInputAction);
125             charencoder.onUnmappableCharacter(unmappableInputAction);
126         }
127         final ManagedNHttpClientConnection conn = new ManagedNHttpClientConnectionImpl(
128                 id,
129                 this.log,
130                 this.headerlog,
131                 this.wirelog,
132                 iosession,
133                 config.getBufferSize(),
134                 config.getFragmentSizeHint(),
135                 this.allocator,
136                 chardecoder, charencoder, config.getMessageConstraints(),
137                 null, null, null,
138                 this.responseParserFactory);
139         iosession.setAttribute(IOEventDispatch.CONNECTION_KEY, conn);
140         return conn;
141     }
142 
143 }