View Javadoc

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