Thanks for all the help. I got it working. Here is the solution:

public class CdataWriterInterceptor extends AbstractPhaseInterceptor<Message> {

        public CdataWriterInterceptor() {
                super(Phase.PRE_STREAM);
                addAfter(AttachmentOutInterceptor.class.getName());
        }

        @Override
        public void handleMessage(Message message) {
                message.put("disable.outputstream.optimization", Boolean.TRUE);
                XMLStreamWriter writer =
StaxUtils.createXMLStreamWriter(message.getContent(OutputStream.class));
                message.setContent(XMLStreamWriter.class, new 
CDataXMLStreamWriter(writer));
        }
}

public class CDataXMLStreamWriter extends DelegatingXMLStreamWriter {

        private String currentElementName;

        public CDataXMLStreamWriter(XMLStreamWriter del) {
                super(del);
        }

        @Override
        public void writeCharacters(String text) throws XMLStreamException {
                boolean useCData = checkIfCDATAneededForCurrentElement();
                if (useCData) {
                        System.out.println("WritingCData" + text);
                        super.writeCData(text);
                }else {
                super.writeCharacters(text);
                }
        }

        private boolean checkIfCDATAneededForCurrentElement() {
                if("MessageBody".equals(currentElementName)) return true;
                return false;
        }

        public void writeStartElement(String prefix, String local, String
uri) throws XMLStreamException {
                currentElementName = local;
                super.writeStartElement(prefix, local, uri);
        }
}

On 1 March 2012 23:19, Daniel Kulp <[email protected]> wrote:
>
>
> Add a line:
>
> message.put("disable.outputstream.optimization", Boolean.TRUE);
>
> into the handleMessage method of your interceptor.  That likely will fix that.
>
>
> Dan
>
>
> On Thursday, March 01, 2012 1:42:00 PM Idar Borlaug wrote:
>> Ok i am now trying to create an interceptor that captures element name
>> and prints CDATA in the correct element.
>>
>> The problem is my code intercept everyting until soap:body tag, then it
>> stops.
>>
>> I probably have it injected the wrong way in the cxf interceptro
>> chain. Can anyone point me in the right direction. the output is now:
>> <soap:Envelope
>> xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/";><soap:Body><![CDATA[
>> ]]><Insert
>>
>> public class CDataXMLStreamWriter extends DelegatingXMLStreamWriter {
>>
>>       private String currentElementName;
>>
>>       public CDataXMLStreamWriter(XMLStreamWriter del) {
>>               super(del);
>>       }
>>
>>       @Override
>>       public void writeCharacters(String text) throws XMLStreamException {
>>               boolean useCData = checkIfCDATAneededForCurrentElement();
>>               if (useCData) {
>>                       System.out.println("WritingCData" + text);
>>                       super.writeCData(text);
>>               }else {
>>               super.writeCharacters(text);
>>               }
>>       }
>>
>>       private boolean checkIfCDATAneededForCurrentElement() {
>>               System.out.println("ElementName " + currentElementName);
>>               return true;
>>       }
>>
>>       public void writeStartElement(String prefix, String local, String
>> uri) throws XMLStreamException {
>>               currentElementName = local;
>>               super.writeStartElement(prefix, local, uri);
>>       }
>> }
>>
>> public class CdataWriterInterceptor extends
>> AbstractPhaseInterceptor<Message> {
>>
>>       public CdataWriterInterceptor() {
>>               super(Phase.WRITE);
>>               addBefore(BareOutInterceptor.class.getName());
>>       }
>>
>>       @Override
>>       public void handleMessage(Message message) {
>>               System.out.println("setting altinnfix on message");
>>               XMLStreamWriter writer =
>> StaxUtils.createXMLStreamWriter(message.getContent(OutputStream.class));
>>               message.setContent(XMLStreamWriter.class, new
>> CDataXMLStreamWriter(writer)); }
>> }
> --
> Daniel Kulp
> [email protected] - http://dankulp.com/blog
> Talend Community Coder - http://coders.talend.com



-- 
Idar

Reply via email to