View Javadoc

1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/java/org/apache/commons/httpclient/params/DefaultHttpParamsFactory.java $
3    * $Revision: 1425331 $
4    * $Date: 2012-12-22 18:29:41 +0000 (Sat, 22 Dec 2012) $
5    *
6    * ====================================================================
7    *
8    *  Licensed to the Apache Software Foundation (ASF) under one or more
9    *  contributor license agreements.  See the NOTICE file distributed with
10   *  this work for additional information regarding copyright ownership.
11   *  The ASF licenses this file to You under the Apache License, Version 2.0
12   *  (the "License"); you may not use this file except in compliance with
13   *  the License.  You may obtain a copy of the License at
14   *
15   *      http://www.apache.org/licenses/LICENSE-2.0
16   *
17   *  Unless required by applicable law or agreed to in writing, software
18   *  distributed under the License is distributed on an "AS IS" BASIS,
19   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   *  See the License for the specific language governing permissions and
21   *  limitations under the License.
22   * ====================================================================
23   *
24   * This software consists of voluntary contributions made by many
25   * individuals on behalf of the Apache Software Foundation.  For more
26   * information on the Apache Software Foundation, please see
27   * <http://www.apache.org/>.
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      /* (non-Javadoc)
57       * @see org.apache.commons.httpclient.params.HttpParamsFactory#getDefaultParams()
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         // TODO: To be removed. Provided for backward compatibility
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         // TODO: To be removed. Provided for backward compatibility
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         // TODO: To be removed. Provided for backward compatibility
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 }