Skip to content

Commit

Permalink
Ensure response stream and response are closed
Browse files Browse the repository at this point in the history
  • Loading branch information
driseley committed Aug 9, 2018
1 parent a6f7e7b commit a72b2f1
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/main/java/edu/ohio/ais/rundeck/HttpWorkflowStepPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
Expand Down Expand Up @@ -238,7 +240,7 @@ public Description getDescription() {
.build();
}

protected HttpClient getHttpClient(Map<String, Object> options) throws GeneralSecurityException {
protected CloseableHttpClient getHttpClient(Map<String, Object> options) throws GeneralSecurityException {
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

httpClientBuilder.disableAuthCaching();
Expand Down Expand Up @@ -277,8 +279,9 @@ protected void doRequest(Map<String, Object> options, HttpUriRequest request, In
if(attempts > MAX_ATTEMPTS) {
throw new StepException("Unable to complete request after maximum number of attempts.", StepFailureReason.IOFailure);
}
CloseableHttpResponse response = null;
try {
HttpResponse response = this.getHttpClient(options).execute(request);
response = this.getHttpClient(options).execute(request);

//print the response content
if(options.containsKey("printResponse") && Boolean.parseBoolean(options.get("printResponse").toString()) ||
Expand Down Expand Up @@ -386,6 +389,14 @@ protected void doRequest(Map<String, Object> options, HttpUriRequest request, In
StepException sse = new StepException("Error when sending request: " + se.getMessage(), Reason.HTTPFailure);
se.initCause(se);
throw sse;
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

Expand Down Expand Up @@ -586,17 +597,20 @@ private StringBuffer getPageContent(HttpResponse response) {
if ( reponseEntity != null ) {
try {
rd = new BufferedReader(new InputStreamReader(reponseEntity.getContent()));
} catch (IOException e) {
e.printStackTrace();
}

String line = "";
try {
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (rd != null) {
try {
rd.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

Expand Down

0 comments on commit a72b2f1

Please sign in to comment.