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      public boolean isExpectContinueEnabled() {
90          return expectContinueEnabled;
91      }
92  
93      public HttpHost getProxy() {
94          return proxy;
95      }
96  
97      public InetAddress getLocalAddress() {
98          return localAddress;
99      }
100 
101     public boolean isStaleConnectionCheckEnabled() {
102         return staleConnectionCheckEnabled;
103     }
104 
105     public String getCookieSpec() {
106         return cookieSpec;
107     }
108 
109     public boolean isRedirectsEnabled() {
110         return redirectsEnabled;
111     }
112 
113     public boolean isRelativeRedirectsAllowed() {
114         return relativeRedirectsAllowed;
115     }
116 
117     public boolean isCircularRedirectsAllowed() {
118         return circularRedirectsAllowed;
119     }
120 
121     public int getMaxRedirects() {
122         return maxRedirects;
123     }
124 
125     public boolean isAuthenticationEnabled() {
126         return authenticationEnabled;
127     }
128 
129     public Collection<String> getTargetPreferredAuthSchemes() {
130         return targetPreferredAuthSchemes;
131     }
132 
133     public Collection<String> getProxyPreferredAuthSchemes() {
134         return proxyPreferredAuthSchemes;
135     }
136 
137     public int getConnectionRequestTimeout() {
138         return connectionRequestTimeout;
139     }
140 
141     public int getConnectTimeout() {
142         return connectTimeout;
143     }
144 
145     public int getSocketTimeout() {
146         return socketTimeout;
147     }
148 
149     @Override
150     protected RequestConfig clone() throws CloneNotSupportedException {
151         return (RequestConfig) super.clone();
152     }
153 
154     @Override
155     public String toString() {
156         final StringBuilder builder = new StringBuilder();
157         builder.append(", expectContinueEnabled=").append(expectContinueEnabled);
158         builder.append(", proxy=").append(proxy);
159         builder.append(", localAddress=").append(localAddress);
160         builder.append(", staleConnectionCheckEnabled=").append(staleConnectionCheckEnabled);
161         builder.append(", cookieSpec=").append(cookieSpec);
162         builder.append(", redirectsEnabled=").append(redirectsEnabled);
163         builder.append(", relativeRedirectsAllowed=").append(relativeRedirectsAllowed);
164         builder.append(", maxRedirects=").append(maxRedirects);
165         builder.append(", circularRedirectsAllowed=").append(circularRedirectsAllowed);
166         builder.append(", authenticationEnabled=").append(authenticationEnabled);
167         builder.append(", targetPreferredAuthSchemes=").append(targetPreferredAuthSchemes);
168         builder.append(", proxyPreferredAuthSchemes=").append(proxyPreferredAuthSchemes);
169         builder.append(", connectionRequestTimeout=").append(connectionRequestTimeout);
170         builder.append(", connectTimeout=").append(connectTimeout);
171         builder.append(", socketTimeout=").append(socketTimeout);
172         builder.append("]");
173         return builder.toString();
174     }
175 
176     public static RequestConfig.Builder custom() {
177         return new Builder();
178     }
179 
180     public static RequestConfig.Builder copy(final RequestConfig config) {
181         return new Builder()
182             .setExpectContinueEnabled(config.isExpectContinueEnabled())
183             .setProxy(config.getProxy())
184             .setLocalAddress(config.getLocalAddress())
185             .setStaleConnectionCheckEnabled(config.isStaleConnectionCheckEnabled())
186             .setCookieSpec(config.getCookieSpec())
187             .setRedirectsEnabled(config.isRedirectsEnabled())
188             .setRelativeRedirectsAllowed(config.isRelativeRedirectsAllowed())
189             .setCircularRedirectsAllowed(config.isCircularRedirectsAllowed())
190             .setMaxRedirects(config.getMaxRedirects())
191             .setAuthenticationEnabled(config.isAuthenticationEnabled())
192             .setTargetPreferredAuthSchemes(config.getProxyPreferredAuthSchemes())
193             .setProxyPreferredAuthSchemes(config.getProxyPreferredAuthSchemes())
194             .setConnectionRequestTimeout(config.getConnectionRequestTimeout())
195             .setConnectTimeout(config.getConnectTimeout())
196             .setSocketTimeout(config.getSocketTimeout());
197     }
198 
199     public static class Builder {
200 
201         private boolean expectContinueEnabled;
202         private HttpHost proxy;
203         private InetAddress localAddress;
204         private boolean staleConnectionCheckEnabled;
205         private String cookieSpec;
206         private boolean redirectsEnabled;
207         private boolean relativeRedirectsAllowed;
208         private boolean circularRedirectsAllowed;
209         private int maxRedirects;
210         private boolean authenticationEnabled;
211         private Collection<String> targetPreferredAuthSchemes;
212         private Collection<String> proxyPreferredAuthSchemes;
213         private int connectionRequestTimeout;
214         private int connectTimeout;
215         private int socketTimeout;
216 
217         Builder() {
218             super();
219             this.staleConnectionCheckEnabled = true;
220             this.redirectsEnabled = true;
221             this.maxRedirects = 50;
222             this.relativeRedirectsAllowed = true;
223             this.authenticationEnabled = true;
224             this.connectionRequestTimeout = -1;
225             this.connectTimeout = -1;
226             this.socketTimeout = -1;
227         }
228 
229         public Builder setExpectContinueEnabled(final boolean expectContinueEnabled) {
230             this.expectContinueEnabled = expectContinueEnabled;
231             return this;
232         }
233 
234         public Builder setProxy(final HttpHost proxy) {
235             this.proxy = proxy;
236             return this;
237         }
238 
239         public Builder setLocalAddress(final InetAddress localAddress) {
240             this.localAddress = localAddress;
241             return this;
242         }
243 
244         public Builder setStaleConnectionCheckEnabled(final boolean staleConnectionCheckEnabled) {
245             this.staleConnectionCheckEnabled = staleConnectionCheckEnabled;
246             return this;
247         }
248 
249         public Builder setCookieSpec(final String cookieSpec) {
250             this.cookieSpec = cookieSpec;
251             return this;
252         }
253 
254         public Builder setRedirectsEnabled(final boolean redirectsEnabled) {
255             this.redirectsEnabled = redirectsEnabled;
256             return this;
257         }
258 
259         public Builder setRelativeRedirectsAllowed(final boolean relativeRedirectsAllowed) {
260             this.relativeRedirectsAllowed = relativeRedirectsAllowed;
261             return this;
262         }
263 
264         public Builder setCircularRedirectsAllowed(final boolean circularRedirectsAllowed) {
265             this.circularRedirectsAllowed = circularRedirectsAllowed;
266             return this;
267         }
268 
269         public Builder setMaxRedirects(final int maxRedirects) {
270             this.maxRedirects = maxRedirects;
271             return this;
272         }
273 
274         public Builder setAuthenticationEnabled(final boolean authenticationEnabled) {
275             this.authenticationEnabled = authenticationEnabled;
276             return this;
277         }
278 
279         public Builder setTargetPreferredAuthSchemes(final Collection<String> targetPreferredAuthSchemes) {
280             this.targetPreferredAuthSchemes = targetPreferredAuthSchemes;
281             return this;
282         }
283 
284         public Builder setProxyPreferredAuthSchemes(final Collection<String> proxyPreferredAuthSchemes) {
285             this.proxyPreferredAuthSchemes = proxyPreferredAuthSchemes;
286             return this;
287         }
288 
289         public Builder setConnectionRequestTimeout(final int connectionRequestTimeout) {
290             this.connectionRequestTimeout = connectionRequestTimeout;
291             return this;
292         }
293 
294         public Builder setConnectTimeout(final int connectTimeout) {
295             this.connectTimeout = connectTimeout;
296             return this;
297         }
298 
299         public Builder setSocketTimeout(final int socketTimeout) {
300             this.socketTimeout = socketTimeout;
301             return this;
302         }
303 
304         public RequestConfig build() {
305             return new RequestConfig(
306                     expectContinueEnabled,
307                     proxy,
308                     localAddress,
309                     staleConnectionCheckEnabled,
310                     cookieSpec,
311                     redirectsEnabled,
312                     relativeRedirectsAllowed,
313                     circularRedirectsAllowed,
314                     maxRedirects,
315                     authenticationEnabled,
316                     targetPreferredAuthSchemes,
317                     proxyPreferredAuthSchemes,
318                     connectionRequestTimeout,
319                     connectTimeout,
320                     socketTimeout);
321         }
322 
323     }
324 
325 }