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.Closeable;
30  import java.io.IOException;
31  
32  import org.apache.http.HttpEntity;
33  import org.apache.http.HttpResponse;
34  import org.apache.http.client.HttpClient;
35  import org.apache.http.client.methods.CloseableHttpResponse;
36  import org.apache.http.util.EntityUtils;
37  
38  /**
39   * Convenience methods for closing response and client objects.
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 ignore) {
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 = HttpClients.createDefault();
125      * try {
126      *   httpClient.execute(request);
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     public static void closeQuietly(final HttpClient httpClient) {
139         if (httpClient != null) {
140             if (httpClient instanceof Closeable) {
141                 try {
142                     ((Closeable) httpClient).close();
143                 } catch (final IOException ignore) {
144                 }
145             }
146         }
147     }
148 
149 }