cxf-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sergey Beryozkin <sberyoz...@gmail.com>
Subject Re: Encoding special characters when marshalling
Date Mon, 10 May 2010 16:44:54 GMT
Just in case, here is some more info, not sure if it can help.
You can do @Produces("application/xml;charset=...") and JAXBElementProvider
will notice the custom charset, it can be set dynamically if needed by
returning Response.
You can also register a custom XMLStreamWriter, by either overriding
JAXBElementProvider#getStreamWriter or registering it from a custom
ResponseHandler filter, example :

JAXBElementProvider provider = new JAXBElementProvider(); {
            @Override
            protected XMLStreamWriter getStreamWriter(Object obj,
OutputStream os, MediaType mt) {
                return new
CDataWriter(StaxUtils.createXMLStreamWriter(os));
            }
        };

public static class CDataWriter extends DelegatingXMLStreamWriter {
        List<String> elementNames;
        public CDataWriter(XMLStreamWriter writer) {
            super(writer);
        }

}

this writer can be configured somehow, say with the names of elements (may
be containing some patterns) whose content has to be processed (CData-ed,
etc), so whenever a matching element starts and ends, CData can be
started/ended, etc...

hope it helps, Sergey

On Mon, May 10, 2010 at 5:30 PM, Sergey Beryozkin <sberyozkin@gmail.com>wrote:

> Hi
>
> On Mon, May 10, 2010 at 2:54 AM, Mustafa Sezgin <msezgin@aconex.com>wrote:
>
>>
>> Hi all,
>>
>>
>> Im trying to find the best solution to a character encoding issue we are
>> experiencing. Some of the strings we marshal can contain some chars which
>> end up making the XML document generated invalid, and as a result, we get
>> the following XML returned to the client
>>
>>
>>
>>
>> <ns1:XMLFault xmlns:ns1="http://cxf.apache.org/bindings/xformat"><ns1:faultstring
>> xmlns:ns1="http://cxf.apache.org/bindings/xformat">java.lang.IllegalArgumentException:
>> Wrong media type parameter, seperator is
>> missing</ns1:faultstring></ns1:XMLFault>
>>
>>
> Looks like the exception is thrown here at the moment of generating the
> error response, specifically, a media type contains a parameter with no
> value, I've removed this restriction.
>
>
>
>>
>> What is the best way to go about this? We want to apply a solution to all
>> of our services which will encode special chars like '&', '<' and '>' so
>> that they do not affect the generated XML response.
>> Is this something we have to do manually using an outbound interceptor or
>> is there a flag somewhere relating to the JAXB marshaller that we can enable
>> which will do the encoding for us automatically?
>>
>> How exactly are such strings marshalled ? I've tried to reproduce but I
> can see illegal characters such as '&' and '<' being escaped. Can you show
> some sample code please ?
>
> thanks, Sergey
>
>
>>
>> Thanks in advance!
>
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message