cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alexander Daniel <>
Subject Re: How to use SOAP to deliver a Cocoon report?
Date Sun, 27 Sep 2009 20:09:08 GMT
On 24.09.2009, at 17:56, David Beasley wrote:

> Can anyone help me with using the Axis-based Cocoon SOAP server?
> I want to be able to deliver an XML-formatted report in response to  
> a SOAP request. I've written a pipeline that produces the report.  
> Cocoon will handle SOAP requests ok, but only as a Reader service  
> (not a Generator or Transformer). So, I'm trying to work out how I  
> integrate the SOAP request with the report-generating pipeline I've  
> written.
> It seems to me that I need to write some java code, to be invvoked  
> by the SOAP request, and have this code itself invoke Cocoon to  
> generate the XML report - which the java code then includes in the  
> body of the SOAP response.
> Is this the best way to do it? And if so, does anyone have any hints/ 
> tips/sample code?
> Or is there some other way to integrate the result of a Cocoon  
> pipeline with the response to a SOAP request, that doesnt involve  
> calling out to a java routine which then calls back into Cocoon?
> I'm thinking that life might be easier if there were a separate SOAP  
> Generator and SOAP Serializer, and I could then plug my report- 
> generating pipeline into the middle. Or is it not as simple as that?
> David Beasley

I can you provide some sample code for a SOAP transformer which  
expects as input the SOAP request surrounded by a special element  
which attributes specify the endpoint and the soapAction. The input is  
replaced by the SOAP response of the server.

<map:generate src="soapRequest.xml"/>
<map:transform type="soap"/>
<map:serialize type="xml"/>

Sample soapRequest.xml:
<soapTransformer:call endpointUri=" 
" soapAction=""  
	<SOAP-ENV:Envelope xmlns:SOAP-ENV=" 
			<GetPrimeNumbers xmlns="">

public class SoapTransformer extends AbstractSAXTransformer {

	private SoapClient soapClient;
	private String endpointUri;
	private String soapAction;

	public SoapTransformer() {
		defaultNamespaceURI = " 

	public void startTransformingElement(String uri, String name, String  
raw, Attributes attr) throws ProcessingException, IOException,  
SAXException {
		endpointUri = attr.getValue("endpointUri");
		soapAction = attr.getValue("soapAction");

	public void endTransformingElement(String uri, String name, String  
raw) throws ProcessingException, IOException, SAXException {
		String soapRequest = endSerializedXMLRecording();
		String soapResponse = getSoapClient().call(endpointUri, soapAction,  

	private void toSax(String soapResponse) throws SAXException,  
IOException {
		XMLReader xmlReader = XMLReaderFactory.createXMLReader();
		xmlReader.parse(new InputSource(new StringReader(soapResponse)));

	public void setSoapClient(SoapClient soapClient) {
		this.soapClient = soapClient;

	public SoapClient getSoapClient() {
		return soapClient;


public interface SoapClient {
	public String call(String endpointUri, String soapAction, String  

public class SoapClientImpl implements SoapClient {

	private static final String ENCODING = "utf-8";

	public String call(String endpointUri, String soapAction, String  
soapRequest) {
		try {
			HttpURLConnection httpConn = createHttpConnection(endpointUri);
			configureHttpHeaders(httpConn, soapAction, soapRequest);

			OutputStream out = httpConn.getOutputStream();
			return IOUtils.toString(httpConn.getInputStream(), ENCODING);			
		} catch (Exception e) {
			throw new SoapException(String.format("SOAP call to %s failed:  
soapAction='%s', soapRequest='%s'", endpointUri, soapAction,  
soapRequest), e);

	private void configureHttpHeaders(HttpURLConnection httpConn, String  
soapAction, String soapRequest) throws UnsupportedEncodingException,  
ProtocolException {
		httpConn.setRequestProperty("Content-Length", String.valueOf 
		httpConn.setRequestProperty("Content-Type", "text/xml; charset=" +  
		httpConn.setRequestProperty("SOAPAction", soapAction);

	private HttpURLConnection createHttpConnection(String endpointUri)  
throws MalformedURLException, IOException {
		URL url = new URL(endpointUri);
		URLConnection connection = url.openConnection();
		HttpURLConnection httpConn = (HttpURLConnection) connection;
		return httpConn;



To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message