Support http DELETE with expected 204 no content

This commit is contained in:
Greg Schueler 2014-03-07 12:16:10 -08:00
parent e548c14b24
commit 29459d9ee1
2 changed files with 48 additions and 5 deletions

View file

@ -325,6 +325,22 @@ class ApiCall {
RundeckApiLoginException, RundeckApiTokenException { RundeckApiLoginException, RundeckApiTokenException {
return execute(new HttpDelete(client.getUrl() + client.getApiEndpoint() + apiPath), parser); return execute(new HttpDelete(client.getUrl() + client.getApiEndpoint() + apiPath), parser);
} }
/**
* Execute an HTTP DELETE request to the RunDeck instance, on the given path, and expect a 204 response.
*
* @param apiPath on which we will make the HTTP request - see {@link ApiPathBuilder}
* @throws RundeckApiException in case of error when calling the API
* @throws RundeckApiLoginException if the login fails (in case of login-based authentication)
* @throws RundeckApiTokenException if the token is invalid (in case of token-based authentication)
*/
public void delete(ApiPathBuilder apiPath) throws RundeckApiException,
RundeckApiLoginException, RundeckApiTokenException {
InputStream response = execute(new HttpDelete(client.getUrl() + client.getApiEndpoint() + apiPath));
if(null!=response){
throw new RundeckApiException("Unexpected Rundeck response content, expected no content!");
}
}
/** /**
* Execute an HTTP request to the RunDeck instance. We will login first, and then execute the API call. At the end, * Execute an HTTP request to the RunDeck instance. We will login first, and then execute the API call. At the end,
@ -377,7 +393,8 @@ class ApiCall {
// in case of error, we get a redirect to /api/error // in case of error, we get a redirect to /api/error
// that we need to follow manually for POST and DELETE requests (as GET) // that we need to follow manually for POST and DELETE requests (as GET)
if (response.getStatusLine().getStatusCode() / 100 == 3) { int statusCode = response.getStatusLine().getStatusCode();
if (statusCode / 100 == 3) {
String newLocation = response.getFirstHeader("Location").getValue(); String newLocation = response.getFirstHeader("Location").getValue();
try { try {
EntityUtils.consume(response.getEntity()); EntityUtils.consume(response.getEntity());
@ -387,22 +404,26 @@ class ApiCall {
request = new HttpGet(newLocation); request = new HttpGet(newLocation);
try { try {
response = httpClient.execute(request); response = httpClient.execute(request);
statusCode = response.getStatusLine().getStatusCode();
} catch (IOException e) { } catch (IOException e) {
throw new RundeckApiException("Failed to execute an HTTP GET on url : " + request.getURI(), e); throw new RundeckApiException("Failed to execute an HTTP GET on url : " + request.getURI(), e);
} }
} }
// check the response code (should be 2xx, even in case of error : error message is in the XML result) // check the response code (should be 2xx, even in case of error : error message is in the XML result)
if (response.getStatusLine().getStatusCode() / 100 != 2) { if (statusCode / 100 != 2) {
if (response.getStatusLine().getStatusCode() == 403 && if (statusCode == 403 &&
(client.getToken() != null || client.getSessionID() != null)) { (client.getToken() != null || client.getSessionID() != null)) {
throw new RundeckApiTokenException("Invalid Token or sessionID ! Got HTTP response '" + response.getStatusLine() throw new RundeckApiTokenException("Invalid Token or sessionID ! Got HTTP response '" + response.getStatusLine()
+ "' for " + request.getURI()); + "' for " + request.getURI());
} else { } else {
throw new RundeckApiException("Invalid HTTP response '" + response.getStatusLine() + "' for " throw new RundeckApiException.RundeckApiHttpStatusException("Invalid HTTP response '" + response.getStatusLine() + "' for "
+ request.getURI()); + request.getURI(), statusCode);
} }
} }
if(statusCode==204){
return null;
}
if (response.getEntity() == null) { if (response.getEntity() == null) {
throw new RundeckApiException("Empty RunDeck response ! HTTP status line is : " throw new RundeckApiException("Empty RunDeck response ! HTTP status line is : "
+ response.getStatusLine()); + response.getStatusLine());

View file

@ -82,5 +82,27 @@ public class RundeckApiException extends RuntimeException {
super(message, cause); super(message, cause);
} }
} }
/**
* Error due to unexpected HTTP status
*/
public static class RundeckApiHttpStatusException extends RundeckApiAuthException {
private static final long serialVersionUID = 1L;
private int statusCode;
public RundeckApiHttpStatusException(String message, int statusCode) {
super(message);
this.statusCode = statusCode;
}
public RundeckApiHttpStatusException(String message, Throwable cause, int statusCode) {
super(message, cause);
this.statusCode = statusCode;
}
public int getStatusCode() {
return statusCode;
}
}
} }