diff --git a/pom.xml b/pom.xml index 6890387..6c3eafc 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ edu.ohio.ais.rundeck rundeck-http-plugin - 1.0.8 + 1.0.9-SNAPSHOT jar ${project.groupId}:${project.artifactId} Perform HTTP requests with authentication as a Rundeck workflow step. diff --git a/src/main/java/edu/ohio/ais/rundeck/HttpWorkflowStepPlugin.java b/src/main/java/edu/ohio/ais/rundeck/HttpWorkflowStepPlugin.java index a5130d1..37e0263 100644 --- a/src/main/java/edu/ohio/ais/rundeck/HttpWorkflowStepPlugin.java +++ b/src/main/java/edu/ohio/ais/rundeck/HttpWorkflowStepPlugin.java @@ -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; @@ -238,7 +240,7 @@ public Description getDescription() { .build(); } - protected HttpClient getHttpClient(Map options) throws GeneralSecurityException { + protected CloseableHttpClient getHttpClient(Map options) throws GeneralSecurityException { HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); httpClientBuilder.disableAuthCaching(); @@ -277,8 +279,9 @@ protected void doRequest(Map 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()) || @@ -386,6 +389,14 @@ protected void doRequest(Map 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(); + } + } } } @@ -580,21 +591,27 @@ public void executeStep(PluginStepContext pluginStepContext, Map private StringBuffer getPageContent(HttpResponse response) { BufferedReader rd = null; - try { - rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); - } catch (IOException e) { - e.printStackTrace(); - } - + HttpEntity reponseEntity = response.getEntity(); StringBuffer result = new StringBuffer(); - String line = ""; - try { - while ((line = rd.readLine()) != null) { - result.append(line); + if ( reponseEntity != null ) { + try { + rd = new BufferedReader(new InputStreamReader(reponseEntity.getContent())); + 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(); + } + } } - } catch (IOException e) { - e.printStackTrace(); } return result; diff --git a/src/test/java/edu/ohio/ais/rundeck/HttpWorkflowStepPluginTest.java b/src/test/java/edu/ohio/ais/rundeck/HttpWorkflowStepPluginTest.java index d98f729..da362ee 100644 --- a/src/test/java/edu/ohio/ais/rundeck/HttpWorkflowStepPluginTest.java +++ b/src/test/java/edu/ohio/ais/rundeck/HttpWorkflowStepPluginTest.java @@ -26,6 +26,7 @@ public class HttpWorkflowStepPluginTest { protected static final String REMOTE_OAUTH_EXPIRED_URL = "/oauth-expired"; protected static final String ERROR_URL_500 = "/error500"; protected static final String ERROR_URL_401 = "/error401"; + protected static final String NO_CONTENT_URL = "/nocontent204"; protected static final String OAUTH_CLIENT_MAP_KEY = OAuthClientTest.CLIENT_VALID + "@" + OAuthClientTest.BASE_URI + OAuthClientTest.ENDPOINT_TOKEN; @@ -126,6 +127,11 @@ public void setUp() { WireMock.stubFor(WireMock.request(method, WireMock.urlEqualTo(ERROR_URL_500)) .willReturn(WireMock.aResponse() .withStatus(500))); + + // 204 No Content + WireMock.stubFor(WireMock.request(method, WireMock.urlEqualTo(NO_CONTENT_URL)) + .willReturn(WireMock.aResponse() + .withStatus(204))); } // Simple bogus URL that yields a 404 @@ -302,4 +308,16 @@ public void canHandle500ErrorWithOAuth() throws StepException { this.plugin.executeStep(new PluginStepContextImpl(), options); } + + @Test + public void canPrintNoContent() throws StepException { + Map options = new HashMap<>(); + + options.put("remoteUrl", OAuthClientTest.BASE_URI + NO_CONTENT_URL); + options.put("method", "GET"); + options.put("printResponse",true); + options.put("printResponseToFile",false); + + this.plugin.executeStep(new PluginStepContextImpl(), options); + } }