You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Juan José Vázquez Delgado <ju...@gmail.com> on 2009/07/13 10:11:04 UTC

Removing XML namespaces and prefixes

Hi,

I have a JAX-RS web service which uses JAXB for binding. Everything is
ok, but currently I have a Flash client which is not ready to
understand neither namespaces nor prefixes at all. I have to clean the
output XML of namespaces and prefixes, included xmlns attributes, but
I would´nt like having to modify my xml schemas so I´m looking for a
solution on binding time.

My approach has been to extend JAXBElementProvider in order to wrap
the default XMLStreamWriter, CachingXmlEventWriter in this case. So I
have something like this:

public class MyCustomJAXBElementProvider extends JAXBElementProvider {

    @Override
    protected void marshalToWriter(Marshaller ms, Object obj,
            XMLStreamWriter writer, MediaType mt) throws Exception {
        CachingXmlEventWriterWrapper wrappedWriter = new
CachingXmlEventWriterWrapper(writer);
        super.marshalToWriter(ms, obj, wrappedWriter, mt);
    }

}

class CachingXmlEventWriterWrapper implements XMLStreamWriter {

     private XMLStreamWriter wrappedStreamWriter;

     public CachingXmlEventWriterWrapper(XMLStreamWriter wrappedStreamWriter) {
            this.wrappedStreamWriter = wrappedStreamWriter;

     }

    ............

}

The wrapper implementation tries to generate a "clean" XML
overwritting "setPrefix", "writeEmptyElement" and others methods  but
I didn´t get to remove the "xmlns" attributes. Any ideas?.

Thanks in advance.

Regards,

Juanjo.

Re: Removing XML namespaces and prefixes

Posted by Sergey Beryozkin <se...@iona.com>.
Excellent, thanks for confirming it's working for you now...

Cheers, Sergey


Juan José Vázquez Delgado wrote:
> 
> Hi,
> 
>>
>> You might want to check the methods like getNamespaceContext() (probably
>> return an empty implementation), getPrefix(), getDefaultNamespace
> 
> Finally, it has been enough to implement a dummy NamespaceContext like
> this:
> 
> class EmptyNamespaceContext implements NamespaceContext {
> 
>         public String getNamespaceURI(String prefix) {
>             return "";
>         }
> 
>         public String getPrefix(String namespaceURI) {
>             return "";
>         }
> 
>         public Iterator getPrefixes(String namespaceURI) {
>             return null;
>         }
> 
>     }
> 
> and overwrite the getNamespaceContext method to retrieve a new
> instance of EmptyNamespaceContext:
> 
>        @Override
>         public NamespaceContext getNamespaceContext() {
>             return new EmptyNamespaceContext();
>         }
> 
>>
>> By the way, it could be simpler if your writer just extends
>>
>> http://svn.apache.org/repos/asf/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/DelegatingXMLStreamWriter.java
> 
> With this my code stays cleaner.
> 
> Sergey, thank you again for your support and keep up the good work!.
> 
> Regards,
> 
> Juanjo.
> 
> 

-- 
View this message in context: http://www.nabble.com/Removing-XML-namespaces-and-prefixes-tp24457677p24462048.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: Removing XML namespaces and prefixes

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi, sorry for a delay.

I can not reproduce it, it works fine for me on the trunk and the same 
code does live in 2.4.x.
I'm wondering what can be interfering in your case though.
Can you also try CXF 2.5.0 ? and may be provide a simple Maven-based 
test case ?

This is my client code:

@Test
     public void testGetModel() {
         BookStore store =
             JAXRSClientFactory.create("http://localhost:" + PORT, 
BookStore.class);
         TestModel model = store.getModel();
         System.out.println(model.getName());
     }

     @Test
     public void testGetModels() {
         BookStore store =
             JAXRSClientFactory.create("http://localhost:" + PORT, 
BookStore.class);
         List<TestModel> models = store.getModels();
         for (TestModel model : models) {
             System.out.println(model.getName());
         }
     }

where store.getModel() and store.getModels() are implemented exactly as 
you show below; it works fine for me

Sergey

