1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider.http;
18
19 import java.time.Duration;
20
21 import org.apache.commons.httpclient.Cookie;
22 import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
23 import org.apache.commons.httpclient.params.HttpConnectionParams;
24 import org.apache.commons.vfs2.FileSystem;
25 import org.apache.commons.vfs2.FileSystemConfigBuilder;
26 import org.apache.commons.vfs2.FileSystemOptions;
27 import org.apache.commons.vfs2.UserAuthenticator;
28
29
30
31
32 public class HttpFileSystemConfigBuilder extends FileSystemConfigBuilder {
33
34 protected static final String KEY_FOLLOW_REDIRECT = "followRedirect";
35
36 protected static final String KEY_USER_AGENT = "userAgent";
37
38 private static final HttpFileSystemConfigBuilderleSystemConfigBuilder.html#HttpFileSystemConfigBuilder">HttpFileSystemConfigBuilder BUILDER = new HttpFileSystemConfigBuilder();
39
40 private static final int DEFAULT_MAX_HOST_CONNECTIONS = 5;
41
42 private static final int DEFAULT_MAX_CONNECTIONS = 50;
43
44 private static final Duration DEFAULT_CONNECTION_TIMEOUT = Duration.ZERO;
45
46 private static final Duration DEFAULT_SO_TIMEOUT = Duration.ZERO;
47
48 private static final boolean DEFAULT_FOLLOW_REDIRECT = true;
49
50 private static final String DEFAULT_USER_AGENT = "Jakarta-Commons-VFS";
51
52 private static final String KEY_PREEMPTIVE_AUTHENTICATION = "preemptiveAuth";
53
54
55
56
57
58
59 public static HttpFileSystemConfigBuilder getInstance() {
60 return BUILDER;
61 }
62
63 private HttpFileSystemConfigBuilder() {
64 super("http.");
65 }
66
67
68
69
70
71
72
73 protected HttpFileSystemConfigBuilder(final String prefix) {
74 super(prefix);
75 }
76
77 @Override
78 protected Class<? extends FileSystem> getConfigClass() {
79 return HttpFileSystem.class;
80 }
81
82
83
84
85
86
87
88
89
90 @Deprecated
91 public int getConnectionTimeout(final FileSystemOptions opts) {
92 return getDurationInteger(opts, HttpConnectionParams.CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
93 }
94
95
96
97
98
99
100
101
102 public Duration getConnectionTimeoutDuration(final FileSystemOptions opts) {
103 return getDuration(opts, HttpConnectionParams.CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
104 }
105
106
107
108
109
110
111
112 public Cookie[] getCookies(final FileSystemOptions opts) {
113 return getParam(opts, "cookies");
114 }
115
116
117
118
119
120
121
122
123
124 public boolean getFollowRedirect(final FileSystemOptions opts) {
125 return getBoolean(opts, KEY_FOLLOW_REDIRECT, DEFAULT_FOLLOW_REDIRECT);
126 }
127
128
129
130
131
132
133
134
135 public int getMaxConnectionsPerHost(final FileSystemOptions opts) {
136 return getInteger(opts, HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, DEFAULT_MAX_HOST_CONNECTIONS);
137 }
138
139
140
141
142
143
144
145
146 public int getMaxTotalConnections(final FileSystemOptions opts) {
147 return getInteger(opts, HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, DEFAULT_MAX_CONNECTIONS);
148 }
149
150
151
152
153
154
155
156 public UserAuthenticator getProxyAuthenticator(final FileSystemOptions opts) {
157 return getParam(opts, "proxyAuthenticator");
158 }
159
160
161
162
163
164
165
166
167
168 public String getProxyHost(final FileSystemOptions opts) {
169 return getString(opts, "proxyHost");
170 }
171
172
173
174
175
176
177
178
179
180 public int getProxyPort(final FileSystemOptions opts) {
181 return getInteger(opts, "proxyPort", 0);
182 }
183
184
185
186
187
188
189
190
191
192 @Deprecated
193 public int getSoTimeout(final FileSystemOptions opts) {
194 return getDurationInteger(opts, HttpConnectionParams.SO_TIMEOUT, DEFAULT_SO_TIMEOUT);
195 }
196
197
198
199
200
201
202
203
204 public Duration getSoTimeoutDuration(final FileSystemOptions opts) {
205 return getDuration(opts, HttpConnectionParams.SO_TIMEOUT, DEFAULT_SO_TIMEOUT);
206 }
207
208
209
210
211
212
213
214 public String getUrlCharset(final FileSystemOptions opts) {
215 return getString(opts, "urlCharset");
216 }
217
218
219
220
221
222
223
224 public String getUserAgent(final FileSystemOptions opts) {
225 final String userAgent = getParam(opts, KEY_USER_AGENT);
226 return userAgent != null ? userAgent : DEFAULT_USER_AGENT;
227 }
228
229
230
231
232
233
234
235
236 public boolean isPreemptiveAuth(final FileSystemOptions opts) {
237 return getBoolean(opts, KEY_PREEMPTIVE_AUTHENTICATION, Boolean.FALSE).booleanValue();
238 }
239
240
241
242
243
244
245
246
247 public void setConnectionTimeout(final FileSystemOptions opts, final Duration timeout) {
248 setParam(opts, HttpConnectionParams.CONNECTION_TIMEOUT, timeout);
249 }
250
251
252
253
254
255
256
257
258
259 @Deprecated
260 public void setConnectionTimeout(final FileSystemOptions opts, final int timeout) {
261 setConnectionTimeout(opts, Duration.ofMillis(timeout));
262 }
263
264
265
266
267
268
269
270 public void setCookies(final FileSystemOptions opts, final Cookie[] cookies) {
271 setParam(opts, "cookies", cookies);
272 }
273
274
275
276
277
278
279
280
281
282 public void setFollowRedirect(final FileSystemOptions opts, final boolean redirect) {
283 setParam(opts, KEY_FOLLOW_REDIRECT, redirect);
284 }
285
286
287
288
289
290
291
292
293 public void setMaxConnectionsPerHost(final FileSystemOptions opts, final int maxHostConnections) {
294 setParam(opts, HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, Integer.valueOf(maxHostConnections));
295 }
296
297
298
299
300
301
302
303
304 public void setMaxTotalConnections(final FileSystemOptions opts, final int maxTotalConnections) {
305 setParam(opts, HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, Integer.valueOf(maxTotalConnections));
306 }
307
308
309
310
311
312
313
314
315
316 public void setPreemptiveAuth(final FileSystemOptions opts, final boolean preemptiveAuth) {
317 setParam(opts, KEY_PREEMPTIVE_AUTHENTICATION, Boolean.valueOf(preemptiveAuth));
318 }
319
320
321
322
323
324
325
326 public void setProxyAuthenticator(final FileSystemOptions opts, final UserAuthenticator authenticator) {
327 setParam(opts, "proxyAuthenticator", authenticator);
328 }
329
330
331
332
333
334
335
336
337
338
339
340 public void setProxyHost(final FileSystemOptions opts, final String proxyHost) {
341 setParam(opts, "proxyHost", proxyHost);
342 }
343
344
345
346
347
348
349
350
351
352 public void setProxyPort(final FileSystemOptions opts, final int proxyPort) {
353 setParam(opts, "proxyPort", Integer.valueOf(proxyPort));
354 }
355
356
357
358
359
360
361
362
363 public void setSoTimeout(final FileSystemOptions opts, final Duration timeout) {
364 setParam(opts, HttpConnectionParams.SO_TIMEOUT, timeout);
365 }
366
367
368
369
370
371
372
373
374
375 @Deprecated
376 public void setSoTimeout(final FileSystemOptions opts, final int timeout) {
377 setSoTimeout(opts, Duration.ofMillis(timeout));
378 }
379
380
381
382
383
384
385
386 public void setUrlCharset(final FileSystemOptions opts, final String charset) {
387 setParam(opts, "urlCharset", charset);
388 }
389
390
391
392
393
394
395
396 public void setUserAgent(final FileSystemOptions opts, final String userAgent) {
397 setParam(opts, "userAgent", userAgent);
398 }
399 }