You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Mustafa Sezgin <ms...@aconex.com> on 2010/05/10 03:54:46 UTC

Encoding special characters when marshalling

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> 


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? 


Thanks in advance! 

Re: Encoding special characters when marshalling

Posted by Sergey Beryozkin <sb...@gmail.com>.
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 <sb...@gmail.com>wrote:

> Hi
>
> On Mon, May 10, 2010 at 2:54 AM, Mustafa Sezgin <ms...@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!
>
>
>

Re: Encoding special characters when marshalling

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

On Mon, May 10, 2010 at 2:54 AM, Mustafa Sezgin <ms...@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!