ESBCOMP-79 Add support for a better JSON ouput by ignoring namespace in XML

master
David BRASSELY 2013-11-18 10:49:29 +01:00
parent 5b88b6c65a
commit 5b27594abe
3 changed files with 34 additions and 6 deletions

View File

@ -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;
}
}

View File

@ -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) {

View File

@ -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("(</)(\\w+:)(.*?>)", "$1$3"); /* remove closing tags prefix */
}
public static String convertXmlToString(Source source) throws Exception {
return convertXmlToString(source, false);
}
private static boolean isBuiltInType(QName typeName) {