User login

How to manipulate web service request parameters using WSO2 ESB?

I have a web service which accepts following soap envelop.

   <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
      <soapenv:Body>
         <p:getDownloadsByType xmlns:p="http://ot.collab.wso2.org">
            <downloadedResource xmlns="http://ot.collab.wso2.org">%zip%</downloadedResource>
            <limitStart xmlns="http://ot.collab.wso2.org">1</limitStart>
            <limitEnd xmlns="http://ot.collab.wso2.org">10</limitEnd>
         </p:getDownloadsByType>
      </soapenv:Body>
   </soapenv:Envelope>

However my service cannot deal with requests when value of 'limitEnd' is 1000. So I am using WSO2 ESB to manipulate web service request to make that value to '10'.Following are the steps I followed.

1. Create a Proxy Service wrapping the original service.

eg:

	<syn:proxy name="MyProxy" transports="https http vfs mailto" startOnLoad="true">
		<syn:target inSequence="KPTPMG16InputRequest" outSequence="main" />
		<syn:publishWSDL uri="http://localhost:9762/services/OTWebAccessService?wsdl" />
	</syn:proxy>

2. Create a sequence which will check for specific condition & do the manipulation if necessary.

eg:

	<syn:sequence name="KPTPMG16InputRequest">
		<syn:filter xmlns:p="http://ot.collab.wso2.org"
			xmlns:ns1="http://org.apache.synapse/xsd"
			xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
			source="//p:limitEnd" regex="10">
			<syn:xslt key="KPTPMG16-XSLT" />
		</syn:filter>
		<syn:send>
			<syn:endpoint>
				<syn:address
					uri="http://localhost:9762/services/OTWebAccessService" />
			</syn:endpoint>
		</syn:send>
	</syn:sequence>

3. The manipulation will happen through a XSLT transformation.

	<syn:localEntry key="KPTPMG16-XSLT">
		<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
			xmlns:p="http://ot.collab.wso2.org"
			xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"
			xmlns:fn="http://www.w3.org/2005/02/xpath-functions" version="2.0"
			exclude-result-prefixes="p fn">
			<xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
			<xsl:template match="/">
				<xsl:apply-templates select="//p:getDownloadsByType" />
			</xsl:template>

			<xsl:template match="//p:getDownloadsByType">
				<p:getDownloadsByType>
					<downloadedResource
						xmlns="http://ot.collab.wso2.org">
						<xsl:value-of select="//p:downloadedResource" />
					</downloadedResource>
					<limitStart xmlns="http://ot.collab.wso2.org">
						<xsl:value-of select="//p:limitStart" />
					</limitStart>
					<limitEnd xmlns="http://ot.collab.wso2.org">
						10
					</limitEnd>
				</p:getDownloadsByType>
			</xsl:template>
		</xsl:stylesheet>
	</syn:localEntry>

Following is the full ESB configuration.

<?xml version="1.0" encoding="UTF-8"?>
<syn:definitions xmlns:syn="http://ws.apache.org/ns/synapse">
	<syn:registry provider="org.wso2.esb.registry.ESBRegistry">
		<syn:parameter name="root">file:registry/</syn:parameter>
	</syn:registry>
	<syn:proxy name="MyProxy" transports="https http vfs mailto" startOnLoad="true">
		<syn:target inSequence="KPTPMG16InputRequest" outSequence="main" />
		<syn:publishWSDL uri="http://localhost:9762/services/OTWebAccessService?wsdl" />
	</syn:proxy>
	<syn:sequence name="main">
		<syn:in>
			<syn:log level="full" />
			<syn:send />
		</syn:in>
		<syn:out>
			<syn:send />
		</syn:out>
	</syn:sequence>

	<syn:sequence name="KPTPMG16InputRequest">
		<syn:filter xmlns:p="http://ot.collab.wso2.org"
			xmlns:ns1="http://org.apache.synapse/xsd"
			xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
			source="//p:limitEnd" regex="1000">
			<syn:xslt key="KPTPMG16-XSLT" />
		</syn:filter>
		<syn:send>
			<syn:endpoint>
				<syn:address
					uri="http://localhost:9762/services/OTWebAccessService" />
			</syn:endpoint>
		</syn:send>
	</syn:sequence>
	
	
	<syn:localEntry key="KPTPMG16-XSLT">
		<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
			xmlns:p="http://ot.collab.wso2.org"
			xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"
			xmlns:fn="http://www.w3.org/2005/02/xpath-functions" version="2.0"
			exclude-result-prefixes="p fn">
			<xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
			<xsl:template match="/">
				<xsl:apply-templates select="//p:getDownloadsByType" />
			</xsl:template>

			<xsl:template match="//p:getDownloadsByType">
				<p:getDownloadsByType>
					<downloadedResource
						xmlns="http://ot.collab.wso2.org">
						<xsl:value-of select="//p:downloadedResource" />
					</downloadedResource>
					<limitStart xmlns="http://ot.collab.wso2.org">
						<xsl:value-of select="//p:limitStart" />
					</limitStart>
					<limitEnd xmlns="http://ot.collab.wso2.org">
						10
					</limitEnd>
				</p:getDownloadsByType>
			</xsl:template>
		</xsl:stylesheet>
	</syn:localEntry>
	
</syn:definitions>