Transfer Service:

- Rehydration of exception received from target repo


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@22413 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Brian Remmington
2010-09-10 16:57:06 +00:00
parent cd7099a140
commit 6a868744f5
3 changed files with 23 additions and 112 deletions

View File

@@ -19,19 +19,19 @@
package org.alfresco.repo.web.scripts.transfer;
import java.io.StringWriter;
import javax.servlet.http.HttpServletRequest;
import org.alfresco.service.cmr.transfer.TransferException;
import org.alfresco.service.cmr.transfer.TransferProgress;
import org.alfresco.service.cmr.transfer.TransferReceiver;
import org.alfresco.util.json.ExceptionJsonSerializer;
import org.alfresco.util.json.JsonSerializer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse;
import org.springframework.extensions.webscripts.json.JSONWriter;
import org.springframework.extensions.webscripts.servlet.WebScriptServletRequest;
/**
@@ -46,6 +46,7 @@ public class StatusCommandProcessor implements CommandProcessor
private static final String MSG_CAUGHT_UNEXPECTED_EXCEPTION = "transfer_service.receiver.caught_unexpected_exception";
private TransferReceiver receiver;
private JsonSerializer<Throwable, JSONObject> errorSerializer = new ExceptionJsonSerializer();
private final static Log logger = LogFactory.getLog(StatusCommandProcessor.class);
@@ -72,21 +73,17 @@ public class StatusCommandProcessor implements CommandProcessor
{
TransferProgress progress = receiver.getProgressMonitor().getProgress(transferId);
// return the unique transfer id (the lock id)
StringWriter stringWriter = new StringWriter(300);
JSONWriter jsonWriter = new JSONWriter(stringWriter);
jsonWriter.startObject();
jsonWriter.writeValue("transferId", transferId);
jsonWriter.writeValue("status", progress.getStatus().toString());
jsonWriter.writeValue("currentPosition", progress.getCurrentPosition());
jsonWriter.writeValue("endPosition", progress.getEndPosition());
JSONObject progressObject = new JSONObject();
progressObject.put("transferId", transferId);
progressObject.put("status", progress.getStatus().toString());
progressObject.put("currentPosition", progress.getCurrentPosition());
progressObject.put("endPosition", progress.getEndPosition());
if (progress.getError() != null)
{
jsonWriter.startValue("error");
TransferProcessorUtil.writeError(progress.getError(), jsonWriter);
JSONObject errorObject = errorSerializer.serialize(progress.getError());
progressObject.put("error", errorObject);
}
jsonWriter.endObject();
String response = stringWriter.toString();
String response = progressObject.toString();
resp.setContentType("application/json");
resp.setContentEncoding("UTF-8");
@@ -119,4 +116,9 @@ public class StatusCommandProcessor implements CommandProcessor
this.receiver = receiver;
}
public void setErrorSerializer(JsonSerializer<Throwable, JSONObject> errorSerializer)
{
this.errorSerializer = errorSerializer;
}
}

View File

@@ -1,94 +0,0 @@
/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.web.scripts.transfer;
import java.io.IOException;
import java.io.StringWriter;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.service.cmr.transfer.TransferException;
import org.springframework.extensions.webscripts.json.JSONWriter;
public class TransferProcessorUtil
{
public static String writeError(TransferException ex) throws IOException
{
StringWriter stringWriter = new StringWriter(300);
JSONWriter jsonWriter = new JSONWriter(stringWriter);
writeError(ex, jsonWriter);
return stringWriter.toString();
}
public static void writeError(Throwable ex, JSONWriter jsonWriter) throws IOException
{
jsonWriter.startObject();
jsonWriter.writeValue("errorType", JSONWriter.encodeJSONString(ex.getClass().getName()));
if (AlfrescoRuntimeException.class.isAssignableFrom(ex.getClass()))
{
AlfrescoRuntimeException alfEx = (AlfrescoRuntimeException)ex;
jsonWriter.writeValue("errorId", JSONWriter.encodeJSONString(alfEx.getMsgId()));
jsonWriter.startValue("errorParams");
jsonWriter.startArray();
Object[] msgParams = alfEx.getMsgParams();
if (msgParams != null)
{
for (Object param : msgParams)
{
if (param != null)
{
jsonWriter.writeValue(JSONWriter.encodeJSONString(param.toString()));
}
else
{
jsonWriter.writeNullValue();
}
}
}
jsonWriter.endArray();
}
jsonWriter.endObject();
}
/**
* @param stringWriter
* @param msgParams
*/
public static String writeErrorParams(Object[] msgParams)
{
if (msgParams == null) return "";
StringWriter writer = new StringWriter(300);
boolean first = true;
for (Object param : msgParams) {
if (!first) {
writer.write(",");
}
if (param != null) {
writer.write("\"");
writer.write(JSONWriter.encodeJSONString(param.toString()));
writer.write("\"");
} else {
writer.write("null");
}
first = false;
}
return writer.toString();
}
}

View File

@@ -25,13 +25,15 @@ import java.util.TreeMap;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.service.cmr.transfer.TransferException;
import org.alfresco.util.json.ExceptionJsonSerializer;
import org.alfresco.util.json.JsonSerializer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;
import org.springframework.extensions.webscripts.AbstractWebScript;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse;
import org.springframework.extensions.webscripts.json.JSONWriter;
/**
* @author brian
@@ -43,7 +45,7 @@ public class TransferWebScript extends AbstractWebScript
private boolean enabled = true;
private Map<String, CommandProcessor> processors = new TreeMap<String, CommandProcessor>();
private JsonSerializer<Throwable, JSONObject> errorSerializer = new ExceptionJsonSerializer();
public void setEnabled(boolean enabled)
{
@@ -102,7 +104,8 @@ public class TransferWebScript extends AbstractWebScript
{
log.debug("transfer exception caught", ex);
res.setStatus(Status.STATUS_INTERNAL_SERVER_ERROR);
String error = TransferProcessorUtil.writeError(ex);
JSONObject errorObject = errorSerializer.serialize(ex);
String error = errorObject.toString();
res.setContentType("application/json");
res.setContentEncoding("UTF-8");