users@jersey.java.net

Re: [Jersey] entities encoding issues in the response

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 15 May 2008 18:12:30 +0200

Hi Arul,

The write to method is not going to serialize out any XML. It is just
going to serialize out the String "value" as UTF-8 encoded characters.

In the writeTo method you need to use JAXB marshalling on the CellType
object. And you need to use the Apache XML serializer [1] or wrapping
the XMLStreamWriter as recommended by Tatu. I think the latter is easier.

Tatu, is there a helper class in Woodstox for XMLStreamWriter that is
the equivalent of javax.xml.stream.util.StreamReaderDelegate ?

Paul.

[1] https://jaxb.dev.java.net/faq/JaxbCDATASample.java


Arul Dhesiaseelan wrote:
> Paul,
>
> Would this work for me as it is? Or, do I need to do any specific
> changes for CDATA handling? Please clarify.
>
> @ProduceMime("application/xml")
> @Provider
> public class CDATAProvider implements MessageBodyWriter<CellType> {
> public void writeTo(CellType cellType,
> Class<?> type, Type genericType, Annotation annotations[],
> MediaType mediaType, MultivaluedMap<String, Object> headers,
> OutputStream out) throws IOException {
> out.write(cellType.getValue().getBytes()); }
>
> public boolean isWriteable(Class<?> type, Type genericType,
> Annotation annotations[]) { return
> CellType.class.isAssignableFrom(type);
> }
>
> public long getSize(CellType cellType) {
> return -1;
> }
> }
>
> Here is the CellType JAXB type.
>
> @XmlAccessorType(XmlAccessType.FIELD)
> @XmlType(name = "cellType", propOrder = {"value"})
> public class CellType {
> @XmlValue
> protected String value;
> @XmlAttribute
> protected String image;
> public String getValue() {
> return value;
> }
> public void setValue(String value) {
> this.value = value;
> }
> public String getImage() {
> return image;
> }
> public void setImage(String value) {
> this.image = value;
> }
> }
>
> Thanks!
> Arul
>
> Paul Sandoz wrote:
>> Tatu Saloranta wrote:
>>> The question, then, is if and how to configure Jersey
>>> to use that stream writer impl.
>>
>> Right, the current way to do this is provide your own JAXBContext
>> implementation (see ContextResolver<JAXBContext> for the JAXB Java
>> types that require CDATA encoding). The JAXBContext would return a
>> marshaller that would use the StAX wrapping as you suggested. But IMHO
>> that is a still lot of work. Almost requires a supplied out of the box
>> solution, but i am not sure the use-case warrants the degree of work
>> required.
>>
>> The other, simpler way, is to roll your own message body writer for
>> certain JAXB types that require such CDATA functionality.
>>
>> Paul.
>>
>>> It would even be
>>> possible to create one's own XMLOutputFactory to
>>> produce such wrapped writers (and use system property
>>> to make JDK use that).
>>> But hopefully that would not be necessary.
>>>
>>> -+ Tatu +-
>>>
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>

-- 
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109