From b34a6e39db3dad61d9aa9f9646e1c394188c5d4f Mon Sep 17 00:00:00 2001 From: Vincent Behar Date: Tue, 26 Jul 2011 14:26:08 +0200 Subject: [PATCH] follow redirects to /api/error for POST and DELETE requests --- src/main/java/org/rundeck/api/ApiCall.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/main/java/org/rundeck/api/ApiCall.java b/src/main/java/org/rundeck/api/ApiCall.java index 2281804..e701034 100644 --- a/src/main/java/org/rundeck/api/ApiCall.java +++ b/src/main/java/org/rundeck/api/ApiCall.java @@ -214,6 +214,23 @@ class ApiCall { } } + // in case of error, we get a redirect to /api/error + // that we need to follow manually for POST and DELETE requests (as GET) + if (response.getStatusLine().getStatusCode() / 100 == 3) { + String newLocation = response.getFirstHeader("Location").getValue(); + try { + EntityUtils.consume(response.getEntity()); + } catch (IOException e) { + throw new RundeckApiException("Failed to consume entity (release connection)", e); + } + request = new HttpGet(newLocation); + try { + response = httpClient.execute(request); + } catch (IOException 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) if (response.getStatusLine().getStatusCode() / 100 != 2) { throw new RundeckApiException("Invalid HTTP response '" + response.getStatusLine() + "' for "