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;
32
33 import java.io.IOException;
34 import java.io.OutputStream;
35 import java.io.UnsupportedEncodingException;
36
37 import org.apache.commons.httpclient.HeaderElement;
38 import org.apache.commons.httpclient.NameValuePair;
39
40 /***
41 * A RequestEntity that contains a String.
42 *
43 * @since 3.0
44 */
45 public class StringRequestEntity implements RequestEntity {
46
47 /*** The content */
48 private byte[] content;
49
50 /*** The charset */
51 private String charset;
52
53 /*** The content type (i.e. text/html; charset=EUC-JP). */
54 private String contentType;
55
56 /***
57 * <p>Creates a new entity with the given content. This constructor
58 * will use the default platform charset to convert the content string
59 * and will provide no content type.</p>
60 *
61 * <p>This constructor may be deprecated or changed to use the
62 * default HTTP content charset (ISO-8859-1) in the release 3.1</p>
63 *
64 * <p>It is strongly recommended to use
65 * {@link #StringRequestEntity(String, String, String)} constructor
66 * instead.</p>
67 *
68 * @see #StringRequestEntity(String, String, String)
69 *
70 * @param content The content to set.
71 */
72 public StringRequestEntity(String content) {
73 super();
74 if (content == null) {
75 throw new IllegalArgumentException("The content cannot be null");
76 }
77 this.contentType = null;
78 this.charset = null;
79 this.content = content.getBytes();
80 }
81
82 /***
83 * Creates a new entity with the given content, content type, and charset.
84 *
85 * @param content The content to set.
86 * @param contentType The type of the content, or <code>null</code>. The value retured
87 * by {@link #getContentType()}. If this content type contains a charset and the charset
88 * parameter is null, the content's type charset will be used.
89 * @param charset The charset of the content, or <code>null</code>. Used to convert the
90 * content to bytes. If the content type does not contain a charset and charset is not null,
91 * then the charset will be appended to the content type.
92 */
93 public StringRequestEntity(String content, String contentType, String charset)
94 throws UnsupportedEncodingException {
95 super();
96 if (content == null) {
97 throw new IllegalArgumentException("The content cannot be null");
98 }
99
100 this.contentType = contentType;
101 this.charset = charset;
102
103
104 if (contentType != null) {
105 HeaderElement[] values = HeaderElement.parseElements(contentType);
106 NameValuePair charsetPair = null;
107 for (int i = 0; i < values.length; i++) {
108 if ((charsetPair = values[i].getParameterByName("charset")) != null) {
109
110 break;
111 }
112 }
113 if (charset == null && charsetPair != null) {
114
115 this.charset = charsetPair.getValue();
116 } else if (charset != null && charsetPair == null) {
117
118 this.contentType = contentType + "; charset=" + charset;
119 }
120 }
121 if (this.charset != null) {
122 this.content = content.getBytes(this.charset);
123 } else {
124 this.content = content.getBytes();
125 }
126 }
127
128
129
130
131 public String getContentType() {
132 return contentType;
133 }
134
135 /***
136 * @return <code>true</code>
137 */
138 public boolean isRepeatable() {
139 return true;
140 }
141
142
143
144
145 public void writeRequest(OutputStream out) throws IOException {
146 if (out == null) {
147 throw new IllegalArgumentException("Output stream may not be null");
148 }
149 out.write(this.content);
150 out.flush();
151 }
152
153 /***
154 * @return The length of the content.
155 */
156 public long getContentLength() {
157 return this.content.length;
158 }
159
160 /***
161 * @return Returns the content.
162 */
163 public String getContent() {
164 if (this.charset != null) {
165 try {
166 return new String(this.content, this.charset);
167 } catch (UnsupportedEncodingException e) {
168 return new String(this.content);
169 }
170 } else {
171 return new String(this.content);
172 }
173 }
174
175 /***
176 * @return Returns the charset used to convert the content to bytes. <code>null</code> if
177 * no charset as been specified.
178 */
179 public String getCharset() {
180 return charset;
181 }
182 }