adding support to for CDATA when returning xml

123 views
Skip to first unread message

David Aranda

unread,
Mar 16, 2018, 11:57:40 AM3/16/18
to dropwizard-user
Hello everyone , in the project i am working on, i need to add cdata to end result xml, since DropWizard uses jersey and it uses JAXB i was reading some articles about how to read , i successfully added the CDATA part by using a custom XmlAdapter but then end result was still being escaped , i was looking for a way to do that and i found this code but i don't know how to configure that on Dropwizard 

writer = new BufferedWriter(new FileWriter(selectedFile));
			context = JAXBContext.newInstance(Book.class);
			Marshaller m = context.createMarshaller();
			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
			m.setProperty("com.sun.xml.internal.bind.characterEscapeHandler",
				new CharacterEscapeHandler() {
					@Override
					public void escape(char[] ch, int start, int length,
							boolean isAttVal, Writer writer)
							throws IOException {
						writer.write(ch, start, length);
					}
				});

also i've tried add a custom JAXBContext like is explained here:
https://automationrhapsody.com/rest-performance-problems-with-dropwizard-and-jersey-jaxb-provider/
and in the run method of my application i have this code 

final Client client = new JerseyClientBuilder(environment) 
                 .using(configuration.getJerseyClientConfiguration()) 
                 .withProvider(CustomJAXBContextConfiguration.class) 
                 .using(environment) .build(getName()); 
client.property(CharacterEscapeHandler.class.getName(),new CDataCharacterEscapeHandler()) ; 
environment.jersey().property(CharacterEscapeHandler.class.getName(),new CDataCharacterEscapeHandler()) ; 
environment.jersey().register(client); environment.jersey().register(CustomJAXBContextConfiguration.class);

but it doesn't work either,


is there a way to add a property to the marshaller jersey in Dropwizard is using ? or how i can achieve that 
thank you to all of you 



Tatu Saloranta

unread,
Mar 16, 2018, 2:17:36 PM3/16/18
to dropwiz...@googlegroups.com
It would help if you explained in bit more detail what you try to achieve here.
Term "cdata", for example, has at least 2 meanings with XML (generally, "character data" for text contained; as well as CDATA sections
for manual escaping -- rarely produced by generators, and having no semantic difference from 'regular' textual content).

And if you are trying to force use of CDATA sections, follow-up question would be ... why? Given that it is not different, semantically,
from other text segments. It's just a convenience feature if manually modifying xml content, and processing systems are not allowed to
treat it any differently.

-+ Tatu +-

--
You received this message because you are subscribed to the Google Groups "dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dropwizard-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Aranda

unread,
Mar 16, 2018, 6:17:26 PM3/16/18
to dropwizard-user
Hello , with CDATA i mean this adding this <![CDATA[ textvalue ]]> to a xml value like this 
<message><![CDATA[ some embedded & xml</data>]]></message>

currently i was able to the <![CDATA[ ]]>  but the problem is the still jersey jaxb in dropwizard is still escaping it so it ends up like this:
<message>\&lt;![CDATA[Welcom some embedded &amp; xml</data]]&gt;></message>

and to answer you why i need that: its because when an xml has this section it can use characters '&' without the need to be converted to &amp; for example
so its not the same, also this end xml is going to be consumed by other system which we had no way to modify.

thanks for your answer :D
To unsubscribe from this group and stop receiving emails from it, send an email to dropwizard-us...@googlegroups.com.

Tatu Saloranta

unread,
Mar 16, 2018, 6:53:43 PM3/16/18
to dropwiz...@googlegroups.com
On Fri, Mar 16, 2018 at 3:17 PM, David Aranda <david....@globant.com> wrote:
Hello , with CDATA i mean this adding this <![CDATA[ textvalue ]]> to a xml value like this 
<message><![CDATA[ some embedded & xml</data>]]></message>

currently i was able to the <![CDATA[ ]]>  but the problem is the still jersey jaxb in dropwizard is still escaping it so it ends up like this:
<message>\&lt;![CDATA[Welcom some embedded &amp; xml</data]]&gt;></message>

and to answer you why i need that: its because when an xml has this section it can use characters '&' without the need to be converted to &amp; for example
so its not the same, also this end xml is going to be consumed by other system which we had no way to modify.

Still, semantically content are no different; and escaping of `&` is automatically handled by XML writing library.
JAXB definitely adds such escaping as necessary. And on the other end, parser will unescape it (or handle CDATA section).
In the end different representations are exactly same and tools should not treat them any differently; ones that do can be considered broken.

Having said that, I don't know if there is a way to force JAXB to use CDATA sections; maybe someone else does.
I would just try to avoid the problem altogether and keep things simple.

Good luck!

-+ Tatu +-

 

The information contained in this e-mail may be confidential. It has been sent for the sole use of the intended recipient(s). If the reader of this message is not an intended recipient, you are hereby notified that any unauthorized review, use, disclosure, dissemination, distribution or copying of this communication, or any of its contents, is strictly prohibited. If you have received it by mistake please let us know by e-mail immediately and delete it from your system. Many thanks.

 

La información contenida en este mensaje puede ser confidencial. Ha sido enviada para el uso exclusivo del destinatario(s) previsto. Si el lector de este mensaje no fuera el destinatario previsto, por el presente queda Ud. notificado que cualquier lectura, uso, publicación, diseminación, distribución o copiado de esta comunicación o su contenido está estrictamente prohibido. En caso de que Ud. hubiera recibido este mensaje por error le agradeceremos notificarnos por e-mail inmediatamente y eliminarlo de su sistema. Muchas gracias.


--
You received this message because you are subscribed to the Google Groups "dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dropwizard-user+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages