Archive for August, 2012

Converting JSON REST Requests to SOAP

August 30, 2012 Leave a comment


Today, we make a SOAP Web Service accessible as a JSON REST resource using Membrane ESB.

This can, for example, help to make your SOA available to mobile devices, as JSON processing usually uses less resources than fully-fledged SOAP.

The SOAP Web Service

The sample Web Service is called ArticleService and is used to maintain a list of Articles identified by their ID. Each article has a name, a description and a price (amount and currency).

Its WSDL contains, among other things, the list of the service’s operations:

  1. create: creates a new Article

    A new article is described using the following format:

        <!-- possible value: EUR, possible value: USD -->

    An ID will be automatically generated for the new article and is returned in the SOAP response.

  2. get: retrieves information about one Article, given its ID.

    The ID is specified using the following format:

    <!-- Pattern: [A-Z]{2}-\d{5} -->
  3. getAll: retrieves information about all articles.
  4. delete: deletes information of one Article, given its ID.

    The request format is the same as for the get operation.

Translation to REST

For the advertised functionality, we define the following HTTP methods, to manage the REST-style resources, “articles“.

  1. POST /articles

    Creates a new article. The message entity uses JSON encoding to describe the article:

      "article": {
        "description":"FTGFOP1, 1kg",
        "price": {"amount":21.70, "currency":"EUR"},

    HTTP status code “204 No Content” is returned and the URL of the new resource is returned in the “Location” HTTP header field.

  2. GET /articles/AR-00001

    Retrieves the article resource specified by the ID. HTTP status code “200 OK” and the JSON-encoded resource are returned. If no article with the given ID exists, HTTP status code “404 Not found” is returned.

  3. GET /articles

    Retrieves a list of all article resources. Returns HTTP status code “200 OK” and the JSON-encoded list.

  4. DELETE /articles/AR-00001

    Deletes the specified article. HTTP status code “204 No content” is returned, even if no article with the given ID existed.

Any HTTP status code >=400 indicates an error, as per the HTTP specification.

Using Membrane’s rest2Soap Feature

Membrane ESB offers out-of-the-box a rest2Soap feature with JSON support.

Monitoring the Progression of a Sample Request

For example, we send a REST request to Membrane to create an article. (A request of “REST Type 1”, which we just defined.)

POST /articles/ HTTP/1.1
User-Agent: curl/7.23.1 (x86_64-pc-win32) libcurl/7.23.1 OpenSSL/0.9.8r zlib/1.2.5
Host: localhost:2000
Accept: */*
Content-Type: application/json
Content-Length: 102


Membrane automatically translates the request into an XML format representing the HTTP request, called the “in-between XML document”:

<?xml version="1.0" ?>
<http:request xmlns:http=""
	method="POST" http-version="1.1">
	<uri value="/articles/">
		<header name="User-Agent">curl/7.23.1 (x86_64-pc-win32) libcurl/7.23.1
			OpenSSL/0.9.8r zlib/1.2.5</header>
		<header name="Host">localhost:2000</header>
		<header name="Accept">*/*</header>
		<header name="Content-Type">application/json</header>
		<header name="Content-Length">102</header>
		<header name="X-Forwarded-For"></header>
	<body type="json">
		<root type="o">
			<article type="o">
				<name type="s">Test</name>
				<description type="s">?</description>
				<price type="o">
					<amount type="f">1.00</amount>
					<currency type="s">EUR</currency>
				<id type="s">AR-00000</id>

This document fully describes the request at the point in the diagram marked by the asterisk ‘*‘. As you can see, every piece of data from the HTTP request was mapped to some element or attribute. The JSON body of the request was also parsed and translated into XML. (The same also automagically works with XML bodies – although no “translation” is really needed in this case: The XML document from the request body would simply be inserted below the <body> node, with type="xml".)

This XML document is then used as input for the XSL Transformation. The fine-grained structure now makes it very easy to access specific data, which keeps the stylesheets very brief and concise, as well as easy to develop.

Configuring the XSLT component

At this point, to comply with the REST interface described above, we manually need to configure the XSLT component of the rest2soap feature, mapping REST requests to SOAP operations. We create a file “request.xsl” describing the translation of the “in-between XML document” to SOAP:

<pre><?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl=""
	<xsl:template match="/">
		<s11:Envelope >
				<!-- switch over operations -->
					<xsl:when test="/*/@method = 'GET' and count(//path/component) = 1">
						<as:getAll />
					<xsl:when test="/*/@method = 'GET'">
							<id><xsl:value-of select="(//path/component)[last()]"/></id>
					<xsl:when test="/*/@method = 'DELETE'">
							<id><xsl:value-of select="(//path/component)[last()]"/></id>
					<xsl:when test="/*/@method = 'POST'">
							<xsl:apply-templates select="/*/body/root/*" />

	<!-- remove 'type' attribute: it's an artifact of the JSON2XML converter -->
	<xsl:template match="@type" />
	<!-- leave other elements alone -->
	<xsl:template match="*|@*|text()">
		<xsl:copy><xsl:apply-templates select="*|@*|text()"/></xsl:copy>

Basically each type of REST request is mapped to a SOAP operation.

We then create a second file called “response.xsl” with the following content to handle SOAP responses:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl=""
	<xsl:template match="/s11:Envelope">

			<!-- status 500 on SOAP Faults -->
			<xsl:when test="/*/*/*[local-name() = 'Fault']">
				<status code="500">Internal SOAP Server Error</status>
				<body type="xml">
					<response><xsl:apply-templates select="/*/*/*/*" /></response>

			<xsl:when test="/*/*/*/*[local-name() = 'createResponse']">
				<status code="201">Created</status>
					<!-- use ID from SOAP response for HTTP "Location" header -->
					<header name="Location">/articles/<xsl:value-of select="/*/*/*/id" /></header>
					<header name="Content-Type" remove="true" />
				<body type="plain"></body>

			<xsl:when test="//*[local-name() = 'deleteResponse']">
				<status code="204">No Content</status>
					<header name="Content-Type" remove="true" />
				<body type="plain"></body>

			<xsl:when test="//*[local-name() = 'getResponse']">
				<!-- set status 404, if response is empty -->
				<xsl:if test="count(/*/*/*/*) = 0">
					<status code="404">Not found</status>
				<body type="xml">
					<response><xsl:apply-templates select="/*/*/*/*" /></response>

				<!-- unwrap the SOAP message content -->
				<body type="xml">
					<response><xsl:apply-templates select="/*/*/*/*" /></response>


	<!-- leave other elements alone -->
	<xsl:template match="*|@*|text()">
		<xsl:copy><xsl:apply-templates select="*|@*|text()"/></xsl:copy>

Again, we basically just translated the rules we defined above (in the SOAP and REST section) concerning the HTTP responses (status codes, headers, etc.) into XSLT.

Download and set up Membrane ESB

We now download the Membrane ESB distribution (3.5.2 or later) from the website, and change conf/proxies.xml to

<proxies xmlns=""
 	<serviceProxy port="2000">
 				responseType="json" />
 		<target host="" />

We put request.xsl and response.xsl into the conf directory of the distribution.

We then start Membrane using memrouter.bat.


Testing it

Using the curl utility on Windows, we can issue the following commands:

C:\ > curl http://localhost:2000/articles/
{"response":[{"name":"Black Tea","description":"English Breakfast Black Tea","price":{"amount":4,"currency":"EUR"},"id":"AR-00001"},{"name": "Assam Tea","description":"Finest Assam Tea","price":{"amount":2.5,"currency":"EUR"},"id":"AR-00002"}]}

C:\ > curl --header "Content-Type: application/json" -d "{\"article\":{\"name\":\"Test\",\"description\":\"?\",\"price\":{\"amount\":1.00,\"currency\":\"EUR\"},\"id\":\"AR-00000\"}}" http://localhost:2000/articles/

C:\ > curl http://localhost:2000/articles/AR-00043

C:\ > curl -X DELETE http://localhost:2000/articles/AR-00043
C:\ >

Mapping REST to *Your* SOAP Web Service

To realize a REST interface for another SOAP web service, or to proxy other operations, some tweeks to the stylesheets above are needed.

A good way to develop the REST-to-SOAP and SOAP-to-REST XSLT documents, is to set, stdout

in “conf/“. This turns on debug output. (You need to restart Membrane for the setting to take effect.) It allows you to see the “in-between XML document” created internally: When passing the arrow marked with a star * in the diagram, this document will be written to the log (standard output in our case).

Categories: General, Router

Easy Proxying of SOAP Web Services

August 16, 2012 Leave a comment

Proxying SOAP Web Services

To proxy a SOAP Web Service with a HTTP endpoint, two types of requests need to be handled:

  1. Retrieval of the WSDL document, e.g. a
  2. The SOAP request, e.g. a

The document returned by the first request, the WSDL document, is a technical description of the web service, its data structures and operations. Amongst other things, it describes the structure of the SOAP requests and their responses. The SOAP requests, the second type of requests, actually contain data, initiate actions and actually perform the tasks of the web service.

Requests of both types are forwarded by the proxy to the backend service, as by any generic HTTP proxy.

Additionally, the WSDL document needs to be slightly modified by the proxy before it is returned to the caller: It contains the SOAP endpoint’s address (“; in the example) and therefore influences the second type of requests. As we want these requests to be sent from the client to the proxy instead of directly to the backend service, we have to change this address to point to the proxy instead of the backend service:

Suppose our SOAP proxy is running on http://soapproxy.mycompany.example/. We then have to change “” to “http://soapproxy.mycompany.example/axis2/services/BLZService“.

A Concrete Example

For example, look at At the very bottom you will the three endpoint addresses for this web service. All of them will have to be changed by the proxy.

Action! – The Proxy Configuration

Doing all this is a piece of cake using Membrane ESB as SOAP proxy:

We download the Membrane ESB distribution (3.5 or later) from the website, and change conf/proxies.xml to

<proxies xmlns=""

	<soapProxy port="80" wsdl="">

The new soapProxy element will handle requests of both types!

We then start Membrane using memrouter.bat.


Inspecting the result

From the machine itself, where you are running Membrane, you can retrieve the modified WSDL document using this URL: http://localhost/axis2/services/BLZService?wsdl
You will see that “” was changed to “localhost“. If you use your qualified hostname instead of “localhost“, this will also work.

Of course, you can use any SOAP client, for example soapUI, to access the service via the proxy: Simply specify the WSDL location using the proxy’s address (http://localhost/axis2/services/BLZService?wsdl or use the proxy’s qualified hostname).

The Next Steps

Adding additional features to the proxy is also very simple. We can, for example, change conf/proxies.xml to

<proxies xmlns=""

	<soapProxy port="80" wsdl="">
		<validator />

The change will be automatically be picked up by Membrane within a few seconds. With the validator interceptor, Membrane will perform XML Schema validation on both requests and responses, ensuring that all requests and responses adhere to the data structures and rules documented in the WSDL document and the referenced XML Schema documents. This can, for example, help you protect your web service against certain kinds of attacks.

You can find a wide range of features in Membrane’s configuration reference.

For a step-by-step tutorial, follow the SOAP quick start guide.

Categories: General