Guest User

Untitled

a guest
Nov 19th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. public static String getSomething(final String uuid) {
  2. String serviceUrl = getServiceUrl();
  3. String path = "user/" + , uuid);
  4. String requestUrl = serviceUrl + path;
  5. String httpMethod = "GET";
  6.  
  7. Response response = client
  8. .target(serviceUrl)
  9. .path(path)
  10. .request(ExtendedMediaType.APPLICATION_UTF8)
  11. .get();
  12.  
  13. if (response.getStatus() == Response.Status.OK.getStatusCode()) {
  14. // HTTP 200
  15. return response.readEntity(String.class);
  16. } else {
  17. // confusing code comes here just because
  18. // I need to decide the type of HTTP 404...
  19.  
  20. // trying to parse response body
  21. try {
  22. String responseBody = response.readEntity(String.class);
  23. ObjectMapper mapper = new ObjectMapper();
  24. ErrorInfo errorInfo = mapper.readValue(responseBody, ErrorInfo.class);
  25.  
  26. // re-throw the original exception
  27. throw new Exception("Response error code: " + response.getInternalErrorCode());
  28.  
  29. } catch (IOException e) {
  30. // this is a real HTTP 404
  31. throw new ServiceUnavailableError(response, requestUrl, httpMethod);
  32. }
  33.  
  34. // this exception will never be thrown
  35. throw new Exception("UNEXPECTED ERRORS, BETTER IF YOU DO NOT SEE IT IN THE LOG");
  36. }
  37.  
  38. public static String getString(final String processId, final String key) {
  39. String serviceUrl = getServiceUrl();
  40. String path = String.format("key/%s", key);
  41. String requestUrl = serviceUrl + path;
  42. String httpMethod = "GET";
  43.  
  44. log(requestUrl);
  45.  
  46. Response response = client
  47. .target(serviceUrl)
  48. .path(path)
  49. .request(ExtendedMediaType.APPLICATION_JSON_UTF8)
  50. .header(CustomHttpHeader.PROCESS_ID, processId)
  51. .get();
  52.  
  53. if (response.getStatus() == Response.Status.OK.getStatusCode()) {
  54. return response.readEntity(String.class);
  55. } else {
  56. String body = response.readEntity(String.class);
  57. ObjectMapper mapper = new ObjectMapper();
  58. ErrorInfo errorInfo = mapper.readValue(body, ErrorInfo.class);
  59. throw new Exception("Response error code: " + response.getInternalErrorCode());
  60. }
  61.  
  62. throw new AnyServerError(response, requestUrl, httpMethod);
  63. }
Add Comment
Please, Sign In to add comment