Search code examples
javaxmljbosssmooks

Java to XML (SOAP) with Smooks using FreeMarker


Hey StackOverflow users,

I'm currently working on a SOA Project. As an Application Server I use JBoss 5.1 with JBoss ESB 4.11 deployed.

I try to implement a Web Service which recieves SOAP Messages from Clients and send responses as SOAP messages as well.
When a SOAP message request is recieved by this Web Service I'm using Smooks to transform this message into Java Objects so i can process the request.
When im done processing, I want to transform Java Objects to XML (a SOAP reply), again with Smooks.

I'm stuck on the transformation from Java to XML.
My Action Chain in the jboss-esb.xml file looks like this:

<services>
  <service name="myWS" description="A Service" category="WS">
        <listeners>
            <jms-listener name="myListener" busidref="myChannel"/>
        </listeners>
        <actions mep="RequestResponse" inXsd="in.xsd" outXsd="out.xsd">
            <!-- Transform incomming SOAP Message into JavaBean objects -->
            <action name="xml2java-transform" class="org.jboss.soa.esb.smooks.SmooksAction">
                <property name="smooksConfig" value="/smooks/smooks-config-soap2java.xml"/>
                <property name="resultType" value="JAVA" />
            </action>

            <action name="processRequest" class="example.soa.ProcessRequest" process="process">
                <property name="config" value="val"/>
            </action>

            <!-- Transform outgoing JavaBean objects into SOAP Message -->
            <action name="java2xml-transform" class="org.jboss.soa.esb.smooks.SmooksAction">
                <property name="smooksConfig" value="/smooks/smooks-config-java2soap.xml"/>
                <property name="reportPath" value="/smooks/report.html"/>
                <property name="resultType" value="STRING" />
            </action>
        </actions>
    </service>
</services>

My smooksConfig of the second SmooksAction looks like this:

<smooks-resource-list   xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
                    xmlns:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd"
                    xmlns:core="http://www.milyn.org/xsd/smooks/smooks-core-1.4.xsd">

<core:filterSettings type="SAX" defaultSerialization="false"/> 

<resource-config selector="example.JavaObjectMessage">
    <resource>org.milyn.delivery.DomModelCreator</resource>
</resource-config>


<ftl:freemarker applyOnElement="example.JavaObjectMessage">
    <!--<ftl:template>/freemarker/acknowledge.flt</ftl:template> -->
    <ftl:template>
        <!-- 
        <sg:Message xmlns:sg="urn:http://example">
            <sg:MessageType>${.vars[example.JavaObjectMessage].messageType}</sg:MessageType>
        </sg:Message >
        -->
    </ftl:template>
</ftl:freemarker>
</smooks-resource-list>

After the processRequest Action is done processing the incomming request it will attatch the example.JavaObjectMessage class to the esb message. So the second SmooksAction will have access to this Object.

My question is: how can i access the attributes of the 'example.JavaObjectMessage' in the smooksConfig? And to what does the applyOnElement refer to in the flt:tamplate section?

I already read the Smooks User Guide and in the JBoss Community I posted this question too.

I appreciate any help!

Regards


Solution

  • I finally solved the problem! I had a hard time to understand the mechanism of how to transform Java to XML with the SmooksAction and im sure i still don't understand everything.

    Anyways what i did was:
    1. took a closer look at the SAX filter within the smooks-config.xml file
    2. found out, that the SAX filter will create an XML document with the package name of the Java Object as root element and all its attributes as child elements.
    3. the XML document created by the SAX filter can then be used to create a virtual object (in this case a HashMap)
    4. the HashMap can then be used as the input for the FreeMarker template

    So my smooks-config.xml file looks now like this:

    <?xml version="1.0" encoding='UTF-8'?>
    <smooks-resource-list   xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
                        xmlns:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd"
                        xmlns:jb="http://www.milyn.org/xsd/smooks/javabean-1.2.xsd"
                        xmlns:core="http://www.milyn.org/xsd/smooks/smooks-core-1.4.xsd">
    
    <resource-config selector="global-parameters">
        <param name="stream.filter.type">SAX</param>
    </resource-config>
    
    <jb:bean beanId="message" class="java.util.HashMap" createOnElement="example.JavaObjectMessage">
        <jb:value property="messageType" decoder="String" data="example.JavaObjectMessage/messageType"/>
    </jb:bean>
    
    <ftl:freemarker applyOnElement="example.JavaObjectMessage">
        <ftl:template>
        <!--     
            <sg:Message xmlns:sg="urn:http://message">
                <sg:MessageType>${message.messageType}</sg:MessageType>
            </sg:Message>
        --> 
        </ftl:template>
    </ftl:freemarker>
    </smooks-resource-list>
    

    as explained before: first use the SAX filter, second create a virtual object (HashMap in this case), third: do the templating with free marker