cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alexander Daniel <alexander.dan...@gmx.at>
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.

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

Sample soapRequest.xml:
<soapTransformer:call endpointUri="http://www50.brinkster.com/vbfacileinpt/np.asmx 
" soapAction="http://microsoft.com/webservices/GetPrimeNumbers"  
xmlns:soapTransformer="http://cocoon.apache.org/transformation/soap/ 
1.0">
	<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/ 
">
		<SOAP-ENV:Header/>
		<SOAP-ENV:Body>
			<GetPrimeNumbers xmlns="http://microsoft.com/webservices/">
				<max>11</max>
			</GetPrimeNumbers>
		</SOAP-ENV:Body>
	</SOAP-ENV:Envelope>
</soapTransformer:call>

public class SoapTransformer extends AbstractSAXTransformer {

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

	public SoapTransformer() {
		defaultNamespaceURI = "http://cocoon.apache.org/transformation/soap/1.0 
";
	}

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

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

	private void toSax(String soapResponse) throws SAXException,  
IOException {
		XMLReader xmlReader = XMLReaderFactory.createXMLReader();
		xmlReader.setContentHandler(contentHandler);
		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  
soapRequest);
}

public class SoapClientImpl implements SoapClient {

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

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

			OutputStream out = httpConn.getOutputStream();
			out.write(soapRequest.getBytes(ENCODING));
			out.close();
			
			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 
(soapRequest.getBytes(ENCODING).length));
		httpConn.setRequestProperty("Content-Type", "text/xml; charset=" +  
ENCODING);
		httpConn.setRequestProperty("SOAPAction", soapAction);
		httpConn.setRequestMethod("POST");
		httpConn.setDoOutput(true);
		httpConn.setDoInput(true);
	}

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

}

Alex


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Mime
View raw message