1 /*
2 * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0.1/httpcore/src/main/java/org/apache/http/params/HttpProtocolParams.java $
3 * $Revision: 744530 $
4 * $Date: 2009-02-14 18:09:27 +0100 (Sat, 14 Feb 2009) $
5 *
6 * ====================================================================
7 * Licensed to the Apache Software Foundation (ASF) under one
8 * or more contributor license agreements. See the NOTICE file
9 * distributed with this work for additional information
10 * regarding copyright ownership. The ASF licenses this file
11 * to you under the Apache License, Version 2.0 (the
12 * "License"); you may not use this file except in compliance
13 * with 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,
18 * software distributed under the License is distributed on an
19 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20 * KIND, either express or implied. See the License for the
21 * specific language governing permissions and limitations
22 * under the License.
23 * ====================================================================
24 *
25 * This software consists of voluntary contributions made by many
26 * individuals on behalf of the Apache Software Foundation. For more
27 * information on the Apache Software Foundation, please see
28 * <http://www.apache.org/>.
29 *
30 */
31
32 package org.apache.http.params;
33
34 import org.apache.http.HttpVersion;
35 import org.apache.http.ProtocolVersion;
36 import org.apache.http.protocol.HTTP;
37
38 /**
39 * Utility class for accessing protocol parameters in {@link HttpParams}.
40 *
41 *
42 * @version $Revision: 744530 $
43 *
44 * @since 4.0
45 *
46 * @see CoreProtocolPNames
47 */
48 public final class HttpProtocolParams implements CoreProtocolPNames {
49
50 private HttpProtocolParams() {
51 super();
52 }
53
54 /**
55 * Obtains value of the {@link CoreProtocolPNames#HTTP_ELEMENT_CHARSET} parameter.
56 * If not set, defaults to <code>US-ASCII</code>.
57 *
58 * @param params HTTP parameters.
59 * @return HTTP element charset.
60 */
61 public static String getHttpElementCharset(final HttpParams params) {
62 if (params == null) {
63 throw new IllegalArgumentException("HTTP parameters may not be null");
64 }
65 String charset = (String) params.getParameter
66 (CoreProtocolPNames.HTTP_ELEMENT_CHARSET);
67 if (charset == null) {
68 charset = HTTP.DEFAULT_PROTOCOL_CHARSET;
69 }
70 return charset;
71 }
72
73 /**
74 * Sets value of the {@link CoreProtocolPNames#HTTP_ELEMENT_CHARSET} parameter.
75 *
76 * @param params HTTP parameters.
77 * @param charset HTTP element charset.
78 */
79 public static void setHttpElementCharset(final HttpParams params, final String charset) {
80 if (params == null) {
81 throw new IllegalArgumentException("HTTP parameters may not be null");
82 }
83 params.setParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET, charset);
84 }
85
86 /**
87 * Obtains value of the {@link CoreProtocolPNames#HTTP_CONTENT_CHARSET} parameter.
88 * If not set, defaults to <code>ISO-8859-1</code>.
89 *
90 * @param params HTTP parameters.
91 * @return HTTP content charset.
92 */
93 public static String getContentCharset(final HttpParams params) {
94 if (params == null) {
95 throw new IllegalArgumentException("HTTP parameters may not be null");
96 }
97 String charset = (String) params.getParameter
98 (CoreProtocolPNames.HTTP_CONTENT_CHARSET);
99 if (charset == null) {
100 charset = HTTP.DEFAULT_CONTENT_CHARSET;
101 }
102 return charset;
103 }
104
105 /**
106 * Sets value of the {@link CoreProtocolPNames#HTTP_CONTENT_CHARSET} parameter.
107 *
108 * @param params HTTP parameters.
109 * @param charset HTTP content charset.
110 */
111 public static void setContentCharset(final HttpParams params, final String charset) {
112 if (params == null) {
113 throw new IllegalArgumentException("HTTP parameters may not be null");
114 }
115 params.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, charset);
116 }
117
118 /**
119 * Obtains value of the {@link CoreProtocolPNames#PROTOCOL_VERSION} parameter.
120 * If not set, defaults to {@link HttpVersion#HTTP_1_1}.
121 *
122 * @param params HTTP parameters.
123 * @return HTTP protocol version.
124 */
125 public static ProtocolVersion getVersion(final HttpParams params) {
126 if (params == null) {
127 throw new IllegalArgumentException("HTTP parameters may not be null");
128 }
129 Object param = params.getParameter
130 (CoreProtocolPNames.PROTOCOL_VERSION);
131 if (param == null) {
132 return HttpVersion.HTTP_1_1;
133 }
134 return (ProtocolVersion)param;
135 }
136
137 /**
138 * Sets value of the {@link CoreProtocolPNames#PROTOCOL_VERSION} parameter.
139 *
140 * @param params HTTP parameters.
141 * @param version HTTP protocol version.
142 */
143 public static void setVersion(final HttpParams params, final ProtocolVersion version) {
144 if (params == null) {
145 throw new IllegalArgumentException("HTTP parameters may not be null");
146 }
147 params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, version);
148 }
149
150 /**
151 * Obtains value of the {@link CoreProtocolPNames#USER_AGENT} parameter.
152 * If not set, returns <code>null</code>.
153 *
154 * @param params HTTP parameters.
155 * @return User agent string.
156 */
157 public static String getUserAgent(final HttpParams params) {
158 if (params == null) {
159 throw new IllegalArgumentException("HTTP parameters may not be null");
160 }
161 return (String) params.getParameter(CoreProtocolPNames.USER_AGENT);
162 }
163
164 /**
165 * Sets value of the {@link CoreProtocolPNames#USER_AGENT} parameter.
166 *
167 * @param params HTTP parameters.
168 * @param useragent User agent string.
169 */
170 public static void setUserAgent(final HttpParams params, final String useragent) {
171 if (params == null) {
172 throw new IllegalArgumentException("HTTP parameters may not be null");
173 }
174 params.setParameter(CoreProtocolPNames.USER_AGENT, useragent);
175 }
176
177 /**
178 * Obtains value of the {@link CoreProtocolPNames#USE_EXPECT_CONTINUE} parameter.
179 * If not set, returns <code>false</code>.
180 *
181 * @param params HTTP parameters.
182 * @return User agent string.
183 */
184 public static boolean useExpectContinue(final HttpParams params) {
185 if (params == null) {
186 throw new IllegalArgumentException("HTTP parameters may not be null");
187 }
188 return params.getBooleanParameter
189 (CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
190 }
191
192 /**
193 * Sets value of the {@link CoreProtocolPNames#USE_EXPECT_CONTINUE} parameter.
194 *
195 * @param params HTTP parameters.
196 * @param b expect-continue flag.
197 */
198 public static void setUseExpectContinue(final HttpParams params, boolean b) {
199 if (params == null) {
200 throw new IllegalArgumentException("HTTP parameters may not be null");
201 }
202 params.setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, b);
203 }
204
205 }