1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
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
141
142
143 public void setCharSet(String charSet) {
144 super.setCharSet(charSet);
145 this.content = null;
146 }
147
148 }