Setting CharacterEscapeHandler variable on a Marshaller doesn't work as expected in CXF

强颜欢笑 提交于 2021-02-19 04:16:00

问题


I'm using Apache CXF to implement some WebServices at server side. I have to implement a WebService that returns a string (Holder) with some values separated by tab character. Apache CXF encodes character tab as a tab, but our client (that can not be change...) doesn't accept it and only read tabs encoded as 	 .

So I tried to simply make a replaceAll on the string to change \t for 	 , but an escapeHandler on Marshaller changes it to 	 .

Then I tried to create a customCharacterEscapeHandler and set in the marshall com.sun.xml.bind.marshaller.CharacterEscapeHandler property.

<jaxws:endpoint 
  id="wsContainer" 
  implementor="com.xxxxx.xxxxx.xxxxx.webServices.impl.EOSWebServiceImpl"
  address="/ws" >

  <jaxws:dataBinding>   
      <bean class="org.apache.cxf.jaxb.JAXBDataBinding">
        <property name="marshallerProperties">
            <map>
                <entry key="jaxb.encoding" value="UTF-8"/> 
                <entry key="com.sun.xml.bind.marshaller.CharacterEscapeHandler" value-ref="customCharacterEscapeHandler"/>
            </map>        
        </property>
      </bean>
  </jaxws:dataBinding>

</jaxws:endpoint>

And my customCharacterEscapeHandler is:

public class CustomCharacterEscapeHandler implements CharacterEscapeHandler {

    private final CharsetEncoder encoder;

    public CustomCharacterEscapeHandler(String charsetName) {
        this.encoder = Charset.forName(charsetName).newEncoder(); 
    }

    public void escape(char[] ch, int start, int length, boolean isAttVal, Writer out) throws IOException {
        int limit = start+length;
        for (int i = start; i < limit; i++) {
            switch (ch[i]) {
            case SoapUtils.SOAP_FIELD_SEP: //Cambios sobre el NioEscapeHandler para escapar tabuladores y saltos de linea
                out.write("&#09;");
                break;
            case SoapUtils.SOAP_RSEP:
                out.write("&#13;");
                break;
            case '&':
                out.write("&amp;");
                break;
            case '<':
                out.write("&lt;");
                break;
            case '>':
                out.write("&gt;");
                break;
            case '\"':
                if (isAttVal) {
                    out.write("&quot;");
                } else {
                    out.write('\"');
                }
                break;
            default:
                if( encoder.canEncode(ch[i]) ) {
                    out.write(ch[i]);
                } else {
                    out.write("&#");
                    out.write(Integer.toString(ch[i]));
                    out.write(';');
                }
            }
        }
    }
}

This should work but it doesn't, because this escapeHandler replace correctly the tab character, but some other escapeHandler is running after this and replaces again '&' character, so I have &amp;#9; again ...

What should I do to get in the client a tab encoded as &#9; ??

PS: I'm using Apache CXF 2.5.X


回答1:


I also had the issue, based on my observations it seems that that JAXB CharacterEscapeHandler is not supported by the XMLStreamWriter used the CXF StaxOutInterceptor.

I workaround the issue by injecting a custom org.codehaus.stax2.io.EscapingWriterFactory to the StaxOutInterceptor.

Here's the implementation of the custom EscapingWriterFactory:

public class CustomXmlEscapingWriterFactory implements
org.codehaus.stax2.io.EscapingWriterFactory{
    public Writer createEscapingWriterFor(final Writer out, String enc)
            throws UnsupportedEncodingException {
        return new Writer(){

            @Override
            public void write(char[] cbuf, int off, int len) throws IOException {

              String escapedStr =   StringEscapeUtils.
                      escapeXml(new String(cbuf)).trim();

              out.write(escapedStr.toCharArray(), off, escapedStr.toCharArray().length);

            }

            @Override
            public void flush() throws IOException {
                out.flush();

            }

            @Override
            public void close() throws IOException {
                out.close();

            }

        };
    }

    public Writer createEscapingWriterFor(OutputStream out, String enc)
            throws UnsupportedEncodingException {
        throw new IllegalArgumentException("not supported");
    }
}

Here's the CXF interceptor that inject the custom EscapingWriterFactory

public class CustomXmlEscapingWriterInterceptor extends AbstractPhaseInterceptor<Message> {

    public CustomXmlEscapingWriterInterceptor() {
        super(Phase.PRE_STREAM);
        addBefore(StaxOutInterceptor.class.getName());
    }

    @Override
    public void handleMessage(Message message) {

        WstxOutputFactory factory = new WstxOutputFactory();

        factory.setProperty(XMLOutputFactory2.P_TEXT_ESCAPER, new CustomXmlEscapingWriterFactory());

        message.put(XMLOutputFactory.class.getName(), factory);
    }
}


来源:https://stackoverflow.com/questions/16099783/setting-characterescapehandler-variable-on-a-marshaller-doesnt-work-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!