You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by mule1 <ua...@yahoo.com> on 2007/11/23 17:11:25 UTC

what would be the best place to introduce xslt transformer for response message

Hello,

I want to introduce my own interceptor in my jaxws configuration where
before the response xml is generated, I want it to go through an xslt
transformer to transform the response xml. How can I introduce this
interceptor?
-- 
View this message in context: http://www.nabble.com/what-would-be-the-best-place-to-introduce-xslt-transformer-for-response-message-tf4862565.html#a13914839
Sent from the cxf-user mailing list archive at Nabble.com.


RE: what would be the best place to introduce xslt transformer for response message

Posted by yanke <ya...@dplanet.ch>.
Hi,
I just tried implementing an OutInterceptor to handle an XSLT.
I found an example in a 2007 message on this mailing list: 
http://old.nabble.com/what-would-be-the-best-place-to-introduce-xslt-transformer-for-response-message-td13914839.html#a13914839

I still have a small problem: the output message contains 2 SOAP headers, 1
of the message before translating (with an empty SOAP body) and 1 with my
translated message:

I just need to remove the first SOAP header (the bold part)

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns1:searchResponse xmlns:ns1="http://www.mydomain.com/schemaname">
</ns1:searchResponse>
</soap:Body>
</soap:Envelope>
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<ns2:SearchResponseNew xmlns:ns2="http://www.mydomain.com/newschemaname">
</ns2:SearchResponseNew>
</soap:Body>
</soap:Envelope>

any idea??

thanks in advance,
yanke

Here is my Interceptor code:
----------------------------------
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;

