Skip to content

Commit

Permalink
9.50 stabilization fixed (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennec authored May 21, 2018
1 parent 301501c commit 9d5a6f3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 9d5a6f3

Please sign in to comment.