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.

Reply via email to