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