1 /*
2 * ====================================================================
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 * ====================================================================
20 *
21 * This software consists of voluntary contributions made by many
22 * individuals on behalf of the Apache Software Foundation. For more
23 * information on the Apache Software Foundation, please see
24 * <http://www.apache.org/>.
25 *
26 */
27
28 package org.apache.http.entity;
29
30 import java.io.ByteArrayInputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.OutputStream;
34 import java.io.UnsupportedEncodingException;
35 import java.nio.charset.Charset;
36 import java.nio.charset.UnsupportedCharsetException;
37
38 import org.apache.http.annotation.NotThreadSafe;
39 import org.apache.http.protocol.HTTP;
40
41 /**
42 * A self contained, repeatable entity that obtains its content from
43 * a {@link String}.
44 *
45 * @since 4.0
46 */
47 @NotThreadSafe
48 public class StringEntity extends AbstractHttpEntity implements Cloneable {
49
50 protected final byte[] content;
51
52 /**
53 * Creates a StringEntity with the specified content and content type.
54 *
55 * @param string content to be used. Not {@code null}.
56 * @param contentType content type to be used. May be {@code null}, in which case the default
57 * MIME type {@link ContentType#TEXT_PLAIN} is assumed.
58 *
59 * @throws IllegalArgumentException if the string parameter is null
60 *
61 * @since 4.2
62 */
63 public StringEntity(final String string, final ContentType contentType) {
64 super();
65 if (string == null) {
66 throw new IllegalArgumentException("Source string may not be null");
67 }
68 Charset charset = contentType != null ? contentType.getCharset() : null;
69 if (charset == null) {
70 charset = HTTP.DEF_CONTENT_CHARSET;
71 }
72 try {
73 this.content = string.getBytes(charset.name());
74 } catch (UnsupportedEncodingException ex) {
75 // should never happen
76 throw new UnsupportedCharsetException(charset.name());
77 }
78 if (contentType != null) {
79 setContentType(contentType.toString());
80 }
81 }
82
83 /**
84 * Creates a StringEntity with the specified content, MIME type and charset
85 *
86 * @param string content to be used. Not {@code null}.
87 * @param mimeType MIME type to be used. May be {@code null}, in which case the default
88 * is {@link HTTP#PLAIN_TEXT_TYPE} i.e. "text/plain"
89 * @param charset character set to be used. May be {@code null}, in which case the default
90 * is {@link HTTP#DEF_CONTENT_CHARSET} i.e. "ISO-8859-1"
91 *
92 * @since 4.1
93 * @throws IllegalArgumentException if the string parameter is null
94 *
95 * @deprecated (4.1.3) use {@link #StringEntity(String, ContentType)}
96 */
97 @Deprecated
98 public StringEntity(final String string, String mimeType, String charset)
99 throws UnsupportedEncodingException {
100 super();
101 if (string == null) {
102 throw new IllegalArgumentException("Source string may not be null");
103 }
104 if (mimeType == null) {
105 mimeType = HTTP.PLAIN_TEXT_TYPE;
106 }
107 if (charset == null) {
108 charset = HTTP.DEFAULT_CONTENT_CHARSET;
109 }
110 this.content = string.getBytes(charset);
111 setContentType(mimeType + HTTP.CHARSET_PARAM + charset);
112 }
113
114 /**
115 * Creates a StringEntity with the specified content and charset. The MIME type defaults
116 * to "text/plain".
117 *
118 * @param string content to be used. Not {@code null}.
119 * @param charset character set to be used. May be {@code null}, in which case the default
120 * is {@link HTTP#DEF_CONTENT_CHARSET} is assumed
121 *
122 * @throws IllegalArgumentException if the string parameter is null
123 * @throws UnsupportedEncodingException if the charset is not supported.
124 */
125 public StringEntity(final String string, final String charset)
126 throws UnsupportedEncodingException {
127 this(string, ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), charset));
128 }
129
130 /**
131 * Creates a StringEntity with the specified content and charset. The MIME type defaults
132 * to "text/plain".
133 *
134 * @param string content to be used. Not {@code null}.
135 * @param charset character set to be used. May be {@code null}, in which case the default
136 * is {@link HTTP#DEF_CONTENT_CHARSET} is assumed
137 *
138 * @throws IllegalArgumentException if the string parameter is null
139 *
140 * @since 4.2
141 */
142 public StringEntity(final String string, final Charset charset) {
143 this(string, ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), charset));
144 }
145
146 /**
147 * Creates a StringEntity with the specified content. The content type defaults to
148 * {@link ContentType#TEXT_PLAIN}.
149 *
150 * @param string content to be used. Not {@code null}.
151 *
152 * @throws IllegalArgumentException if the string parameter is null
153 * @throws UnsupportedEncodingException if the default HTTP charset is not supported.
154 */
155 public StringEntity(final String string)
156 throws UnsupportedEncodingException {
157 this(string, ContentType.DEFAULT_TEXT);
158 }
159
160 public boolean isRepeatable() {
161 return true;
162 }
163
164 public long getContentLength() {
165 return this.content.length;
166 }
167
168 public InputStream getContent() throws IOException {
169 return new ByteArrayInputStream(this.content);
170 }
171
172 public void writeTo(final OutputStream outstream) throws IOException {
173 if (outstream == null) {
174 throw new IllegalArgumentException("Output stream may not be null");
175 }
176 outstream.write(this.content);
177 outstream.flush();
178 }
179
180 /**
181 * Tells that this entity is not streaming.
182 *
183 * @return <code>false</code>
184 */
185 public boolean isStreaming() {
186 return false;
187 }
188
189 @Override
190 public Object clone() throws CloneNotSupportedException {
191 return super.clone();
192 }
193
194 } // class StringEntity