1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
54
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 }