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.hc.core5.http.io.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.nio.charset.Charset;
35  import java.nio.charset.StandardCharsets;
36  
37  import org.apache.hc.core5.annotation.Contract;
38  import org.apache.hc.core5.annotation.ThreadingBehavior;
39  import org.apache.hc.core5.http.ContentType;
40  import org.apache.hc.core5.util.Args;
41  
42  /**
43   * A self contained, repeatable entity that obtains its content from a {@link String}.
44   * <p>
45   * This class contains {@link ThreadingBehavior#IMMUTABLE immutable attributes} but subclasses may contain
46   * additional immutable or mutable attributes.
47   * </p>
48   *
49   * @since 4.0
50   */
51  @Contract(threading = ThreadingBehavior.IMMUTABLE)
52  public class StringEntity extends AbstractHttpEntity {
53  
54      private final byte[] content;
55  
56      /**
57       * Constructs a StringEntity with the specified content and content type.
58       *
59       * @param string          The content to be used. Not {@code null}.
60       * @param contentType     The content type to be used. May be {@code null}, in which case the default MIME type {@link ContentType#TEXT_PLAIN} is assumed.
61       * @param contentEncoding The content encoding string, may be null.
62       * @param chunked         Whether this entity should be chunked.
63       * @throws NullPointerException Thrown if string is null.
64       * @since 5.0
65       */
66      public StringEntity(
67              final String string, final ContentType contentType, final String contentEncoding, final boolean chunked) {
68          super(contentType, contentEncoding, chunked);
69          Args.notNull(string, "Source string");
70          final Charset charset = ContentType.getCharset(contentType, StandardCharsets.UTF_8);
71          this.content = string.getBytes(charset);
72      }
73  
74      /**
75       * Constructs a StringEntity with the specified content and content type.
76       * <p>
77       * The new instance:
78       * </p>
79       * <ul>
80       * <li>does not define a content encoding.</li>
81       * </ul>
82       *
83       * @param string          The content to be used. Not {@code null}.
84       * @param contentType     The content type to be used. May be {@code null}, in which case the default MIME type {@link ContentType#TEXT_PLAIN} is assumed.
85       * @param chunked         Whether this entity should be chunked.
86       * @throws NullPointerException Thrown if string is null.
87       */
88      public StringEntity(final String string, final ContentType contentType, final boolean chunked) {
89          this(string, contentType, null, chunked);
90      }
91  
92      /**
93       * Constructs a StringEntity with the specified content and content type.
94       * <p>
95       * The new instance:
96       * </p>
97       * <ul>
98       * <li>is not chunked.</li>
99       * <li>does not define a content encoding.</li>
100      * </ul>
101      *
102      * @param string          The content to be used. Not {@code null}.
103      * @param contentType     The content type to be used. May be {@code null}, in which case the default MIME type {@link ContentType#TEXT_PLAIN} is assumed.
104      * @throws NullPointerException Thrown if string is null.
105      */
106     public StringEntity(final String string, final ContentType contentType) {
107         this(string, contentType, null, false);
108     }
109 
110     /**
111      * Constructs a StringEntity with the specified content and charset. The MIME type defaults to "text/plain".
112      * <p>
113      * The new instance:
114      * </p>
115      * <ul>
116      * <li>is not chunked.</li>
117      * <li>sets the content type to {@code "text/plain"} and the given Charset.</li>
118      * </ul>
119      *
120      * @param string  The content to be used. Not {@code null}.
121      * @param charset The character set to be used. May be {@code null}, in which case the default is {@link StandardCharsets#UTF_8} is assumed.
122      * @throws NullPointerException Thrown if string is null.
123      * @since 4.2
124      */
125     public StringEntity(final String string, final Charset charset) {
126         this(string, ContentType.TEXT_PLAIN.withCharset(charset));
127     }
128 
129     /**
130      * Constructs a StringEntity with the specified content and content type.
131      * <p>
132      * The new instance:
133      * </p>
134      * <ul>
135      * <li>sets the content type to {@code "text/plain"} and the given Charset.</li>
136      * </ul>
137      *
138      * @param string          The content to be used. Not {@code null}.
139      * @param charset The character set to be used. May be {@code null}, in which case the default is {@link StandardCharsets#UTF_8} is assumed.
140      * @param chunked         Whether this entity should be chunked.
141      * @throws NullPointerException Thrown if string is null.
142      */
143     public StringEntity(final String string, final Charset charset, final boolean chunked) {
144         this(string, ContentType.TEXT_PLAIN.withCharset(charset), chunked);
145     }
146 
147     /**
148      * Constructs a StringEntity with the specified content and content type.
149      * <p>
150      * The new instance:
151      * </p>
152      * <ul>
153      * <li>is not chunked.</li>
154      * <li>sets the content type to {@code "text/plain"} and the given Charset.</li>
155      * </ul>
156      *
157      * @param string          The content to be used. Not {@code null}.
158      * @throws NullPointerException Thrown if string is null.
159      */
160     public StringEntity(final String string) {
161         this(string, ContentType.DEFAULT_TEXT);
162     }
163 
164     /**
165      * {@inheritDoc}
166      * <p>
167      * This implementation always returns {@code true}.
168      * </p>
169      */
170     @Override
171     public final boolean isRepeatable() {
172         return true;
173     }
174 
175     @Override
176     public final long getContentLength() {
177         return this.content.length;
178     }
179 
180     @Override
181     public final InputStream getContent() throws IOException {
182         return new ByteArrayInputStream(this.content);
183     }
184 
185     @Override
186     public final void writeTo(final OutputStream outStream) throws IOException {
187         Args.notNull(outStream, "Output stream");
188         outStream.write(this.content);
189         outStream.flush();
190     }
191 
192     /**
193      * {@inheritDoc}
194      * <p>
195      * This implementation always returns {@code false}.
196      * </p>
197      */
198     @Override
199     public final boolean isStreaming() {
200         return false;
201     }
202 
203     /**
204      * {@inheritDoc}
205      * <p>
206      * This implementation is a no-op.
207      * </p>
208      */
209     @Override
210     public final void close() throws IOException {
211         // nothing to do
212     }
213 
214 }