1 /*
2 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpConstants.java,v 1.10 2003/04/17 11:34:19 olegk Exp $
3 * $Revision: 1.10 $
4 * $Date: 2003/04/17 11:34:19 $
5 *
6 * ====================================================================
7 *
8 * The Apache Software License, Version 1.1
9 *
10 * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
11 * reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
23 * distribution.
24 *
25 * 3. The end-user documentation included with the redistribution, if
26 * any, must include the following acknowlegement:
27 * "This product includes software developed by the
28 * Apache Software Foundation (http://www.apache.org/)."
29 * Alternately, this acknowlegement may appear in the software itself,
30 * if and wherever such third-party acknowlegements normally appear.
31 *
32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33 * Foundation" must not be used to endorse or promote products derived
34 * from this software without prior written permission. For written
35 * permission, please contact apache@apache.org.
36 *
37 * 5. Products derived from this software may not be called "Apache"
38 * nor may "Apache" appear in their names without prior written
39 * permission of the Apache Group.
40 *
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 * ====================================================================
54 *
55 * This software consists of voluntary contributions made by many
56 * individuals on behalf of the Apache Software Foundation. For more
57 * information on the Apache Software Foundation, please see
58 * <http://www.apache.org/>.
59 *
60 * [Additional notices, if required by prior licensing conditions]
61 *
62 */
63
64 package org.apache.commons.httpclient;
65
66 import java.io.UnsupportedEncodingException;
67
68 import org.apache.commons.logging.Log;
69 import org.apache.commons.logging.LogFactory;
70
71
72 /***
73 * HTTP content conversion routines.
74 *
75 * @author Oleg Kalnichevski
76 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
77 */
78 public class HttpConstants {
79
80 /*** Character set used to encode HTTP protocol elements */
81 public static final String HTTP_ELEMENT_CHARSET = "US-ASCII";
82
83 /*** Default content encoding chatset */
84 public static final String DEFAULT_CONTENT_CHARSET = "ISO-8859-1";
85
86 /*** Log object for this class. */
87 private static final Log LOG = LogFactory.getLog(HttpConstants.class);
88
89 /***
90 * Converts the specified string to a byte array of HTTP element characters.
91 * This method is to be used when encoding content of HTTP elements (such as
92 * request headers)
93 *
94 * @param data the string to be encoded
95 * @return The resulting byte array.
96 */
97 public static byte[] getBytes(final String data) {
98 if (data == null) {
99 throw new IllegalArgumentException("Parameter may not be null");
100 }
101
102 try {
103 return data.getBytes(HTTP_ELEMENT_CHARSET);
104 } catch (UnsupportedEncodingException e) {
105
106 if (LOG.isWarnEnabled()) {
107 LOG.warn("Unsupported encoding: "
108 + HTTP_ELEMENT_CHARSET
109 + ". System default encoding used");
110 }
111
112 return data.getBytes();
113 }
114 }
115
116 /***
117 * Converts the byte array of HTTP element characters to a string This
118 * method is to be used when decoding content of HTTP elements (such as
119 * response headers)
120 *
121 * @param data the byte array to be encoded
122 * @param offset the index of the first byte to encode
123 * @param length the number of bytes to encode
124 * @return The resulting string.
125 */
126 public static String getString(final byte[] data, int offset, int length) {
127
128 if (data == null) {
129 throw new IllegalArgumentException("Parameter may not be null");
130 }
131
132 try {
133 return new String(data, offset, length, HTTP_ELEMENT_CHARSET);
134 } catch (UnsupportedEncodingException e) {
135
136 if (LOG.isWarnEnabled()) {
137 LOG.warn("Unsupported encoding: "
138 + HTTP_ELEMENT_CHARSET
139 + ". System default encoding used");
140 }
141
142 return new String(data, offset, length);
143 }
144 }
145
146 /***
147 * Converts the byte array of HTTP element characters to a string This
148 * method is to be used when decoding content of HTTP elements (such as
149 * response headers)
150 *
151 * @param data the byte array to be encoded
152 * @return The resulting string.
153 */
154 public static String getString(final byte[] data) {
155 return getString(data, 0, data.length);
156 }
157
158 /***
159 * Converts the specified string to a byte array of HTTP content charachetrs
160 * This method is to be used when encoding content of HTTP request/response
161 * If the specified charset is not supported, default HTTP content encoding
162 * (ISO-8859-1) is applied
163 *
164 * @param data the string to be encoded
165 * @param charset the desired character encoding
166 * @return The resulting byte array.
167 */
168 public static byte[] getContentBytes(final String data, String charset) {
169
170 if (data == null) {
171 throw new IllegalArgumentException("Parameter may not be null");
172 }
173
174 if ((charset == null) || (charset.equals(""))) {
175 charset = DEFAULT_CONTENT_CHARSET;
176 }
177
178 try {
179 return data.getBytes(charset);
180 } catch (UnsupportedEncodingException e) {
181
182 if (LOG.isWarnEnabled()) {
183 LOG.warn("Unsupported encoding: "
184 + charset
185 + ". HTTP default encoding used");
186 }
187
188 try {
189 return data.getBytes(DEFAULT_CONTENT_CHARSET);
190 } catch (UnsupportedEncodingException e2) {
191
192 if (LOG.isWarnEnabled()) {
193 LOG.warn("Unsupported encoding: "
194 + DEFAULT_CONTENT_CHARSET
195 + ". System encoding used");
196 }
197
198 return data.getBytes();
199 }
200 }
201 }
202
203 /***
204 * Converts the byte array of HTTP content characters to a string This
205 * method is to be used when decoding content of HTTP request/response If
206 * the specified charset is not supported, default HTTP content encoding
207 * (ISO-8859-1) is applied
208 *
209 * @param data the byte array to be encoded
210 * @param offset the index of the first byte to encode
211 * @param length the number of bytes to encode
212 * @param charset the desired character encoding
213 * @return The result of the conversion.
214 */
215 public static String getContentString(
216 final byte[] data,
217 int offset,
218 int length,
219 String charset
220 ) {
221
222 if (data == null) {
223 throw new IllegalArgumentException("Parameter may not be null");
224 }
225
226 if ((charset == null) || (charset.equals(""))) {
227 charset = DEFAULT_CONTENT_CHARSET;
228 }
229
230 try {
231 return new String(data, offset, length, charset);
232 } catch (UnsupportedEncodingException e) {
233
234 if (LOG.isWarnEnabled()) {
235 LOG.warn("Unsupported encoding: "
236 + DEFAULT_CONTENT_CHARSET
237 + ". Default HTTP encoding used");
238 }
239
240 try {
241 return new String(data, offset, length, DEFAULT_CONTENT_CHARSET);
242 } catch (UnsupportedEncodingException e2) {
243
244 if (LOG.isWarnEnabled()) {
245 LOG.warn("Unsupported encoding: "
246 + DEFAULT_CONTENT_CHARSET
247 + ". System encoding used");
248 }
249
250 return new String(data, offset, length);
251 }
252 }
253 }
254
255
256 /***
257 * Converts the byte array of HTTP content characters to a string This
258 * method is to be used when decoding content of HTTP request/response If
259 * the specified charset is not supported, default HTTP content encoding
260 * (ISO-8859-1) is applied
261 *
262 * @param data the byte array to be encoded
263 * @param charset the desired character encoding
264 * @return The result of the conversion.
265 */
266 public static String getContentString(final byte[] data, String charset) {
267 return getContentString(data, 0, data.length, charset);
268 }
269
270 /***
271 * Converts the specified string to a byte array of HTTP content characters
272 * using default HTTP content encoding (ISO-8859-1) This method is to be
273 * used when encoding content of HTTP request/response
274 *
275 * @param data the string to be encoded
276 * @return The byte array as above.
277 */
278 public static byte[] getContentBytes(final String data) {
279 return getContentBytes(data, null);
280 }
281
282 /***
283 * Converts the byte array of HTTP content characters to a string using
284 * default HTTP content encoding (ISO-8859-1) This method is to be used when
285 * decoding content of HTTP request/response
286 *
287 * @param data the byte array to be encoded
288 * @param offset the index of the first byte to encode
289 * @param length the number of bytes to encode
290 * @return The string representation of the byte array.
291 */
292 public static String getContentString(final byte[] data, int offset, int length) {
293 return getContentString(data, offset, length, null);
294 }
295
296 /***
297 * Converts the byte array of HTTP content characters to a string using
298 * default HTTP content encoding (ISO-8859-1) This method is to be used when
299 * decoding content of HTTP request/response
300 *
301 * @param data the byte array to be encoded
302 * @return The string representation of the byte array.
303 */
304 public static String getContentString(final byte[] data) {
305 return getContentString(data, null);
306 }
307
308 /***
309 * Converts the specified string to byte array of ASCII characters.
310 *
311 * @param data the string to be encoded
312 * @return The string as a byte array.
313 */
314 public static byte[] getAsciiBytes(final String data) {
315
316 if (data == null) {
317 throw new IllegalArgumentException("Parameter may not be null");
318 }
319
320 try {
321 return data.getBytes("US-ASCII");
322 } catch (UnsupportedEncodingException e) {
323 throw new RuntimeException("HttpClient requires ASCII support");
324 }
325 }
326
327 /***
328 * Converts the byte array of ASCII characters to a string. This method is
329 * to be used when decoding content of HTTP elements (such as response
330 * headers)
331 *
332 * @param data the byte array to be encoded
333 * @param offset the index of the first byte to encode
334 * @param length the number of bytes to encode
335 * @return The string representation of the byte array
336 */
337 public static String getAsciiString(final byte[] data, int offset, int length) {
338
339 if (data == null) {
340 throw new IllegalArgumentException("Parameter may not be null");
341 }
342
343 try {
344 return new String(data, offset, length, "US-ASCII");
345 } catch (UnsupportedEncodingException e) {
346 throw new RuntimeException("HttpClient requires ASCII support");
347 }
348 }
349
350 /***
351 * Converts the byte array of ASCII characters to a string. This method is
352 * to be used when decoding content of HTTP elements (such as response
353 * headers)
354 *
355 * @param data the byte array to be encoded
356 * @return The string representation of the byte array
357 */
358 public static String getAsciiString(final byte[] data) {
359 return getAsciiString(data, 0, data.length);
360 }
361 }
This page was automatically generated by Maven