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