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 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
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
140
141
142 public void setCharSet(String charSet) {
143 super.setCharSet(charSet);
144 this.content = null;
145 }
146
147 }