cxf-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cogitate <mu...@etrade.com>
Subject removing namespaces and prefixes for Doc/Literal/Bare or pure xml service
Date Thu, 04 Mar 2010 22:56:04 GMT

i am writing a pure-xml service and followed all the instructions for writing
an xml service
http://cxf.apache.org/docs/pure-xml.html

i have looked at this :
http://old.nabble.com/Removing-XML-namespaces-and-prefixes-td24457677.html#a24457677

i have the service interface setup as Doc/Literal/Bare however , the
transport xml still has prefixes and URIs like the following :
<ns3:balances xmlns:ns3="http://schemas.mycompany.com/SVC">
<account_id/>
<account_name/>
</ns3:balances>

i also tried setting <xformat:binding rootNode="blah"> for bindings in the
wsdl.however , this doesn't seem to be picked up.

so ,following
org.apache.cxf.binding.xml.interceptor.XMLMessageOutInterceptor i wrote a
CustomXMLMessageOutInterceptor that extends from this and did an
"..addAfter(XMLMessageOutInterceptor.class.getName()).."

Code fragment :
public void handleMessage(Message message)
{
     BindingOperationInfo operation = (BindingOperationInfo)    
message.getExchange().get(BindingOperationInfo.class.getName());
        out.println("bio="+operation);

        MessageContentsList objs =
MessageContentsList.getContentsList(message);
        List<MessagePartInfo> parts =
operation.getOutput().getMessageParts();
        XMLStreamWriter writer = message.getContent(XMLStreamWriter.class);
        Exchange exchange = message.getExchange();
        for (MessagePartInfo part : parts)
        {
            out.println(part);
            QName temp = part.getName();
            out.println("prefix="+temp.getPrefix()+"
nsURI="+temp.getNamespaceURI()+" local part="+temp.getLocalPart());
            try
            {
                writer.setDefaultNamespace("");
                writer.writeStartElement(temp.getLocalPart());
                writer.writeEndElement();
            }
            catch (XMLStreamException e)
            {
                e.printStackTrace();
            }
        }

....

    [java] javax.xml.stream.XMLStreamException: Trying to output second
root, 
<balances>
     [java]     at
com.ctc.wstx.sw.BaseStreamWriter.throwOutputError(BaseStreamWriter.java:1473)
     [java]     at
com.ctc.wstx.sw.BaseStreamWriter.throwOutputError(BaseStreamWriter.java:1480)
     [java]     at
com.ctc.wstx.sw.BaseStreamWriter.reportNwfStructure(BaseStreamWriter.java:1508)
     [java]     at
com.ctc.wstx.sw.BaseNsStreamWriter.checkStartElement(BaseNsStreamWriter.java:444)
     [java]     at
com.ctc.wstx.sw.BaseNsStreamWriter.writeStartElement(BaseNsStreamWriter.java:292)
     [java]     at
com.mycomp.utils.interceptor.CustomXMLMessageOutInterceptor.writeMessage(CustomXMLMessageOutInterceptor.java:77)
     [java]     at
com.mycomp.utils.interceptor.CustomXMLMessageOutInterceptor.handleMessage(CustomXMLMessageOutInterceptor.java:48)
     [java]     at
org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:243)
     [java]     at
org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:484)
     [java]     at
org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:310)
     [java]     at
org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:262)
     [java]     at
org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:73)
     [java]     at
org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:124)


this shows that the underlying XmlStreamWriter doesn't allow non-namespace
values to be sent across the wire.

the alternative that i tried was trying to extend from 
LoggingOutInterceptor and do an *addBefore* loggingout interceptor which
provides a way to manipulate the output stream by writing to
cachedoutputstream and registering my own call back.

also wrote "onClose(CachedOutputStream cos)" and used a stringbuilder ->
stuffed it back into a DOM object,removed namespaces using an XSLT
stylesheet and tried to set this outputstream by doing the following:

class MessageCallback implements CachedOutputStreamCallback
    {
        private Message message;
        private OutputStream origStream;

        public MessageCallback(final Message msg, final OutputStream os)
        {
            message = msg;
            origStream = os;
        }

        public void onFlush(CachedOutputStream cos){}

        public void onClose(CachedOutputStream cos)
        {
            out.println("############# Calling onClose ########### ");
            StringBuilder strContent = new StringBuilder();
            try
            {
                cos.writeCacheTo(strContent,limit);
            }
            catch(Exception e)
            {
                out.println("############# Exception writeCacheTo
########### ");
                e.printStackTrace();
            }
            out.println("########### OriginalXML :############");
            out.println(strContent.toString());

           OutputStream finalOut = new ByteArrayOutputStream(2048);
            try
            {
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document doc = db.parse(bAIS);
                out.println("######### Manipulated XML::############");
                String xmlDoc = getXMLDoc(doc);
                out.println(xmlDoc);
                byte xmlDocBytes[] = xmlDoc.getBytes();
                ByteArrayInputStream docBAIS = new
ByteArrayInputStream(xmlDocBytes);
                IOUtils.copyAndCloseInput(docBAIS,finalOut);
                finalOut.flush();
                finalOut.close();
            }
            catch(Exception e)
            {
                out.println("####### Excepsion############");
                e.printStackTrace();
            }

            try
            {
                cos.lockOutputStream();
                cos.resetOut(null,false);
            }
            catch(Exception e)
            {
                //ignore
            }
            message.setContent(OutputStream.class,finalOut);
}

however , it seems this message doesn't showup in the "Outbound Message" of
the loggingoutinterceptor and as such seems to be called *after*
loggingoutinterceptor even though it's declared as :
 public PhaseMessageOutInterceptor() {
        this(Phase.PRE_STREAM);
    }
    public PhaseMessageOutInterceptor(String phase) {
        super(phase);
        addBefore(LoggingOutInterceptor.class.getName());
    }

i am sure i have misunderstood something , so any help would be valuable.
thanks and regards






-- 
View this message in context: http://old.nabble.com/removing-namespaces-and-prefixes-for-Doc-Literal-Bare-or-pure-xml-service-tp27787785p27787785.html
Sent from the cxf-user mailing list archive at Nabble.com.


Mime
View raw message