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.core5.http.impl.io;
29
30 import java.io.IOException;
31 import java.net.Socket;
32
33 import org.apache.hc.core5.annotation.Contract;
34 import org.apache.hc.core5.annotation.ThreadingBehavior;
35 import org.apache.hc.core5.http.ClassicHttpRequest;
36 import org.apache.hc.core5.http.ClassicHttpResponse;
37 import org.apache.hc.core5.http.ContentLengthStrategy;
38 import org.apache.hc.core5.http.config.CharCodingConfig;
39 import org.apache.hc.core5.http.config.Http1Config;
40 import org.apache.hc.core5.http.impl.CharCodingSupport;
41 import org.apache.hc.core5.http.io.HttpConnectionFactory;
42 import org.apache.hc.core5.http.io.HttpMessageParserFactory;
43 import org.apache.hc.core5.http.io.HttpMessageWriterFactory;
44
45
46
47
48
49
50 @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
51 public class DefaultBHttpServerConnectionFactory implements HttpConnectionFactory<DefaultBHttpServerConnection> {
52
53 private final String scheme;
54 private final Http1Config http1Config;
55 private final CharCodingConfig charCodingConfig;
56 private final ContentLengthStrategy incomingContentStrategy;
57 private final ContentLengthStrategy outgoingContentStrategy;
58 private final HttpMessageParserFactory<ClassicHttpRequest> requestParserFactory;
59 private final HttpMessageWriterFactory<ClassicHttpResponse> responseWriterFactory;
60
61 public DefaultBHttpServerConnectionFactory(
62 final String scheme,
63 final Http1Config http1Config,
64 final CharCodingConfig charCodingConfig,
65 final ContentLengthStrategy incomingContentStrategy,
66 final ContentLengthStrategy outgoingContentStrategy,
67 final HttpMessageParserFactory<ClassicHttpRequest> requestParserFactory,
68 final HttpMessageWriterFactory<ClassicHttpResponse> responseWriterFactory) {
69 super();
70 this.scheme = scheme;
71 this.http1Config = http1Config != null ? http1Config : Http1Config.DEFAULT;
72 this.charCodingConfig = charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT;
73 this.incomingContentStrategy = incomingContentStrategy;
74 this.outgoingContentStrategy = outgoingContentStrategy;
75 this.requestParserFactory = requestParserFactory;
76 this.responseWriterFactory = responseWriterFactory;
77 }
78
79 public DefaultBHttpServerConnectionFactory(
80 final String scheme,
81 final Http1Config http1Config,
82 final CharCodingConfig charCodingConfig,
83 final HttpMessageParserFactory<ClassicHttpRequest> requestParserFactory,
84 final HttpMessageWriterFactory<ClassicHttpResponse> responseWriterFactory) {
85 this(scheme, http1Config, charCodingConfig, null, null, requestParserFactory, responseWriterFactory);
86 }
87
88 public DefaultBHttpServerConnectionFactory(
89 final String scheme,
90 final Http1Config http1Config,
91 final CharCodingConfig charCodingConfig) {
92 this(scheme, http1Config, charCodingConfig, null, null, null, null);
93 }
94
95 @Override
96 public DefaultBHttpServerConnection createConnection(final Socket socket) throws IOException {
97 final DefaultBHttpServerConnectiontBHttpServerConnection.html#DefaultBHttpServerConnection">DefaultBHttpServerConnection conn = new DefaultBHttpServerConnection(
98 this.scheme,
99 this.http1Config,
100 CharCodingSupport.createDecoder(this.charCodingConfig),
101 CharCodingSupport.createEncoder(this.charCodingConfig),
102 this.incomingContentStrategy,
103 this.outgoingContentStrategy,
104 this.requestParserFactory,
105 this.responseWriterFactory);
106 conn.bind(socket);
107 return conn;
108 }
109
110
111
112
113
114
115 public static Builder builder() {
116 return new Builder();
117 }
118
119
120
121
122
123
124 public static final class Builder {
125 private String scheme;
126 private Http1Config http1Config;
127 private CharCodingConfig charCodingConfig;
128 private ContentLengthStrategy incomingContentLengthStrategy;
129 private ContentLengthStrategy outgoingContentLengthStrategy;
130 private HttpMessageParserFactory<ClassicHttpRequest> requestParserFactory;
131 private HttpMessageWriterFactory<ClassicHttpResponse> responseWriterFactory;
132
133 private Builder() {}
134
135 public Builder scheme(final String scheme) {
136 this.scheme = scheme;
137 return this;
138 }
139
140 public Builder http1Config(final Http1Config http1Config) {
141 this.http1Config = http1Config;
142 return this;
143 }
144
145 public Builder charCodingConfig(final CharCodingConfig charCodingConfig) {
146 this.charCodingConfig = charCodingConfig;
147 return this;
148 }
149
150 public Builder incomingContentLengthStrategy(final ContentLengthStrategy incomingContentLengthStrategy) {
151 this.incomingContentLengthStrategy = incomingContentLengthStrategy;
152 return this;
153 }
154
155 public Builder outgoingContentLengthStrategy(final ContentLengthStrategy outgoingContentLengthStrategy) {
156 this.outgoingContentLengthStrategy = outgoingContentLengthStrategy;
157 return this;
158 }
159
160 public Builder requestParserFactory(
161 final HttpMessageParserFactory<ClassicHttpRequest> requestParserFactory) {
162 this.requestParserFactory = requestParserFactory;
163 return this;
164 }
165
166 public Builder responseWriterFactory(
167 final HttpMessageWriterFactory<ClassicHttpResponse> responseWriterFactory) {
168 this.responseWriterFactory = responseWriterFactory;
169 return this;
170 }
171
172 public DefaultBHttpServerConnectionFactory build() {
173 return new DefaultBHttpServerConnectionFactory(
174 scheme,
175 http1Config,
176 charCodingConfig,
177 incomingContentLengthStrategy,
178 outgoingContentLengthStrategy,
179 requestParserFactory,
180 responseWriterFactory);
181 }
182 }
183 }