Home > Monitor, Router > Tutorial: Mapping SOAP Operations to REST Resources

Tutorial: Mapping SOAP Operations to REST Resources

REST 2 SOAP Mapping

In this Tutorial we will see how to use the REST2SOAPInterceptor. It will allow us to call a SOAP Web Service by using REST resource URIs. For example take a look at the BLZService at the following location:

http://www.thomas-bayer.com/axis2/services/BLZService?wsdl

To call the getBank operation you have to send a SOAP message like the following:

<soapenv:Envelope xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/”
   xmlns:blz=”http://thomas-bayer.com/blz/”>
  <soapenv:Header/>
  <soapenv:Body>
    <blz:getBank>
      <blz:blz>37050198</blz:blz>
    </blz:getBank>
  </soapenv:Body>
</soapenv:Envelope>

Now let us imagine that there is a REST resource for banks. Probably the URI to get a representation of a bank will look like this:

http://localhost:2000/bank/37050198

This mapping can be done by the REST2SOAPInterceptor without modifing the SOAP service.

REST2SOAPInterceptor

First we will take a look at how the interceptor works. It is configured by a list of mappings. Each of them contains the information to map HTTP requests to SOAP operations. This allows us to map many SOAP operations with one interceptor. Finding the right mapping for a given HTTP request is done by a regular expression. Every mapping contains a regular expression that is matched against the URL of the HTTP request. The mapping which regular expression matches first is taken. With that mapping the interceptor creates a XML document from the HTTP request.

For example for the  following HTTP request

GET /bank/37050198 HTTP/1.1
Host: www.thomas-bayer.com
User-Agent: Jakarta Commons-HttpClient/3.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive

the interceptor will create the following XML document:

<request method="GET" http-version="1.1">
  <uri value="/bank/37050198">
    <host>www.thomas-bayer.com</host>
    <path>
      <component>bank</component>
      <component>37050198</component>
    </path>
  </uri>
  <headers>
    <header name="User-Agent">Jakarta Commons-HttpClient/3.1</header>
    <header name="Host">www.thomas-bayer.com</header>
  </headers>
</request>

Than a  XSLT transformation is applied to the XML document. The transformation creates the SOAP message for the SOAP operation we want to call. Finally the transformed message is send to the endpoint of the SOAP service. After receiving the response the intercetor can by configured to apply another XSLT transformation to strip the SOAP envelope of the response. The service endpoint and the references to the XSLT stylesheets are defined in the mapping.

RESTfull BLZ Service

  1. Create a stylesheet called blz-httpget2soap-request.xsl. It will be used by the interceptor to create the SOAP message. It should have the following content:
    <xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”
      xmlns:s11=”http://schemas.xmlsoap.org/soap/envelope/”
      xmlns:blz=”http://thomas-bayer.com/blz/”>
    
      <xsl:template match=”/”>
        <s11:Envelope >
          <s11:Body>
            <blz:getBank>
              <blz:blz><xsl:value-of select=”//path/component[2]“/></blz:blz>
            </blz:getBank>
          </s11:Body>
        </s11:Envelope>
      </xsl:template>
    </xsl:stylesheet>
    
  2. Create a stylesheet called strip-soap-envelope.xslto strip the SOAP envelope from the response. It should have the following content:
    <xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”
      xmlns:s11=”http://schemas.xmlsoap.org/soap/envelope/”>
    
      <xsl:template match=”/”>
        <xsl:apply-templates select=”//s11:Body/*”/>
      </xsl:template>
    
      <xsl:template match=”@*|node()”>
        <xsl:copy>
          <xsl:apply-templates select=”@*”/>
          <xsl:apply-templates/>
        </xsl:copy>
      </xsl:template>
    </xsl:stylesheet>
    
  3. Copy the stylesheets into the configuration folder of your monitor or router installation directory.
  4. Open monitor-beans.xml and add a bean for the REST2SOAP interceptor to the list of interceptors in the transport bean.
    <bean id="transport" class="com.predic8.membrane.core.transport.http.HttpTransport">
      [...]
      <property name="interceptors">
        <list>
          [...]
          <bean class="com.predic8.membrane.core.interceptor.rest.REST2SOAPInterceptor">
            <property name="mappings">
              <map>
                <entry key="/bank/.*">
                  <map>
                    <entry key="SOAPAction" value=""/>
              	<entry key="SOAPURL" value="/axis2/services/BLZService" />
              	<entry key="requestXSLT" value="configuration/blz-httpget2soap-request.xsl" />
              	<entry key="responseXSLT" value="configuration/strip-soap-envelope.xsl" />
                  </map>
                 </entry>
              </map>
            </property>
          </bean>
          [...]
        </list>
      </property>
      [...]
    </bean>
    
  5. Start the Membrane monitor.
  6. Create a rule that listens on port 2000 and redirect requests to thomas-bayer.com:80.
  7. Open the link http://localhost:2000/bank/37050198 in your browser.
Advertisements
Categories: Monitor, Router
  1. May 11, 2011 at 13:18

    I think the stripping of the SOAP envelope and body should be done by the REST2SOAP interceptor.

    • May 11, 2011 at 13:46

      What about the SOAP header? If we strip the SOAP envelope we might loose data from the SOAP header.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: