View Javadoc

1   /*
2    * ====================================================================
3    *
4    *  Licensed to the Apache Software Foundation (ASF) under one or more
5    *  contributor license agreements.  See the NOTICE file distributed with
6    *  this work for additional information regarding copyright ownership.
7    *  The ASF licenses this file to You under the Apache License, Version 2.0
8    *  (the "License"); you may not use this file except in compliance with
9    *  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, software
14   *  distributed under the License is distributed on an "AS IS" BASIS,
15   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *  See the License for the specific language governing permissions and
17   *  limitations under the License.
18   * ====================================================================
19   *
20   * This software consists of voluntary contributions made by many
21   * individuals on behalf of the Apache Software Foundation.  For more
22   * information on the Apache Software Foundation, please see
23   * <http://www.apache.org/>.
24   *
25   */
26  
27  package org.apache.http.client.params;
28  
29  import org.apache.http.annotation.Immutable;
30  
31  import org.apache.http.params.HttpConnectionParams;
32  import org.apache.http.params.HttpParams;
33  
34  /**
35   * An adaptor for manipulating HTTP client parameters in {@link HttpParams}.
36   *
37   * @since 4.0
38   */
39  @Immutable
40  public class HttpClientParams {
41  
42      private HttpClientParams() {
43          super();
44      }
45  
46      public static boolean isRedirecting(final HttpParams params) {
47          if (params == null) {
48              throw new IllegalArgumentException("HTTP parameters may not be null");
49          }
50          return params.getBooleanParameter
51              (ClientPNames.HANDLE_REDIRECTS, true);
52      }
53  
54      public static void setRedirecting(final HttpParams params, boolean value) {
55          if (params == null) {
56              throw new IllegalArgumentException("HTTP parameters may not be null");
57          }
58          params.setBooleanParameter
59              (ClientPNames.HANDLE_REDIRECTS, value);
60      }
61  
62      public static boolean isAuthenticating(final HttpParams params) {
63          if (params == null) {
64              throw new IllegalArgumentException("HTTP parameters may not be null");
65          }
66          return params.getBooleanParameter
67              (ClientPNames.HANDLE_AUTHENTICATION, true);
68      }
69  
70      public static void setAuthenticating(final HttpParams params, boolean value) {
71          if (params == null) {
72              throw new IllegalArgumentException("HTTP parameters may not be null");
73          }
74          params.setBooleanParameter
75              (ClientPNames.HANDLE_AUTHENTICATION, value);
76      }
77  
78      public static String getCookiePolicy(final HttpParams params) {
79          if (params == null) {
80              throw new IllegalArgumentException("HTTP parameters may not be null");
81          }
82          String cookiePolicy = (String)
83              params.getParameter(ClientPNames.COOKIE_POLICY);
84          if (cookiePolicy == null) {
85              return CookiePolicy.BEST_MATCH;
86          }
87          return cookiePolicy;
88      }
89  
90      public static void setCookiePolicy(final HttpParams params, final String cookiePolicy) {
91          if (params == null) {
92              throw new IllegalArgumentException("HTTP parameters may not be null");
93          }
94          params.setParameter(ClientPNames.COOKIE_POLICY, cookiePolicy);
95      }
96  
97      /**
98       * Set the parameter {@code ClientPNames.CONN_MANAGER_TIMEOUT}.
99       *
100      * @since 4.2
101      */
102     public static void setConnectionManagerTimeout(final HttpParams params, long timeout) {
103         if (params == null) {
104             throw new IllegalArgumentException("HTTP parameters may not be null");
105         }
106         params.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, timeout);
107     }
108 
109     /**
110      * Get the connectiion manager timeout value.
111      * This is defined by the parameter {@code ClientPNames.CONN_MANAGER_TIMEOUT}.
112      * Failing that it uses the parameter {@code CoreConnectionPNames.CONNECTION_TIMEOUT}
113      * which defaults to 0 if not defined.
114      *
115      * @since 4.2
116      * @return the timeout value
117      */
118     public static long getConnectionManagerTimeout(final HttpParams params) {
119         if (params == null) {
120             throw new IllegalArgumentException("HTTP parameters may not be null");
121         }
122         Long timeout = (Long) params.getParameter(ClientPNames.CONN_MANAGER_TIMEOUT);
123         if (timeout != null) {
124             return timeout.longValue();
125         }
126         return HttpConnectionParams.getConnectionTimeout(params);
127     }
128 
129 }