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