1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
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 * Configuration options for HTTP.
31 *
32 * @deprecated Use {@link org.apache.commons.vfs2.provider.http5}.
33 */
34 @Deprecated
35 public class HttpFileSystemConfigBuilder extends FileSystemConfigBuilder {
36
37 /**
38 * Keys for FileSystemOptions.
39 */
40 protected static final String KEY_FOLLOW_REDIRECT = "followRedirect";
41
42 /**
43 * Keys for FileSystemOptions.
44 */
45 protected static final String KEY_USER_AGENT = "userAgent";
46
47 private static final HttpFileSystemConfigBuilder BUILDER = new HttpFileSystemConfigBuilder();
48
49 private static final int DEFAULT_MAX_HOST_CONNECTIONS = 5;
50
51 private static final int DEFAULT_MAX_CONNECTIONS = 50;
52
53 private static final Duration DEFAULT_CONNECTION_TIMEOUT = Duration.ZERO;
54
55 private static final Duration DEFAULT_SO_TIMEOUT = Duration.ZERO;
56
57 private static final boolean DEFAULT_FOLLOW_REDIRECT = true;
58
59 private static final String DEFAULT_USER_AGENT = "Jakarta-Commons-VFS";
60
61 private static final String KEY_PREEMPTIVE_AUTHENTICATION = "preemptiveAuth";
62
63 /**
64 * Gets the singleton builder.
65 *
66 * @return the singleton builder.
67 */
68 public static HttpFileSystemConfigBuilder getInstance() {
69 return BUILDER;
70 }
71
72 private HttpFileSystemConfigBuilder() {
73 super("http.");
74 }
75
76 /**
77 * Creates new config builder.
78 *
79 * @param prefix String for properties of this file system.
80 * @since 2.0
81 */
82 protected HttpFileSystemConfigBuilder(final String prefix) {
83 super(prefix);
84 }
85
86 @Override
87 protected Class<? extends FileSystem> getConfigClass() {
88 return HttpFileSystem.class;
89 }
90
91 /**
92 * Gets the connection timeout.
93 *
94 * @param opts The FileSystem options.
95 * @return The connection timeout.
96 * @since 2.1
97 * @deprecated Use {@link #getConnectionTimeoutDuration(FileSystemOptions)}.
98 */
99 @Deprecated
100 public int getConnectionTimeout(final FileSystemOptions opts) {
101 return getDurationInteger(opts, HttpConnectionParams.CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
102 }
103
104 /**
105 * Gets the connection timeout.
106 *
107 * @param opts The FileSystem options.
108 * @return The connection timeout.
109 * @since 2.8.0
110 */
111 public Duration getConnectionTimeoutDuration(final FileSystemOptions opts) {
112 return getDuration(opts, HttpConnectionParams.CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
113 }
114
115 /**
116 * Gets the cookies to add to the request.
117 *
118 * @param opts The FileSystem options.
119 * @return the Cookie array.
120 */
121 public Cookie[] getCookies(final FileSystemOptions opts) {
122 return getParam(opts, "cookies");
123 }
124
125 /**
126 * Gets whether to follow redirects for the connection.
127 *
128 * @param opts The FileSystem options.
129 * @return {@code true} to follow redirects, {@code false} not to.
130 * @see #setFollowRedirect
131 * @since 2.1
132 */
133 public boolean getFollowRedirect(final FileSystemOptions opts) {
134 return getBoolean(opts, KEY_FOLLOW_REDIRECT, DEFAULT_FOLLOW_REDIRECT);
135 }
136
137 /**
138 * Gets the maximum number of connections allowed per host.
139 *
140 * @param opts The FileSystemOptions.
141 * @return The maximum number of connections allowed per host.
142 * @since 2.0
143 */
144 public int getMaxConnectionsPerHost(final FileSystemOptions opts) {
145 return getInteger(opts, HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, DEFAULT_MAX_HOST_CONNECTIONS);
146 }
147
148 /**
149 * Gets the maximum number of connections allowed.
150 *
151 * @param opts The FileSystemOptions.
152 * @return The maximum number of connections allowed.
153 * @since 2.0
154 */
155 public int getMaxTotalConnections(final FileSystemOptions opts) {
156 return getInteger(opts, HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, DEFAULT_MAX_CONNECTIONS);
157 }
158
159 /**
160 * Gets the proxy authenticator where the system should get the credentials from.
161 *
162 * @param opts The FileSystem options.
163 * @return The UserAuthenticator.
164 */
165 public UserAuthenticator getProxyAuthenticator(final FileSystemOptions opts) {
166 return getParam(opts, "proxyAuthenticator");
167 }
168
169 /**
170 * Gets the proxy to use for http connection. You have to set the ProxyPort too if you would like to have the proxy
171 * really used.
172 *
173 * @param opts The FileSystem options.
174 * @return proxyHost
175 * @see #setProxyPort
176 */
177 public String getProxyHost(final FileSystemOptions opts) {
178 return getString(opts, "proxyHost");
179 }
180
181 /**
182 * Gets the proxy-port to use for http the connection. You have to set the ProxyHost too if you would like to have
183 * the proxy really used.
184 *
185 * @param opts The FileSystem options.
186 * @return proxyPort: the port number or 0 if it is not set
187 * @see #setProxyHost
188 */
189 public int getProxyPort(final FileSystemOptions opts) {
190 return getInteger(opts, "proxyPort", 0);
191 }
192
193 /**
194 * Gets the socket timeout.
195 *
196 * @param opts The FileSystemOptions.
197 * @return The socket timeout.
198 * @since 2.1
199 * @deprecated Use {@link #getSoTimeoutDuration(FileSystemOptions)}.
200 */
201 @Deprecated
202 public int getSoTimeout(final FileSystemOptions opts) {
203 return getDurationInteger(opts, HttpConnectionParams.SO_TIMEOUT, DEFAULT_SO_TIMEOUT);
204 }
205
206 /**
207 * Gets the socket timeout.
208 *
209 * @param opts The FileSystemOptions.
210 * @return The socket timeout.
211 * @since 2.8.0
212 */
213 public Duration getSoTimeoutDuration(final FileSystemOptions opts) {
214 return getDuration(opts, HttpConnectionParams.SO_TIMEOUT, DEFAULT_SO_TIMEOUT);
215 }
216
217 /**
218 * Gets the charset used for url encoding.
219 *
220 * @param opts The FileSystem options.
221 * @return the charset name.
222 */
223 public String getUrlCharset(final FileSystemOptions opts) {
224 return getString(opts, "urlCharset");
225 }
226
227 /**
228 * Gets the user agent string.
229 *
230 * @param opts the file system options to modify
231 * @return User provided User-Agent string, otherwise default of: Commons-VFS
232 */
233 public String getUserAgent(final FileSystemOptions opts) {
234 final String userAgent = getParam(opts, KEY_USER_AGENT);
235 return userAgent != null ? userAgent : DEFAULT_USER_AGENT;
236 }
237
238 /**
239 * Determines if the FileSystemOptions indicate that preemptive authentication is requested.
240 *
241 * @param opts The FileSystemOptions.
242 * @return true if preemptiveAuth is requested.
243 * @since 2.0
244 */
245 public boolean isPreemptiveAuth(final FileSystemOptions opts) {
246 return getBoolean(opts, KEY_PREEMPTIVE_AUTHENTICATION, Boolean.FALSE).booleanValue();
247 }
248
249 /**
250 * The connection timeout.
251 *
252 * @param opts The FileSystem options.
253 * @param timeout The connection timeout.
254 * @since 2.8.0
255 */
256 public void setConnectionTimeout(final FileSystemOptions opts, final Duration timeout) {
257 setParam(opts, HttpConnectionParams.CONNECTION_TIMEOUT, timeout);
258 }
259
260 /**
261 * The connection timeout.
262 *
263 * @param opts The FileSystem options.
264 * @param timeout The connection timeout.
265 * @since 2.1
266 * @deprecated Use {@link #setConnectionTimeout(FileSystemOptions, Duration)}.
267 */
268 @Deprecated
269 public void setConnectionTimeout(final FileSystemOptions opts, final int timeout) {
270 setConnectionTimeout(opts, Duration.ofMillis(timeout));
271 }
272
273 /**
274 * The cookies to add to the request.
275 *
276 * @param opts The FileSystem options.
277 * @param cookies An array of Cookies.
278 */
279 public void setCookies(final FileSystemOptions opts, final Cookie[] cookies) {
280 setParam(opts, "cookies", cookies);
281 }
282
283 /**
284 * Sets whether to follow redirects for the connection.
285 *
286 * @param opts The FileSystem options.
287 * @param redirect {@code true} to follow redirects, {@code false} not to.
288 * @see #setFollowRedirect
289 * @since 2.1
290 */
291 public void setFollowRedirect(final FileSystemOptions opts, final boolean redirect) {
292 setParam(opts, KEY_FOLLOW_REDIRECT, redirect);
293 }
294
295 /**
296 * Sets the maximum number of connections allowed to any host.
297 *
298 * @param opts The FileSystem options.
299 * @param maxHostConnections The maximum number of connections to a host.
300 * @since 2.0
301 */
302 public void setMaxConnectionsPerHost(final FileSystemOptions opts, final int maxHostConnections) {
303 setParam(opts, HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, Integer.valueOf(maxHostConnections));
304 }
305
306 /**
307 * Sets the maximum number of connections allowed.
308 *
309 * @param opts The FileSystem options.
310 * @param maxTotalConnections The maximum number of connections.
311 * @since 2.0
312 */
313 public void setMaxTotalConnections(final FileSystemOptions opts, final int maxTotalConnections) {
314 setParam(opts, HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, Integer.valueOf(maxTotalConnections));
315 }
316
317 /**
318 * Sets the given value for preemptive HTTP authentication (using BASIC) on the given FileSystemOptions object.
319 * Defaults to false if not set. It may be appropriate to set to true in cases when the resulting chattiness of the
320 * conversation outweighs any architectural desire to use a stronger authentication scheme than basic/preemptive.
321 *
322 * @param opts The FileSystemOptions.
323 * @param preemptiveAuth the desired setting; true=enabled and false=disabled.
324 */
325 public void setPreemptiveAuth(final FileSystemOptions opts, final boolean preemptiveAuth) {
326 setParam(opts, KEY_PREEMPTIVE_AUTHENTICATION, Boolean.valueOf(preemptiveAuth));
327 }
328
329 /**
330 * Sets the proxy authenticator where the system should get the credentials from.
331 *
332 * @param opts The FileSystem options.
333 * @param authenticator The UserAuthenticator.
334 */
335 public void setProxyAuthenticator(final FileSystemOptions opts, final UserAuthenticator authenticator) {
336 setParam(opts, "proxyAuthenticator", authenticator);
337 }
338
339 /**
340 * Sets the proxy to use for http connection.
341 * <p>
342 * You have to set the ProxyPort too if you would like to have the proxy really used.
343 * </p>
344 *
345 * @param opts The FileSystem options.
346 * @param proxyHost the host
347 * @see #setProxyPort
348 */
349 public void setProxyHost(final FileSystemOptions opts, final String proxyHost) {
350 setParam(opts, "proxyHost", proxyHost);
351 }
352
353 /**
354 * Sets the proxy-port to use for http connection. You have to set the ProxyHost too if you would like to have the
355 * proxy really used.
356 *
357 * @param opts The FileSystem options.
358 * @param proxyPort the port
359 * @see #setProxyHost
360 */
361 public void setProxyPort(final FileSystemOptions opts, final int proxyPort) {
362 setParam(opts, "proxyPort", Integer.valueOf(proxyPort));
363 }
364
365 /**
366 * The socket timeout.
367 *
368 * @param opts The FileSystem options.
369 * @param timeout socket timeout.
370 * @since 2.8.0
371 */
372 public void setSoTimeout(final FileSystemOptions opts, final Duration timeout) {
373 setParam(opts, HttpConnectionParams.SO_TIMEOUT, timeout);
374 }
375
376 /**
377 * The socket timeout.
378 *
379 * @param opts The FileSystem options.
380 * @param timeout socket timeout.
381 * @since 2.1
382 * @deprecated Use {@link #setSoTimeout(FileSystemOptions, Duration)}.
383 */
384 @Deprecated
385 public void setSoTimeout(final FileSystemOptions opts, final int timeout) {
386 setSoTimeout(opts, Duration.ofMillis(timeout));
387 }
388
389 /**
390 * Sets the charset used for url encoding.
391 *
392 * @param opts The FileSystem options.
393 * @param charset the charset name.
394 */
395 public void setUrlCharset(final FileSystemOptions opts, final String charset) {
396 setParam(opts, "urlCharset", charset);
397 }
398
399 /**
400 * Sets the user agent to attach to the outgoing http methods.
401 *
402 * @param opts the file system options to modify
403 * @param userAgent User Agent String
404 */
405 public void setUserAgent(final FileSystemOptions opts, final String userAgent) {
406 setParam(opts, "userAgent", userAgent);
407 }
408 }