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