CXF拦截器无法正常工作

时间:2014-11-14 08:20:17

标签: java web-services cxf

我创建了web服务cxf客户端,只是简单地向给定端点发送soap消息,我认为有一些奇怪的问题。 首先,我不知道为什么我会得到很多调试日志,其次,更重要的问题是拦截器不起作用。也不是我的或来自CXF框架的人。 我已经使用wsdl中的wsimport创建了类,所以我认为这些问题与它们无关(我已经完成了很多客户端服务应用程序并且没有使用此工具的任何问题)。 这是我的主要课程:

    System.setProperty("com.sun.net.httpserver.HttpServerProvider",
               "org.mortbay.jetty.j2se6.JettyHttpServerProvider");
    JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
    factory.getOutInterceptors().add(new LoggingInInterceptor());
    factory.getInInterceptors().add(new LoggingOutInterceptor());
    factory.setAddress(ADDRESS);
    factory.setServiceClass(WebserviceInterface.class);
    factory.setUsername("usr");
    factory.setPassword("pass");
    Main main = new Main();
    main.operation = (WebserviceInterface) factory.create();

    WebServiceRequest request = main.generateRequest();

    main.operation.someWebServiceOperation(request);

这里我添加了默认的cxf拦截器,但它没有工作,所以我创建了自己的拦截器: IN:

public class LoggingInInterceptor extends AbstractSoapInterceptor {

private static final Logger LOG = Logger.getLogger(LoggingInInterceptor.class.getName());

public LoggingInInterceptor () {
    super(Phase.RECEIVE);
}

@Override
public void handleMessage(SoapMessage message) throws Fault {
    LOG.log(Level.INFO, "\n\n\n\nhandleMessage called\n\n\n\n");
    InputStream is = message.getContent(InputStream.class);
    CachedOutputStream os = new CachedOutputStream();
    try {
        IOUtils.copy(is, os);
        os.flush();
        message.setContent(InputStream.class, os.getInputStream());
        is.close();
        GregorianCalendar calendar = new GregorianCalendar();
        Date date = new Date();
        calendar.setTime(date); //gets current date and time
        XMLGregorianCalendar timestamp = DatatypeFactory
                                            .newInstance()
                                            .newXMLGregorianCalendar(calendar);
        System.out.println("Received at " + timestamp + ":\n" + XMLFormatter.format(IOUtils.toString(os.getInputStream())));//NOPMD
        System.out.println("----------------------------" + "\n\n");//NOPMD
    } catch (IOException e) {
        LOG.log(Level.WARNING, e.getMessage(), IOException.class);
    } catch (DocumentException e) {
        LOG.log(Level.WARNING, e.getMessage(), DocumentException.class);
    } catch (DatatypeConfigurationException e) {
        LOG.log(Level.WARNING, e.getMessage(), DatatypeConfigurationException.class);
    }
}

}

OUT:

public class LogginOutInterceptor extends LoggingOutInterceptor {

private static final Logger LOG = Logger.getLogger(LogginOutInterceptor.class.getName());

public LogginOutInterceptor() {
    super(Phase.PRE_STREAM);
}

@Override
public void handleMessage(Message message) {
    OutputStream out = message.getContent(OutputStream.class);
    final CacheAndWriteOutputStream newOut = new CacheAndWriteOutputStream(out);
    message.setContent(OutputStream.class, newOut);
    newOut.registerCallback(new LoggingCallback());
}

public class LoggingCallback implements CachedOutputStreamCallback {

    @Override
    public void onFlush(CachedOutputStream cos) {
    }

    /**
     * Prints outgoing message on the screen. 
     */
    @Override
    public void onClose(CachedOutputStream cos) {
        try {
            StringBuilder builder = new StringBuilder();
            cos.writeCacheTo(builder, limit);
            String soapXml = builder.toString();
            GregorianCalendar calendar = new GregorianCalendar();
            Date date = new Date();
            calendar.setTime(date); //gets current date and time
            XMLGregorianCalendar timestamp = DatatypeFactory
                                                .newInstance()
                                                .newXMLGregorianCalendar(calendar);
            System.out.println("Sent at " + timestamp + ":\n" + XMLFormatter.format(soapXml));//NOPMD
            System.out.println("----------------------------" + "\n\n");//NOPMD
        } catch (Exception e) {
            LOG.log(Level.WARNING, e.getMessage(), Exception.class);
        }
    }
}

}

我得到Skipping interceptor org.apache.cxf.interceptor.LoggingInInterceptor: Phase receive specified does not exist.但我不知道为什么。

我的第二个问题是为什么我在控制台上获得了这么多日志?我得到这样的东西:

08:37:33.829 [main] DEBUG org.apache.cxf.endpoint.ClientImpl - Interceptors contributed by databinding: []
08:37:33.832 [main] DEBUG o.a.cxf.phase.PhaseInterceptorChain - Adding interceptor org.apache.cxf.ws.policy.PolicyOutInterceptor@194164c to phase setup
08:37:33.833 [main] WARN  o.a.cxf.phase.PhaseInterceptorChain - Skipping interceptor com.steria.sm.LoggingInInterceptor: Phase receive specified does not exist.
08:37:33.833 [main] DEBUG o.a.cxf.phase.PhaseInterceptorChain - Adding interceptor org.apache.cxf.interceptor.MessageSenderInterceptor@1fc9a67 to phase prepare-send
08:37:33.834 [main] DEBUG o.a.cxf.phase.PhaseInterceptorChain - Adding interceptor org.apache.cxf.jaxws.interceptors.SwAOutInterceptor@fcf5ba to phase pre-logical
08:37:33.835 [main] DEBUG o.a.cxf.phase.PhaseInterceptorChain - Adding interceptor org.apache.cxf.jaxws.interceptors.WrapperClassOutInterceptor@16889eb to phase pre-logical
08:37:33.836 [main] DEBUG o.a.cxf.phase.PhaseInterceptorChain - Adding interceptor org.apache.cxf.jaxws.interceptors.HolderOutInterceptor@36c8f3 to phase pre-logical
08:37:33.837 [main] DEBUG o.a.cxf.phase.PhaseInterceptorChain - Adding interceptor org.apache.cxf.interceptor.AttachmentOutInterceptor@c3020c to phase pre-stream
08:37:33.838 [main] DEBUG o.a.cxf.phase.PhaseInterceptorChain - Adding interceptor org.apache.cxf.interceptor.StaxOutInterceptor@bb8a39 to phase pre-stream
08:37:33.839 [main] DEBUG o.a.cxf.phase.PhaseInterceptorChain - Adding interceptor org.apache.cxf.binding.soap.interceptor.SoapHeaderOutFilterInterceptor@6fd7d9 to phase pre-logical
08:37:33.840 [main] DEBUG o.a.cxf.phase.PhaseInterceptorChain - Adding interceptor org.apache.cxf.wsdl.interceptors.BareOutInterceptor@c412dd to phase marshal
08:37:33.841 [main] DEBUG o.a.cxf.phase.PhaseInterceptorChain - Adding interceptor org.apache.cxf.binding.soap.interceptor.SoapPreProtocolOutInterceptor@b2aa22 to phase post-logical
08:37:33.842 [main] DEBUG o.a.cxf.phase.PhaseInterceptorChain - Adding interceptor org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor@1bfcec0 to phase write

我不想发布所有这些日志,因为它们很多。

有谁知道我错了什么?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:7)

我认为你已经互换了拦截器:

factory.getOutInterceptors().add(new LoggingInInterceptor());
factory.getInInterceptors().add(new LoggingOutInterceptor());
相关问题