On 02/11/11 10:31, sotiris wrote:
> Hi Sergey,
>
> Thanks for your reply. My bad I didn't mention the version I was trying it
> on in the first place.
> It was 2.3.1, so after your suggestion I retested it with 2.4.3, but
> unfortunately with the same results.
>
> For a minute I thought that it might be my structures that are causing this
> behavior, so I did a very simple test with the simplest possible model.
>
> The result is unfortunately the same.
>
> I'm attaching the code in case there is something obvious that I'm missing.
>
> ---------------------------TestModel---------------------------------
> import javax.xml.bind.annotation.XmlAccessType;
> import javax.xml.bind.annotation.XmlAccessorType;
> import javax.xml.bind.annotation.XmlAttribute;
> import javax.xml.bind.annotation.XmlRootElement;
>
> @XmlRootElement(name="testRoot")
> @XmlAccessorType(XmlAccessType.FIELD)
> public class TestModel {
> 	
> 	private String name;
>
> 	@XmlAttribute
> 	private String attr;
> 	
>
> 	public TestModel() {
> 	}
>
> 	public TestModel(String name, String attr) {
> 		this.name = name;
> 		this.attr = attr;
> 	}
>
> 	public String getName() {
> 		return name;
> 	}
>
> 	public void setName(String name) {
> 		this.name = name;
> 	}
>
> 	public String getAttr() {
> 		return attr;
> 	}
>
> 	public void setAttr(String attr) {
> 		this.attr = attr;
> 	}
> 	
> }
> ---------------------------TestManager-------------------------------
> import java.util.List;
>
> public interface TestManager {
> 	
> 	TestModel getModel();
> 	
> 	List<TestModel>  getModels();
> }
>
> ---------------------------TestManagerImpl---------------------------
> import java.util.ArrayList;
> import java.util.List;
>
> import javax.ws.rs.GET;
> import javax.ws.rs.Path;
>
> import org.springframework.stereotype.Service;
>
> import TestManager;
> import TestModel;
>
> @Service("testManager")
> public class TestManagerImpl implements TestManager {
> 	
> 	@GET
> 	@Path("/model/")
> 	public TestModel getModel() {
> 		return new TestModel("test", "attr");
> 	}
>
> 	@GET
> 	@Path("/models/")
> 	public List<TestModel>  getModels() {
> 		ArrayList<TestModel>  models = new ArrayList<TestModel>();
> 		models.add(new TestModel("test1", "attr"));
> 		models.add(new TestModel("test2", "attr"));
> 		return models;
> 	}
>
> }
>
>
> -----------------------------------cxf-servlet.xml----------------------------------
> <jaxrs:server address="/rs/test/">
>          <jaxrs:serviceBeans>
>              <ref bean="testManager"/>
>          </jaxrs:serviceBeans>
>          <jaxrs:providers>
>              <ref bean="ignoreNsResponseHandler"/>
>          </jaxrs:providers>
>          <jaxrs:extensionMappings>
>              <entry key="json" value="application/json"/>
>              <entry key="xml" value="application/xml"/>
>              <entry key="feed" value="application/atom+xml"/>
>          </jaxrs:extensionMappings>
> </jaxrs:server>
>
>
>
>
> -----Results------------
> -----------rs/test/model/-------------
> <testRoot attr="attr">
> <name>test</name>
> </testRoot>
> -----------rs/test/models/------------
> <testRoots><testRoot attr="attr"><name>test1</name></testRoot>JAXBException
> occurred : Trying to output second root,<testRoot>. Trying to output second
> root,<testRoot>.
>
>
>
> Regards,
> Sotiris
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Removing-XML-namespaces-and-prefixes-tp565416p4957728.html
> Sent from the cxf-user mailing list archive at Nabble.com.


-- 
Sergey Beryozkin

http://sberyozkin.blogspot.com

Talend Community Coders
http://coders.talend.com/

Re: Removing XML namespaces and prefixes

Posted by sotiris <s_...@hotmail.com>.
Hi Sergey,



Sergey Beryozkin-5 wrote:
> 
> Hi
> 
> Do you use the default JAXB provider or configure it somehow ?
> 
> Cheers, Sergey
> 
> 

Well, the truth is that I was configuring the JAXB provider with some extra
jaxb context classes, but I have completely removed this configuration and
still I get the same error.

Thanks and Regards,
Sotiris

--
View this message in context: http://cxf.547215.n5.nabble.com/Removing-XML-namespaces-and-prefixes-tp565416p4957981.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Removing XML namespaces and prefixes

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi

Do you use the default JAXB provider or configure it somehow ?

