Tutorial 4 - POSTing XML to an XPE Pipeline
Description
This tutorial will demonstrate how data can be POSTed to an XPE pipeline.
We will alter the pipeline from Tutorial 3 to simply accept an XML posted to it as well as a HTTP GET request having parameters. This increases the flexibility of our pipelines and illustrates how XPE can support RESTfull web services (without the need of SOAP).
We will be creating the following XPE pipeline:
Step 1 - Define the XML to send
Since our Tutorial 4 pipeline is about authenticating a username/password pair against a database, then all that is required is a simple XML such as:
<username>
fred
</username>
<password>
flintstone
</password>
</authenticate>
If you haven't already noticed this is the same xml structure that we created via the prepareRequest.xsl file from the previous tutorial.
Posting the XML
Apart from the XML itself, we need a way of posting the XML to our Pipeline. In this case a browser is of no use as they are built to perform HTTP GET requests.
Fortunately there are numerous tools and utilities that can perform this task for us available free on the internet.
A powerfull and simple tool that will perform the job is Apache jmeter available from the Apache Software Foundation. The rest of this tutorial will assume you are using jmeter to post data to our pipelines. If you decide to use another utility, you may need to alter the tutorial somewhat.
Step 2 - Configuring XPE to accept POSTed data
Up untill now we have only configured XPE pipes to accept HTTP GET requests. Recall this is done in the urlPattern.xml file with an entry such as:
<map pattern="/tutorial/tut3/dbauthenticate" xpipe="http://www.xml.org/pipe/xpe/tutorial/tut3/dbauthenticate" />
</method>
To accept HTTP POST requests we simply add the required url pattern as follows:
<map pattern="/tutorial/tut3/dbauthenticate" xpipe="http://www.xml.org/pipe/xpe/tutorial/tut3/dbauthenticate" />
</method>
Add this xml fragment to the urlPattern.xml file. The file should now look like:
<urlPattern xmlns="http://www.xml.org/xml/pipe" >
<method name="GET" >
<map pattern="/tutorial/tut1/echo" xpipe="http://www.xml.org/pipe/xpe/tutorial/tut1/echo" />
<map pattern="/tutorial/tut2/authenticate" xpipe="http://www.xml.org/pipe/xpe/tutorial/tut2/authenticate" />
<map pattern="/tutorial/tut3/dbauthenticate" xpipe="http://www.xml.org/pipe/xpe/tutorial/tut3/dbauthenticate" />
</method>
<method name="POST" >
<map pattern="/tutorial/tut3/dbauthenticate" xpipe="http://www.xml.org/pipe/xpe/tutorial/tut3/dbauthenticate" />
</method>
</urlPattern>
XPE allows the same pipeline to be shared transparently by both GET and POST requests. Normally all that is required to perform this is to extract the request data differently at the start of the pipeline. This will be illustrated next.
We have chosen to keep the name of the new POST pipeline as /tutorial/tut3/dbauthenticate even though this is tutorial 4 to illustrate how the same pipeline can accept both GET and POST requests.
Step 3 - Changing the pipeline to accept HTTP XML body
In tutorial 3 we extracted the username and password from the url parameters. Now we will change the first pipeline filter to optionally extract the username and password from the XML we are posting to the pipeline. In our case this will be trivial because if you recall we are already extracting the username and password from the previous tutorial and creating a business data instance that is identical to the data we will post to the pipeline. The only change we need to do is to the prepareRequest.xsl to accept this new XML:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:http="http://www.xml.org/pipe/HTTP"
xmlns:xpe="http://www.xml.org/pipe/xpe" >
<xsl:import href="copy.xsl" />
<xsl:output method="xml" />
<xsl:template match="http:httpRequest" >
<request xmlns="" >
<xsl:choose xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:when test="//http:params/http:param[@name='password'] and //http:params/http:param[@name='username']" >
<authentication xmlns="" >
<username>
<xsl:value-of select="//http:params/http:param[@name='username']/@value" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" />
</username>
<password>
<xsl:value-of select="//http:params/http:param[@name='password']/@value" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" />
</password>
</authentication>
</xsl:when>
<xsl:when test="//authentication" >
<xsl:copy-of select="//authentication" />
</xsl:when>
<xsl:otherwise>
<result status="error" xmlns="" >
<error>
username and password are required parameters.
</error>
</result>
</xsl:otherwise>
</xsl:choose>
</request>
</xsl:template>
</xsl:stylesheet>
In this xslt file we have added code to detect the presence of an authentication element. If found then the entire element is simply copied into the XML stream.
Build and deploy the application and check for any deployment errors.
Post this XML to our tutorial pipeline using your utility of choice. a sample jmeter test is included in the tutorials/solutions/tutorial4 directory. If you use jmeter, then the client would look something like:
Now once you POST this data to our pipeline, you should either receive a success result:
if the username/password match those in the database, or an error result if they do not:
<error>
Username not found or password is invalid.
</error>
</result>
Summary
This tutorial has demonstrated:
- How to POST data to an XPE pipeline.
- How XPE can make shareing of pipelines by both HTTP GET and POST requests almost trivial.
The next tutorial will illustrate how XPE can be used to generate and process FORMS utilising simple RESTfull web services for business processing.
Any Problems?
If you have problems getting this tutorial to work look at the completed solution in the tutorials/solutions/tutorial4 directory It should have all files required to buld and run the tutorial.