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