Cheers, Sergey
On 02/11/11 10:31, sotiris wrote:
> Hi Sergey,
>
> Thanks for your reply. My bad I didn't mention the version I was trying it
> on in the first place.
> It was 2.3.1, so after your suggestion I retested it with 2.4.3, but
> unfortunately with the same results.
>
> For a minute I thought that it might be my structures that are causing this
> behavior, so I did a very simple test with the simplest possible model.
>
> The result is unfortunately the same.
>
> I'm attaching the code in case there is something obvious that I'm missing.
>
> ---------------------------TestModel---------------------------------
> import javax.xml.bind.annotation.XmlAccessType;
> import javax.xml.bind.annotation.XmlAccessorType;
> import javax.xml.bind.annotation.XmlAttribute;
> import javax.xml.bind.annotation.XmlRootElement;
>
> @XmlRootElement(name="testRoot")
> @XmlAccessorType(XmlAccessType.FIELD)
> public class TestModel {
> 	
> 	private String name;
>
> 	@XmlAttribute
> 	private String attr;
> 	
>
> 	public TestModel() {
> 	}
>
> 	public TestModel(String name, String attr) {
> 		this.name = name;
> 		this.attr = attr;
> 	}
>
> 	public String getName() {
> 		return name;
> 	}
>
> 	public void setName(String name) {
> 		this.name = name;
> 	}
>
> 	public String getAttr() {
> 		return attr;
> 	}
>
> 	public void setAttr(String attr) {
> 		this.attr = attr;
> 	}
> 	
> }
> ---------------------------TestManager-------------------------------
> import java.util.List;
>
> public interface TestManager {
> 	
> 	TestModel getModel();
> 	
> 	List<TestModel>  getModels();
> }
>
> ---------------------------TestManagerImpl---------------------------
> import java.util.ArrayList;
> import java.util.List;
>
> import javax.ws.rs.GET;
> import javax.ws.rs.Path;
>
> import org.springframework.stereotype.Service;
>
> import TestManager;
> import TestModel;
>
> @Service("testManager")
> public class TestManagerImpl implements TestManager {
> 	
> 	@GET
> 	@Path("/model/")
> 	public TestModel getModel() {
> 		return new TestModel("test", "attr");
> 	}
>
> 	@GET
> 	@Path("/models/")
> 	public List<TestModel>  getModels() {
> 		ArrayList<TestModel>  models = new ArrayList<TestModel>();
> 		models.add(new TestModel("test1", "attr"));
> 		models.add(new TestModel("test2", "attr"));
> 		return models;
> 	}
>
> }
>
>
> -----------------------------------cxf-servlet.xml----------------------------------
> <jaxrs:server address="/rs/test/">
>          <jaxrs:serviceBeans>
>              <ref bean="testManager"/>
>          </jaxrs:serviceBeans>
>          <jaxrs:providers>
>              <ref bean="ignoreNsResponseHandler"/>
>          </jaxrs:providers>
>          <jaxrs:extensionMappings>
>              <entry key="json" value="application/json"/>
>              <entry key="xml" value="application/xml"/>
>              <entry key="feed" value="application/atom+xml"/>
>          </jaxrs:extensionMappings>
> </jaxrs:server>
>
>
>
>
> -----Results------------
> -----------rs/test/model/-------------
> <testRoot attr="attr">
> <name>test</name>
> </testRoot>
> -----------rs/test/models/------------
> <testRoots><testRoot attr="attr"><name>test1</name></testRoot>JAXBException
> occurred : Trying to output second root,<testRoot>. Trying to output second
> root,<testRoot>.
>
>
>
> Regards,
> Sotiris
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Removing-XML-namespaces-and-prefixes-tp565416p4957728.html
> Sent from the cxf-user mailing list archive at Nabble.com.


Re: Removing XML namespaces and prefixes

Posted by sotiris <s_...@hotmail.com>.
Hi Sergey,

Thanks for your reply. My bad I didn't mention the version I was trying it
on in the first place. 
It was 2.3.1, so after your suggestion I retested it with 2.4.3, but
unfortunately with the same results.

For a minute I thought that it might be my structures that are causing this
behavior, so I did a very simple test with the simplest possible model.

The result is unfortunately the same.

I'm attaching the code in case there is something obvious that I'm missing.

---------------------------TestModel---------------------------------
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="testRoot")
@XmlAccessorType(XmlAccessType.FIELD)
public class TestModel {
	
	private String name;

	@XmlAttribute
	private String attr;
	

	public TestModel() {
	}

	public TestModel(String name, String attr) {
		this.name = name;
		this.attr = attr;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAttr() {
		return attr;
	}

	public void setAttr(String attr) {
		this.attr = attr;
	}
	
}
---------------------------TestManager-------------------------------
import java.util.List;

public interface TestManager {
	
	TestModel getModel();
	
	List<TestModel> getModels();
}

---------------------------TestManagerImpl---------------------------
import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

import org.springframework.stereotype.Service;

import TestManager;
import TestModel;

@Service("testManager")
public class TestManagerImpl implements TestManager {
	
	@GET
	@Path("/model/")
	public TestModel getModel() {
		return new TestModel("test", "attr");
	}

	@GET
	@Path("/models/")
	public List<TestModel> getModels() {
		ArrayList<TestModel> models = new ArrayList<TestModel>();
		models.add(new TestModel("test1", "attr"));
		models.add(new TestModel("test2", "attr"));
		return models;
	}

}


-----------------------------------cxf-servlet.xml----------------------------------
<jaxrs:server address="/rs/test/">
        <jaxrs:serviceBeans>
            <ref bean="testManager"/>
        </jaxrs:serviceBeans>
        <jaxrs:providers>
            <ref bean="ignoreNsResponseHandler"/>
        </jaxrs:providers>
        <jaxrs:extensionMappings>
            <entry key="json" value="application/json"/>
            <entry key="xml" value="application/xml"/>
            <entry key="feed" value="application/atom+xml"/>
        </jaxrs:extensionMappings>
</jaxrs:server>




-----Results------------
-----------rs/test/model/-------------
<testRoot attr="attr">
<name>test</name>
</testRoot>
-----------rs/test/models/------------
<testRoots><testRoot attr="attr"><name>test1</name></testRoot>JAXBException
occurred : Trying to output second root, <testRoot>. Trying to output second
root, <testRoot>. 



Regards,
Sotiris

--
View this message in context: http://cxf.547215.n5.nabble.com/Removing-XML-namespaces-and-prefixes-tp565416p4957728.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Removing XML namespaces and prefixes

Posted by Sergey Beryozkin <sb...@gmail.com>.
H
On 27/10/11 11:58, sotiris wrote:
> Hi,
>
> I had the same need and this post was the best solution I could found.
> Thanks for that, everything worked fine. With one exception though. When the
> returned object is a list then I get the following exception
>
> [javax.xml.stream.XMLStreamException: Trying to output second root,<draw>]
> 	at
> com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:322)
> 	at
> com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:172)
> 	at
> org.apache.cxf.jaxrs.provider.JAXBElementProvider.marshalToWriter(JAXBElementProvider.java:435)
> 	at


