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