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  package org.apache.http.client.utils;
28  
29  import java.io.IOException;
30  
31  import org.apache.http.HttpEntity;
32  import org.apache.http.HttpResponse;
33  import org.apache.http.client.HttpClient;
34  import org.apache.http.client.methods.CloseableHttpResponse;
35  import org.apache.http.impl.client.CloseableHttpClient;
36  import org.apache.http.util.EntityUtils;
37  
38  /**
39   * Static helpers for dealing with {@link HttpResponse}s.
40   *
41   * @since 4.2
42   */
43  public class HttpClientUtils {
44  
45      private HttpClientUtils() {
46      }
47  
48      /**
49       * Unconditionally close a response.
50       * <p>
51       * Example Code:
52       *
53       * <pre>
54       * HttpResponse httpResponse = null;
55       * try {
56       *     httpResponse = httpClient.execute(httpGet);
57       * } catch (Exception e) {
58       *     // error handling
59       * } finally {
60       *     HttpClientUtils.closeQuietly(httpResponse);
61       * }
62       * </pre>
63       *
64       * @param response
65       *            the HttpResponse to release resources, may be null or already
66       *            closed.
67       *
68       * @since 4.2
69       */
70      public static void closeQuietly(final HttpResponse response) {
71          if (response != null) {
72              final HttpEntity entity = response.getEntity();
73              if (entity != null) {
74                  try {
75                      EntityUtils.consume(entity);
76                  } catch (final IOException ex) {
77                  }
78              }
79          }
80      }
81  
82      /**
83       * Unconditionally close a response.
84       * <p>
85       * Example Code:
86       *
87       * <pre>
88       * HttpResponse httpResponse = null;
89       * try {
90       *     httpResponse = httpClient.execute(httpGet);
91       * } catch (Exception e) {
92       *     // error handling
93       * } finally {
94       *     HttpClientUtils.closeQuietly(httpResponse);
95       * }
96       * </pre>
97       *
98       * @param response
99       *            the HttpResponse to release resources, may be null or already
100      *            closed.
101      *
102      * @since 4.3
103      */
104     public static void closeQuietly(final CloseableHttpResponse response) {
105         if (response != null) {
106             try {
107                 try {
108                     EntityUtils.consume(response.getEntity());
109                 } finally {
110                     response.close();
111                 }
112             } catch (final IOException ex) {
113             }
114         }
115     }
116 
117     /**
118      * Unconditionally close a httpClient. Shuts down the underlying connection
119      * manager and releases the resources.
120      * <p>
121      * Example Code:
122      *
123      * <pre>
124      * HttpClient httpClient = null;
125      * try {
126      *   httpClient = new DefaultHttpClient(...);
127      * } catch (Exception e) {
128      *   // error handling
129      * } finally {
130      *   HttpClientUtils.closeQuietly(httpClient);
131      * }
132      * </pre>
133      *
134      * @param httpClient
135      *            the HttpClient to close, may be null or already closed.
136      * @since 4.2
137      *
138      * @deprecated (4.3) do not use.
139      */
140     @Deprecated
141     public static void closeQuietly(final HttpClient httpClient) {
142         if (httpClient != null) {
143             httpClient.getConnectionManager().shutdown();
144         }
145     }
146 
147     /**
148      * Unconditionally close a httpClient. Shuts down the underlying connection
149      * manager and releases the resources.
150      * <p>
151      * Example Code:
152      *
153      * <pre>
154      * CloseableHttpClient httpClient = HttpClients.createDefault();
155      * try {
156      *   ...
157      * } catch (Exception e) {
158      *   // error handling
159      * } finally {
160      *   HttpClientUtils.closeQuietly(httpClient);
161      * }
162      * </pre>
163      *
164      * @param httpClient
165      *            the HttpClient to close, may be null or already closed.
166      * @since 4.3
167      */
168     public static void closeQuietly(final CloseableHttpClient httpClient) {
169         if (httpClient != null) {
170             try {
171                 httpClient.close();
172             } catch (final IOException ex) {
173             }
174         }
175     }
176 
177 }