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.params;
29  
30  import org.apache.http.HttpVersion;
31  import org.apache.http.ProtocolVersion;
32  import org.apache.http.protocol.HTTP;
33  
34  import java.nio.charset.CodingErrorAction;
35  
36  /**
37   * Utility class for accessing protocol parameters in {@link HttpParams}.
38   *
39   * @since 4.0
40   *
41   * @see CoreProtocolPNames
42   */
43  public final class HttpProtocolParams implements CoreProtocolPNames {
44  
45      private HttpProtocolParams() {
46          super();
47      }
48  
49      /**
50       * Obtains value of the {@link CoreProtocolPNames#HTTP_ELEMENT_CHARSET} parameter.
51       * If not set, defaults to <code>US-ASCII</code>.
52       *
53       * @param params HTTP parameters.
54       * @return HTTP element charset.
55       */
56      public static String getHttpElementCharset(final HttpParams params) {
57          if (params == null) {
58              throw new IllegalArgumentException("HTTP parameters may not be null");
59          }
60          String charset = (String) params.getParameter
61              (CoreProtocolPNames.HTTP_ELEMENT_CHARSET);
62          if (charset == null) {
63              charset = HTTP.DEF_PROTOCOL_CHARSET.name();
64          }
65          return charset;
66      }
67  
68      /**
69       * Sets value of the {@link CoreProtocolPNames#HTTP_ELEMENT_CHARSET} parameter.
70       *
71       * @param params HTTP parameters.
72       * @param charset HTTP element charset.
73       */
74      public static void setHttpElementCharset(final HttpParams params, final String charset) {
75          if (params == null) {
76              throw new IllegalArgumentException("HTTP parameters may not be null");
77          }
78          params.setParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET, charset);
79      }
80  
81      /**
82       * Obtains value of the {@link CoreProtocolPNames#HTTP_CONTENT_CHARSET} parameter.
83       * If not set, defaults to <code>ISO-8859-1</code>.
84       *
85       * @param params HTTP parameters.
86       * @return HTTP content charset.
87       */
88      public static String getContentCharset(final HttpParams params) {
89          if (params == null) {
90              throw new IllegalArgumentException("HTTP parameters may not be null");
91          }
92          String charset = (String) params.getParameter
93              (CoreProtocolPNames.HTTP_CONTENT_CHARSET);
94          if (charset == null) {
95              charset = HTTP.DEF_CONTENT_CHARSET.name();
96          }
97          return charset;
98      }
99  
100     /**
101      * Sets value of the {@link CoreProtocolPNames#HTTP_CONTENT_CHARSET} parameter.
102      *
103      * @param params HTTP parameters.
104      * @param charset HTTP content charset.
105      */
106     public static void setContentCharset(final HttpParams params, final String charset) {
107         if (params == null) {
108             throw new IllegalArgumentException("HTTP parameters may not be null");
109         }
110         params.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, charset);
111     }
112 
113     /**
114      * Obtains value of the {@link CoreProtocolPNames#PROTOCOL_VERSION} parameter.
115      * If not set, defaults to {@link HttpVersion#HTTP_1_1}.
116      *
117      * @param params HTTP parameters.
118      * @return HTTP protocol version.
119      */
120     public static ProtocolVersion getVersion(final HttpParams params) {
121         if (params == null) {
122             throw new IllegalArgumentException("HTTP parameters may not be null");
123         }
124         Object param = params.getParameter
125             (CoreProtocolPNames.PROTOCOL_VERSION);
126         if (param == null) {
127             return HttpVersion.HTTP_1_1;
128         }
129         return (ProtocolVersion)param;
130     }
131 
132     /**
133      * Sets value of the {@link CoreProtocolPNames#PROTOCOL_VERSION} parameter.
134      *
135      * @param params HTTP parameters.
136      * @param version HTTP protocol version.
137      */
138     public static void setVersion(final HttpParams params, final ProtocolVersion version) {
139         if (params == null) {
140             throw new IllegalArgumentException("HTTP parameters may not be null");
141         }
142         params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, version);
143     }
144 
145     /**
146      * Obtains value of the {@link CoreProtocolPNames#USER_AGENT} parameter.
147      * If not set, returns <code>null</code>.
148      *
149      * @param params HTTP parameters.
150      * @return User agent string.
151      */
152     public static String getUserAgent(final HttpParams params) {
153         if (params == null) {
154             throw new IllegalArgumentException("HTTP parameters may not be null");
155         }
156         return (String) params.getParameter(CoreProtocolPNames.USER_AGENT);
157     }
158 
159     /**
160      * Sets value of the {@link CoreProtocolPNames#USER_AGENT} parameter.
161      *
162      * @param params HTTP parameters.
163      * @param useragent User agent string.
164      */
165     public static void setUserAgent(final HttpParams params, final String useragent) {
166         if (params == null) {
167             throw new IllegalArgumentException("HTTP parameters may not be null");
168         }
169         params.setParameter(CoreProtocolPNames.USER_AGENT, useragent);
170     }
171 
172     /**
173      * Obtains value of the {@link CoreProtocolPNames#USE_EXPECT_CONTINUE} parameter.
174      * If not set, returns <code>false</code>.
175      *
176      * @param params HTTP parameters.
177      * @return User agent string.
178      */
179     public static boolean useExpectContinue(final HttpParams params) {
180         if (params == null) {
181             throw new IllegalArgumentException("HTTP parameters may not be null");
182         }
183         return params.getBooleanParameter
184             (CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
185     }
186 
187     /**
188      * Sets value of the {@link CoreProtocolPNames#USE_EXPECT_CONTINUE} parameter.
189      *
190      * @param params HTTP parameters.
191      * @param b expect-continue flag.
192      */
193     public static void setUseExpectContinue(final HttpParams params, boolean b) {
194         if (params == null) {
195             throw new IllegalArgumentException("HTTP parameters may not be null");
196         }
197         params.setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, b);
198     }
199 
200     /**
201      * Obtains value of the {@link CoreProtocolPNames#HTTP_MALFORMED_INPUT_ACTION} parameter.
202      * @param params HTTP parameters.
203      * @return Action to perform upon receiving a malformed input
204      *
205      * @since 4.2
206      */
207     public static CodingErrorAction getMalformedInputAction(final HttpParams params) {
208         if (params == null) {
209             throw new IllegalArgumentException("HTTP parameters may not be null");
210         }
211         Object param = params.getParameter(CoreProtocolPNames.HTTP_MALFORMED_INPUT_ACTION);
212         if (param == null) {
213             // the default CodingErrorAction
214             return CodingErrorAction.REPORT;
215         }
216         return (CodingErrorAction) param;
217     }
218 
219     /**
220      * Sets value of the {@link CoreProtocolPNames#HTTP_MALFORMED_INPUT_ACTION} parameter.
221      * @param params HTTP parameters
222      * @param action action to perform on malformed inputs
223      *
224      * @since 4.2
225      */
226     public static void setMalformedInputAction(final HttpParams params, CodingErrorAction action) {
227         if (params == null) {
228             throw new IllegalArgumentException("HTTP parameters may not be null");
229         }
230         params.setParameter(CoreProtocolPNames.HTTP_MALFORMED_INPUT_ACTION, action);
231     }
232 
233     /**
234      * Obtains the value of the  {@link CoreProtocolPNames#HTTP_UNMAPPABLE_INPUT_ACTION} parameter.
235      * @param params HTTP parameters
236      * @return Action to perform upon receiving a unmapped input
237      *
238      * @since 4.2
239      */
240     public static CodingErrorAction getUnmappableInputAction(final HttpParams params) {
241         if (params == null) {
242             throw new IllegalArgumentException("HTTP parameters may not be null");
243         }
244         Object param = params.getParameter(CoreProtocolPNames.HTTP_UNMAPPABLE_INPUT_ACTION);
245         if (param == null) {
246             // the default CodingErrorAction
247             return CodingErrorAction.REPORT;
248         }
249         return (CodingErrorAction) param;
250     }
251 
252     /**
253      * Sets the value of the {@link CoreProtocolPNames#HTTP_UNMAPPABLE_INPUT_ACTION} parameter.
254      * @param params HTTP parameters
255      * @param action action to perform on un mappable inputs
256      *
257      * @since 4.2
258      */
259     public static void setUnmappableInputAction(final HttpParams params, CodingErrorAction action) {
260         if (params == null) {
261             throw new IllegalArgumentException("HTTP parameters may no be null");
262         }
263         params.setParameter(CoreProtocolPNames.HTTP_UNMAPPABLE_INPUT_ACTION, action);
264     }
265 }