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.util;
29  
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.io.InputStreamReader;
33  import java.io.Reader;
34  import java.io.UnsupportedEncodingException;
35  import java.nio.charset.Charset;
36  import java.nio.charset.UnsupportedCharsetException;
37  
38  import org.apache.http.HeaderElement;
39  import org.apache.http.HttpEntity;
40  import org.apache.http.HttpResponse;
41  import org.apache.http.NameValuePair;
42  import org.apache.http.ParseException;
43  import org.apache.http.entity.ContentType;
44  import org.apache.http.protocol.HTTP;
45  
46  /**
47   * Static helpers for dealing with {@link HttpEntity}s.
48   *
49   * @since 4.0
50   */
51  public final class EntityUtils {
52  
53      private EntityUtils() {
54      }
55  
56      /**
57       * Ensures that the entity content is fully consumed and the content stream, if exists,
58       * is closed. The process is done, <i>quietly</i> , without throwing any IOException.
59       *
60       * @param entity the entity to consume.
61       *
62       *
63       * @since 4.2
64       */
65      public static void consumeQuietly(final HttpEntity entity) {
66          try {
67            consume(entity);
68          } catch (final IOException ignore) {
69          }
70      }
71  
72      /**
73       * Ensures that the entity content is fully consumed and the content stream, if exists,
74       * is closed.
75       *
76       * @param entity the entity to consume.
77       * @throws IOException if an error occurs reading the input stream
78       *
79       * @since 4.1
80       */
81      public static void consume(final HttpEntity entity) throws IOException {
82          if (entity == null) {
83              return;
84          }
85          if (entity.isStreaming()) {
86              final InputStream instream = entity.getContent();
87              if (instream != null) {
88                  instream.close();
89              }
90          }
91      }
92  
93      /**
94       * Updates an entity in a response by first consuming an existing entity, then setting the new one.
95       *
96       * @param response the response with an entity to update; must not be null.
97       * @param entity the entity to set in the response.
98       * @throws IOException if an error occurs while reading the input stream on the existing
99       * entity.
100      * @throws IllegalArgumentException if response is null.
101      *
102      * @since 4.3
103      */
104     public static void updateEntity(
105             final HttpResponse response, final HttpEntity entity) throws IOException {
106         Args.notNull(response, "Response");
107         consume(response.getEntity());
108         response.setEntity(entity);
109     }
110 
111     /**
112      * Read the contents of an entity and return it as a byte array.
113      *
114      * @param entity the entity to read from=
115      * @return byte array containing the entity content. May be null if
116      *   {@link HttpEntity#getContent()} is null.
117      * @throws IOException if an error occurs reading the input stream
118      * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE
119      */
120     public static byte[] toByteArray(final HttpEntity entity) throws IOException {
121         Args.notNull(entity, "Entity");
122         final InputStream instream = entity.getContent();
123         if (instream == null) {
124             return null;
125         }
126         try {
127             Args.check(entity.getContentLength() <= Integer.MAX_VALUE,
128                     "HTTP entity too large to be buffered in memory");
129             int i = (int)entity.getContentLength();
130             if (i < 0) {
131                 i = 4096;
132             }
133             final ByteArrayBuffer buffer = new ByteArrayBuffer(i);
134             final byte[] tmp = new byte[4096];
135             int l;
136             while((l = instream.read(tmp)) != -1) {
137                 buffer.append(tmp, 0, l);
138             }
139             return buffer.toByteArray();
140         } finally {
141             instream.close();
142         }
143     }
144 
145     /**
146      * Obtains character set of the entity, if known.
147      *
148      * @param entity must not be null
149      * @return the character set, or null if not found
150      * @throws ParseException if header elements cannot be parsed
151      * @throws IllegalArgumentException if entity is null
152      *
153      * @deprecated (4.1.3) use {@link ContentType#getOrDefault(HttpEntity)}
154      */
155     @Deprecated
156     public static String getContentCharSet(final HttpEntity entity) throws ParseException {
157         Args.notNull(entity, "Entity");
158         String charset = null;
159         if (entity.getContentType() != null) {
160             final HeaderElement values[] = entity.getContentType().getElements();
161             if (values.length > 0) {
162                 final NameValuePair param = values[0].getParameterByName("charset");
163                 if (param != null) {
164                     charset = param.getValue();
165                 }
166             }
167         }
168         return charset;
169     }
170 
171     /**
172      * Obtains MIME type of the entity, if known.
173      *
174      * @param entity must not be null
175      * @return the character set, or null if not found
176      * @throws ParseException if header elements cannot be parsed
177      * @throws IllegalArgumentException if entity is null
178      *
179      * @since 4.1
180      *
181      * @deprecated (4.1.3) use {@link ContentType#getOrDefault(HttpEntity)}
182      */
183     @Deprecated
184     public static String getContentMimeType(final HttpEntity entity) throws ParseException {
185         Args.notNull(entity, "Entity");
186         String mimeType = null;
187         if (entity.getContentType() != null) {
188             final HeaderElement values[] = entity.getContentType().getElements();
189             if (values.length > 0) {
190                 mimeType = values[0].getName();
191             }
192         }
193         return mimeType;
194     }
195 
196     /**
197      * Get the entity content as a String, using the provided default character set
198      * if none is found in the entity.
199      * If defaultCharset is null, the default "ISO-8859-1" is used.
200      *
201      * @param entity must not be null
202      * @param defaultCharset character set to be applied if none found in the entity
203      * @return the entity content as a String. May be null if
204      *   {@link HttpEntity#getContent()} is null.
205      * @throws ParseException if header elements cannot be parsed
206      * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE
207      * @throws IOException if an error occurs reading the input stream
208      * @throws UnsupportedCharsetException Thrown when the named charset is not available in
209      * this instance of the Java virtual machine
210      */
211     public static String toString(
212             final HttpEntity entity, final Charset defaultCharset) throws IOException, ParseException {
213         Args.notNull(entity, "Entity");
214         final InputStream instream = entity.getContent();
215         if (instream == null) {
216             return null;
217         }
218         try {
219             Args.check(entity.getContentLength() <= Integer.MAX_VALUE,
220                     "HTTP entity too large to be buffered in memory");
221             int i = (int)entity.getContentLength();
222             if (i < 0) {
223                 i = 4096;
224             }
225             Charset charset = null;
226             try {
227                 final ContentType contentType = ContentType.get(entity);
228                 if (contentType != null) {
229                     charset = contentType.getCharset();
230                 }
231             } catch (final UnsupportedCharsetException ex) {
232                 throw new UnsupportedEncodingException(ex.getMessage());
233             }
234             if (charset == null) {
235                 charset = defaultCharset;
236             }
237             if (charset == null) {
238                 charset = HTTP.DEF_CONTENT_CHARSET;
239             }
240             final Reader reader = new InputStreamReader(instream, charset);
241             final CharArrayBuffer buffer = new CharArrayBuffer(i);
242             final char[] tmp = new char[1024];
243             int l;
244             while((l = reader.read(tmp)) != -1) {
245                 buffer.append(tmp, 0, l);
246             }
247             return buffer.toString();
248         } finally {
249             instream.close();
250         }
251     }
252 
253     /**
254      * Get the entity content as a String, using the provided default character set
255      * if none is found in the entity.
256      * If defaultCharset is null, the default "ISO-8859-1" is used.
257      *
258      * @param entity must not be null
259      * @param defaultCharset character set to be applied if none found in the entity
260      * @return the entity content as a String. May be null if
261      *   {@link HttpEntity#getContent()} is null.
262      * @throws ParseException if header elements cannot be parsed
263      * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE
264      * @throws IOException if an error occurs reading the input stream
265      * @throws UnsupportedCharsetException Thrown when the named charset is not available in
266      * this instance of the Java virtual machine
267      */
268     public static String toString(
269             final HttpEntity entity, final String defaultCharset) throws IOException, ParseException {
270         return toString(entity, defaultCharset != null ? Charset.forName(defaultCharset) : null);
271     }
272 
273     /**
274      * Read the contents of an entity and return it as a String.
275      * The content is converted using the character set from the entity (if any),
276      * failing that, "ISO-8859-1" is used.
277      *
278      * @param entity the entity to convert to a string; must not be null
279      * @return String containing the content.
280      * @throws ParseException if header elements cannot be parsed
281      * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE
282      * @throws IOException if an error occurs reading the input stream
283      * @throws UnsupportedCharsetException Thrown when the named charset is not available in
284      * this instance of the Java virtual machine
285      */
286     public static String toString(final HttpEntity entity)
287         throws IOException, ParseException {
288         return toString(entity, (Charset)null);
289     }
290 
291 }