diff --git a/src/com/ppm/integration/agilesdk/connector/jira/JIRAIntegrationConnector.properties b/src/com/ppm/integration/agilesdk/connector/jira/JIRAIntegrationConnector.properties index cdc9851..73184ae 100644 --- a/src/com/ppm/integration/agilesdk/connector/jira/JIRAIntegrationConnector.properties +++ b/src/com/ppm/integration/agilesdk/connector/jira/JIRAIntegrationConnector.properties @@ -27,7 +27,7 @@ ERROR_CONNECTIVITY_ERROR = Connectivity Error ERROR_DOMAIN_NOT_FOUND = JIRA Server returns 404: Not Found. Check whether your JIRA server address is valid. ERROR_PROJECT_NOT_FOUND = Cannot found Project \"{0}\" ERROR_RELEASE_NOT_FOUND = Cannot found Release \"{0}\" -ERROR_AUTHENTICATION_FAILED = JIRA Server returns 401: Unathorized. Check whether your account and password are correct. +ERROR_AUTHENTICATION_FAILED = JIRA Server returns 401: Unauthorized. Check whether your account and password are correct. ERROR_RELEASE_IS_NOT_STARTED = Release \"{0}\" is not started ERROR_RELEASE_IS_FINISHED = Release \"{0}\" is finished ERROR_CANNOT_GET_CURRENT_SPRINT = Cannot get current sprint diff --git a/src/com/ppm/integration/agilesdk/connector/jira/rest/util/RestWrapper.java b/src/com/ppm/integration/agilesdk/connector/jira/rest/util/RestWrapper.java index b43965c..b69bc8f 100644 --- a/src/com/ppm/integration/agilesdk/connector/jira/rest/util/RestWrapper.java +++ b/src/com/ppm/integration/agilesdk/connector/jira/rest/util/RestWrapper.java @@ -2,6 +2,7 @@ package com.ppm.integration.agilesdk.connector.jira.rest.util; import com.ppm.integration.agilesdk.connector.jira.rest.util.exception.RestRequestException; +import org.apache.commons.lang.StringUtils; import org.apache.wink.client.ClientResponse; import org.apache.wink.client.Resource; import org.apache.wink.client.RestClient; @@ -91,29 +92,38 @@ public ClientResponse sendGet(String uri) { Resource resource = this.getJIRAResource(uri); ClientResponse response = resource.get(); - int statusCode = response.getStatusCode(); + checkResponseStatus(200, response, uri, "GET", null); - if (statusCode != 200) { - // for easier troubleshooting, include the request URI in the exception message - throw new RestRequestException( - statusCode, - String.format("Unexpected HTTP response status code %s for %s", statusCode, uri)); + return response; + } + + private void checkResponseStatus(int expectedHttpStatusCode, ClientResponse response, String uri, String verb, String payload) { + + if (response.getStatusCode() != expectedHttpStatusCode) { + StringBuilder errorMessage = new StringBuilder(String.format("## Unexpected HTTP response status code %s for %s uri %s, expected %s", response.getStatusCode(), verb, uri, expectedHttpStatusCode)); + if (payload != null) { + errorMessage.append(System.lineSeparator()).append(System.lineSeparator()).append("# Sent Payload:").append(System.lineSeparator()).append(payload); + } + String responseStr = null; + try { + responseStr = response.getEntity(String.class); + } catch (Exception e) { + // we don't do anything if we cannot get the response. + } + if (!StringUtils.isBlank(responseStr)) { + errorMessage.append(System.lineSeparator()).append(System.lineSeparator()).append("# Received Response:").append(System.lineSeparator()).append(responseStr); + } + + throw new RestRequestException(response.getStatusCode(), errorMessage.toString()); } - return response; } public ClientResponse sendPost(String uri, String jsonPayload, int expectedHttpStatusCode) { Resource resource = this.getJIRAResource(uri); ClientResponse response = resource.post(jsonPayload); - int statusCode = response.getStatusCode(); - - if (statusCode != expectedHttpStatusCode) { - // for easier troubleshooting, include the request URI in the exception message - throw new RestRequestException(statusCode, - String.format("Unexpected HTTP response status code %s for %s, expected %s", statusCode, uri, expectedHttpStatusCode)); - } + checkResponseStatus(expectedHttpStatusCode, response, uri, "POST", jsonPayload); return response; } @@ -122,13 +132,7 @@ public ClientResponse sendPut(String uri, String jsonPayload, int expectedHttpSt Resource resource = this.getJIRAResource(uri); ClientResponse response = resource.put(jsonPayload); - int statusCode = response.getStatusCode(); - - if (statusCode != expectedHttpStatusCode) { - // for easier troubleshooting, include the request URI in the exception message - throw new RestRequestException(statusCode, - String.format("Unexpected HTTP response status code %s for %s, expected %s", statusCode, uri, expectedHttpStatusCode)); - } + checkResponseStatus(expectedHttpStatusCode, response, uri, "PUT", jsonPayload); return response; } diff --git a/src/com/ppm/integration/agilesdk/connector/jira/rest/util/exception/JIRAConnectivityExceptionHandler.java b/src/com/ppm/integration/agilesdk/connector/jira/rest/util/exception/JIRAConnectivityExceptionHandler.java index 56c2a92..bd19f0e 100644 --- a/src/com/ppm/integration/agilesdk/connector/jira/rest/util/exception/JIRAConnectivityExceptionHandler.java +++ b/src/com/ppm/integration/agilesdk/connector/jira/rest/util/exception/JIRAConnectivityExceptionHandler.java @@ -3,6 +3,7 @@ import java.lang.Thread.UncaughtExceptionHandler; +import com.ppm.integration.agilesdk.provider.Providers; import org.apache.wink.client.ClientRuntimeException; import com.ppm.integration.IntegrationException; @@ -37,8 +38,9 @@ private void handleClientException(RestRequestException e, Class cls) { throw IntegrationException.build(cls).setErrorCode("PPM_INT_JIRA_ERR_" + e.getStatusCode()) .setMessage("ERROR_BAD_REQUEST"); case 401: + String error_message_auth = Providers.getLocalizationProvider(JIRAIntegrationConnector.class).getConnectorText("ERROR_AUTHENTICATION_FAILED"); throw IntegrationException.build(cls).setErrorCode("PPM_INT_JIRA_ERR_" + e.getStatusCode()) - .setMessage("ERROR_AUTHENTICATION_FAILED"); + .setMessage(error_message_auth); default: throw IntegrationException.build(cls).setErrorCode("PPM_INT_JIRA_ERR_202") .setMessage("ERROR_CONNECTIVITY_ERROR", e.getMessage());