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 /***
58 * Creates a new entity with the given content
59 *
60 * @param content The content to set.
61 */
62 public StringRequestEntity(String content) {
63 super();
64 if (content == null) {
65 throw new IllegalArgumentException("The content cannot be null");
66 }
67 this.contentType = null;
68 this.charset = null;
69 this.content = content.getBytes();
70 }
71
72 /***
73 * Creates a new entity with the given content, content type, and charset.
74 *
75 * @param content The content to set.
76 * @param contentType The type of the content, or <code>null</code>. The value retured
77 * by {@link #getContentType()}. If this content type contains a charset and the charset
78 * parameter is null, the content's type charset will be used.
79 * @param charset The charset of the content, or <code>null</code>. Used to convert the
80 * content to bytes. If the content type does not contain a charset and charset is not null,
81 * then the charset will be appended to the content type.
82 */
83 public StringRequestEntity(String content, String contentType, String charset)
84 throws UnsupportedEncodingException {
85 super();
86 if (content == null) {
87 throw new IllegalArgumentException("The content cannot be null");
88 }
89
90 this.contentType = contentType;
91 this.charset = charset;
92
93
94 if (contentType != null) {
95 HeaderElement[] values = HeaderElement.parseElements(contentType);
96 NameValuePair charsetPair = null;
97 for (int i = 0; i < values.length; i++) {
98 if ((charsetPair = values[i].getParameterByName("charset")) != null) {
99
100 break;
101 }
102 }
103 if (charset == null && charsetPair != null) {
104
105 this.charset = charsetPair.getValue();
106 } else if (charset != null && charsetPair == null) {
107
108 this.contentType = contentType + "; charset=" + charset;
109 }
110 }
111 if (this.charset != null) {
112 this.content = content.getBytes(this.charset);
113 } else {
114 this.content = content.getBytes();
115 }
116 }
117
118
119
120
121 public String getContentType() {
122 return contentType;
123 }
124
125 /***
126 * @return <code>true</code>
127 */
128 public boolean isRepeatable() {
129 return true;
130 }
131
132
133
134
135 public void writeRequest(OutputStream out) throws IOException {
136 if (out == null) {
137 throw new IllegalArgumentException("Output stream may not be null");
138 }
139 out.write(this.content);
140 out.flush();
141 }
142
143 /***
144 * @return The length of the content.
145 */
146 public long getContentLength() {
147 return this.content.length;
148 }
149
150 /***
151 * @return Returns the content.
152 */
153 public String getContent() {
154 if (this.charset != null) {
155 try {
156 return new String(this.content, this.charset);
157 } catch (UnsupportedEncodingException e) {
158 return new String(this.content);
159 }
160 } else {
161 return new String(this.content);
162 }
163 }
164
165 /***
166 * @return Returns the charset used to convert the content to bytes. <code>null</code> if
167 * no charset as been specified.
168 */
169 public String getCharset() {
170 return charset;
171 }
172 }