This may have been fixed in CXF 2.4.3 - can you try it ? I recall there 
were some issues with applying a transform feature to collections, but 
that is now tested...

Cheers, Sergey

> org.apache.cxf.jaxrs.provider.JAXBElementProvider.marshal(JAXBElementProvider.java:394)
> 	at
> org.apache.cxf.jaxrs.provider.JAXBElementProvider.marshalCollectionMember(JAXBElementProvider.java:323)
> 	at
> org.apache.cxf.jaxrs.provider.JAXBElementProvider.marshalCollection(JAXBElementProvider.java:299)
> 	at
> org.apache.cxf.jaxrs.provider.JAXBElementProvider.writeTo(JAXBElementProvider.java:243)
> 	at
> org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.serializeMessage(JAXRSOutInterceptor.java:249)
> 	at
> org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.processResponse(JAXRSOutInterceptor.java:139)
> 	at
> org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.handleMessage(JAXRSOutInterceptor.java:78)
> 	at
> org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:255)
> 	at
> org.apache.cxf.interceptor.OutgoingChainInterceptor.handleMessage(OutgoingChainInterceptor.java:77)
> 	at
> org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:255)
> 	at
> org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:113)
> 	at
> org.apache.cxf.transport.servlet.ServletDestination.invoke(ServletDestination.java:97)
> 	at
> org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:461)
> 	at
> org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:149)
> 	at
> org.apache.cxf.transport.servlet.AbstractCXFServlet.invoke(AbstractCXFServlet.java:148)
> 	at
> org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:179)
> 	at
> org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet(AbstractHTTPServlet.java:108)
> 	at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
>
> Do you know what the cause is and how I can bypass this?
>
> Thanks and Regards,
> Sotiris
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Removing-XML-namespaces-and-prefixes-tp565416p4942643.html
> Sent from the cxf-user mailing list archive at Nabble.com.


Re: Removing XML namespaces and prefixes

Posted by sotiris <s_...@hotmail.com>.
Hi,

I had the same need and this post was the best solution I could found.
Thanks for that, everything worked fine. With one exception though. When the
returned object is a list then I get the following exception

[javax.xml.stream.XMLStreamException: Trying to output second root, <draw>]
	at
com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:322)
	at
com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:172)
	at
org.apache.cxf.jaxrs.provider.JAXBElementProvider.marshalToWriter(JAXBElementProvider.java:435)
	at
org.apache.cxf.jaxrs.provider.JAXBElementProvider.marshal(JAXBElementProvider.java:394)
	at
org.apache.cxf.jaxrs.provider.JAXBElementProvider.marshalCollectionMember(JAXBElementProvider.java:323)
	at
org.apache.cxf.jaxrs.provider.JAXBElementProvider.marshalCollection(JAXBElementProvider.java:299)
	at
org.apache.cxf.jaxrs.provider.JAXBElementProvider.writeTo(JAXBElementProvider.java:243)
	at
org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.serializeMessage(JAXRSOutInterceptor.java:249)
	at
org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.processResponse(JAXRSOutInterceptor.java:139)
	at
org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.handleMessage(JAXRSOutInterceptor.java:78)
	at
org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:255)
	at
org.apache.cxf.interceptor.OutgoingChainInterceptor.handleMessage(OutgoingChainInterceptor.java:77)
	at
org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:255)
	at
org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:113)
	at
org.apache.cxf.transport.servlet.ServletDestination.invoke(ServletDestination.java:97)
	at
org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:461)
	at
org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:149)
	at
org.apache.cxf.transport.servlet.AbstractCXFServlet.invoke(AbstractCXFServlet.java:148)
	at
org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:179)
	at
org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet(AbstractHTTPServlet.java:108)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)

Do you know what the cause is and how I can bypass this?

Thanks and Regards,
Sotiris

--
View this message in context: http://cxf.547215.n5.nabble.com/Removing-XML-namespaces-and-prefixes-tp565416p4942643.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Removing XML namespaces and prefixes

Posted by Juan José Vázquez Delgado <ju...@gmail.com>.
Hi,

>
> You might want to check the methods like getNamespaceContext() (probably
> return an empty implementation), getPrefix(), getDefaultNamespace

Finally, it has been enough to implement a dummy NamespaceContext like this:

class EmptyNamespaceContext implements NamespaceContext {

        public String getNamespaceURI(String prefix) {
            return "";
        }

        public String getPrefix(String namespaceURI) {
            return "";
        }

        public Iterator getPrefixes(String namespaceURI) {
            return null;
        }

    }

and overwrite the getNamespaceContext method to retrieve a new
instance of EmptyNamespaceContext:

       @Override
        public NamespaceContext getNamespaceContext() {
            return new EmptyNamespaceContext();
        }

>
> By the way, it could be simpler if your writer just extends
>
> http://svn.apache.org/repos/asf/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/DelegatingXMLStreamWriter.java

