1515
1616import static feign .Util .UTF_8 ;
1717import static feign .Util .enumForName ;
18+ import feign .Client ;
19+ import feign .Request ;
20+ import feign .Response ;
21+ import feign .Util ;
22+ import java .io .IOException ;
23+ import java .io .InputStream ;
24+ import java .io .InputStreamReader ;
25+ import java .io .Reader ;
26+ import java .net .URI ;
27+ import java .net .URISyntaxException ;
28+ import java .nio .charset .Charset ;
29+ import java .util .ArrayList ;
30+ import java .util .Collection ;
31+ import java .util .HashMap ;
32+ import java .util .List ;
33+ import java .util .Map ;
1834import org .apache .hc .client5 .http .classic .HttpClient ;
1935import org .apache .hc .client5 .http .config .Configurable ;
2036import org .apache .hc .client5 .http .config .RequestConfig ;
2137import org .apache .hc .client5 .http .impl .classic .HttpClientBuilder ;
2238import org .apache .hc .client5 .http .protocol .HttpClientContext ;
23- import org .apache .hc .core5 .http .*;
39+ import org .apache .hc .core5 .http .ClassicHttpRequest ;
40+ import org .apache .hc .core5 .http .ClassicHttpResponse ;
41+ import org .apache .hc .core5 .http .ContentType ;
42+ import org .apache .hc .core5 .http .Header ;
43+ import org .apache .hc .core5 .http .HttpEntity ;
44+ import org .apache .hc .core5 .http .HttpHost ;
45+ import org .apache .hc .core5 .http .NameValuePair ;
2446import org .apache .hc .core5 .http .io .entity .ByteArrayEntity ;
2547import org .apache .hc .core5 .http .io .entity .EntityUtils ;
2648import org .apache .hc .core5 .http .io .entity .StringEntity ;
2749import org .apache .hc .core5 .http .io .support .ClassicRequestBuilder ;
2850import org .apache .hc .core5 .net .URIBuilder ;
2951import org .apache .hc .core5 .net .URLEncodedUtils ;
30- import java .io .*;
31- import java .net .URI ;
32- import java .net .URISyntaxException ;
33- import java .nio .charset .Charset ;
34- import java .util .*;
35- import feign .*;
3652
3753/**
3854 * This module directs Feign's http requests to Apache's
@@ -97,8 +113,7 @@ ClassicHttpRequest toClassicHttpRequest(Request request, Request.Options options
97113 requestBuilder .setUri (uri .getScheme () + "://" + uri .getAuthority () + uri .getRawPath ());
98114
99115 // request query params
100- final List <NameValuePair > queryParams =
101- URLEncodedUtils .parse (uri , requestBuilder .getCharset ());
116+ final List <NameValuePair > queryParams = URLEncodedUtils .parse (uri , requestBuilder .getCharset ());
102117 for (final NameValuePair queryParam : queryParams ) {
103118 requestBuilder .addParameter (queryParam );
104119 }
@@ -174,22 +189,22 @@ Response toFeignResponse(ClassicHttpResponse httpResponse, Request request) thro
174189
175190 final String reason = httpResponse .getReasonPhrase ();
176191
177- final Map <String , Collection <String >> headers = new HashMap <String , Collection < String > >();
192+ final Map <String , Collection <String >> headers = new HashMap <>();
178193 for (final Header header : httpResponse .getHeaders ()) {
179194 final String name = header .getName ();
180195 final String value = header .getValue ();
181196
182197 Collection <String > headerValues = headers .get (name );
183198 if (headerValues == null ) {
184- headerValues = new ArrayList <String >();
199+ headerValues = new ArrayList <>();
185200 headers .put (name , headerValues );
186201 }
187202 headerValues .add (value );
188203 }
189204
190205 return Response .builder ()
191- .protocolVersion (enumForName ( Request . ProtocolVersion . class ,
192- httpResponse .getVersion ().format ()))
206+ .protocolVersion (
207+ enumForName ( Request . ProtocolVersion . class , httpResponse .getVersion ().format ()))
193208 .status (statusCode )
194209 .reason (reason )
195210 .headers (headers )
@@ -222,7 +237,6 @@ public InputStream asInputStream() throws IOException {
222237 return entity .getContent ();
223238 }
224239
225- @ SuppressWarnings ("deprecation" )
226240 @ Override
227241 public Reader asReader () throws IOException {
228242 return new InputStreamReader (asInputStream (), UTF_8 );
@@ -236,7 +250,11 @@ public Reader asReader(Charset charset) throws IOException {
236250
237251 @ Override
238252 public void close () throws IOException {
239- EntityUtils .consume (entity );
253+ try {
254+ EntityUtils .consume (entity );
255+ } finally {
256+ httpResponse .close ();
257+ }
240258 }
241259 };
242260 }
0 commit comments