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       * @throws UnsupportedCharsetException Thrown when the named charset is not available in
62       * this instance of the Java virtual machine
63       * @since 4.2
64       */
65      public StringEntity(final String string, final ContentType contentType) throws UnsupportedCharsetException {
66          super();
67          Args.notNull(string, "Source string");
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 (final 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       * @throws  UnsupportedEncodingException If the named charset is not supported.
92       *
93       * @since 4.1
94       * @throws IllegalArgumentException if the string parameter is null
95       *
96       * @deprecated (4.1.3) use {@link #StringEntity(String, ContentType)}
97       */
98      @Deprecated
99      public StringEntity(
100             final String string, final String mimeType, final String charset) throws UnsupportedEncodingException {
101         super();
102         Args.notNull(string, "Source string");
103         final String mt = mimeType != null ? mimeType : HTTP.PLAIN_TEXT_TYPE;
104         final String cs = charset != null ? charset :HTTP.DEFAULT_CONTENT_CHARSET;
105         this.content = string.getBytes(cs);
106         setContentType(mt + HTTP.CHARSET_PARAM + cs);
107     }
108 
109     /**
110      * Creates a StringEntity with the specified content and charset. The MIME type defaults
111      * to "text/plain".
112      *
113      * @param string content to be used. Not {@code null}.
114      * @param charset character set to be used. May be {@code null}, in which case the default
115      *   is {@link HTTP#DEF_CONTENT_CHARSET} is assumed
116      *
117      * @throws IllegalArgumentException if the string parameter is null
118      * @throws UnsupportedCharsetException Thrown when the named charset is not available in
119      * this instance of the Java virtual machine
120      */
121     public StringEntity(final String string, final String charset)
122             throws UnsupportedCharsetException {
123         this(string, ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), charset));
124     }
125 
126     /**
127      * Creates a StringEntity with the specified content and charset. The MIME type defaults
128      * to "text/plain".
129      *
130      * @param string content to be used. Not {@code null}.
131      * @param charset character set to be used. May be {@code null}, in which case the default
132      *   is {@link HTTP#DEF_CONTENT_CHARSET} is assumed
133      *
134      * @throws IllegalArgumentException if the string parameter is null
135      *
136      * @since 4.2
137      */
138     public StringEntity(final String string, final Charset charset) {
139         this(string, ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), charset));
140     }
141 
142     /**
143      * Creates a StringEntity with the specified content. The content type defaults to
144      * {@link ContentType#TEXT_PLAIN}.
145      *
146      * @param string content to be used. Not {@code null}.
147      *
148      * @throws IllegalArgumentException if the string parameter is null
149      * @throws UnsupportedEncodingException if the default HTTP charset is not supported.
150      */
151     public StringEntity(final String string)
152             throws UnsupportedEncodingException {
153         this(string, ContentType.DEFAULT_TEXT);
154     }
155 
156     public boolean isRepeatable() {
157         return true;
158     }
159 
160     public long getContentLength() {
161         return this.content.length;
162     }
163 
164     public InputStream getContent() throws IOException {
165         return new ByteArrayInputStream(this.content);
166     }
167 
168     public void writeTo(final OutputStream outstream) throws IOException {
169         Args.notNull(outstream, "Output stream");
170         outstream.write(this.content);
171         outstream.flush();
172     }
173 
174     /**
175      * Tells that this entity is not streaming.
176      *
177      * @return <code>false</code>
178      */
179     public boolean isStreaming() {
180         return false;
181     }
182 
183     @Override
184     public Object clone() throws CloneNotSupportedException {
185         return super.clone();
186     }
187 
188 } // class StringEntity