@@ -22,8 +22,8 @@ public final class DownloaderTestImpl extends Downloader {
2222 /**
2323 * Should be the latest Firefox ESR version.
2424 */
25- private static final String USER_AGENT
26- = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0" ;
25+ private static final String USER_AGENT =
26+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0" ;
2727 private static DownloaderTestImpl instance ;
2828 private final RateLimitedClientWrapper clientWrapper ;
2929
@@ -65,41 +65,36 @@ public Response execute(@Nonnull final Request request)
6565 }
6666
6767 final okhttp3 .Request .Builder requestBuilder = new okhttp3 .Request .Builder ()
68- .method (httpMethod , requestBody ).url (url )
69- .addHeader ("User-Agent" , USER_AGENT );
70-
71- for (final Map .Entry <String , List <String >> pair : headers .entrySet ()) {
72- final String headerName = pair .getKey ();
73- final List <String > headerValueList = pair .getValue ();
68+ .method (httpMethod , requestBody )
69+ .url (url )
70+ .addHeader ("User-Agent" , USER_AGENT );
71+
72+ headers .forEach ((headerName , headerValueList ) -> {
73+ requestBuilder .removeHeader (headerName );
74+ headerValueList .forEach (headerValue ->
75+ requestBuilder .addHeader (headerName , headerValue ));
76+ });
77+
78+ try (okhttp3 .Response response =
79+ clientWrapper .executeRequestWithLimit (requestBuilder .build ())
80+ ) {
81+ if (response .code () == 429 ) {
82+ throw new ReCaptchaException ("reCaptcha Challenge requested" , url );
83+ }
7484
75- if ( headerValueList . size () > 1 ) {
76- requestBuilder . removeHeader ( headerName );
77- for ( final String headerValue : headerValueList ) {
78- requestBuilder . addHeader ( headerName , headerValue );
85+ String responseBodyToReturn = null ;
86+ try ( ResponseBody body = response . body ()) {
87+ if ( body != null ) {
88+ responseBodyToReturn = body . string ( );
7989 }
80- } else if (headerValueList .size () == 1 ) {
81- requestBuilder .header (headerName , headerValueList .get (0 ));
8290 }
83- }
84-
85- final okhttp3 .Response response =
86- clientWrapper .executeRequestWithLimit (requestBuilder .build ());
87-
88- if (response .code () == 429 ) {
89- response .close ();
9091
91- throw new ReCaptchaException ("reCaptcha Challenge requested" , url );
92+ return new Response (
93+ response .code (),
94+ response .message (),
95+ response .headers ().toMultimap (),
96+ responseBodyToReturn ,
97+ response .request ().url ().toString ());
9298 }
93-
94- final ResponseBody body = response .body ();
95- String responseBodyToReturn = null ;
96-
97- if (body != null ) {
98- responseBodyToReturn = body .string ();
99- }
100-
101- final String latestUrl = response .request ().url ().toString ();
102- return new Response (response .code (), response .message (), response .headers ().toMultimap (),
103- responseBodyToReturn , latestUrl );
10499 }
105100}
0 commit comments