Commits

Asankha Perera  committed 585570c

add xml-to-json XSLT stylesheet and a sample on the conversion

  • Participants
  • Parent commits 7a45d73

Comments (0)

Files changed (3)

File modules/core/src/main/java/org/adroitlogic/ultraesb/core/helper/TransformationUtils.java

                 }
             } else {
                 sourceNode = ValidationUtils.getSourceNode(exprn, msg, ns);
+                logger.debug("Got source node using : {}", exprn);
             }
 
             // prepare to capture result of transformation, either as DOM or into a File (Stream)
                 XMLSupport.getInstance().returnTransformer(xsltFilename, transformer);
             }
 
+            logger.debug("XSLT transformation with {} completed", xsltFilename);
+
             if (domResult) {
                 Node node = ((DOMResult) resultNode).getNode();
                 if (node instanceof Document) {

File modules/sample/src/test/java/org/adroitlogic/ultraesb/json/SOAPToJSONTest.java

 package org.adroitlogic.ultraesb.json;
 
 import junit.extensions.TestSetup;
-import junit.framework.Assert;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
+import junit.framework.*;
 import org.adroitlogic.ultraesb.ServerManager;
 import org.adroitlogic.ultraesb.UltraServer;
 import org.apache.http.HttpResponse;
         return setup;
     }
 
-    public void testRESTcall() throws Exception {
+    public void testRESTcallWithSAXStreaming() throws Exception {
 
         logger.debug("*** Get a new stock quote ***");
-        HttpGet httpGet = new HttpGet(proxyUrl + "?symbol=ADRT");
+        HttpGet httpGet = new HttpGet(proxyUrl+"-1" + "?symbol=ADRT");
         HttpResponse response = client.execute(httpGet);
         Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
         Assert.assertEquals("application/json", response.getFirstHeader("Content-Type").getValue());
         response.getEntity().writeTo(baos);
         String responseStr = baos.toString();
 
-        System.out.println(responseStr);
-
-        //Pattern pattern = Pattern.compile("last>([0-9|\\.]*)<");
         Pattern pattern = Pattern.compile("\"\\$\":\"([0-9|\\.]*)\"}");
         Matcher matcher = pattern.matcher(responseStr);
 
         logger.info("Last : " + matcher.group(1));
         Assert.assertTrue(responseStr.indexOf("Envelope") == -1);
     }
+
+    public void testRESTcallWithXSLT() throws Exception {
+
+        logger.debug("*** Get a new stock quote ***");
+        HttpGet httpGet = new HttpGet(proxyUrl+"-2" + "?symbol=ADRT");
+        HttpResponse response = client.execute(httpGet);
+        Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+        Assert.assertEquals("application/json", response.getFirstHeader("Content-Type").getValue());
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        response.getEntity().writeTo(baos);
+        String responseStr = baos.toString();
+
+        Pattern pattern = Pattern.compile("\"last\":([0-9|\\.]*),");
+        Matcher matcher = pattern.matcher(responseStr);
+
+        Assert.assertTrue(matcher.find());
+        logger.info("Last : " + matcher.group(1));
+        Assert.assertTrue(responseStr.indexOf("Envelope") == -1);
+    }
 }

File resources/samples/conf/ultra-sample-112.xml

     'symbol' parameter by passing it into an XQuery script 'samples/resources/sample5.xq' to generate a new message
     payload. This new payload is then sent to the http://localhost:9000/service/SimpleStockQuoteService as a POST
     request, to which a SOAP response is returned. The SOAP message is converted into a simplified REST format-->
-    <u:proxy id="json-stockquote">
+    <u:proxy id="json-stockquote-1">
         <u:transport id="http-8280"/>
         <u:target>
             <u:inSequence>
         </u:target>
     </u:proxy>
 
+    <u:proxy id="json-stockquote-2">
+        <u:transport id="http-8280"/>
+        <u:target>
+            <u:inSequence>
+                <u:java import="java.util.*;"><![CDATA[
+                    @SuppressWarnings("unchecked")
+                    Map<String, String> params = (Map<String, String>) msg.getMessageProperty("QUERY_PARAM_MAP");
+
+                    // there are many ways to create a request - e.g. XSLT, XQuery, Scripting languages. For now a simple text manipulation
+                    mediation.setPayloadFromString(msg,
+                        "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soap=\"http://soap.services.samples/\">" +
+                        "<soapenv:Body><soap:getQuote><request><symbol>" +
+                        params.get("symbol") +
+                        "</symbol></request></soap:getQuote></soapenv:Body></soapenv:Envelope>");
+
+                    msg.addMessageProperty("HTTP_METHOD", "POST");
+                    msg.setContentType("text/xml");
+                ]]></u:java>
+            </u:inSequence>
+            <u:inDestination>
+                <u:address>http://localhost:9000/service/SimpleStockQuoteService</u:address>
+            </u:inDestination>
+            <u:outSequence>
+                <u:java><![CDATA[
+                    // retain only the first child of a SOAP 1.1 message (extend the XPath for SOAP 1.2 if required)
+                    String[][] ns = {{"s11", "http://schemas.xmlsoap.org/soap/envelope/"}};
+                    mediation.retainElementAsPayload(msg, "/s11:Envelope/s11:Body/child::*[position()=1]", ns);
+
+                    // See sample #206 on how the XSLT parameters can be further tuned as per
+                    // http://www.bramstein.com/projects/xsltjson/ - Note, the output from this transformation is not XML
+                    mediation.transform(msg, "conf/xml-to-json-xslt-2.xsl");
+                    msg.setContentType("application/json");
+                ]]></u:java>
+            </u:outSequence>
+            <u:outDestination>
+                <u:address type="response"/>
+            </u:outDestination>
+        </u:target>
+    </u:proxy>
+
 </beans>