未调用CXF拦截器来读取http响应

时间:2013-07-15 17:59:02

标签: cxf httpresponse interceptor

我正在编写Web服务的客户端,我想使用拦截器做两件事 - 1)记录传出的XML消息和2)读取HTTP响应,而不是SOAP响应。

服务端只有一个void方法,他们只是通过HTTP响应代码确认收到了我的消息。

这是他们的反应:

HTTP/1.1 202 Accepted
Date: Wed, 10 Jul 2013 23:06:36 GMT
Server: Oracle-Application-Server-11g
Content-Length: 0
X-ORACLE-DMS-ECID: 004sJ1KLm7yB_6RjMX^QsA0002X30001U5
X-Powered-By: Servlet/2.5 JSP/2.1
Content-Type: 
Content-Language: en
X-F5-ServerSSL-Profile: serverssl_sys_soaorexafun
X-F5-ForwardProxy-Host: 64.181.184.15%819 443

我需要“HTTP / 1.1 202 Accepted”回复。

我的拦截器代码是:

package com.cambiahealth.orservice.client;

import org.apache.cxf.binding.soap.interceptor.EndpointSelectionInterceptor;
import org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.apache.log4j.Logger;

public class CoverOregonServiceInterceptor extends AbstractPhaseInterceptor<Message> {

    static Logger logger = Logger.getLogger(CoverOregonServiceInterceptor.class.getName());
    private static Integer responseCode = 0;

    public CoverOregonServiceInterceptor() {
        super(Phase.POST_INVOKE);
        addAfter(ReadHeadersInterceptor.class.getName());
        addAfter(EndpointSelectionInterceptor.class.getName());
    }

    @Override
    public void handleMessage(Message message) throws Fault {
        try{
            responseCode = (Integer) message.get(Message.RESPONSE_CODE);
            logger.info("Response Code is " + responseCode);
        } catch (Exception e){
            logger.error("Failed to read the response code from Soap message", e);
        }        
    }

    public Integer getResponseCode(){
        return responseCode;
    }

}

我客户端的代码是:

String wsdlUrl = coverOregonServiceUrl + "?wsdl";
CoverOregonServiceInterceptor interceptor = new CoverOregonServiceInterceptor();
CoverOregonServiceInterceptor outInterceptor = new CoverOregonServiceInterceptor();
InboundEnrollmentManagementClient client = new InboundEnrollmentManagementClient(
                    new URL(wsdlUrl));
InboundEnrollManagementPortType port = client.getInboundEnrollManagementPort();
Client cxfClient = ClientProxy.getClient(port);
cxfClient.getInInterceptors().add(interceptor); 
cxfClient.getOutFaultInterceptors().add(outInterceptor);
port.statusupdate(request);

但是,我的拦截器根本没有被调用/触发。我尝试了各种不同的阶段 - READ,RECEIVE,PRE_INVOKE,POST_INVOKE,INVOKE,POST_STREAM,SEND。但它们都没有奏效。我尝试了一个类似的拦截器来记录XML消息,然后再发送请求。它也没有在那里工作。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

想出来。我正在调用的服务没有返回,所以我不能使用入站拦截器链。

cxfClient.getInInterceptors().add(interceptor);

我在出站链中使用它并且它有效。

cxfClient.getOutInterceptors().add(outInterceptor);