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