racterEscapeHandler变量无法按预期在CXF中工

racterEscapeHandler变量无法按预期在CXF中工

本文介绍了在Marshaller上设置CharacterEscapeHandler变量无法按预期在CXF中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Apache CXF在服务器端实现一些WebServices.我必须实现一个WebService,该服务返回一个带有一些由制表符分隔的值的字符串(保持器). Apache CXF将字符选项卡编码为选项卡,但是我们的客户端(无法更改...)不接受它,而仅读取编码为	的选项卡. .

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 	 .

因此,我试图简单地对字符串进行replaceAll,以将\ t更改为	. ,但Marshaller上的escapeHandler将其更改为	.

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

然后,我尝试创建一个customCharacterEscapeHandler并在marshall的com.sun.xml.bind.marshaller.CharacterEscapeHandler属性中进行设置.

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>

我的customCharacterEscapeHandler是:

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(';');
                }
            }
        }
    }
}

这应该可以,但是不能,因为此escapeHandler可以正确替换制表符,但是此后正在运行其他逃逸处理程序,并再次替换'&'.角色,所以我有&#9;再次...

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

我应该怎么做才能在客户端中获得一个编码为&#9;的标签. ??

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

PS:我正在使用Apache CXF 2.5.X

PS: I'm using Apache CXF 2.5.X

推荐答案

我也遇到了问题,根据我的观察,似乎XMLStreamWriter不支持JAXB CharacterEscapeHandler,而CXF使用了StaxOutInterceptor.

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.

我通过将自定义org.codehaus.stax2.io.EscapingWriterFactory注入StaxOutInterceptor来解决此问题.

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

这是自定义EscapingWriterFactory的实现:

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");
    }
}

这里是注入自定义EscapingWriterFactory的CXF拦截器

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);
    }
}

这篇关于在Marshaller上设置CharacterEscapeHandler变量无法按预期在CXF中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 23:54