This page last changed on Oct 02, 2006 by lajos@mulesource.com.

This example demonstrates how to invoke an ASPX web service from Mule and transform the result using XSLT and deserialise the result to a StockQuote javabean. The example demonstrates using REST and SOAP to invoke the service.

The configuration uses the System stream connector to receive a stock symbol from a user (System.in), invokes the StockQuote service, transforms the result using the XSLT transformer and then uses the XmlToObject transformer to convert the result into a StockQuote javabean. Finally the Quote is sent to the output console (System.out).


Running the Application

Prerequisites

To run you will need the following -

Optional

  • If you are asked to execute any Maven commands you'll need the Maven build tool from Apache. Mule has been tested with Maven 1.0.2. (Download Maven 1.0.2)

Running

  1. Open a shell*.
  2. From the root of the Mule distribution go to examples/ant/stockquote or examples/maven/stockquote.
  3. Run the 'stockquote.bat' or 'stockquote.sh' script - you will be prompted to choose a configuration:
    1. REST - use the REST configuration
    2. SOAP - use the SOAP configuration
    3. WSDL - use the WSDL configuration
  4. Enter your option and hit 'Return' - this will display a welcome screen and prompt you to enter a stock symbol.
  5. Type: 'ibm' and hit enter.
  6. The stock information will be displayed.
  7. To shutdown Mule, type 'CTRL-C'

-
*Shell refers to a command shell in unix or linux and command prompt in windows.

Walk Through

This walkthough will use the REST configuration for demonstration, but the SOAP configuration is very similar.
Assuming you've had a look at the Echo Example we'll just look at the configuration for this example without going into step-by-step detail.

First the transformers need to be configured -

<transformers>
    <transformer name="XmlToObject" className="org.mule.transformers.xml.XmlToObject"/>
    <transformer name="SgmlDecoder" className="org.mule.transformers.codec.SgmlEntityDecoder"/>
    <transformer name="Xslt" className="org.mule.transformers.xml.XsltTransformer">
        <properties>
            <property name="xslFile" value="stock.xsl"/>
        </properties>
    </transformer>
</transformers>

There are 3 transformers, XmlToObject, SgmlDecoder and Xslt. The Xslt transformer requires that you set the xslFile to a resource located on the file system on classpath. These transformers are chained together.

The stock quote service uses the REST Service Wrapper component that Allows REST services to be proxied to act like local Mule services.

<mule-descriptor name="RESTServiceWrapper"
    implementation="org.mule.components.rest.RestServiceWrapper">
    <inbound-router>
        <endpoint address="stream://System.in"/>
    </inbound-router>
    <outbound-router>
        <router className="org.mule.routing.outbound.FilteringOutboundRouter">
            <endpoint address="stream://System.out" transformers="SgmlDecoder Xslt XmlToObject"/>
        </router>
    </outbound-router>
    <properties>
        <property name="serviceUrl" value="http://www.webservicex.net/stockquote.asmx/GetQuote"/>
        <property name="payloadParameterName" value="symbol"/>
        <property name="httpMethod" value="POST"/>
        <property name="errorExpression" value="[^&lt;.*]"/>
    </properties>
</mule-descriptor>

The component receives stock symbols from System.in and will output the result to System.out. Notice the transformers set on the outbound endpoint. Mule allows you to combine multiple transformers into a transform pipeline.

The RestServiceWrapper has a number of properties configured. The serviceUrl is the URL of the REST service to invoke. The payloadParameterName is the name of the parameter to use set the message payload on the serviceUrl. The httpMethod can either be GET or POST. Finally the errorExpression is a Regular Expression that is applied to the result of the service invocation to determine if an error occurred. In this case the StockQuote service returns a non-xml error string.

The Soap Version

The difference in the two methods is in the configuration. The SOAP way of doing things in Mule differs in that We now use a Bridge Component that takes the input from one endpoint and passes it directly to the outbound router, and we an Axis outbound endpoint configured with Named Parameters that map to the Stock Quote service.

<mule-descriptor name="serviceProxy" implementation="org.mule.components.simple.BridgeComponent">
    <inbound-router>
        <endpoint address="stream://System.in"/>
    </inbound-router>
    <outbound-router>
    <!-- we use a chaining transformer to send the results of one endpoint execution as the input
       for the next endpoint.  In this case it writes it out to System.out -->
    <router className="org.mule.routing.outbound.ChainingRouter">
        <endpoint address="axis:http://www.webservicex.net/stockquote.asmx?method=GetQuote">
            <properties>
                <property name="soapAction" value="${methodNamespace}${method}"/>
                <map name="soapMethods">
                    <list name="qname{GetQuote:http://www.webserviceX.NET/}">
                        <entry value="symbol;string;in"/>
                        <entry value="GetQuoteResult;string;out"/>
                    </list>
                </map>
            </properties>
        </endpoint>
        <endpoint address="stream://System.out" transformers="SgmlDecoder Xslt XmlToObject"/>
    </router>
    </outbound-router>
</mule-descriptor>

You can see the full configuration files here -

Mule Config Graph for the Stock Quote Exampls

This is a configuration graph generated for the Stok Quote examples using the Mule Config Graph Tool.
(Click on the thumb image for a bigger picture)

SOAP Configuration

REST Configuration


Document generated by Confluence on Oct 03, 2006 09:23