diff --git a/spring-security-rest-custom/src/test/java/org/baeldung/live/HttpLiveServiceTemp.java b/spring-security-rest-custom/src/test/java/org/baeldung/live/HttpLiveServiceTemp.java index 03378284b1a9..334e81c91526 100644 --- a/spring-security-rest-custom/src/test/java/org/baeldung/live/HttpLiveServiceTemp.java +++ b/spring-security-rest-custom/src/test/java/org/baeldung/live/HttpLiveServiceTemp.java @@ -86,17 +86,10 @@ final String expandSafe(final String urlArg) throws IOException { } final Pair expandSingleLevelSafe(final String url) throws IOException { - HttpGet request = null; - HttpEntity httpEntity = null; - InputStream entityContentStream = null; - + final HttpGet request = new HttpGet(url); try { - request = new HttpGet(url); final HttpResponse httpResponse = client.execute(request); - httpEntity = httpResponse.getEntity(); - entityContentStream = httpEntity.getContent(); - final int statusCode = httpResponse.getStatusLine().getStatusCode(); if (statusCode != 301 && statusCode != 302) { return new ImmutablePair(statusCode, url); @@ -105,34 +98,20 @@ final Pair expandSingleLevelSafe(final String url) throws IOExc Preconditions.checkState(headers.length == 1); final String newUrl = headers[0].getValue(); + EntityUtils.consume(httpResponse.getEntity()); return new ImmutablePair(statusCode, newUrl); } catch (final IllegalArgumentException uriEx) { return new ImmutablePair(500, url); } finally { - if (request != null) { - request.releaseConnection(); - } - if (entityContentStream != null) { - entityContentStream.close(); - } - if (httpEntity != null) { - EntityUtils.consume(httpEntity); - } + request.releaseConnection(); } } final String expandSingleLevel(final String url) throws IOException { - HttpGet request = null; - HttpEntity httpEntity = null; - InputStream entityContentStream = null; - + final HttpGet request = new HttpGet(url); try { - request = new HttpGet(url); - final HttpResponse httpResponse = client.execute(request); - - httpEntity = httpResponse.getEntity(); - entityContentStream = httpEntity.getContent(); + final HttpResponse httpResponse = client.execute(request); final int statusCode = httpResponse.getStatusLine().getStatusCode(); if (statusCode != 301 && statusCode != 302) { return url; @@ -140,20 +119,12 @@ final String expandSingleLevel(final String url) throws IOException { final Header[] headers = httpResponse.getHeaders(HttpHeaders.LOCATION); Preconditions.checkState(headers.length == 1); final String newUrl = headers[0].getValue(); - + EntityUtils.consume(httpResponse.getEntity()); return newUrl; } catch (final IllegalArgumentException uriEx) { return url; } finally { - if (request != null) { - request.releaseConnection(); - } - if (entityContentStream != null) { - entityContentStream.close(); - } - if (httpEntity != null) { - EntityUtils.consume(httpEntity); - } + request.releaseConnection(); } }