View Javadoc
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 org.apache.commons.httpclient.Cookie;
20  import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
21  import org.apache.commons.httpclient.params.HttpConnectionParams;
22  import org.apache.commons.vfs2.FileSystem;
23  import org.apache.commons.vfs2.FileSystemConfigBuilder;
24  import org.apache.commons.vfs2.FileSystemOptions;
25  import org.apache.commons.vfs2.UserAuthenticator;
26  
27  /**
28   * Configuration options for HTTP.
29   */
30  public class HttpFileSystemConfigBuilder extends FileSystemConfigBuilder {
31  
32      protected static final String KEY_FOLLOW_REDIRECT = "followRedirect";
33  
34      protected static final String KEY_USER_AGENT = "userAgent";
35  
36      private static final HttpFileSystemConfigBuilderleSystemConfigBuilder.html#HttpFileSystemConfigBuilder">HttpFileSystemConfigBuilder BUILDER = new HttpFileSystemConfigBuilder();
37  
38      private static final int DEFAULT_MAX_HOST_CONNECTIONS = 5;
39  
40      private static final int DEFAULT_MAX_CONNECTIONS = 50;
41  
42      private static final int DEFAULT_CONNECTION_TIMEOUT = 0;
43  
44      private static final int DEFAULT_SO_TIMEOUT = 0;
45  
46      private static final boolean DEFAULT_FOLLOW_REDIRECT = true;
47  
48      private static final String DEFAULT_USER_AGENT = "Jakarta-Commons-VFS";
49  
50      private static final String KEY_PREEMPTIVE_AUTHENTICATION = "preemptiveAuth";
51  
52      /**
53       * Creates new config builder.
54       *
55       * @param prefix String for properties of this file system.
56       * @since 2.0
57       */
58      protected HttpFileSystemConfigBuilder(final String prefix) {
59          super(prefix);
60      }
61  
62      private HttpFileSystemConfigBuilder() {
63          super("http.");
64      }
65  
66      /**
67       * Gets the singleton builder.
68       *
69       * @return the singleton builder.
70       */
71      public static HttpFileSystemConfigBuilder getInstance() {
72          return BUILDER;
73      }
74  
75      /**
76       * Sets the charset used for url encoding.
77       *
78       * @param opts The FileSystem options.
79       * @param chaset the chaset
80       */
81      public void setUrlCharset(final FileSystemOptions opts, final String chaset) {
82          setParam(opts, "urlCharset", chaset);
83      }
84  
85      /**
86       * Sets the charset used for url encoding.
87       *
88       * @param opts The FileSystem options.
89       * @return the chaset
90       */
91      public String getUrlCharset(final FileSystemOptions opts) {
92          return getString(opts, "urlCharset");
93      }
94  
95      /**
96       * Sets the proxy to use for http connection.
97       * <p>
98       * You have to set the ProxyPort too if you would like to have the proxy really used.
99       * </p>
100      *
101      * @param opts The FileSystem options.
102      * @param proxyHost the host
103      * @see #setProxyPort
104      */
105     public void setProxyHost(final FileSystemOptions opts, final String proxyHost) {
106         setParam(opts, "proxyHost", proxyHost);
107     }
108 
109     /**
110      * Sets the proxy-port to use for http connection. You have to set the ProxyHost too if you would like to have the
111      * proxy really used.
112      *
113      * @param opts The FileSystem options.
114      * @param proxyPort the port
115      * @see #setProxyHost
116      */
117     public void setProxyPort(final FileSystemOptions opts, final int proxyPort) {
118         setParam(opts, "proxyPort", Integer.valueOf(proxyPort));
119     }
120 
121     /**
122      * Gets the proxy to use for http connection. You have to set the ProxyPort too if you would like to have the proxy
123      * really used.
124      *
125      * @param opts The FileSystem options.
126      * @return proxyHost
127      * @see #setProxyPort
128      */
129     public String getProxyHost(final FileSystemOptions opts) {
130         return getString(opts, "proxyHost");
131     }
132 
133     /**
134      * Gets the proxy-port to use for http the connection. You have to set the ProxyHost too if you would like to have
135      * the proxy really used.
136      *
137      * @param opts The FileSystem options.
138      * @return proxyPort: the port number or 0 if it is not set
139      * @see #setProxyHost
140      */
141     public int getProxyPort(final FileSystemOptions opts) {
142         return getInteger(opts, "proxyPort", 0);
143     }
144 
145     /**
146      * Sets the proxy authenticator where the system should get the credentials from.
147      *
148      * @param opts The FileSystem options.
149      * @param authenticator The UserAuthenticator.
150      */
151     public void setProxyAuthenticator(final FileSystemOptions opts, final UserAuthenticator authenticator) {
152         setParam(opts, "proxyAuthenticator", authenticator);
153     }
154 
155     /**
156      * Gets the proxy authenticator where the system should get the credentials from.
157      *
158      * @param opts The FileSystem options.
159      * @return The UserAuthenticator.
160      */
161     public UserAuthenticator getProxyAuthenticator(final FileSystemOptions opts) {
162         return (UserAuthenticator) getParam(opts, "proxyAuthenticator");
163     }
164 
165     /**
166      * The cookies to add to the request.
167      *
168      * @param opts The FileSystem options.
169      * @param cookies An array of Cookies.
170      */
171     public void setCookies(final FileSystemOptions opts, final Cookie[] cookies) {
172         setParam(opts, "cookies", cookies);
173     }
174 
175     /**
176      * Sets whether to follow redirects for the connection.
177      *
178      * @param opts The FileSystem options.
179      * @param redirect {@code true} to follow redirects, {@code false} not to.
180      * @see #setFollowRedirect
181      * @since 2.1
182      */
183     public void setFollowRedirect(final FileSystemOptions opts, final boolean redirect) {
184         setParam(opts, KEY_FOLLOW_REDIRECT, redirect);
185     }
186 
187     /**
188      * Gets the cookies to add to the request.
189      *
190      * @param opts The FileSystem options.
191      * @return the Cookie array.
192      */
193     public Cookie[] getCookies(final FileSystemOptions opts) {
194         return (Cookie[]) getParam(opts, "cookies");
195     }
196 
197     /**
198      * Gets whether to follow redirects for the connection.
199      *
200      * @param opts The FileSystem options.
201      * @return {@code true} to follow redirects, {@code false} not to.
202      * @see #setFollowRedirect
203      * @since 2.1
204      */
205     public boolean getFollowRedirect(final FileSystemOptions opts) {
206         return getBoolean(opts, KEY_FOLLOW_REDIRECT, DEFAULT_FOLLOW_REDIRECT);
207     }
208 
209     /**
210      * Sets the maximum number of connections allowed.
211      *
212      * @param opts The FileSystem options.
213      * @param maxTotalConnections The maximum number of connections.
214      * @since 2.0
215      */
216     public void setMaxTotalConnections(final FileSystemOptions opts, final int maxTotalConnections) {
217         setParam(opts, HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, Integer.valueOf(maxTotalConnections));
218     }
219 
220     /**
221      * Gets the maximum number of connections allowed.
222      *
223      * @param opts The FileSystemOptions.
224      * @return The maximum number of connections allowed.
225      * @since 2.0
226      */
227     public int getMaxTotalConnections(final FileSystemOptions opts) {
228         return getInteger(opts, HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, DEFAULT_MAX_CONNECTIONS);
229     }
230 
231     /**
232      * Sets the maximum number of connections allowed to any host.
233      *
234      * @param opts The FileSystem options.
235      * @param maxHostConnections The maximum number of connections to a host.
236      * @since 2.0
237      */
238     public void setMaxConnectionsPerHost(final FileSystemOptions opts, final int maxHostConnections) {
239         setParam(opts, HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, Integer.valueOf(maxHostConnections));
240     }
241 
242     /**
243      * Gets the maximum number of connections allowed per host.
244      *
245      * @param opts The FileSystemOptions.
246      * @return The maximum number of connections allowed per host.
247      * @since 2.0
248      */
249     public int getMaxConnectionsPerHost(final FileSystemOptions opts) {
250         return getInteger(opts, HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, DEFAULT_MAX_HOST_CONNECTIONS);
251     }
252 
253     /**
254      * Determines if the FileSystemOptions indicate that preemptive authentication is requested.
255      *
256      * @param opts The FileSystemOptions.
257      * @return true if preemptiveAuth is requested.
258      * @since 2.0
259      */
260     public boolean isPreemptiveAuth(final FileSystemOptions opts) {
261         return getBoolean(opts, KEY_PREEMPTIVE_AUTHENTICATION, Boolean.FALSE).booleanValue();
262     }
263 
264     /**
265      * Sets the given value for preemptive HTTP authentication (using BASIC) on the given FileSystemOptions object.
266      * Defaults to false if not set. It may be appropriate to set to true in cases when the resulting chattiness of the
267      * conversation outweighs any architectural desire to use a stronger authentication scheme than basic/preemptive.
268      *
269      * @param opts The FileSystemOptions.
270      * @param preemptiveAuth the desired setting; true=enabled and false=disabled.
271      */
272     public void setPreemptiveAuth(final FileSystemOptions opts, final boolean preemptiveAuth) {
273         setParam(opts, KEY_PREEMPTIVE_AUTHENTICATION, Boolean.valueOf(preemptiveAuth));
274     }
275 
276     /**
277      * The connection timeout.
278      *
279      * @param opts The FileSystem options.
280      * @param connectionTimeout The connection timeout.
281      * @since 2.1
282      */
283     public void setConnectionTimeout(final FileSystemOptions opts, final int connectionTimeout) {
284         setParam(opts, HttpConnectionParams.CONNECTION_TIMEOUT, Integer.valueOf(connectionTimeout));
285     }
286 
287     /**
288      * Gets the connection timeout.
289      *
290      * @param opts The FileSystem options.
291      * @return The connection timeout.
292      * @since 2.1
293      */
294     public int getConnectionTimeout(final FileSystemOptions opts) {
295         return getInteger(opts, HttpConnectionParams.CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
296     }
297 
298     /**
299      * The socket timeout.
300      *
301      * @param opts The FileSystem options.
302      * @param soTimeout socket timeout.
303      * @since 2.1
304      */
305     public void setSoTimeout(final FileSystemOptions opts, final int soTimeout) {
306         setParam(opts, HttpConnectionParams.SO_TIMEOUT, Integer.valueOf(soTimeout));
307     }
308 
309     /**
310      * Gets the socket timeout.
311      *
312      * @param opts The FileSystemOptions.
313      * @return The socket timeout.
314      * @since 2.1
315      */
316     public int getSoTimeout(final FileSystemOptions opts) {
317         return getInteger(opts, HttpConnectionParams.SO_TIMEOUT, DEFAULT_SO_TIMEOUT);
318     }
319 
320     /**
321      * Sets the user agent to attach to the outgoing http methods
322      *
323      * @param opts the file system options to modify
324      * @param userAgent User Agent String
325      */
326     public void setUserAgent(final FileSystemOptions opts, final String userAgent) {
327         setParam(opts, "userAgent", userAgent);
328     }
329 
330     /**
331      * Gets the user agent string
332      *
333      * @param opts the file system options to modify
334      * @return User provided User-Agent string, otherwise default of: Commons-VFS
335      */
336     public String getUserAgent(final FileSystemOptions opts) {
337         final String userAgent = (String) getParam(opts, KEY_USER_AGENT);
338         return userAgent != null ? userAgent : DEFAULT_USER_AGENT;
339     }
340 
341     @Override
342     protected Class<? extends FileSystem> getConfigClass() {
343         return HttpFileSystem.class;
344     }
345 }