With this my code stays cleaner.

Sergey, thank you again for your support and keep up the good work!.

Regards,

Juanjo.

Re: Removing XML namespaces and prefixes

Posted by Sergey Beryozkin <se...@iona.com>.
Hi

You might want to check the methods like getNamespaceContext() (probably
return an empty implementation), getPrefix(), getDefaultNamespace

By the way, it could be simpler if your writer just extends 

http://svn.apache.org/repos/asf/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/DelegatingXMLStreamWriter.java

cheers, Sergey


Juan José Vázquez Delgado wrote:
> 
> Hi,
> 
> First of all thank you for your help.
> 
>>
>> Custom providers can also extend this method and wrap
>> the writer with yet another writer as you do, but in your specific case
>> you
>> need to register a writer using a response filter, here's an example :
>>
>> http://svn.apache.org/repos/asf/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/XmlStreamWriterProvider.java
>>
>> you can register this filter the same way as the custom JAXB provider.
> 
> I have implemented a NoNamespacesResponseHandler getting a cleaner
> solution to my problem. Thanks.
> 
> In the other hand, my problem seems to rely on the
> CustomXmlStreamWriter I´m providing. Overwriting some methods I have
> been able to remove all prefixes in the XML but no the "xmlns"
> attributes in head elements. I´m getting something like this:
> 
> <?xml version='1.0' encoding='UTF-8'?>
> <house xmlns:ns2="http://namespace2" xmlns="http://namespace1">
>     <window>....</window>
>     .....
> </house>
> 
> where "window" elements don´t have any prefixes but
> xmlns:ns2="http://namespace2" xmlns="http://namespace1" are printed as
> attributes in "house" element.
> 
> The "NoNamespacesWriterWrapper" class is now:
> 
> class NoNamespacesWriterWrapper implements XMLStreamWriter {
> 
>         private XMLStreamWriter wrappedStreamWriter;
> 
>         public NoNamespacesWriterWrapper(XMLStreamWriter
> wrappedStreamWriter) {
>             this.wrappedStreamWriter = wrappedStreamWriter;
>         }
> 
>         public void close() throws XMLStreamException {
>             wrappedStreamWriter.close();
>         }
> 
>         public void flush() throws XMLStreamException {
>             wrappedStreamWriter.flush();
>         }
> 
>         public NamespaceContext getNamespaceContext() {
>             return wrappedStreamWriter.getNamespaceContext();
>         }
> 
>         public String getPrefix(String uri) throws XMLStreamException {
>             return wrappedStreamWriter.getPrefix(uri);
>         }
> 
>         public Object getProperty(String name) throws
> IllegalArgumentException {
>             return wrappedStreamWriter.getProperty(name);
>         }
> 
>         public void setDefaultNamespace(String uri) throws
> XMLStreamException {
>             wrappedStreamWriter.setDefaultNamespace(uri);
>         }
> 
>         public void setNamespaceContext(NamespaceContext context)
>                 throws XMLStreamException {
>             wrappedStreamWriter.setNamespaceContext(context);
>         }
> 
>         public void setPrefix(String prefix, String uri)
>                 throws XMLStreamException {
>             wrappedStreamWriter.setPrefix(prefix, uri);
>         }
> 
>         public void writeAttribute(String localName, String value)
>                 throws XMLStreamException {
>             wrappedStreamWriter.writeAttribute(localName, value);
>         }
> 
>         public void writeAttribute(String namespaceURI, String localName,
>                 String value) throws XMLStreamException {
>             writeAttribute(localName, value);
>         }
> 
>         public void writeAttribute(String prefix, String namespaceURI,
>                 String localName, String value) throws XMLStreamException
> {
>             writeAttribute(localName, value);
>         }
> 
>         public void writeCData(String data) throws XMLStreamException {
>             wrappedStreamWriter.writeCData(data);
>         }
> 
>         public void writeCharacters(String text) throws XMLStreamException
> {
>             wrappedStreamWriter.writeCharacters(text);
>         }
> 
>         public void writeCharacters(char[] text, int start, int len)
>                 throws XMLStreamException {
>             wrappedStreamWriter.writeCharacters(text, start, len);
>         }
> 
>         public void writeComment(String data) throws XMLStreamException {
>             wrappedStreamWriter.writeComment(data);
>         }
> 
>         public void writeDTD(String dtd) throws XMLStreamException {
>             wrappedStreamWriter.writeDTD(dtd);
>         }
> 
>         public void writeDefaultNamespace(String namespaceURI)
>                 throws XMLStreamException {
>             wrappedStreamWriter.writeDefaultNamespace(namespaceURI);
>         }
> 
>         public void writeEmptyElement(String localName)
>                 throws XMLStreamException {
>             wrappedStreamWriter.writeEmptyElement(localName);
>         }
> 
>         public void writeEmptyElement(String namespaceURI, String
> localName)
>                 throws XMLStreamException {
>             writeEmptyElement(localName);
>         }
> 
>         public void writeEmptyElement(String prefix, String localName,
>                 String namespaceURI) throws XMLStreamException {
>             writeEmptyElement(localName);
>         }
> 
>         public void writeEndDocument() throws XMLStreamException {
>             wrappedStreamWriter.writeEndDocument();
>         }
> 
>         public void writeEndElement() throws XMLStreamException {
>             wrappedStreamWriter.writeEndElement();
>         }
> 
>         public void writeEntityRef(String name) throws XMLStreamException
> {
>             wrappedStreamWriter.writeEntityRef(name);
>         }
> 
>         public void writeNamespace(String prefix, String namespaceURI)
>                 throws XMLStreamException {
>             wrappedStreamWriter.writeNamespace(prefix, namespaceURI);
> 
>         }
> 
>         public void writeProcessingInstruction(String target)
>                 throws XMLStreamException {
>             wrappedStreamWriter.writeProcessingInstruction(target);
>         }
> 
>         public void writeProcessingInstruction(String target, String data)
>                 throws XMLStreamException {
>             wrappedStreamWriter.writeProcessingInstruction(target, data);
>         }
> 
>         public void writeStartDocument() throws XMLStreamException {
>             wrappedStreamWriter.writeStartDocument();
>         }
> 
>         public void writeStartDocument(String version)
>                 throws XMLStreamException {
>             wrappedStreamWriter.writeStartDocument();
>         }
> 
>         public void writeStartDocument(String encoding, String version)
>                 throws XMLStreamException {
>             wrappedStreamWriter.writeStartDocument(encoding, version);
>         }
> 
>         public void writeStartElement(String localName)
>                 throws XMLStreamException {
>             wrappedStreamWriter.writeStartElement(localName);
>         }
> 
>         public void writeStartElement(String namespaceURI, String
> localName)
>                 throws XMLStreamException {
>             writeStartElement(localName);
>         }
> 
>         public void writeStartElement(String prefix, String localName,
>                 String namespaceURI) throws XMLStreamException {
>             writeStartElement(localName);
>         }
> 
>     }
> 
> Thanks and regards,
> 
> Juanjo.
> 
> 

