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