View Javadoc

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  import org.apache.http.util.Args;
41  
42  /**
43   * A self contained, repeatable entity that obtains its content from
44   * a {@link String}.
45   *
46   * @since 4.0
47   */
48  @NotThreadSafe
49  public class StringEntity extends AbstractHttpEntity implements Cloneable {
50  
51      protected final byte[] content;
52  
53      /**
54       * Creates a StringEntity with the specified content and content type.
55       *
56       * @param string content to be used. Not {@code null}.
57       * @param contentType content type to be used. May be {@code null}, in which case the default
58       *   MIME type {@link ContentType#TEXT_PLAIN} is assumed.
59       *
60       * @throws IllegalArgumentException if the string parameter is null
61       *
62       * @since 4.2
63       */
64      public StringEntity(final String string, final ContentType contentType) {
65          super();
66          Args.notNull(string, "Source string");
67          Charset charset = contentType != null ? contentType.getCharset() : null;
68          if (charset == null) {
69              charset = HTTP.DEF_CONTENT_CHARSET;
70          }
71          try {
72              this.content = string.getBytes(charset.name());
73          } catch (final UnsupportedEncodingException ex) {
74              // should never happen
75              throw new UnsupportedCharsetException(charset.name());
76          }
77          if (contentType != null) {
78              setContentType(contentType.toString());
79          }
80      }
81  
82      /**
83       * Creates a StringEntity with the specified content, MIME type and charset
84       *
85       * @param string content to be used. Not {@code null}.
86       * @param mimeType MIME type to be used. May be {@code null}, in which case the default
87       *   is {@link HTTP#PLAIN_TEXT_TYPE} i.e. "text/plain"
88       * @param charset character set to be used. May be {@code null}, in which case the default
89       *   is {@link HTTP#DEF_CONTENT_CHARSET} i.e. "ISO-8859-1"
90       *
91       * @since 4.1
92       * @throws IllegalArgumentException if the string parameter is null
93       *
94       * @deprecated (4.1.3) use {@link #StringEntity(String, ContentType)}
95       */
96      @Deprecated
97      public StringEntity(final String string, String mimeType, String charset)
98              throws UnsupportedEncodingException {
99          super();
100         Args.notNull(string, "Source string");
101         if (mimeType == null) {
102             mimeType = HTTP.PLAIN_TEXT_TYPE;
103         }
104         if (charset == null) {
105             charset = HTTP.DEFAULT_CONTENT_CHARSET;
106         }
107         this.content = string.getBytes(charset);
108         setContentType(mimeType + HTTP.CHARSET_PARAM + charset);
109     }
110 
111     /**
112      * Creates a StringEntity with the specified content and charset. The MIME type defaults
113      * to "text/plain".
114      *
115      * @param string content to be used. Not {@code null}.
116      * @param charset character set to be used. May be {@code null}, in which case the default
117      *   is {@link HTTP#DEF_CONTENT_CHARSET} is assumed
118      *
119      * @throws IllegalArgumentException if the string parameter is null
120      * @throws UnsupportedCharsetException Thrown when the named charset is not available in
121      * this instance of the Java virtual machine
122      */
123     public StringEntity(final String string, final String charset)
124             throws UnsupportedEncodingException {
125         this(string, ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), charset));
126     }
127 
128     /**
129      * Creates a StringEntity with the specified content and charset. The MIME type defaults
130      * to "text/plain".
131      *
132      * @param string content to be used. Not {@code null}.
133      * @param charset character set to be used. May be {@code null}, in which case the default
134      *   is {@link HTTP#DEF_CONTENT_CHARSET} is assumed
135      *
136      * @throws IllegalArgumentException if the string parameter is null
137      *
138      * @since 4.2
139      */
140     public StringEntity(final String string, final Charset charset) {
141         this(string, ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), charset));
142     }
143 
144     /**
145      * Creates a StringEntity with the specified content. The content type defaults to
146      * {@link ContentType#TEXT_PLAIN}.
147      *
148      * @param string content to be used. Not {@code null}.
149      *
150      * @throws IllegalArgumentException if the string parameter is null
151      * @throws UnsupportedEncodingException if the default HTTP charset is not supported.
152      */
153     public StringEntity(final String string)
154             throws UnsupportedEncodingException {
155         this(string, ContentType.DEFAULT_TEXT);
156     }
157 
158     public boolean isRepeatable() {
159         return true;
160     }
161 
162     public long getContentLength() {
163         return this.content.length;
164     }
165 
166     public InputStream getContent() throws IOException {
167         return new ByteArrayInputStream(this.content);
168     }
169 
170     public void writeTo(final OutputStream outstream) throws IOException {
171         Args.notNull(outstream, "Output stream");
172         outstream.write(this.content);
173         outstream.flush();
174     }
175 
176     /**
177      * Tells that this entity is not streaming.
178      *
179      * @return <code>false</code>
180      */
181     public boolean isStreaming() {
182         return false;
183     }
184 
185     @Override
186     public Object clone() throws CloneNotSupportedException {
187         return super.clone();
188     }
189 
190 } // class StringEntity