From 5b27594abe2e399b97d5d03bd483bbcc5d2faa75 Mon Sep 17 00:00:00 2001 From: David BRASSELY Date: Mon, 18 Nov 2013 10:49:29 +0100 Subject: [PATCH] ESBCOMP-79 Add support for a better JSON ouput by ignoring namespace in XML --- .../jbiadapter/InboundConfiguration.java | 14 ++++++++++-- .../jbiadapter/inbound/InboundDelegator.java | 4 +++- .../jbiadapter/util/JbiMessageUtil.java | 22 ++++++++++++++++--- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/ojc-core/restbc/jbiadapter/src/com/sun/jbi/restbc/jbiadapter/InboundConfiguration.java b/ojc-core/restbc/jbiadapter/src/com/sun/jbi/restbc/jbiadapter/InboundConfiguration.java index c16e5b87d..a25e08e5e 100755 --- a/ojc-core/restbc/jbiadapter/src/com/sun/jbi/restbc/jbiadapter/InboundConfiguration.java +++ b/ojc-core/restbc/jbiadapter/src/com/sun/jbi/restbc/jbiadapter/InboundConfiguration.java @@ -33,6 +33,7 @@ public class InboundConfiguration { private final static String FORWARD_AS_ATTACHMENT_PROP = "forward-as-attachment"; private final static String TIMEOUT_PROP = "timeout"; private final static String MSG_TYPE = "message-type"; + private final static String STRIP_NAMESPACES = "strip-namespaces"; private String httpListenerName; private String path; @@ -45,6 +46,8 @@ public class InboundConfiguration { private boolean forwardAsAttachment = false; private long timeout = 60000; private String msgType; + // This property is only used in the case of a JSON output + private boolean stripNamespaces = false; private ServiceUnit serviceUnit; private RestOperation restOp; @@ -92,6 +95,8 @@ public class InboundConfiguration { forwardAsAttachment = Boolean.parseBoolean(PropertiesUtil.safeGetProperty(p, FORWARD_AS_ATTACHMENT_PROP, "false")); timeout = Long.parseLong(PropertiesUtil.safeGetProperty(p, TIMEOUT_PROP, "60000")); msgType = PropertiesUtil.safeGetProperty(p, MSG_TYPE); + + stripNamespaces = Boolean.parseBoolean(PropertiesUtil.safeGetProperty(p, STRIP_NAMESPACES, "false")); } /** @@ -191,6 +196,11 @@ public class InboundConfiguration { public EndpointIdentifier getEndpointIdentifier() { return endpointIdentifier; } - - + + /** + * @return the stripNamespaces + */ + public boolean isStripNamespaces() { + return stripNamespaces; + } } diff --git a/ojc-core/restbc/jbiadapter/src/com/sun/jbi/restbc/jbiadapter/inbound/InboundDelegator.java b/ojc-core/restbc/jbiadapter/src/com/sun/jbi/restbc/jbiadapter/inbound/InboundDelegator.java index 16b5b5964..7a9877fbf 100755 --- a/ojc-core/restbc/jbiadapter/src/com/sun/jbi/restbc/jbiadapter/inbound/InboundDelegator.java +++ b/ojc-core/restbc/jbiadapter/src/com/sun/jbi/restbc/jbiadapter/inbound/InboundDelegator.java @@ -336,7 +336,9 @@ public class InboundDelegator { break; } else if (PathUtil.isJSONMediaType(acceptableMediaType)) { if (!method.equalsIgnoreCase("head")) { - String xmlPayloadAsString = JbiMessageUtil.convertXmlToString(xmlPayload); + String xmlPayloadAsString = JbiMessageUtil.convertXmlToString(xmlPayload, + inboundConfig.isStripNamespaces()); + com.sun.jbi.restbc.jbiadapter.org.json.JSONObject jsonObject = com.sun.jbi.restbc.jbiadapter.org.json.XML.toJSONObject(xmlPayloadAsString); if (jsonObject != null) { diff --git a/ojc-core/restbc/jbiadapter/src/com/sun/jbi/restbc/jbiadapter/util/JbiMessageUtil.java b/ojc-core/restbc/jbiadapter/src/com/sun/jbi/restbc/jbiadapter/util/JbiMessageUtil.java index 85133ee77..63bccfbfd 100755 --- a/ojc-core/restbc/jbiadapter/src/com/sun/jbi/restbc/jbiadapter/util/JbiMessageUtil.java +++ b/ojc-core/restbc/jbiadapter/src/com/sun/jbi/restbc/jbiadapter/util/JbiMessageUtil.java @@ -405,14 +405,30 @@ public class JbiMessageUtil { return doc; } - - public static String convertXmlToString(Source source) throws Exception { + public static String convertXmlToString(Source source, boolean stripNamespaces) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(source, new StreamResult(baos)); - return baos.toString(); + String xmlPayload = baos.toString(); + if (stripNamespaces) { + return stripNamespaces(xmlPayload); + } + + return xmlPayload; + } + + private static String stripNamespaces(String xmlPayload) { + return xmlPayload.replaceAll("(<\\?[^<]*\\?>)?", ""). /* remove preamble */ + replaceAll("xmlns.*?(\"|\').*?(\"|\')", "") /* remove xmlns declaration */ + .replaceAll("(<)(\\w+:)(.*?>)", "$1$3") /* remove opening tag prefix */ + .replaceAll("()", "$1$3"); /* remove closing tags prefix */ + + } + + public static String convertXmlToString(Source source) throws Exception { + return convertXmlToString(source, false); } private static boolean isBuiltInType(QName typeName) {