View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.client.config;
29  
30  import java.net.InetAddress;
31  import java.util.Collection;
32  
33  import org.apache.http.HttpHost;
34  
35  public class RequestConfig implements Cloneable {
36  
37      public static final RequestConfig DEFAULT = new Builder().build();
38  
39      private final boolean expectContinueEnabled;
40      private final HttpHost proxy;
41      private final InetAddress localAddress;
42      private final boolean staleConnectionCheckEnabled;
43      private final String cookieSpec;
44      private final boolean redirectsEnabled;
45      private final boolean relativeRedirectsAllowed;
46      private final boolean circularRedirectsAllowed;
47      private final int maxRedirects;
48      private final boolean authenticationEnabled;
49      private final Collection<String> targetPreferredAuthSchemes;
50      private final Collection<String> proxyPreferredAuthSchemes;
51      private final int connectionRequestTimeout;
52      private final int connectTimeout;
53      private final int socketTimeout;
54  
55      RequestConfig(
56              final boolean expectContinueEnabled,
57              final HttpHost proxy,
58              final InetAddress localAddress,
59              final boolean staleConnectionCheckEnabled,
60              final String cookieSpec,
61              final boolean redirectsEnabled,
62              final boolean relativeRedirectsAllowed,
63              final boolean circularRedirectsAllowed,
64              final int maxRedirects,
65              final boolean authenticationEnabled,
66              final Collection<String> targetPreferredAuthSchemes,
67              final Collection<String> proxyPreferredAuthSchemes,
68              final int connectionRequestTimeout,
69              final int connectTimeout,
70              final int socketTimeout) {
71          super();
72          this.expectContinueEnabled = expectContinueEnabled;
73          this.proxy = proxy;
74          this.localAddress = localAddress;
75          this.staleConnectionCheckEnabled = staleConnectionCheckEnabled;
76          this.cookieSpec = cookieSpec;
77          this.redirectsEnabled = redirectsEnabled;
78          this.relativeRedirectsAllowed = relativeRedirectsAllowed;
79          this.circularRedirectsAllowed = circularRedirectsAllowed;
80          this.maxRedirects = maxRedirects;
81          this.authenticationEnabled = authenticationEnabled;
82          this.targetPreferredAuthSchemes = targetPreferredAuthSchemes;
83          this.proxyPreferredAuthSchemes = proxyPreferredAuthSchemes;
84          this.connectionRequestTimeout = connectionRequestTimeout;
85          this.connectTimeout = connectTimeout;
86          this.socketTimeout = socketTimeout;
87      }
88  
89      /**
90       * Determines whether the 'Expect: 100-Continue' handshake is enabled
91       * for entity enclosing methods. The purpose of the 'Expect: 100-Continue'
92       * handshake is to allow a client that is sending a request message with
93       * a request body to determine if the origin server is willing to
94       * accept the request (based on the request headers) before the client
95       * sends the request body.
96       * <p/>
97       * The use of the 'Expect: 100-continue' handshake can result in
98       * a noticeable performance improvement for entity enclosing requests
99       * (such as POST and PUT) that require the target server's
100      * authentication.
101      * <p/>
102      * 'Expect: 100-continue' handshake should be used with caution, as it
103      * may cause problems with HTTP servers and proxies that do not support
104      * HTTP/1.1 protocol.
105      * <p/>
106      * Default: <code>false</code>
107      */
108     public boolean isExpectContinueEnabled() {
109         return expectContinueEnabled;
110     }
111 
112     /**
113      * Returns HTTP proxy to be used for request execution.
114      * <p/>
115      * Default: <code>null</code>
116      */
117     public HttpHost getProxy() {
118         return proxy;
119     }
120 
121     /**
122      * Returns local address to be used for request execution.
123      * <p/>
124      * On machines with multiple network interfaces, this parameter
125      * can be used to select the network interface from which the
126      * connection originates.
127      * <p/>
128      * Default: <code>null</code>
129      */
130     public InetAddress getLocalAddress() {
131         return localAddress;
132     }
133 
134     /**
135      * Determines whether stale connection check is to be used. The stale
136      * connection check can cause up to 30 millisecond overhead per request and
137      * should be used only when appropriate. For performance critical
138      * operations this check should be disabled.
139      * <p/>
140      * Default: <code>true</code>
141      */
142     public boolean isStaleConnectionCheckEnabled() {
143         return staleConnectionCheckEnabled;
144     }
145 
146     /**
147      * Determines the name of the cookie specification to be used for HTTP state
148      * management.
149      * <p/>
150      * Default: <code>null</code>
151      */
152     public String getCookieSpec() {
153         return cookieSpec;
154     }
155 
156     /**
157      * Determines whether redirects should be handled automatically.
158      * <p/>
159      * Default: <code>true</code>
160      */
161     public boolean isRedirectsEnabled() {
162         return redirectsEnabled;
163     }
164 
165     /**
166      * Determines whether relative redirects should be rejected. HTTP specification
167      * requires the location value be an absolute URI.
168      * <p/>
169      * Default: <code>true</code>
170      */
171     public boolean isRelativeRedirectsAllowed() {
172         return relativeRedirectsAllowed;
173     }
174 
175     /**
176      * Determines whether circular redirects (redirects to the same location) should
177      * be allowed. The HTTP spec is not sufficiently clear whether circular redirects
178      * are permitted, therefore optionally they can be enabled
179      * <p/>
180      * Default: <code>false</code>
181      */
182     public boolean isCircularRedirectsAllowed() {
183         return circularRedirectsAllowed;
184     }
185 
186     /**
187      * Returns the maximum number of redirects to be followed. The limit on number
188      * of redirects is intended to prevent infinite loops.
189      * <p/>
190      * Default: <code>50</code>
191      */
192     public int getMaxRedirects() {
193         return maxRedirects;
194     }
195 
196     /**
197      * Determines whether authentication should be handled automatically.
198      * <p/>
199      * Default: <code>true</code>
200      */
201     public boolean isAuthenticationEnabled() {
202         return authenticationEnabled;
203     }
204 
205     /**
206      * Determines the order of preference for supported authentication schemes
207      * when authenticating with the target host.
208      * <p/>
209      * Default: <code>null</code>
210      */
211     public Collection<String> getTargetPreferredAuthSchemes() {
212         return targetPreferredAuthSchemes;
213     }
214 
215     /**
216      * Determines the order of preference for supported authentication schemes
217      * when authenticating with the proxy host.
218      * <p/>
219      * Default: <code>null</code>
220      */
221     public Collection<String> getProxyPreferredAuthSchemes() {
222         return proxyPreferredAuthSchemes;
223     }
224 
225     /**
226      * Returns the timeout in milliseconds used when requesting a connection
227      * from the connection manager. A timeout value of zero is interpreted
228      * as an infinite timeout.
229      * <p/>
230      * A timeout value of zero is interpreted as an infinite timeout.
231      * A negative value is interpreted as undefined (system default).
232      * <p/>
233      * Default: <code>-1</code>
234      */
235     public int getConnectionRequestTimeout() {
236         return connectionRequestTimeout;
237     }
238 
239     /**
240      * Determines the timeout in milliseconds until a connection is established.
241      * A timeout value of zero is interpreted as an infinite timeout.
242      * <p/>
243      * A timeout value of zero is interpreted as an infinite timeout.
244      * A negative value is interpreted as undefined (system default).
245      * <p/>
246      * Default: <code>-1</code>
247      */
248     public int getConnectTimeout() {
249         return connectTimeout;
250     }
251 
252     /**
253      * Defines the socket timeout (<code>SO_TIMEOUT</code>) in milliseconds,
254      * which is the timeout for waiting for data  or, put differently,
255      * a maximum period inactivity between two consecutive data packets).
256      * <p/>
257      * A timeout value of zero is interpreted as an infinite timeout.
258      * A negative value is interpreted as undefined (system default).
259      * <p/>
260      * Default: <code>-1</code>
261      */
262     public int getSocketTimeout() {
263         return socketTimeout;
264     }
265 
266     @Override
267     protected RequestConfig clone() throws CloneNotSupportedException {
268         return (RequestConfig) super.clone();
269     }
270 
271     @Override
272     public String toString() {
273         final StringBuilder builder = new StringBuilder();
274         builder.append(", expectContinueEnabled=").append(expectContinueEnabled);
275         builder.append(", proxy=").append(proxy);
276         builder.append(", localAddress=").append(localAddress);
277         builder.append(", staleConnectionCheckEnabled=").append(staleConnectionCheckEnabled);
278         builder.append(", cookieSpec=").append(cookieSpec);
279         builder.append(", redirectsEnabled=").append(redirectsEnabled);
280         builder.append(", relativeRedirectsAllowed=").append(relativeRedirectsAllowed);
281         builder.append(", maxRedirects=").append(maxRedirects);
282         builder.append(", circularRedirectsAllowed=").append(circularRedirectsAllowed);
283         builder.append(", authenticationEnabled=").append(authenticationEnabled);
284         builder.append(", targetPreferredAuthSchemes=").append(targetPreferredAuthSchemes);
285         builder.append(", proxyPreferredAuthSchemes=").append(proxyPreferredAuthSchemes);
286         builder.append(", connectionRequestTimeout=").append(connectionRequestTimeout);
287         builder.append(", connectTimeout=").append(connectTimeout);
288         builder.append(", socketTimeout=").append(socketTimeout);
289         builder.append("]");
290         return builder.toString();
291     }
292 
293     public static RequestConfig.Builder custom() {
294         return new Builder();
295     }
296 
297     public static RequestConfig.Builder copy(final RequestConfig config) {
298         return new Builder()
299             .setExpectContinueEnabled(config.isExpectContinueEnabled())
300             .setProxy(config.getProxy())
301             .setLocalAddress(config.getLocalAddress())
302             .setStaleConnectionCheckEnabled(config.isStaleConnectionCheckEnabled())
303             .setCookieSpec(config.getCookieSpec())
304             .setRedirectsEnabled(config.isRedirectsEnabled())
305             .setRelativeRedirectsAllowed(config.isRelativeRedirectsAllowed())
306             .setCircularRedirectsAllowed(config.isCircularRedirectsAllowed())
307             .setMaxRedirects(config.getMaxRedirects())
308             .setAuthenticationEnabled(config.isAuthenticationEnabled())
309             .setTargetPreferredAuthSchemes(config.getTargetPreferredAuthSchemes())
310             .setProxyPreferredAuthSchemes(config.getProxyPreferredAuthSchemes())
311             .setConnectionRequestTimeout(config.getConnectionRequestTimeout())
312             .setConnectTimeout(config.getConnectTimeout())
313             .setSocketTimeout(config.getSocketTimeout());
314     }
315 
316     public static class Builder {
317 
318         private boolean expectContinueEnabled;
319         private HttpHost proxy;
320         private InetAddress localAddress;
321         private boolean staleConnectionCheckEnabled;
322         private String cookieSpec;
323         private boolean redirectsEnabled;
324         private boolean relativeRedirectsAllowed;
325         private boolean circularRedirectsAllowed;
326         private int maxRedirects;
327         private boolean authenticationEnabled;
328         private Collection<String> targetPreferredAuthSchemes;
329         private Collection<String> proxyPreferredAuthSchemes;
330         private int connectionRequestTimeout;
331         private int connectTimeout;
332         private int socketTimeout;
333 
334         Builder() {
335             super();
336             this.staleConnectionCheckEnabled = true;
337             this.redirectsEnabled = true;
338             this.maxRedirects = 50;
339             this.relativeRedirectsAllowed = true;
340             this.authenticationEnabled = true;
341             this.connectionRequestTimeout = -1;
342             this.connectTimeout = -1;
343             this.socketTimeout = -1;
344         }
345 
346         public Builder setExpectContinueEnabled(final boolean expectContinueEnabled) {
347             this.expectContinueEnabled = expectContinueEnabled;
348             return this;
349         }
350 
351         public Builder setProxy(final HttpHost proxy) {
352             this.proxy = proxy;
353             return this;
354         }
355 
356         public Builder setLocalAddress(final InetAddress localAddress) {
357             this.localAddress = localAddress;
358             return this;
359         }
360 
361         public Builder setStaleConnectionCheckEnabled(final boolean staleConnectionCheckEnabled) {
362             this.staleConnectionCheckEnabled = staleConnectionCheckEnabled;
363             return this;
364         }
365 
366         public Builder setCookieSpec(final String cookieSpec) {
367             this.cookieSpec = cookieSpec;
368             return this;
369         }
370 
371         public Builder setRedirectsEnabled(final boolean redirectsEnabled) {
372             this.redirectsEnabled = redirectsEnabled;
373             return this;
374         }
375 
376         public Builder setRelativeRedirectsAllowed(final boolean relativeRedirectsAllowed) {
377             this.relativeRedirectsAllowed = relativeRedirectsAllowed;
378             return this;
379         }
380 
381         public Builder setCircularRedirectsAllowed(final boolean circularRedirectsAllowed) {
382             this.circularRedirectsAllowed = circularRedirectsAllowed;
383             return this;
384         }
385 
386         public Builder setMaxRedirects(final int maxRedirects) {
387             this.maxRedirects = maxRedirects;
388             return this;
389         }
390 
391         public Builder setAuthenticationEnabled(final boolean authenticationEnabled) {
392             this.authenticationEnabled = authenticationEnabled;
393             return this;
394         }
395 
396         public Builder setTargetPreferredAuthSchemes(final Collection<String> targetPreferredAuthSchemes) {
397             this.targetPreferredAuthSchemes = targetPreferredAuthSchemes;
398             return this;
399         }
400 
401         public Builder setProxyPreferredAuthSchemes(final Collection<String> proxyPreferredAuthSchemes) {
402             this.proxyPreferredAuthSchemes = proxyPreferredAuthSchemes;
403             return this;
404         }
405 
406         public Builder setConnectionRequestTimeout(final int connectionRequestTimeout) {
407             this.connectionRequestTimeout = connectionRequestTimeout;
408             return this;
409         }
410 
411         public Builder setConnectTimeout(final int connectTimeout) {
412             this.connectTimeout = connectTimeout;
413             return this;
414         }
415 
416         public Builder setSocketTimeout(final int socketTimeout) {
417             this.socketTimeout = socketTimeout;
418             return this;
419         }
420 
421         public RequestConfig build() {
422             return new RequestConfig(
423                     expectContinueEnabled,
424                     proxy,
425                     localAddress,
426                     staleConnectionCheckEnabled,
427                     cookieSpec,
428                     redirectsEnabled,
429                     relativeRedirectsAllowed,
430                     circularRedirectsAllowed,
431                     maxRedirects,
432                     authenticationEnabled,
433                     targetPreferredAuthSchemes,
434                     proxyPreferredAuthSchemes,
435                     connectionRequestTimeout,
436                     connectTimeout,
437                     socketTimeout);
438         }
439 
440     }
441 
442 }