From 6a868744f59852b48add0fe0deb039bd71a997ca Mon Sep 17 00:00:00 2001 From: Brian Remmington Date: Fri, 10 Sep 2010 16:57:06 +0000 Subject: [PATCH] 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 --- .../transfer/StatusCommandProcessor.java | 32 ++++--- .../transfer/TransferProcessorUtil.java | 94 ------------------- .../scripts/transfer/TransferWebScript.java | 9 +- 3 files changed, 23 insertions(+), 112 deletions(-) delete mode 100644 source/java/org/alfresco/repo/web/scripts/transfer/TransferProcessorUtil.java diff --git a/source/java/org/alfresco/repo/web/scripts/transfer/StatusCommandProcessor.java b/source/java/org/alfresco/repo/web/scripts/transfer/StatusCommandProcessor.java index ea3053eebb..ac5e867544 100644 --- a/source/java/org/alfresco/repo/web/scripts/transfer/StatusCommandProcessor.java +++ b/source/java/org/alfresco/repo/web/scripts/transfer/StatusCommandProcessor.java @@ -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 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 errorSerializer) + { + this.errorSerializer = errorSerializer; + } + } diff --git a/source/java/org/alfresco/repo/web/scripts/transfer/TransferProcessorUtil.java b/source/java/org/alfresco/repo/web/scripts/transfer/TransferProcessorUtil.java deleted file mode 100644 index 98c062ea78..0000000000 --- a/source/java/org/alfresco/repo/web/scripts/transfer/TransferProcessorUtil.java +++ /dev/null @@ -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 . - */ -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(); - } - - -} diff --git a/source/java/org/alfresco/repo/web/scripts/transfer/TransferWebScript.java b/source/java/org/alfresco/repo/web/scripts/transfer/TransferWebScript.java index d2aaec2c7f..09aa2ce97a 100644 --- a/source/java/org/alfresco/repo/web/scripts/transfer/TransferWebScript.java +++ b/source/java/org/alfresco/repo/web/scripts/transfer/TransferWebScript.java @@ -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 processors = new TreeMap(); - + private JsonSerializer 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");