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  
28  package org.apache.hc.client5.http.impl.io;
29  
30  import java.io.IOException;
31  import java.net.Socket;
32  import java.nio.charset.Charset;
33  import java.nio.charset.CharsetDecoder;
34  import java.nio.charset.CharsetEncoder;
35  import java.nio.charset.CodingErrorAction;
36  import java.util.concurrent.atomic.AtomicLong;
37  
38  import org.apache.hc.client5.http.io.ManagedHttpClientConnection;
39  import org.apache.hc.core5.annotation.Contract;
40  import org.apache.hc.core5.annotation.ThreadingBehavior;
41  import org.apache.hc.core5.http.ClassicHttpRequest;
42  import org.apache.hc.core5.http.ClassicHttpResponse;
43  import org.apache.hc.core5.http.ContentLengthStrategy;
44  import org.apache.hc.core5.http.config.CharCodingConfig;
45  import org.apache.hc.core5.http.config.Http1Config;
46  import org.apache.hc.core5.http.impl.DefaultContentLengthStrategy;
47  import org.apache.hc.core5.http.impl.io.DefaultHttpRequestWriterFactory;
48  import org.apache.hc.core5.http.io.HttpConnectionFactory;
49  import org.apache.hc.core5.http.io.HttpMessageParserFactory;
50  import org.apache.hc.core5.http.io.HttpMessageWriterFactory;
51  
52  /**
53   * Factory for {@link ManagedHttpClientConnection} instances.
54   * @since 4.3
55   */
56  @Contract(threading = ThreadingBehavior.STATELESS)
57  public class ManagedHttpClientConnectionFactory implements HttpConnectionFactory<ManagedHttpClientConnection> {
58  
59      private static final AtomicLong COUNTER = new AtomicLong();
60  
61      public static final ManagedHttpClientConnectionFactoryentConnectionFactory.html#ManagedHttpClientConnectionFactory">ManagedHttpClientConnectionFactory INSTANCE = new ManagedHttpClientConnectionFactory();
62  
63      private final Http1Config h1Config;
64      private final CharCodingConfig charCodingConfig;
65      private final HttpMessageWriterFactory<ClassicHttpRequest> requestWriterFactory;
66      private final HttpMessageParserFactory<ClassicHttpResponse> responseParserFactory;
67      private final ContentLengthStrategy incomingContentStrategy;
68      private final ContentLengthStrategy outgoingContentStrategy;
69  
70      public ManagedHttpClientConnectionFactory(
71              final Http1Config h1Config,
72              final CharCodingConfig charCodingConfig,
73              final HttpMessageWriterFactory<ClassicHttpRequest> requestWriterFactory,
74              final HttpMessageParserFactory<ClassicHttpResponse> responseParserFactory,
75              final ContentLengthStrategy incomingContentStrategy,
76              final ContentLengthStrategy outgoingContentStrategy) {
77          super();
78          this.h1Config = h1Config != null ? h1Config : Http1Config.DEFAULT;
79          this.charCodingConfig = charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT;
80          this.requestWriterFactory = requestWriterFactory != null ? requestWriterFactory :
81                  DefaultHttpRequestWriterFactory.INSTANCE;
82          this.responseParserFactory = responseParserFactory != null ? responseParserFactory :
83                  DefaultHttpResponseParserFactory.INSTANCE;
84          this.incomingContentStrategy = incomingContentStrategy != null ? incomingContentStrategy :
85                  DefaultContentLengthStrategy.INSTANCE;
86          this.outgoingContentStrategy = outgoingContentStrategy != null ? outgoingContentStrategy :
87                  DefaultContentLengthStrategy.INSTANCE;
88      }
89  
90      public ManagedHttpClientConnectionFactory(
91              final Http1Config h1Config,
92              final CharCodingConfig charCodingConfig,
93              final HttpMessageWriterFactory<ClassicHttpRequest> requestWriterFactory,
94              final HttpMessageParserFactory<ClassicHttpResponse> responseParserFactory) {
95          this(h1Config, charCodingConfig, requestWriterFactory, responseParserFactory, null, null);
96      }
97  
98      public ManagedHttpClientConnectionFactory(
99              final Http1Config h1Config,
100             final CharCodingConfig charCodingConfig,
101             final HttpMessageParserFactory<ClassicHttpResponse> responseParserFactory) {
102         this(h1Config, charCodingConfig, null, responseParserFactory);
103     }
104 
105     public ManagedHttpClientConnectionFactory() {
106         this(null, null, null);
107     }
108 
109     @Override
110     public ManagedHttpClientConnection createConnection(final Socket socket) throws IOException {
111         CharsetDecoder charDecoder = null;
112         CharsetEncoder charEncoder = null;
113         final Charset charset = this.charCodingConfig.getCharset();
114         final CodingErrorAction malformedInputAction = this.charCodingConfig.getMalformedInputAction() != null ?
115                 this.charCodingConfig.getMalformedInputAction() : CodingErrorAction.REPORT;
116         final CodingErrorAction unmappableInputAction = this.charCodingConfig.getUnmappableInputAction() != null ?
117                 this.charCodingConfig.getUnmappableInputAction() : CodingErrorAction.REPORT;
118         if (charset != null) {
119             charDecoder = charset.newDecoder();
120             charDecoder.onMalformedInput(malformedInputAction);
121             charDecoder.onUnmappableCharacter(unmappableInputAction);
122             charEncoder = charset.newEncoder();
123             charEncoder.onMalformedInput(malformedInputAction);
124             charEncoder.onUnmappableCharacter(unmappableInputAction);
125         }
126         final String id = "http-outgoing-" + Long.toString(COUNTER.getAndIncrement());
127         final DefaultManagedHttpClientConnectionagedHttpClientConnection.html#DefaultManagedHttpClientConnection">DefaultManagedHttpClientConnection conn = new DefaultManagedHttpClientConnection(
128                 id,
129                 charDecoder,
130                 charEncoder,
131                 h1Config,
132                 incomingContentStrategy,
133                 outgoingContentStrategy,
134                 requestWriterFactory,
135                 responseParserFactory);
136         if (socket != null) {
137             conn.bind(socket);
138         }
139         return conn;
140     }
141 
142 }