View Javadoc

1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/java/org/apache/commons/httpclient/HttpConstants.java $
3    * $Revision$
4    * $Date$
5    *
6    * ====================================================================
7    *
8    *  Licensed to the Apache Software Foundation (ASF) under one or more
9    *  contributor license agreements.  See the NOTICE file distributed with
10   *  this work for additional information regarding copyright ownership.
11   *  The ASF licenses this file to You under the Apache License, Version 2.0
12   *  (the "License"); you may not use this file except in compliance with
13   *  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, software
18   *  distributed under the License is distributed on an "AS IS" BASIS,
19   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   *  See the License for the specific language governing permissions and
21   *  limitations under the License.
22   * ====================================================================
23   *
24   * This software consists of voluntary contributions made by many
25   * individuals on behalf of the Apache Software Foundation.  For more
26   * information on the Apache Software Foundation, please see
27   * <http://www.apache.org/>.
28   *
29   */
30  
31  package org.apache.commons.httpclient;
32  
33  import java.io.UnsupportedEncodingException;
34  
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  
38  
39  /***
40   * HTTP content conversion routines.
41   *
42   * @author Oleg Kalnichevski
43   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
44   * 
45   * @deprecated use EncodingUtil class
46   */
47  public class HttpConstants {
48  
49      /*** Character set used to encode HTTP protocol elements */
50      public static final String HTTP_ELEMENT_CHARSET = "US-ASCII";
51  
52      /*** Default content encoding chatset */
53      public static final String DEFAULT_CONTENT_CHARSET = "ISO-8859-1";
54  
55      /*** Log object for this class. */
56      private static final Log LOG = LogFactory.getLog(HttpConstants.class);
57  
58      /***
59       * Converts the specified string to a byte array of HTTP element characters.
60       * This method is to be used when encoding content of HTTP elements (such as
61       * request headers)
62       *
63       * @param data the string to be encoded
64       * @return The resulting byte array.
65       */
66      public static byte[] getBytes(final String data) {
67          if (data == null) {
68              throw new IllegalArgumentException("Parameter may not be null");
69          }
70  
71          try {
72              return data.getBytes(HTTP_ELEMENT_CHARSET);
73          } catch (UnsupportedEncodingException e) {
74  
75              if (LOG.isWarnEnabled()) {
76                  LOG.warn("Unsupported encoding: " 
77                      + HTTP_ELEMENT_CHARSET 
78                      + ". System default encoding used");
79              }
80  
81              return data.getBytes();
82          }
83      }
84  
85      /***
86       * Converts the byte array of HTTP element characters to a string This
87       * method is to be used when decoding content of HTTP elements (such as
88       * response headers)
89       *
90       * @param data the byte array to be encoded
91       * @param offset the index of the first byte to encode
92       * @param length the number of bytes to encode 
93       * @return The resulting string.
94       */
95      public static String getString(final byte[] data, int offset, int length) {
96  
97          if (data == null) {
98              throw new IllegalArgumentException("Parameter may not be null");
99          }
100 
101         try {
102             return new String(data, offset, length, HTTP_ELEMENT_CHARSET);
103         } catch (UnsupportedEncodingException e) {
104 
105             if (LOG.isWarnEnabled()) {
106                 LOG.warn("Unsupported encoding: " 
107                     + HTTP_ELEMENT_CHARSET 
108                     + ". System default encoding used");
109             }
110 
111             return new String(data, offset, length);
112         }
113     }
114 
115     /***
116      * Converts the byte array of HTTP element characters to a string This
117      * method is to be used when decoding content of HTTP elements (such as
118      * response headers)
119      *
120      * @param data the byte array to be encoded
121      * @return The resulting string.
122      */
123     public static String getString(final byte[] data) {
124         return getString(data, 0, data.length);
125     }
126 
127     /***
128      * Converts the specified string to a byte array of HTTP content charachetrs
129      * This method is to be used when encoding content of HTTP request/response
130      * If the specified charset is not supported, default HTTP content encoding
131      * (ISO-8859-1) is applied
132      *
133      * @param data the string to be encoded
134      * @param charset the desired character encoding
135      * @return The resulting byte array.
136      */
137     public static byte[] getContentBytes(final String data, String charset) {
138 
139         if (data == null) {
140             throw new IllegalArgumentException("Parameter may not be null");
141         }
142 
143         if ((charset == null) || (charset.equals(""))) {
144             charset = DEFAULT_CONTENT_CHARSET;
145         }
146 
147         try {
148             return data.getBytes(charset);
149         } catch (UnsupportedEncodingException e) {
150 
151             if (LOG.isWarnEnabled()) {
152                 LOG.warn("Unsupported encoding: " 
153                     + charset 
154                     + ". HTTP default encoding used");
155             }
156 
157             try {
158                 return data.getBytes(DEFAULT_CONTENT_CHARSET);
159             } catch (UnsupportedEncodingException e2) {
160 
161                 if (LOG.isWarnEnabled()) {
162                     LOG.warn("Unsupported encoding: " 
163                         + DEFAULT_CONTENT_CHARSET 
164                         + ". System encoding used");
165                 }
166 
167                 return data.getBytes();
168             }
169         }
170     }
171 
172     /***
173      * Converts the byte array of HTTP content characters to a string This
174      * method is to be used when decoding content of HTTP request/response If
175      * the specified charset is not supported, default HTTP content encoding
176      * (ISO-8859-1) is applied
177      *
178      * @param data the byte array to be encoded
179      * @param offset the index of the first byte to encode
180      * @param length the number of bytes to encode 
181      * @param charset the desired character encoding
182      * @return The result of the conversion.
183      */
184     public static String getContentString(
185         final byte[] data, 
186         int offset, 
187         int length, 
188         String charset
189     ) {
190 
191         if (data == null) {
192             throw new IllegalArgumentException("Parameter may not be null");
193         }
194 
195         if ((charset == null) || (charset.equals(""))) {
196             charset = DEFAULT_CONTENT_CHARSET;
197         }
198 
199         try {
200             return new String(data, offset, length, charset);
201         } catch (UnsupportedEncodingException e) {
202 
203             if (LOG.isWarnEnabled()) {
204                 LOG.warn("Unsupported encoding: " + charset + ". Default HTTP encoding used");
205             }
206 
207             try {
208                 return new String(data, offset, length, DEFAULT_CONTENT_CHARSET);
209             } catch (UnsupportedEncodingException e2) {
210 
211                 if (LOG.isWarnEnabled()) {
212                     LOG.warn("Unsupported encoding: " 
213                         + DEFAULT_CONTENT_CHARSET 
214                         + ". System encoding used");
215                 }
216 
217                 return new String(data, offset, length);
218             }
219         }
220     }
221 
222 
223     /***
224      * Converts the byte array of HTTP content characters to a string This
225      * method is to be used when decoding content of HTTP request/response If
226      * the specified charset is not supported, default HTTP content encoding
227      * (ISO-8859-1) is applied
228      *
229      * @param data the byte array to be encoded
230      * @param charset the desired character encoding
231      * @return The result of the conversion.
232      */
233     public static String getContentString(final byte[] data, String charset) {
234         return getContentString(data, 0, data.length, charset);
235     }
236 
237     /***
238      * Converts the specified string to a byte array of HTTP content characters
239      * using default HTTP content encoding (ISO-8859-1) This method is to be
240      * used when encoding content of HTTP request/response
241      *
242      * @param data the string to be encoded
243      * @return The byte array as above.
244      */
245     public static byte[] getContentBytes(final String data) {
246         return getContentBytes(data, null);
247     }
248 
249     /***
250      * Converts the byte array of HTTP content characters to a string using
251      * default HTTP content encoding (ISO-8859-1) This method is to be used when
252      * decoding content of HTTP request/response
253      *
254      * @param data the byte array to be encoded
255      * @param offset the index of the first byte to encode
256      * @param length the number of bytes to encode 
257      * @return The string representation of the byte array.
258      */
259     public static String getContentString(final byte[] data, int offset, int length) {
260         return getContentString(data, offset, length, null);
261     }
262 
263     /***
264      * Converts the byte array of HTTP content characters to a string using
265      * default HTTP content encoding (ISO-8859-1) This method is to be used when
266      * decoding content of HTTP request/response
267      *
268      * @param data the byte array to be encoded
269      * @return The string representation of the byte array.
270      */
271     public static String getContentString(final byte[] data) {
272         return getContentString(data, null);
273     }
274 
275     /***
276      * Converts the specified string to byte array of ASCII characters.
277      *
278      * @param data the string to be encoded
279      * @return The string as a byte array.
280      */
281     public static byte[] getAsciiBytes(final String data) {
282 
283         if (data == null) {
284             throw new IllegalArgumentException("Parameter may not be null");
285         }
286 
287         try {
288             return data.getBytes("US-ASCII");
289         } catch (UnsupportedEncodingException e) {
290             throw new RuntimeException("HttpClient requires ASCII support");
291         }
292     }
293 
294     /***
295      * Converts the byte array of ASCII characters to a string. This method is
296      * to be used when decoding content of HTTP elements (such as response
297      * headers)
298      *
299      * @param data the byte array to be encoded
300      * @param offset the index of the first byte to encode
301      * @param length the number of bytes to encode 
302      * @return The string representation of the byte array
303      */
304     public static String getAsciiString(final byte[] data, int offset, int length) {
305 
306         if (data == null) {
307             throw new IllegalArgumentException("Parameter may not be null");
308         }
309 
310         try {
311             return new String(data, offset, length, "US-ASCII");
312         } catch (UnsupportedEncodingException e) {
313             throw new RuntimeException("HttpClient requires ASCII support");
314         }
315     }
316 
317     /***
318      * Converts the byte array of ASCII characters to a string. This method is
319      * to be used when decoding content of HTTP elements (such as response
320      * headers)
321      *
322      * @param data the byte array to be encoded
323      * @return The string representation of the byte array
324      */
325     public static String getAsciiString(final byte[] data) {
326         return getAsciiString(data, 0, data.length);
327     }
328 }