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.util.EntityUtils;
35
36 /**
37 * Static helpers for dealing with {@link HttpResponse}s and {@link HttpClient}s.
38 *
39 * @since 4.2
40 */
41 public class HttpClientUtils {
42
43 private HttpClientUtils() {
44 }
45
46 /**
47 * Unconditionally close a response.
48 * <p>
49 * Example Code:
50 *
51 * <pre>
52 * HttpResponse httpResponse = null;
53 * try {
54 * httpResponse = httpClient.execute(httpGet);
55 * } catch (Exception e) {
56 * // error handling
57 * } finally {
58 * HttpClientUtils.closeQuietly(httpResponse);
59 * }
60 * </pre>
61 *
62 * @param response
63 * the HttpResponse to release resources, may be null or already
64 * closed.
65 *
66 * @since 4.2
67 */
68 public static void closeQuietly(final HttpResponse response) {
69 if (response != null) {
70 HttpEntity entity = response.getEntity();
71 if (entity != null) {
72 try {
73 EntityUtils.consume(entity);
74 } catch (final IOException ex) {
75 }
76 }
77 }
78 }
79
80 /**
81 * Unconditionally close a httpClient. Shuts down the underlying connection
82 * manager and releases the resources.
83 * <p>
84 * Example Code:
85 *
86 * <pre>
87 * HttpClient httpClient = null;
88 * try {
89 * httpClient = new DefaultHttpClient(...);
90 * } catch (Exception e) {
91 * // error handling
92 * } finally {
93 * HttpClientUtils.closeQuietly(httpClient);
94 * }
95 * </pre>
96 *
97 * @param httpClient
98 * the HttpClient to close, may be null or already closed.
99 * @since 4.2
100 */
101 public static void closeQuietly(final HttpClient httpClient) {
102 if (httpClient != null) {
103 httpClient.getConnectionManager().shutdown();
104 }
105 }
106
107 }