From bc0add69c8c07674c32164a4343f266c9287f864 Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Fri, 30 Sep 2011 20:51:24 +0000 Subject: [PATCH] CSPACE-3911: Workaround to make it possible to ingest legal XML documents containing XML entities (e.g. '&', '<', '"') into the Imports service, by 'double-encoding' ampersands at the start of those entities. This is intended to preserve those entities past an initial parse, that takes place during a pre-processor stage of the import process. --- .../services/imports/ImportsResource.java | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/services/imports/service/src/main/java/org/collectionspace/services/imports/ImportsResource.java b/services/imports/service/src/main/java/org/collectionspace/services/imports/ImportsResource.java index 4282bec37..2b677c206 100755 --- a/services/imports/service/src/main/java/org/collectionspace/services/imports/ImportsResource.java +++ b/services/imports/service/src/main/java/org/collectionspace/services/imports/ImportsResource.java @@ -94,7 +94,7 @@ public class ImportsResource extends ResourceBase { return result; } - + @Override public String getServiceName(){ return SERVICE_NAME; @@ -217,6 +217,7 @@ public class ImportsResource extends ResourceBase { * trunk/services/imports/service/src/test/resources/requests/authority-request.xml */ public static InputSource payloadToInputSource(String xmlPayload) throws Exception { + xmlPayload = encodeAmpersands(xmlPayload); String requestDir = FileTools.createTmpDir("imports-request-").getCanonicalPath(); File requestFile = FileTools.saveFile(requestDir, "request.xml", xmlPayload, true); if (requestFile == null){ @@ -229,6 +230,7 @@ public class ImportsResource extends ResourceBase { } public static String payloadToFilename(String xmlPayload) throws Exception { + xmlPayload = encodeAmpersands(xmlPayload); String requestDir = FileTools.createTmpDir("imports-request-").getCanonicalPath(); File requestFile = FileTools.saveFile(requestDir, "request.xml", xmlPayload, true); if (requestFile == null){ @@ -238,7 +240,30 @@ public class ImportsResource extends ResourceBase { System.out.println("############## REQUEST_FILENAME: "+requestFilename); return requestFilename; } - + + /** + * Encodes each ampersand ('&') in the incoming XML payload by replacing + * it with the predefined XML entity for an ampersand ('&'). + * + * This is a workaround for the issue described in CSPACE-3911. Its + * intended effect is to have these added ampersand XML entities being + * resolved to 'bare' ampersands during the initial parse, thus preserving + * any XML entities in the payload, which will then be resolved correctly + * during the second parse. + * + * (This is not designed to compensate for instances where the incoming + * XML payload contains 'bare' ampersands - that is, used in any other + * context than as the initial characters in XML entities. In those cases, + * the payload may not be a legal XML document.) + * + * @param xmlPayload + * @return The original XML payload, with each ampersand replaced by + * the predefined XML entity for an ampersand. + */ + private static String encodeAmpersands(String xmlPayload) { + return xmlPayload.replace("&", "&"); + } + public static void expandXmlPayloadToDir(String tenantId, String inputFilename, String templateDir, String outputDir) throws Exception { System.out.println("############## TEMPLATE_DIR: "+templateDir); System.out.println("############## OUTPUT_DIR:"+outputDir); -- 2.47.3