001/* 002 * ==================================================================== 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 * ==================================================================== 020 * 021 * This software consists of voluntary contributions made by many 022 * individuals on behalf of the Apache Software Foundation. For more 023 * information on the Apache Software Foundation, please see 024 * <http://www.apache.org/>. 025 * 026 */ 027package org.apache.http.client.utils; 028 029import java.io.Closeable; 030import java.io.IOException; 031 032import org.apache.http.HttpEntity; 033import org.apache.http.HttpResponse; 034import org.apache.http.client.HttpClient; 035import org.apache.http.client.methods.CloseableHttpResponse; 036import org.apache.http.util.EntityUtils; 037 038/** 039 * Convenience methods for closing response and client objects. 040 * 041 * @since 4.2 042 */ 043public class HttpClientUtils { 044 045 private HttpClientUtils() { 046 } 047 048 /** 049 * Unconditionally close a response. 050 * <p> 051 * Example Code: 052 * 053 * <pre> 054 * HttpResponse httpResponse = null; 055 * try { 056 * httpResponse = httpClient.execute(httpGet); 057 * } catch (Exception e) { 058 * // error handling 059 * } finally { 060 * HttpClientUtils.closeQuietly(httpResponse); 061 * } 062 * </pre> 063 * 064 * @param response 065 * the HttpResponse to release resources, may be null or already 066 * closed. 067 * 068 * @since 4.2 069 */ 070 public static void closeQuietly(final HttpResponse response) { 071 if (response != null) { 072 final HttpEntity entity = response.getEntity(); 073 if (entity != null) { 074 try { 075 EntityUtils.consume(entity); 076 } catch (final IOException ex) { 077 } 078 } 079 } 080 } 081 082 /** 083 * Unconditionally close a response. 084 * <p> 085 * Example Code: 086 * 087 * <pre> 088 * HttpResponse httpResponse = null; 089 * try { 090 * httpResponse = httpClient.execute(httpGet); 091 * } catch (Exception e) { 092 * // error handling 093 * } finally { 094 * HttpClientUtils.closeQuietly(httpResponse); 095 * } 096 * </pre> 097 * 098 * @param response 099 * 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}