如何使用拦截器更改soap fault中的故障代码?

时间:2015-09-21 14:46:43

标签: java spring soap spring-ws

目前肥皂故障中的故障代码有问题,如下所示:

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
   <env:Header/>
   <env:Body>
      <env:Fault xmlns:ns0="http://xmlns.telenet.be/oss/ninas/v001">
         <faultcode>ns0:B-NONUNIQUE-WORKORDERID</faultcode>
         <faultstring>Work order ID is not unique</faultstring>
         <detail/>
      </env:Fault>
   </env:Body>
</env:Envelope>

但我需要这样(没有名称空间前缀):

<faultcode>B-NONUNIQUE-WORKORDERID</faultcode>

我已经尝试过:

  • 首先我用spring aop拦截soap fault并实现EndpointInterceptor来获取messageContext(org.springframework.ws.context)
  • 消息上下文有方法“setResponse” - 它完全包含了WebServiceMessage的API,它的子类非常差,我无法以这种方式改变响应的内部
  • 尝试了其他几种方法,但情况更糟。

任何人都可以提示如何获取和更改故障代码? 或者避免在故障代码中出现名称空间前缀。

1 个答案:

答案 0 :(得分:0)

经过数小时的调查后,我发现了相当难看的解决方案。首先,我的xml配置中有一个spring拦截器:

<sws:interceptors>
        <bean id="attachmentInterceptor"
              class="com.net.tel.interceptors.MessageInterceptor">
        </bean>
</sws:interceptors>

拦截器实施:

public class MessageInterceptor extends EndpointInterceptor {

    private static final transient Logger log = LoggerFactory.getLogger(NCOssNinasAttachmentInterceptor.class);

    private SoapMessageFactory soapMessageFactory;

    @Autowired
    private NCOssNinasAttachmentInterceptor(SoapMessageFactory soapMessageFactory) {
        this.soapMessageFactory = soapMessageFactory;
    }

    @Override
    public void afterCompletion(MessageContext messageContext, Object endpoint, Exception ex) throws Exception {
        SaajSoapMessage message = (SaajSoapMessage) messageContext.getResponse();
        final OutputStream s = new ByteArrayOutputStream();
        message.writeTo(s);
        log.info("SOAP RESPONSE: " + s.toString());
        String modifiedMsg = faultCodeModifier(s.toString());
        log.info("MODIFIED RESPONSE: " + modifiedMsg);
        final InputStream is1 = new ByteArrayInputStream(modifiedMsg.getBytes());
        SoapMessage m = soapMessageFactory.createWebServiceMessage(is1);
        if (messageContext.hasResponse()) messageContext.clearResponse();
        messageContext.setResponse(m);
        log.info("NEW RESPONSE WAS SET");
    }

正如你所看到的那样,我正在以字符串形式获得响应并修改它,写了一个简单(丑陋)的解析器:

private String faultCodeModifier(String message) {
        StringBuilder sb = new StringBuilder();
        String[] arr = message.split("<");
        for (int i = 0; i < arr.length; i++) {
            String b = arr[i];
            if (arr[i].contains("faultcode") && !arr[i].contains("/")) {
                String a = arr[i].split(">")[1];
                String c = a.split(":")[1];
                b = arr[i].split(">")[0] + ">" + c;
            }
            if (!"".equals(arr[i])) sb.append("<" + b);
        }
        return sb.toString();
    }

最后,我正在创建新的响应并将其插入消息上下文中。所有这些都解决了我的问题,我对此并不满意。如果有人有更好的解决方案,请分享。