-- 
View this message in context: http://www.nabble.com/Removing-XML-namespaces-and-prefixes-tp24457677p24459981.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: Removing XML namespaces and prefixes

Posted by Juan José Vázquez Delgado <ju...@gmail.com>.
Hi,

First of all thank you for your help.

>
> Custom providers can also extend this method and wrap
> the writer with yet another writer as you do, but in your specific case you
> need to register a writer using a response filter, here's an example :
>
> http://svn.apache.org/repos/asf/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/XmlStreamWriterProvider.java
>
> you can register this filter the same way as the custom JAXB provider.

I have implemented a NoNamespacesResponseHandler getting a cleaner
solution to my problem. Thanks.

In the other hand, my problem seems to rely on the
CustomXmlStreamWriter I´m providing. Overwriting some methods I have
been able to remove all prefixes in the XML but no the "xmlns"
attributes in head elements. I´m getting something like this:

<?xml version='1.0' encoding='UTF-8'?>
<house xmlns:ns2="http://namespace2" xmlns="http://namespace1">
    <window>....</window>
    .....
</house>

where "window" elements don´t have any prefixes but
xmlns:ns2="http://namespace2" xmlns="http://namespace1" are printed as
attributes in "house" element.

The "NoNamespacesWriterWrapper" class is now:

class NoNamespacesWriterWrapper implements XMLStreamWriter {

        private XMLStreamWriter wrappedStreamWriter;

        public NoNamespacesWriterWrapper(XMLStreamWriter wrappedStreamWriter) {
            this.wrappedStreamWriter = wrappedStreamWriter;
        }

        public void close() throws XMLStreamException {
            wrappedStreamWriter.close();
        }

        public void flush() throws XMLStreamException {
            wrappedStreamWriter.flush();
        }

        public NamespaceContext getNamespaceContext() {
            return wrappedStreamWriter.getNamespaceContext();
        }

        public String getPrefix(String uri) throws XMLStreamException {
            return wrappedStreamWriter.getPrefix(uri);
        }

        public Object getProperty(String name) throws IllegalArgumentException {
            return wrappedStreamWriter.getProperty(name);
        }

        public void setDefaultNamespace(String uri) throws XMLStreamException {
            wrappedStreamWriter.setDefaultNamespace(uri);
        }

        public void setNamespaceContext(NamespaceContext context)
                throws XMLStreamException {
            wrappedStreamWriter.setNamespaceContext(context);
        }

        public void setPrefix(String prefix, String uri)
                throws XMLStreamException {
            wrappedStreamWriter.setPrefix(prefix, uri);
        }

        public void writeAttribute(String localName, String value)
                throws XMLStreamException {
            wrappedStreamWriter.writeAttribute(localName, value);
        }

        public void writeAttribute(String namespaceURI, String localName,
                String value) throws XMLStreamException {
            writeAttribute(localName, value);
        }

        public void writeAttribute(String prefix, String namespaceURI,
                String localName, String value) throws XMLStreamException {
            writeAttribute(localName, value);
        }

        public void writeCData(String data) throws XMLStreamException {
            wrappedStreamWriter.writeCData(data);
        }

