PageRenderTime 58ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/atlassian/jira/plugins/bitbucket/spi/ExtendedResponseHandler.java

https://bitbucket.org/atlassian/jira-bitbucket-connector/
Java | 64 lines | 49 code | 10 blank | 5 comment | 0 complexity | 23261758c485825b7306a1e7a7d04c0d MD5 | raw file
  1. package com.atlassian.jira.plugins.bitbucket.spi;
  2. import java.text.MessageFormat;
  3. import java.util.concurrent.atomic.AtomicReference;
  4. import com.atlassian.sal.api.net.Response;
  5. import com.atlassian.sal.api.net.ResponseException;
  6. import com.atlassian.sal.api.net.ResponseHandler;
  7. /**
  8. * Default implementation of ResponseHandler throws ResponseException if the response is not successful.
  9. * But the unfortunately exception doesn't provide the error code. We need to know if error is UNAUTHORISED or NOT_FOUND.
  10. * This handler keeps the response including error code and provides it for later use.
  11. */
  12. public class ExtendedResponseHandler implements ResponseHandler<Response>
  13. {
  14. private final AtomicReference<ExtendedResponse> extendedResponse = new AtomicReference<ExtendedResponse>();
  15. @Override
  16. public void handle(Response response) throws ResponseException
  17. {
  18. ExtendedResponse er = new ExtendedResponse(response.isSuccessful(), response.getStatusCode(), response.getResponseBodyAsString());
  19. extendedResponse.set(er);
  20. }
  21. public ExtendedResponse getExtendedResponse()
  22. {
  23. return extendedResponse.get();
  24. }
  25. public static class ExtendedResponse
  26. {
  27. private final boolean successful;
  28. private final int statusCode;
  29. private final String responseString;
  30. public ExtendedResponse(boolean successful, int statusCode, String responseString)
  31. {
  32. this.successful = successful;
  33. this.statusCode = statusCode;
  34. this.responseString = responseString;
  35. }
  36. public boolean isSuccessful()
  37. {
  38. return successful;
  39. }
  40. public int getStatusCode()
  41. {
  42. return statusCode;
  43. }
  44. public String getResponseString()
  45. {
  46. return responseString;
  47. }
  48. @Override
  49. public String toString()
  50. {
  51. return MessageFormat.format("successful: {0}, statusCode: {1}, responseString: {2}", successful, statusCode, responseString);
  52. }
  53. }
  54. }