users@jersey.java.net

Re: [Jersey] entities encoding issues in the response

From: Arul Dhesiaseelan <arul_at_fluxcorp.com>
Date: Thu, 15 May 2008 08:56:21 -0600

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
>>
>