View Javadoc

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