1. /*
  2. * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/multipart/StringPart.java,v 1.11 2004/04/18 23:51:37 jsdever Exp $
  3. * $Revision: 1.11 $
  4. * $Date: 2004/04/18 23:51:37 $
  5. *
  6. * ====================================================================
  7. *
  8. * Copyright 2002-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.methods.multipart;
  30. import java.io.OutputStream;
  31. import java.io.IOException;
  32. import org.apache.commons.httpclient.util.EncodingUtil;
  33. import org.apache.commons.logging.Log;
  34. import org.apache.commons.logging.LogFactory;
  35. /**
  36. * Simple string parameter for a multipart post
  37. *
  38. * @author <a href="mailto:mattalbright@yahoo.com">Matthew Albright</a>
  39. * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
  40. * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
  41. * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
  42. *
  43. * @since 2.0
  44. */
  45. public class StringPart extends PartBase {
  46. /** Log object for this class. */
  47. private static final Log LOG = LogFactory.getLog(StringPart.class);
  48. /** Default content encoding of string parameters. */
  49. public static final String DEFAULT_CONTENT_TYPE = "text/plain";
  50. /** Default charset of string parameters*/
  51. public static final String DEFAULT_CHARSET = "US-ASCII";
  52. /** Default transfer encoding of string parameters*/
  53. public static final String DEFAULT_TRANSFER_ENCODING = "8bit";
  54. /** Contents of this StringPart. */
  55. private byte[] content;
  56. /** The String value of this part. */
  57. private String value;
  58. /**
  59. * Constructor.
  60. *
  61. * @param name The name of the part
  62. * @param value the string to post
  63. * @param charset the charset to be used to encode the string, if <code>null</code>
  64. * the {@link #DEFAULT_CHARSET default} is used
  65. */
  66. public StringPart(String name, String value, String charset) {
  67. super(
  68. name,
  69. DEFAULT_CONTENT_TYPE,
  70. charset == null ? DEFAULT_CHARSET : charset,
  71. DEFAULT_TRANSFER_ENCODING
  72. );
  73. if (value == null) {
  74. throw new IllegalArgumentException("Value may not be null");
  75. }
  76. if (value.indexOf(0) != -1) {
  77. // See RFC 2048, 2.8. "8bit Data"
  78. throw new IllegalArgumentException("NULs may not be present in string parts");
  79. }
  80. this.value = value;
  81. }
  82. /**
  83. * Constructor.
  84. *
  85. * @param name The name of the part
  86. * @param value the string to post
  87. */
  88. public StringPart(String name, String value) {
  89. this(name, value, null);
  90. }
  91. /**
  92. * Gets the content in bytes. Bytes are lazily created to allow the charset to be changed
  93. * after the part is created.
  94. *
  95. * @return the content in bytes
  96. */
  97. private byte[] getContent() {
  98. if (content == null) {
  99. content = EncodingUtil.getBytes(value, getCharSet());
  100. }
  101. return content;
  102. }
  103. /**
  104. * Writes the data to the given OutputStream.
  105. * @param out the OutputStream to write to
  106. * @throws IOException if there is a write error
  107. */
  108. protected void sendData(OutputStream out) throws IOException {
  109. LOG.trace("enter sendData(OutputStream)");
  110. out.write(getContent());
  111. }
  112. /**
  113. * Return the length of the data.
  114. * @return The length of the data.
  115. * @throws IOException If an IO problem occurs
  116. * @see org.apache.commons.httpclient.methods.multipart.Part#lengthOfData()
  117. */
  118. protected long lengthOfData() throws IOException {
  119. LOG.trace("enter lengthOfData()");
  120. return getContent().length;
  121. }
  122. /* (non-Javadoc)
  123. * @see org.apache.commons.httpclient.methods.multipart.BasePart#setCharSet(java.lang.String)
  124. */
  125. public void setCharSet(String charSet) {
  126. super.setCharSet(charSet);
  127. this.content = null;
  128. }
  129. }