mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-10-08 14:51:49 +00:00
125781 rmunteanu: Merged 5.1.N (5.1.2) to 5.2.N (5.2.1) 125603 rmunteanu: Merged 5.1.1 (5.1.1) to 5.1.N (5.1.2) 125484 slanglois: MNT-16155 Update source headers - remove old Copyrights from Java and JSP dource files git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@127808 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
147 lines
5.1 KiB
Java
147 lines
5.1 KiB
Java
package org.alfresco.util.json;
|
|
|
|
import java.lang.reflect.Constructor;
|
|
import java.util.Arrays;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
|
|
import org.alfresco.error.AlfrescoRuntimeException;
|
|
import org.alfresco.service.cmr.transfer.TransferException;
|
|
import org.apache.commons.logging.Log;
|
|
import org.apache.commons.logging.LogFactory;
|
|
import org.json.JSONArray;
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
|
|
public class ExceptionJsonSerializer implements JsonSerializer<Throwable, JSONObject>
|
|
{
|
|
private final static Log log = LogFactory.getLog(ExceptionJsonSerializer.class);
|
|
|
|
@Override
|
|
public Throwable deserialize(JSONObject errorJSON)
|
|
{
|
|
if (errorJSON == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
Throwable result = null;
|
|
Object createdObject = null;
|
|
|
|
try
|
|
{
|
|
//errorType and errorMessage should always be reported
|
|
String errorType = errorJSON.getString("errorType");
|
|
String errorMessage = errorJSON.getString("errorMessage");
|
|
|
|
if (errorType == null)
|
|
{
|
|
errorType = Exception.class.getName();
|
|
}
|
|
if (errorMessage == null)
|
|
{
|
|
errorMessage = "";
|
|
}
|
|
//alfrescoErrorId and alfrescoErrorParams will only appear if the
|
|
//throwable object was of a subclass of AlfrescoRuntimeException
|
|
String errorId = errorJSON.optString("alfrescoMessageId", null);
|
|
Object[] errorParams = new Object[0];
|
|
JSONArray errorParamArray = errorJSON.optJSONArray("alfrescoMessageParams");
|
|
if (errorParamArray != null)
|
|
{
|
|
int length = errorParamArray.length();
|
|
errorParams = new Object[length];
|
|
for (int i = 0; i < length; ++i)
|
|
{
|
|
errorParams[i] = errorParamArray.getString(i);
|
|
}
|
|
}
|
|
Class<?> errorClass;
|
|
try
|
|
{
|
|
errorClass = Class.forName(errorType);
|
|
}
|
|
catch (ClassNotFoundException e)
|
|
{
|
|
errorClass = Exception.class;
|
|
}
|
|
Constructor<?> constructor = null;
|
|
try
|
|
{
|
|
try
|
|
{
|
|
constructor = errorClass.getConstructor(String.class, Object[].class);
|
|
createdObject = constructor.newInstance(errorId, errorParams);
|
|
}
|
|
catch (NoSuchMethodException e)
|
|
{
|
|
try
|
|
{
|
|
constructor = errorClass.getConstructor(String.class);
|
|
createdObject = constructor.newInstance(errorId == null ? errorMessage : errorId);
|
|
}
|
|
catch (NoSuchMethodException e1)
|
|
{
|
|
try
|
|
{
|
|
constructor = errorClass.getConstructor();
|
|
createdObject = constructor.newInstance();
|
|
}
|
|
catch (NoSuchMethodException e2)
|
|
{
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
//We don't need to do anything here. Code below will fix things up
|
|
}
|
|
if (createdObject == null || !Throwable.class.isAssignableFrom(createdObject.getClass()))
|
|
{
|
|
result = new TransferException(errorId == null ? errorMessage : errorId, errorParams);
|
|
}
|
|
else
|
|
{
|
|
result = (Throwable)createdObject;
|
|
}
|
|
}
|
|
catch(JSONException ex)
|
|
{
|
|
if (log.isDebugEnabled())
|
|
{
|
|
log.debug("Failed to deserialize Throwable object from JSON object", ex);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
@Override
|
|
public JSONObject serialize(Throwable object)
|
|
{
|
|
JSONObject errorObject = new JSONObject();
|
|
|
|
try
|
|
{
|
|
errorObject.put("errorType", object.getClass().getName());
|
|
errorObject.put("errorMessage", object.getMessage());
|
|
if (AlfrescoRuntimeException.class.isAssignableFrom(object.getClass()))
|
|
{
|
|
AlfrescoRuntimeException alfEx = (AlfrescoRuntimeException)object;
|
|
errorObject.put("alfrescoMessageId", alfEx.getMsgId());
|
|
Object[] msgParams = alfEx.getMsgParams();
|
|
List<Object> params = msgParams == null ? Collections.emptyList() : Arrays.asList(msgParams);
|
|
errorObject.put("alfrescoMessageParams", params);
|
|
}
|
|
}
|
|
catch (JSONException e)
|
|
{
|
|
if (log.isDebugEnabled())
|
|
{
|
|
log.debug("Failed to serialize Throwable object into JSON object", e);
|
|
}
|
|
}
|
|
return errorObject;
|
|
}
|
|
}
|