1. /*
  2. * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/StringRequestEntity.java,v 1.3 2004/07/03 14:27:03 olegk Exp $
  3. * $Revision: 1.3 $
  4. * $Date: 2004/07/03 14:27:03 $
  5. *
  6. * ====================================================================
  7. *
  8. * Copyright 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. * [Additional notices, if required by prior licensing conditions]
  29. *
  30. */
  31. package org.apache.commons.httpclient.methods;
  32. import java.io.IOException;
  33. import java.io.OutputStream;
  34. import java.io.UnsupportedEncodingException;
  35. import org.apache.commons.httpclient.HeaderElement;
  36. import org.apache.commons.httpclient.NameValuePair;
  37. /**
  38. * A RequestEntity that contains a String.
  39. *
  40. * @since 3.0
  41. */
  42. public class StringRequestEntity implements RequestEntity {
  43. /** The content */
  44. private byte[] content;
  45. /** The charset */
  46. private String charset;
  47. /** The content type (i.e. text/html; charset=EUC-JP). */
  48. private String contentType;
  49. /**
  50. * Creates a new entity with the given content
  51. *
  52. * @param content The content to set.
  53. */
  54. public StringRequestEntity(String content) {
  55. super();
  56. if (content == null) {
  57. throw new IllegalArgumentException("The content cannot be null");
  58. }
  59. this.contentType = null;
  60. this.charset = null;
  61. this.content = content.getBytes();
  62. }
  63. /**
  64. * Creates a new entity with the given content, content type, and charset.
  65. *
  66. * @param content The content to set.
  67. * @param contentType The type of the content, or <code>null</code>. The value retured
  68. * by {@link #getContentType()}. If this content type contains a charset and the charset
  69. * parameter is null, the content's type charset will be used.
  70. * @param charset The charset of the content, or <code>null</code>. Used to convert the
  71. * content to bytes. If the content type does not contain a charset and charset is not null,
  72. * then the charset will be appended to the content type.
  73. */
  74. public StringRequestEntity(String content, String contentType, String charset)
  75. throws UnsupportedEncodingException {
  76. super();
  77. if (content == null) {
  78. throw new IllegalArgumentException("The content cannot be null");
  79. }
  80. this.contentType = contentType;
  81. this.charset = charset;
  82. // resolve the content type and the charset
  83. if (contentType != null) {
  84. HeaderElement[] values = HeaderElement.parseElements(contentType);
  85. NameValuePair charsetPair = null;
  86. for (int i = 0; i < values.length; i++) {
  87. if ((charsetPair = values[i].getParameterByName("charset")) != null) {
  88. // charset found
  89. break;
  90. }
  91. }
  92. if (charset == null && charsetPair != null) {
  93. // use the charset from the content type
  94. this.charset = charsetPair.getValue();
  95. } else if (charset != null && charsetPair == null) {
  96. // append the charset to the content type
  97. this.contentType = contentType + "; charset=" + charset;
  98. }
  99. }
  100. if (this.charset != null) {
  101. this.content = content.getBytes(this.charset);
  102. } else {
  103. this.content = content.getBytes();
  104. }
  105. }
  106. /* (non-Javadoc)
  107. * @see org.apache.commons.httpclient.methods.RequestEntity#getContentType()
  108. */
  109. public String getContentType() {
  110. return contentType;
  111. }
  112. /**
  113. * @return <code>true</code>
  114. */
  115. public boolean isRepeatable() {
  116. return true;
  117. }
  118. /* (non-Javadoc)
  119. * @see org.apache.commons.httpclient.RequestEntity#writeRequest(java.io.OutputStream)
  120. */
  121. public void writeRequest(OutputStream out) throws IOException {
  122. if (out == null) {
  123. throw new IllegalArgumentException("Output stream may not be null");
  124. }
  125. out.write(this.content);
  126. out.flush();
  127. }
  128. /**
  129. * @return The length of the content.
  130. */
  131. public long getContentLength() {
  132. return this.content.length;
  133. }
  134. /**
  135. * @return Returns the content.
  136. */
  137. public String getContent() {
  138. if (this.charset != null) {
  139. try {
  140. return new String(this.content, this.charset);
  141. } catch (UnsupportedEncodingException e) {
  142. return new String(this.content);
  143. }
  144. } else {
  145. return new String(this.content);
  146. }
  147. }
  148. /**
  149. * @return Returns the charset used to convert the content to bytes. <code>null</code> if
  150. * no charset as been specified.
  151. */
  152. public String getCharset() {
  153. return charset;
  154. }
  155. }