        public void writeCharacters(String text) throws XMLStreamException {
            wrappedStreamWriter.writeCharacters(text);
        }

        public void writeCharacters(char[] text, int start, int len)
                throws XMLStreamException {
            wrappedStreamWriter.writeCharacters(text, start, len);
        }

        public void writeComment(String data) throws XMLStreamException {
            wrappedStreamWriter.writeComment(data);
        }

        public void writeDTD(String dtd) throws XMLStreamException {
            wrappedStreamWriter.writeDTD(dtd);
        }

        public void writeDefaultNamespace(String namespaceURI)
                throws XMLStreamException {
            wrappedStreamWriter.writeDefaultNamespace(namespaceURI);
        }

        public void writeEmptyElement(String localName)
                throws XMLStreamException {
            wrappedStreamWriter.writeEmptyElement(localName);
        }

        public void writeEmptyElement(String namespaceURI, String localName)
                throws XMLStreamException {
            writeEmptyElement(localName);
        }

        public void writeEmptyElement(String prefix, String localName,
                String namespaceURI) throws XMLStreamException {
            writeEmptyElement(localName);
        }

        public void writeEndDocument() throws XMLStreamException {
            wrappedStreamWriter.writeEndDocument();
        }

        public void writeEndElement() throws XMLStreamException {
            wrappedStreamWriter.writeEndElement();
        }

        public void writeEntityRef(String name) throws XMLStreamException {
            wrappedStreamWriter.writeEntityRef(name);
        }

        public void writeNamespace(String prefix, String namespaceURI)
                throws XMLStreamException {
            wrappedStreamWriter.writeNamespace(prefix, namespaceURI);

        }

        public void writeProcessingInstruction(String target)
                throws XMLStreamException {
            wrappedStreamWriter.writeProcessingInstruction(target);
        }

        public void writeProcessingInstruction(String target, String data)
                throws XMLStreamException {
            wrappedStreamWriter.writeProcessingInstruction(target, data);
        }

        public void writeStartDocument() throws XMLStreamException {
            wrappedStreamWriter.writeStartDocument();
        }

        public void writeStartDocument(String version)
                throws XMLStreamException {
            wrappedStreamWriter.writeStartDocument();
        }

        public void writeStartDocument(String encoding, String version)
                throws XMLStreamException {
            wrappedStreamWriter.writeStartDocument(encoding, version);
        }

        public void writeStartElement(String localName)
                throws XMLStreamException {
            wrappedStreamWriter.writeStartElement(localName);
        }

        public void writeStartElement(String namespaceURI, String localName)
                throws XMLStreamException {
            writeStartElement(localName);
        }

        public void writeStartElement(String prefix, String localName,
                String namespaceURI) throws XMLStreamException {
            writeStartElement(localName);
        }

    }

Thanks and regards,

Juanjo.

Re: Removing XML namespaces and prefixes

Posted by Sergey Beryozkin <se...@iona.com>.
I fixed JAXRSOutInterceptor to handle CachedXmlStreamWriters in all the cases
(at the moment it's handled only if JAXBElementProvider has been explicitly
configured to buffer its output)...

cheers, Sergey



Sergey Beryozkin wrote:
> 
> Hi
> 
> this method
> 
> protected void marshalToWriter(Marshaller ms, Object obj,
>             XMLStreamWriter writer, MediaType mt) throws Exception {
> }
> 
> assumes that the writer has already been created so this method is not
> called at the moment. Custom providers can also extend this method and
> wrap the writer with yet another writer as you do, but in your specific
> case you need to register a writer using a response filter, here's an
> example :
> 
> http://svn.apache.org/repos/asf/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/XmlStreamWriterProvider.java
> 
> you can register this filter the same way as the custom JAXB provider.
> 
> Given that you've actually extended the JAXBProvider, it seems like an
> extra step to go and also create a filter. So I've updated the JAXB
> provider (locally only) with 
> 
> protected XMLStreamWriter getStreamWriter(Object obj, OutputStream os,
> MediaType mt) {}
> 
> (and a similar method for XMLStreamReader)
> 
> so, starting with 2.2.3 you'll be able to avoid registering a filter just
> for the sake of providing a writer to the JAXB provider :
> 
> public class MyCustomJAXBElementProvider extends JAXBElementProvider {
> 
>     @Override
>     protected XMLStreamWriter getStreamWriter(Object obj, OutputStream os,
> MediaType mt) {
>         XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(os);        
>         //return new CachingXmlEventWriterWrapper(writer);
>     }
> }
> 
> Note explicitly registered CachingXmlEventWriters are not supported yet
> (assuming they just cache the XMLEvents), unless you enable a buffering
> mode. I'll fix it too, but in meantime  you might want to use a delegating
> writer instead similar to
> 
> http://svn.apache.org/repos/asf/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/CustomXmlStreamWriter.java
> 
> hope it helps, Sergey
>  
> 
> 
> Juan José Vázquez Delgado wrote:
>> 
>> Hi,
>> 
>> I have a JAX-RS web service which uses JAXB for binding. Everything is
>> ok, but currently I have a Flash client which is not ready to
>> understand neither namespaces nor prefixes at all. I have to clean the
>> output XML of namespaces and prefixes, included xmlns attributes, but
>> I would´nt like having to modify my xml schemas so I´m looking for a
>> solution on binding time.
>> 
>> My approach has been to extend JAXBElementProvider in order to wrap
>> the default XMLStreamWriter, CachingXmlEventWriter in this case. So I
>> have something like this:
>> 
>> public class MyCustomJAXBElementProvider extends JAXBElementProvider {
>> 
>>     @Override
>>     protected void marshalToWriter(Marshaller ms, Object obj,
>>             XMLStreamWriter writer, MediaType mt) throws Exception {
>>         CachingXmlEventWriterWrapper wrappedWriter = new
>> CachingXmlEventWriterWrapper(writer);
>>         super.marshalToWriter(ms, obj, wrappedWriter, mt);
>>     }
>> 
>> }
>> 
>> class CachingXmlEventWriterWrapper implements XMLStreamWriter {
>> 
>>      private XMLStreamWriter wrappedStreamWriter;
>> 
>>      public CachingXmlEventWriterWrapper(XMLStreamWriter
>> wrappedStreamWriter) {
>>             this.wrappedStreamWriter = wrappedStreamWriter;
>> 
>>      }
>> 
>>     ............
>> 
>> }
>> 
>> The wrapper implementation tries to generate a "clean" XML
>> overwritting "setPrefix", "writeEmptyElement" and others methods  but
>> I didn´t get to remove the "xmlns" attributes. Any ideas?.
>> 
>> Thanks in advance.
>> 
>> Regards,
>> 
>> Juanjo.
>> 
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Removing-XML-namespaces-and-prefixes-tp24457677p24458944.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: Removing XML namespaces and prefixes

