1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/multipart/StringPart.java,v 1.7.2.1 2003/10/10 04:16:03 mbecke Exp $
3    * $Revision: 1.7.2.1 $
4    * $Date: 2003/10/10 04:16:03 $
5    *
6    * ====================================================================
7    *
8    * The Apache Software License, Version 1.1
9    *
10   * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
11   * reserved.
12   *
13   * Redistribution and use in source and binary forms, with or without
14   * modification, are permitted provided that the following conditions
15   * are met:
16   *
17   * 1. Redistributions of source code must retain the above copyright
18   *    notice, this list of conditions and the following disclaimer.
19   *
20   * 2. Redistributions in binary form must reproduce the above copyright
21   *    notice, this list of conditions and the following disclaimer in
22   *    the documentation and/or other materials provided with the
23   *    distribution.
24   *
25   * 3. The end-user documentation included with the redistribution, if
26   *    any, must include the following acknowlegement:
27   *       "This product includes software developed by the
28   *        Apache Software Foundation (http://www.apache.org/)."
29   *    Alternately, this acknowlegement may appear in the software itself,
30   *    if and wherever such third-party acknowlegements normally appear.
31   *
32   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33   *    Foundation" must not be used to endorse or promote products derived
34   *    from this software without prior written permission. For written
35   *    permission, please contact apache@apache.org.
36   *
37   * 5. Products derived from this software may not be called "Apache"
38   *    nor may "Apache" appear in their names without prior written
39   *    permission of the Apache Group.
40   *
41   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52   * SUCH DAMAGE.
53   * ====================================================================
54   *
55   * This software consists of voluntary contributions made by many
56   * individuals on behalf of the Apache Software Foundation.  For more
57   * information on the Apache Software Foundation, please see
58   * <http://www.apache.org/>.
59   *
60   * [Additional notices, if required by prior licensing conditions]
61   *
62   */
63  
64  package org.apache.commons.httpclient.methods.multipart;
65  
66  import java.io.OutputStream;
67  import java.io.IOException;
68  import org.apache.commons.httpclient.HttpConstants;
69  import org.apache.commons.logging.Log;
70  import org.apache.commons.logging.LogFactory;
71  
72  /***
73   * Simple string parameter for a multipart post
74   *
75   * @author <a href="mailto:mattalbright@yahoo.com">Matthew Albright</a>
76   * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
77   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
78   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
79   *
80   * @since 2.0
81   */
82  public class StringPart extends PartBase {
83  
84      /*** Log object for this class. */
85      private static final Log LOG = LogFactory.getLog(StringPart.class);
86  
87      /*** Default content encoding of string parameters. */
88      public static final String DEFAULT_CONTENT_TYPE = "text/plain";
89  
90      /*** Default charset of string parameters*/
91      public static final String DEFAULT_CHARSET = "US-ASCII";
92  
93      /*** Default transfer encoding of string parameters*/
94      public static final String DEFAULT_TRANSFER_ENCODING = "8bit";
95  
96      /*** Contents of this StringPart. */
97      private byte[] content;
98      
99      /*** The String value of this part. */
100     private String value;
101 
102     /***
103      * Constructor.
104      *
105      * @param name The name of the part
106      * @param value the string to post
107      * @param charset the charset to be used to encode the string, if <code>null</code> 
108      * the {@link #DEFAULT_CHARSET default} is used
109      */
110     public StringPart(String name, String value, String charset) {
111         
112         super(
113             name,
114             DEFAULT_CONTENT_TYPE,
115             charset == null ? DEFAULT_CHARSET : charset,
116             DEFAULT_TRANSFER_ENCODING
117         );
118         if (value == null) {
119             throw new IllegalArgumentException("Value may not be null");
120         }
121         if (value.indexOf(0) != -1) {
122             // See RFC 2048, 2.8. "8bit Data"
123             throw new IllegalArgumentException("NULs may not be present in string parts");
124         }
125         this.value = value;
126     }
127 
128     /***
129      * Constructor.
130      *
131      * @param name The name of the part
132      * @param value the string to post
133      */
134     public StringPart(String name, String value) {
135         this(name, value, null);
136     }
137     
138     /***
139      * Gets the content in bytes.  Bytes are lazily created to allow the charset to be changed
140      * after the part is created.
141      * 
142      * @return the content in bytes
143      */
144     private byte[] getContent() {
145         if (content == null) {
146             content = HttpConstants.getContentBytes(value, getCharSet());
147         }
148         return content;
149     }
150     
151     /***
152      * Writes the data to the given OutputStream.
153      * @param out the OutputStream to write to
154      * @throws IOException if there is a write error
155      */
156     protected void sendData(OutputStream out) throws IOException {
157         LOG.trace("enter sendData(OutputStream)");
158         out.write(getContent());
159     }
160     
161     /***
162      * Return the length of the data.
163      * @return The length of the data.
164      * @throws IOException If an IO problem occurs
165      * @see org.apache.commons.httpclient.methods.multipart.Part#lengthOfData()
166      */
167     protected long lengthOfData() throws IOException {
168         LOG.trace("enter lengthOfData()");
169         return getContent().length;
170     }
171     
172     /* (non-Javadoc)
173      * @see org.apache.commons.httpclient.methods.multipart.BasePart#setCharSet(java.lang.String)
174      */
175     public void setCharSet(String charSet) {
176         super.setCharSet(charSet);
177         this.content = null;
178     }
179 
180 }
This page was automatically generated by Maven