在封送程序上设置CharacterEscapeHandler变量在CXF中不会按预期工作 [英] Setting CharacterEscapeHandler variable on a Marshaller doesn't work as expected in CXF

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

问题描述

我正在使用ApacheCXF在服务器端实现一些Web服务。我必须实现一个WebService,它返回一个字符串(Holder),其中一些值用制表符分隔。ApacheCXF将字符制表符编码为制表符,但我们的客户端(不能更改...)不接受,并且只读取编码为&;#9;的选项卡。

因此,我尝试简单地将字符串上的replaceAll更改为&;#9;,但Marshaller上的让渡处理程序将其更改为&;amp;#9;。

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

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

我的stomCharacterEscapeHandler是:

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

这应该会起作用,但它不起作用,因为此ajeleHandler正确地替换制表符,但在此之后运行的某个其他loyeHandler将再次替换‘&;’字符,因此我再次...

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

的标签

PS:我使用的是Apache CXF 2.5.X

推荐答案

我也有这个问题,根据我的观察,使用CXF SaxOutInterceptor的XMLStreamWriter似乎不支持JAXB CharacterEscapeHandler。

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

以下是自定义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拦截器

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