@@ -290,14 +290,17 @@ public void process(final String input) {
290290 final String requestUrl = getInput (input , url );
291291 final String requestBody = getInput (input ,
292292 body == null && method .getRequestHasBody () ? INPUT_DESIGNATOR : body );
293- Reader reader = null ;
294- if (requestBody != null ) {
295- reader = doPostOrPut (requestBody , new URL (requestUrl ));
296- }
297- else {
298- reader = doGet (requestUrl );
299- }
300- getReceiver ().process (reader );
293+
294+ final URL urlToOpen = new URL (requestUrl );
295+ final HttpURLConnection connection = requestBody != null ?
296+ doOutput (urlToOpen , requestBody ) : doRedirects (urlToOpen );
297+
298+ final InputStream inputStream = getInputStream (connection );
299+ final String charset = getContentCharset (connection );
300+
301+ getReceiver ().process (new InputStreamReader (
302+ "gzip" .equalsIgnoreCase (connection .getContentEncoding ()) ?
303+ new GZIPInputStream (inputStream ) : inputStream , charset ));
301304 }
302305 catch (final IOException e ) {
303306 throw new MetafactureException (e );
@@ -307,32 +310,6 @@ public void process(final String input) {
307310 }
308311 }
309312
310- private Reader doPostOrPut (final String requestBody , final URL urlToOpen ) throws IOException {
311- final HttpURLConnection connection = (HttpURLConnection ) urlToOpen .openConnection ();
312- connection .setDoOutput (true );
313- connection .setRequestMethod (method .name ());
314- headers .forEach (connection ::setRequestProperty );
315- connection .getOutputStream ().write (requestBody .getBytes ());
316- final InputStream inputStream = getInputStream (connection );
317- return new InputStreamReader (inputStream , getContentCharset (connection ));
318- }
319-
320- private Reader doGet (final String requestUrl ) throws IOException {
321- final Reader reader ;
322- final HttpURLConnection connection ;
323- connection = followRedirects (new URL (requestUrl ));
324- final InputStream inputStream = getInputStream (connection );
325-
326- if ("gzip" .equalsIgnoreCase (connection .getContentEncoding ())) {
327- final GZIPInputStream gzipInputStream = new GZIPInputStream (inputStream );
328- reader = new InputStreamReader (gzipInputStream , getContentCharset (connection ));
329- }
330- else {
331- reader = new InputStreamReader (inputStream , getContentCharset (connection ));
332- }
333- return reader ;
334- }
335-
336313 private String getInput (final String input , final String value ) {
337314 final String result ;
338315
@@ -350,6 +327,46 @@ else if (inputUsed) {
350327 return result ;
351328 }
352329
330+ private HttpURLConnection doOutput (final URL urlToOpen , final String requestBody ) throws IOException {
331+ final HttpURLConnection connection = openConnection (urlToOpen );
332+
333+ connection .setDoOutput (true );
334+ connection .getOutputStream ().write (requestBody .getBytes ());
335+
336+ return connection ;
337+ }
338+
339+ private HttpURLConnection doRedirects (final URL startingUrl ) throws IOException {
340+ URL urlToFollow = startingUrl ;
341+
342+ for (int i = 0 ; i < ALLOWED_REDIRECTIONS ; ++i ) {
343+ final HttpURLConnection connection = openConnection (urlToFollow );
344+ connection .setInstanceFollowRedirects (false ); // Make the logic below easier to detect redirections
345+
346+ switch (connection .getResponseCode ()) {
347+ case HttpURLConnection .HTTP_MOVED_PERM :
348+ case HttpURLConnection .HTTP_MOVED_TEMP :
349+ final String location = URLDecoder .decode (connection .getHeaderField ("Location" ), "UTF-8" );
350+ urlToFollow = new URL (urlToFollow , location ); // Deal with relative URLs
351+ break ;
352+ default :
353+ return connection ;
354+ }
355+ }
356+
357+ throw new IOException ("Too many redirects" );
358+ }
359+
360+ private HttpURLConnection openConnection (final URL urlToOpen ) throws IOException {
361+ final HttpURLConnection connection = (HttpURLConnection ) urlToOpen .openConnection ();
362+
363+ connection .setRequestMethod (method .name ());
364+ connection .setConnectTimeout (CONNECTION_TIMEOUT );
365+ headers .forEach (connection ::setRequestProperty );
366+
367+ return connection ;
368+ }
369+
353370 private InputStream getInputStream (final HttpURLConnection connection ) throws IOException {
354371 try {
355372 return connection .getInputStream ();
@@ -394,36 +411,4 @@ private String getContentCharset(final HttpURLConnection connection) {
394411 return CHARSET_DEFAULT ;
395412 }
396413
397- private HttpURLConnection followRedirects (final URL startingUrl ) throws IOException {
398- int times = 0 ;
399- HttpURLConnection conn ;
400- URL urlToFollow = startingUrl ;
401- while (true ) {
402- times = times + 1 ;
403-
404- if (times > ALLOWED_REDIRECTIONS ) {
405- throw new IOException ("Stuck in redirect loop" );
406- }
407-
408- conn = (HttpURLConnection ) urlToFollow .openConnection ();
409- headers .forEach (conn ::setRequestProperty );
410- conn .setRequestMethod (method .name ());
411- conn .setConnectTimeout (CONNECTION_TIMEOUT );
412- conn .setInstanceFollowRedirects (false ); // Make the logic below easier to detect redirections
413-
414- switch (conn .getResponseCode ()) {
415- case HttpURLConnection .HTTP_MOVED_PERM :
416- case HttpURLConnection .HTTP_MOVED_TEMP :
417- String location = conn .getHeaderField ("Location" );
418- location = URLDecoder .decode (location , "UTF-8" );
419- urlToFollow = new URL (urlToFollow , location ); // Deal with relative URLs
420- continue ;
421- default :
422- break ;
423- }
424- break ;
425- }
426- return conn ;
427- }
428-
429414}
0 commit comments