1 /*
2 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/multipart/StringPart.java,v 1.7 2003/04/06 22:31:54 jsdever Exp $
3 * $Revision: 1.7 $
4 * $Date: 2003/04/06 22:31:54 $
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 Part {
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 /*** Name of this StringPart. */
97 private String name;
98
99 /*** Contents of this StringPart. */
100 private byte[] content;
101
102 /*** Charset of this StringPart. */
103 private String charset;
104
105 /***
106 * Constructor.
107 *
108 * @param name The name of the part
109 * @param value the string to post
110 * @param charset the charset to be used to encode the string
111 */
112 public StringPart(String name, String value, String charset) {
113 LOG.trace("enter StringPart(String, String, String)");
114 if (name == null) {
115 throw new IllegalArgumentException("Name may not be null");
116 }
117 this.name = name;
118 if (charset != null) {
119 this.charset = charset;
120 } else {
121 this.charset = DEFAULT_CHARSET;
122 }
123 if (value == null) {
124 throw new IllegalArgumentException("Value may not be null");
125 }
126 if (value.indexOf(0) != -1) {
127 // See RFC 2048, 2.8. "8bit Data"
128 throw new IllegalArgumentException("NULs may not be present in string parts");
129 }
130 this.content = HttpConstants.getContentBytes(value, this.charset);
131 }
132
133 /***
134 * Constructor.
135 *
136 * @param name The name of the part
137 * @param value the string to post
138 */
139 public StringPart(String name, String value) {
140 this(name, value, null);
141 }
142
143 /***
144 * Return the name of this part.
145 * @return the name of this StringPart.
146 */
147 public String getName() {
148 return name;
149 }
150
151 /***
152 * Return the content type of this part.
153 * @return String The name.
154 */
155 public String getContentType() {
156 return DEFAULT_CONTENT_TYPE;
157 }
158
159 /***
160 * Return the character encoding of this part.
161 * @return String The name.
162 */
163 public String getCharSet() {
164 return this.charset;
165 }
166
167 /***
168 * Return the transfer encoding of this part.
169 * @return String The name.
170 */
171 public String getTransferEncoding() {
172 return DEFAULT_TRANSFER_ENCODING;
173 }
174
175 /***
176 * Writes the data to the given OutputStream.
177 * @param out the OutputStream to write to
178 * @throws IOException if there is a write error
179 */
180 protected void sendData(OutputStream out) throws IOException {
181 LOG.trace("enter sendData(OutputStream)");
182 out.write(this.content);
183 }
184
185 /***
186 * Return the length of the data.
187 * @return The length of the data.
188 * @throws IOException If an IO problem occurs
189 * @see org.apache.commons.httpclient.methods.multipart.Part#lengthOfData()
190 */
191 protected long lengthOfData() throws IOException {
192 LOG.trace("enter lengthOfData()");
193 return this.content.length;
194 }
195 }
This page was automatically generated by Maven