PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/common/src/main/java/com/gooddata/exception/HttpMethodException.java

https://github.com/chrbayer84/GoodData-CL
Java | 138 lines | 56 code | 15 blank | 67 comment | 6 complexity | 58adca50c83b541d129218db43451d4c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. * Copyright (c) 2009, GoodData Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without modification, are permitted provided
  5. * that the following conditions are met:
  6. *
  7. * * Redistributions of source code must retain the above copyright notice, this list of conditions and
  8. * the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
  10. * and the following disclaimer in the documentation and/or other materials provided with the distribution.
  11. * * Neither the name of the GoodData Corporation nor the names of its contributors may be used to endorse
  12. * or promote products derived from this software without specific prior written permission.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  15. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  16. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  19. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  20. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  21. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. /*
  24. * To change this template, choose Tools | Templates
  25. * and open the template in the editor.
  26. */
  27. package com.gooddata.exception;
  28. import java.util.Formatter;
  29. import net.sf.json.JSONException;
  30. import net.sf.json.JSONObject;
  31. import org.apache.commons.httpclient.HttpMethod;
  32. /**
  33. * @author jiri.zaloudek
  34. */
  35. public class HttpMethodException extends GdcRestApiException {
  36. private HttpMethod guiltyMethod = null;
  37. /**
  38. * Constructs an instance of <code>HttpMethodException</code> with the specified detail message.
  39. *
  40. * @param msg the detail message.
  41. */
  42. public HttpMethodException(String msg) {
  43. super(msg);
  44. }
  45. /**
  46. * Returns an error message for <code>HttpMethodException</code>, attempting to
  47. * use GoodData error message or HTTP status line for an exception that was constructed
  48. * from a response to HTTP method call.
  49. */
  50. public String getMessage() {
  51. if (guiltyMethod == null)
  52. return super.getMessage();
  53. String msg = guiltyMethod.getStatusCode() + " " + guiltyMethod.getStatusText();
  54. String body = null;
  55. try {
  56. body = guiltyMethod.getResponseBodyAsString();
  57. } catch (java.io.IOException ioexception) {
  58. /* No body? No problem, msg is already set fine. */
  59. }
  60. if (body != null) {
  61. try {
  62. JSONObject error = JSONObject.fromObject(body);
  63. /* Error structure sometimes lacks the tag... */
  64. if (error.has("error"))
  65. error = error.getJSONObject("error");
  66. msg = formatErrorMessage(error);
  67. } catch (JSONException jsone) {
  68. /* Do not worry about the non-standard or broken
  69. * error JSON. The msg is already meaningful enough.*/
  70. }
  71. }
  72. return msg;
  73. }
  74. /**
  75. * Returns formatted error message if the message contains parameters
  76. * placeholder (%s)
  77. *
  78. * @param error
  79. * @return
  80. */
  81. private static String formatErrorMessage(JSONObject error) {
  82. final String orig = error.getString("message");
  83. if (!orig.contains("%s")) {
  84. return orig;
  85. }
  86. return new Formatter().format(orig, error.getJSONArray("parameters").toArray()).toString();
  87. }
  88. /**
  89. * Returns the request id for <code>HttpMethodException</code> that was constructed
  90. * from a response to HTTP method call.
  91. */
  92. public String getRequestId() {
  93. try {
  94. return guiltyMethod.getResponseHeader("X-GDC-Request").getValue();
  95. } catch (Exception e) {
  96. return null;
  97. }
  98. }
  99. /**
  100. * Constructs an instance of <code>HttpMethodException</code> for a response to HTTP method call,
  101. * interpreting GoodData error structures.
  102. *
  103. * @param method the call
  104. * @param throwable original exception
  105. */
  106. public HttpMethodException(HttpMethod method, Throwable throwable) {
  107. super(throwable);
  108. guiltyMethod = method;
  109. }
  110. /**
  111. * Constructs an instance of <code>HttpMethodException</code> for a response to HTTP method call,
  112. * interpreting GoodData error structures.
  113. *
  114. * @param method the call
  115. */
  116. public HttpMethodException(HttpMethod method) {
  117. super((String) null);
  118. guiltyMethod = method;
  119. }
  120. public HttpMethodException(String msg, Throwable e) {
  121. super(msg, e);
  122. }
  123. }