1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package org.apache.commons.httpclient.params;
32
33 import java.util.ArrayList;
34 import java.util.Arrays;
35
36 import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
37 import org.apache.commons.httpclient.HttpVersion;
38 import org.apache.commons.httpclient.SimpleHttpConnectionManager;
39 import org.apache.commons.httpclient.cookie.CookiePolicy;
40 import org.apache.commons.httpclient.util.DateUtil;
41
42 /***
43 * @since 3.0
44 */
45 public class DefaultHttpParamsFactory implements HttpParamsFactory {
46
47 private HttpParams httpParams;
48
49 /***
50 *
51 */
52 public DefaultHttpParamsFactory() {
53 super();
54 }
55
56
57
58
59 public synchronized HttpParams getDefaultParams() {
60 if (httpParams == null) {
61 httpParams = createParams();
62 }
63
64 return httpParams;
65 }
66
67 protected HttpParams createParams() {
68 HttpClientParams params = new HttpClientParams(null);
69
70 params.setParameter(HttpMethodParams.USER_AGENT, "Jakarta Commons-HttpClient/3.1");
71 params.setVersion(HttpVersion.HTTP_1_1);
72 params.setConnectionManagerClass(SimpleHttpConnectionManager.class);
73 params.setCookiePolicy(CookiePolicy.DEFAULT);
74 params.setHttpElementCharset("US-ASCII");
75 params.setContentCharset("ISO-8859-1");
76 params.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
77
78 ArrayList datePatterns = new ArrayList();
79 datePatterns.addAll(
80 Arrays.asList(
81 new String[] {
82 DateUtil.PATTERN_RFC1123,
83 DateUtil.PATTERN_RFC1036,
84 DateUtil.PATTERN_ASCTIME,
85 "EEE, dd-MMM-yyyy HH:mm:ss z",
86 "EEE, dd-MMM-yyyy HH-mm-ss z",
87 "EEE, dd MMM yy HH:mm:ss z",
88 "EEE dd-MMM-yyyy HH:mm:ss z",
89 "EEE dd MMM yyyy HH:mm:ss z",
90 "EEE dd-MMM-yyyy HH-mm-ss z",
91 "EEE dd-MMM-yy HH:mm:ss z",
92 "EEE dd MMM yy HH:mm:ss z",
93 "EEE,dd-MMM-yy HH:mm:ss z",
94 "EEE,dd-MMM-yyyy HH:mm:ss z",
95 "EEE, dd-MM-yyyy HH:mm:ss z",
96 }
97 )
98 );
99 params.setParameter(HttpMethodParams.DATE_PATTERNS, datePatterns);
100
101
102 String agent = null;
103 try {
104 agent = System.getProperty("httpclient.useragent");
105 } catch (SecurityException ignore) {
106 }
107 if (agent != null) {
108 params.setParameter(HttpMethodParams.USER_AGENT, agent);
109 }
110
111
112 String preemptiveDefault = null;
113 try {
114 preemptiveDefault = System.getProperty("httpclient.authentication.preemptive");
115 } catch (SecurityException ignore) {
116 }
117 if (preemptiveDefault != null) {
118 preemptiveDefault = preemptiveDefault.trim().toLowerCase();
119 if (preemptiveDefault.equals("true")) {
120 params.setParameter(HttpClientParams.PREEMPTIVE_AUTHENTICATION, Boolean.TRUE);
121 } else if (preemptiveDefault.equals("false")) {
122 params.setParameter(HttpClientParams.PREEMPTIVE_AUTHENTICATION, Boolean.FALSE);
123 }
124 }
125
126
127 String defaultCookiePolicy = null;
128 try {
129 defaultCookiePolicy = System.getProperty("apache.commons.httpclient.cookiespec");
130 } catch (SecurityException ignore) {
131 }
132 if (defaultCookiePolicy != null) {
133 if ("COMPATIBILITY".equalsIgnoreCase(defaultCookiePolicy)) {
134 params.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
135 } else if ("NETSCAPE_DRAFT".equalsIgnoreCase(defaultCookiePolicy)) {
136 params.setCookiePolicy(CookiePolicy.NETSCAPE);
137 } else if ("RFC2109".equalsIgnoreCase(defaultCookiePolicy)) {
138 params.setCookiePolicy(CookiePolicy.RFC_2109);
139 }
140 }
141
142 return params;
143 }
144
145 }