Illegal Character - Ctrl-Char

Illegal character - CTRL-CHAR

Thanks guys for you inputs. I am sharing solution might be helpful for others.
The requirement was not to wipe out CONTROL CHAR, it should remain as it is in DB also and one WS sends it across n/w client should able to get the CONTROL CHAR. So I implemented the code as follow:

  1. Encode strings using URLEncoder in Web-Service code.
  2. At client Side decode it using URLDecoder

Sharing sample code and output bellow.

Sample code:

System.out.println("NewSfn");  
System.out.println(URLEncoder.encode("NewSfn", "UTF-8"));
System.out.println(URLDecoder.decode("NewSfn", "UTF-8"));

Output:

NewSfn  
New%0FSfn
NewSfn

So client will recieve CONTROL CHARs.

EDIT: Stack Exchange is not showing CONTROL CHAR above. NewSfn is like this New(CONTROL CHAR)Sfn.

CXF - com.ctc.wstx.exc.WstxUnexpectedCharException: Illegal character ((CTRL-CHAR, code 5))

The solution is pretty simple :

    <jaxws:endpoint id="kservice"  
implementor="#kostrounService"
address="/call_kostroun" >
<jaxws:properties>
<entry key="javax.xml.stream.XMLOutputFactory" valueref="xmlOutputFactory" />
</jaxws:properties>
</jaxws:endpoint>
<bean id="invalidCharHandler" class="com.ctc.wstx.api.InvalidCharHandler$ReplacingHandler">
<constructor-arg value=" "/>
</bean>

<bean id="xmlOutputFactory" class="com.ctc.wstx.stax.WstxOutputFactory"/>

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject">
<ref local="xmlOutputFactory" />
</property>
<property name="targetMethod">
<value>setProperty</value>
</property>
<property name="arguments">
<list>
<util:constant static-field="com.ctc.wstx.api.WstxOutputProperties.P_OUTPUT_INVALID_CHAR_HANDLER"/>
<ref bean="invalidCharHandler" />
</list>
</property>
</bean>

This snippet of configuration remove illegal characters from soap message, and app then run ;-)



Related Topics



Leave a reply



Submit