3

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

所以我试着简单地在字符串上做一个 replaceAll 来改变 \t 为 ,但 Marshaller 上的 escapeHandler 将其更改为 	 .

然后我尝试创建一个 customCharacterEscapeHandler 并在 marshall 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>

我的 customCharacterEscapeHandler 是:

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 正确替换了制表符,但其他一些 escapeHandler 在此之后运行并再次替换 '&' 字符,所以我有 &#9; 再次 ...

我应该怎么做才能在客户端获得一个编码为 的标签???

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

4

1 回答 1

2

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

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

下面是自定义 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);
    }
}
于 2014-11-10T20:32:13.437 回答