import javax.xml.transform.Source;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.io.CachedOutputStream;
import org.apache.cxf.phase.Phase;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class XSLTOutInterceptor  extends AbstractSoapInterceptor  implements
InitializingBean{
	
	private SAXTransformerFactory factory;
	private Resource xsltStyle = new
ClassPathResource("etc/xslt/Converter.xslt");
	private Templates templates;
	

    public XSLTOutInterceptor() {
        super(Phase.PRE_PROTOCOL);
    }
    
    public void handleMessage(SoapMessage message) throws Fault {
        boolean isOutbound = false;
        isOutbound = message == message.getExchange().getOutMessage()
               || message == message.getExchange().getOutFaultMessage();

        if (isOutbound) {
            //replace the original output stream with our own outputstream,
            //so that the output can be cached for xslt transformation,
otherwise
            //the output might be flushed to socket before xslt.
        	OutputStream originalOs = message.getContent(OutputStream.class);
        	CachedOutputStream cos = new CachedOutputStream();
            message.setContent(OutputStream.class, cos);

            // Add an ending interceptor to read the cached output and do
xslt
            message.getInterceptorChain().add(new
XSLTOutEndingInterceptor(originalOs));
        }
    }
    
    public class XSLTOutEndingInterceptor extends AbstractSoapInterceptor {
    	private OutputStream originalOs;
        public XSLTOutEndingInterceptor(OutputStream originalOs) {
            super(Phase.PRE_PROTOCOL_ENDING);
            this.originalOs = originalOs;
        }

        public void handleMessage(SoapMessage message) throws Fault {
            try {
                CachedOutputStream cos =
(CachedOutputStream)message.getContent(OutputStream.class);
                //the output is cached in CachedOutputStream, now lets do
xslt. 
               
                Transformer trans = templates.newTransformer();

                
                // write the transformed result back to the original output
stream         
                trans.transform(new StreamSource(cos.getInputStream()), new
StreamResult(originalOs));
                
                //set the original output stream back
                message.setContent(OutputStream.class, originalOs);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

	/**
	 * Not explicitly documented.
	 * @see
org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		Reader r = new BufferedReader(new
InputStreamReader(xsltStyle.getInputStream(), "UTF-8"));
		Source stylesheet = new StreamSource(r);
		if (factory == null) {
			factory = (SAXTransformerFactory) TransformerFactory.newInstance();
		}

		templates = factory.newTemplates(stylesheet);
		
	}

}

-- 
View this message in context: http://old.nabble.com/what-would-be-the-best-place-to-introduce-xslt-transformer-for-response-message-tp13914839p28523575.html
Sent from the cxf-user mailing list archive at Nabble.com.


RE: what would be the best place to introduce xslt transformer for response message

Posted by "rams.rapo" <ra...@gmail.com>.
I've to take back above query, i do now xslt features..was too fast in
posting message!
http://cxf.apache.org/docs/xslt-feature.html



--
View this message in context: http://cxf.547215.n5.nabble.com/what-would-be-the-best-place-to-introduce-xslt-transformer-for-response-message-tp567899p5722617.html
Sent from the cxf-user mailing list archive at Nabble.com.

RE: what would be the best place to introduce xslt transformer for response message

Posted by "rams.rapo" <sr...@gmail.com>.
This is a very old post but i was trying to do something similar where in
xslt and cachedoutputstream is used to transform soap response.  I'm trying
to understand how outputstream is set on cachedoutputstream and retrieved
back?
*Interceptor #1 * 
 OutputStream originalOs = message.getContent(OutputStream.class);
 CachedOutputStream cos = new CachedOutputStream(); --> this is empty object
 message.setContent(OutputStream.class, cos); 

*Interceptor #2 * 
 trans.transform(new StreamSource(cos.getInputStream()), new
StreamResult(originalOs)); 
 --> Isnt this cos empty?
Running below example resulted in empty streams/failures.

I ended up using staxtransformer - is this the right way to do in cxf or can
we do expliclity xslt processing?

thanks



--
View this message in context: http://cxf.547215.n5.nabble.com/what-would-be-the-best-place-to-introduce-xslt-transformer-for-response-message-tp567899p5722615.html
Sent from the cxf-user mailing list archive at Nabble.com.

RE: what would be the best place to introduce xslt transformer for response message

Posted by "Liu, Jervis" <jl...@iona.com>.
Here you go. It looks like below (roughly):

import java.io.IOException;
import java.io.OutputStream;

import javax.xml.transform.Transformer;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.io.CachedOutputStream;
import org.apache.cxf.phase.Phase;


public class XSLTOutInterceptor extends AbstractSoapInterceptor {
    private OutputStream originalOs;

    public XSLTOutInterceptor() {
        super(Phase.PRE_PROTOCOL);
    }
    
    public void handleMessage(SoapMessage message) throws Fault {
        boolean isOutbound = false;
        isOutbound = message == message.getExchange().getOutMessage()
               || message == message.getExchange().getOutFaultMessage();

        if (isOutbound) {
            //replace the original output stream with our own outputstream,
            //so that the output can be cached for xslt transformation, otherwise
            //the output might be flushed to socket before xslt.
            originalOs = message.getContent(OutputStream.class);
            CachedOutputStream cos = new CachedOutputStream();
            message.setContent(OutputStream.class, cos);

            // Add an ending interceptor to read the cached output and do xslt
            message.getInterceptorChain().add(new XSLTOutEndingInterceptor());
        }
    }
    
    public class XSLTOutEndingInterceptor extends AbstractSoapInterceptor {
        public XSLTOutEndingInterceptor() {
            super(Phase.PRE_PROTOCOL_ENDING);
        }

        public void handleMessage(SoapMessage message) throws Fault {
            try {
                CachedOutputStream cos = (CachedOutputStream)message.getContent(OutputStream.class);
                //the output is cached in CachedOutputStream, now lets do xslt.                
                Transformer trans =m ...      

                // write the transformed result back to the original output stream         
                trans.transform(new StreamSource(cos.getInputStream()), new StreamResult(originalOs));
                
                //set the original output stream back
                message.setContent(OutputStream.class, originalOs);
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }
}
> -----Original Message-----
> From: mule1 [mailto:uap001@yahoo.com]
> Sent: 2007年11月26日 22:29
> To: cxf-user@incubator.apache.org
> Subject: RE: what would be the best place to introduce xslt transformer for
> response message
> 
> 
> Hello,
> 
> I am not clear exactly what you said. I looked at the SAAJOutInterceptor,
> but didn't clearly understand how I can get the message content - either
> bytes or something that I can use to tranform with an xslt transfomer. If
> possible, can you provide me some sample lines of code? Thanks.
> --
> View this message in context:
> http://www.nabble.com/what-would-be-the-best-place-to-introduce-xslt-tr
> ansformer-for-response-message-tf4862565.html#a13950419
> Sent from the cxf-user mailing list archive at Nabble.com.

----------------------------
IONA Technologies PLC (registered in Ireland)
Registered Number: 171387
Registered Address: The IONA Building, Shelbourne Road, Dublin 4, Ireland

RE: what would be the best place to introduce xslt transformer for response message

Posted by mule1 <ua...@yahoo.com>.
Hello,

I am not clear exactly what you said. I looked at the SAAJOutInterceptor,
but didn't clearly understand how I can get the message content - either
bytes or something that I can use to tranform with an xslt transfomer. If
possible, can you provide me some sample lines of code? Thanks.
-- 
View this message in context: http://www.nabble.com/what-would-be-the-best-place-to-introduce-xslt-transformer-for-response-message-tf4862565.html#a13950419
Sent from the cxf-user mailing list archive at Nabble.com.


RE: what would be the best place to introduce xslt transformer for response message

Posted by "Liu, Jervis" <jl...@iona.com>.
I presume you are using SOAP binding. The best place to look at is the SAAJOutInterceptor . What this interceptor does is very similar to what you want to achieve. SAAJOutInterceptor firstly replaces the output stream that writes the output to socket with its own output stream that writes output to a DOM tree. At the SAAJOutEndingInterceptor, once all interceptors that may have impacts on the SOAP Message have finished their job, the DOM is made available for access. What you need to do is in your own XSLTInterceptor, you replace the original output stream with a cached output stream (you can use org.apache.cxf.io. CachedOutputStream), then at your XSLTEndingInterceptro, do an XSLT transformation then write the transformed result back to the original output XSLTInterceptro stream.

Cheers,
Jervis



> -----Original Message-----
> From: mule1 [mailto:uap001@yahoo.com]
> Sent: 2007年11月24日 1:46
> To: cxf-user@incubator.apache.org
> Subject: Re: what would be the best place to introduce xslt transformer for
> response message
> 
> 
> yes after the response XML is geneated, but before it is sent out.
> 
> --
> View this message in context:
> http://www.nabble.com/what-would-be-the-best-place-to-introduce-xslt-tr
> ansformer-for-response-message-tf4862565.html#a13916324
> Sent from the cxf-user mailing list archive at Nabble.com.

----------------------------
IONA Technologies PLC (registered in Ireland)
Registered Number: 171387
Registered Address: The IONA Building, Shelbourne Road, Dublin 4, Ireland

Re: what would be the best place to introduce xslt transformer for response message

Posted by mule1 <ua...@yahoo.com>.
yes after the response XML is geneated, but before it is sent out.

-- 
View this message in context: http://www.nabble.com/what-would-be-the-best-place-to-introduce-xslt-transformer-for-response-message-tf4862565.html#a13916324
Sent from the cxf-user mailing list archive at Nabble.com.


Re: what would be the best place to introduce xslt transformer for response message

Posted by Glen Mazza <gl...@verizon.net>.
I believe you mean *after* the response XML is generated, but before it
is sent out, correct?

Glen

Am Freitag, den 23.11.2007, 08:11 -0800 schrieb mule1:
> Hello,
> 
> I want to introduce my own interceptor in my jaxws configuration where
> before the response xml is generated, I want it to go through an xslt
> transformer to transform the response xml. How can I introduce this
> interceptor?