Posted by Sergey Beryozkin <se...@iona.com>.
Hi

this method

protected void marshalToWriter(Marshaller ms, Object obj,
            XMLStreamWriter writer, MediaType mt) throws Exception {
}

assumes that the writer has already been created so this method is not
called at the moment. Custom providers can also extend this method and wrap
the writer with yet another writer as you do, but in your specific case you
need to register a writer using a response filter, here's an example :

http://svn.apache.org/repos/asf/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/XmlStreamWriterProvider.java

you can register this filter the same way as the custom JAXB provider.

Given that you've actually extended the JAXBProvider, it seems like an extra
step to go and also create a filter. So I've updated the JAXB provider
(locally only) with 

protected XMLStreamWriter getStreamWriter(Object obj, OutputStream os,
MediaType mt) {}

(and a similar method for XMLStreamReader)

so, starting with 2.2.3 you'll be able to avoid registering a filter just
for the sake of providing a writer to the JAXB provider :

public class MyCustomJAXBElementProvider extends JAXBElementProvider {

    @Override
    protected XMLStreamWriter getStreamWriter(Object obj, OutputStream os,
MediaType mt) {
        XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(os);        
        //return new CachingXmlEventWriterWrapper(writer);
    }
}

Note explicitly registered CachingXmlEventWriters are not supported yet
(assuming they just cache the XMLEvents), unless you enable a buffering
mode. I'll fix it too, but in meantime  you might want to use a delegating
writer instead similar to

http://svn.apache.org/repos/asf/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/CustomXmlStreamWriter.java

hope it helps, Sergey
 


Juan José Vázquez Delgado wrote:
> 
> Hi,
> 
> I have a JAX-RS web service which uses JAXB for binding. Everything is
> ok, but currently I have a Flash client which is not ready to
> understand neither namespaces nor prefixes at all. I have to clean the
> output XML of namespaces and prefixes, included xmlns attributes, but
> I would´nt like having to modify my xml schemas so I´m looking for a
> solution on binding time.
> 
> My approach has been to extend JAXBElementProvider in order to wrap
> the default XMLStreamWriter, CachingXmlEventWriter in this case. So I
> have something like this:
> 
> public class MyCustomJAXBElementProvider extends JAXBElementProvider {
> 
>     @Override
>     protected void marshalToWriter(Marshaller ms, Object obj,
>             XMLStreamWriter writer, MediaType mt) throws Exception {
>         CachingXmlEventWriterWrapper wrappedWriter = new
> CachingXmlEventWriterWrapper(writer);
>         super.marshalToWriter(ms, obj, wrappedWriter, mt);
>     }
> 
> }
> 
> class CachingXmlEventWriterWrapper implements XMLStreamWriter {
> 
>      private XMLStreamWriter wrappedStreamWriter;
> 
>      public CachingXmlEventWriterWrapper(XMLStreamWriter
> wrappedStreamWriter) {
>             this.wrappedStreamWriter = wrappedStreamWriter;
> 
>      }
> 
>     ............
> 
> }
> 
> The wrapper implementation tries to generate a "clean" XML
> overwritting "setPrefix", "writeEmptyElement" and others methods  but
> I didn´t get to remove the "xmlns" attributes. Any ideas?.
> 
> Thanks in advance.
> 
> Regards,
> 
> Juanjo.
> 
> 

-- 
View this message in context: http://www.nabble.com/Removing-XML-namespaces-and-prefixes-tp24457677p24458686.html
Sent from the cxf-user mailing list archive at Nabble.com.