/root/projects/repository/source/java/org/alfresco/util/json/ExceptionJsonSerializer.java

https://github.com/deas/alfresco · Java · 164 lines · 133 code · 9 blank · 22 comment · 18 complexity · a5cfa44910f0ad3e811d8141ebbe302f MD5 · raw file

  1. /*
  2. * Copyright (C) 2005-2010 Alfresco Software Limited.
  3. *
  4. * This file is part of Alfresco
  5. *
  6. * Alfresco is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Alfresco is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package org.alfresco.util.json;
  20. import java.lang.reflect.Constructor;
  21. import java.util.Arrays;
  22. import java.util.Collections;
  23. import java.util.List;
  24. import org.alfresco.error.AlfrescoRuntimeException;
  25. import org.alfresco.service.cmr.transfer.TransferException;
  26. import org.apache.commons.logging.Log;
  27. import org.apache.commons.logging.LogFactory;
  28. import org.json.JSONArray;
  29. import org.json.JSONException;
  30. import org.json.JSONObject;
  31. public class ExceptionJsonSerializer implements JsonSerializer<Throwable, JSONObject>
  32. {
  33. private final static Log log = LogFactory.getLog(ExceptionJsonSerializer.class);
  34. @Override
  35. public Throwable deserialize(JSONObject errorJSON)
  36. {
  37. if (errorJSON == null)
  38. {
  39. return null;
  40. }
  41. Throwable result = null;
  42. Object createdObject = null;
  43. try
  44. {
  45. //errorType and errorMessage should always be reported
  46. String errorType = errorJSON.getString("errorType");
  47. String errorMessage = errorJSON.getString("errorMessage");
  48. if (errorType == null)
  49. {
  50. errorType = Exception.class.getName();
  51. }
  52. if (errorMessage == null)
  53. {
  54. errorMessage = "";
  55. }
  56. //alfrescoErrorId and alfrescoErrorParams will only appear if the
  57. //throwable object was of a subclass of AlfrescoRuntimeException
  58. String errorId = errorJSON.optString("alfrescoMessageId", null);
  59. Object[] errorParams = new Object[0];
  60. JSONArray errorParamArray = errorJSON.optJSONArray("alfrescoMessageParams");
  61. if (errorParamArray != null)
  62. {
  63. int length = errorParamArray.length();
  64. errorParams = new Object[length];
  65. for (int i = 0; i < length; ++i)
  66. {
  67. errorParams[i] = errorParamArray.getString(i);
  68. }
  69. }
  70. Class<?> errorClass;
  71. try
  72. {
  73. errorClass = Class.forName(errorType);
  74. }
  75. catch (ClassNotFoundException e)
  76. {
  77. errorClass = Exception.class;
  78. }
  79. Constructor<?> constructor = null;
  80. try
  81. {
  82. try
  83. {
  84. constructor = errorClass.getConstructor(String.class, Object[].class);
  85. createdObject = constructor.newInstance(errorId, errorParams);
  86. }
  87. catch (NoSuchMethodException e)
  88. {
  89. try
  90. {
  91. constructor = errorClass.getConstructor(String.class);
  92. createdObject = constructor.newInstance(errorId == null ? errorMessage : errorId);
  93. }
  94. catch (NoSuchMethodException e1)
  95. {
  96. try
  97. {
  98. constructor = errorClass.getConstructor();
  99. createdObject = constructor.newInstance();
  100. }
  101. catch (NoSuchMethodException e2)
  102. {
  103. }
  104. }
  105. }
  106. }
  107. catch(Exception ex)
  108. {
  109. //We don't need to do anything here. Code below will fix things up
  110. }
  111. if (createdObject == null || !Throwable.class.isAssignableFrom(createdObject.getClass()))
  112. {
  113. result = new TransferException(errorId == null ? errorMessage : errorId, errorParams);
  114. }
  115. else
  116. {
  117. result = (Throwable)createdObject;
  118. }
  119. }
  120. catch(JSONException ex)
  121. {
  122. if (log.isDebugEnabled())
  123. {
  124. log.debug("Failed to deserialize Throwable object from JSON object", ex);
  125. }
  126. }
  127. return result;
  128. }
  129. @Override
  130. public JSONObject serialize(Throwable object)
  131. {
  132. JSONObject errorObject = new JSONObject();
  133. try
  134. {
  135. errorObject.put("errorType", object.getClass().getName());
  136. errorObject.put("errorMessage", object.getMessage());
  137. if (AlfrescoRuntimeException.class.isAssignableFrom(object.getClass()))
  138. {
  139. AlfrescoRuntimeException alfEx = (AlfrescoRuntimeException)object;
  140. errorObject.put("alfrescoMessageId", alfEx.getMsgId());
  141. Object[] msgParams = alfEx.getMsgParams();
  142. List<Object> params = msgParams == null ? Collections.emptyList() : Arrays.asList(msgParams);
  143. errorObject.put("alfrescoMessageParams", params);
  144. }
  145. }
  146. catch (JSONException e)
  147. {
  148. if (log.isDebugEnabled())
  149. {
  150. log.debug("Failed to serialize Throwable object into JSON object", e);
  151. }
  152. }
  153. return errorObject;
  154. }
  155. }