0

I am trying to write a Custom CXF Interceptor to do some validations on SOAP request to a web service. Based on the validation results, I want to block the request to web service and return the response with some modified parameters.

For this, I have written custom CXF ininterceptor extending from AbstractPhaseInterceptor, to run in phase USER_LOGICAL, which does validations, but I am not able to stop the subsequent call to web service and also not able to pass the Custom Response object(Custom Response object type is same as web service return type). How can I do this using interceptors?

4

3 回答 3

1

我对 nadirsaghar 的提示进行了一些研究,发现它是可用的清洁解决方案。在 JAX-WS 中使用 message.getExchange() 非常痛苦,因为您必须自己设置一个管道并填写响应消息......

所以最好这样做,使用 HttpServletResponse。- 你需要在你的路径上有 java servlet-api.jar。如果您在没有 maven 的情况下进行开发,只需从您的网络服务器(例如 tomcat)目录链接它,但将其排除在部署之外。

<!-- With Maven add the following dependency -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <!-- The version should match your WebService version e.g. 3.0 for JDK7-->
    <version>2.5</version>
    <scope>provided</scope>
</dependency>

提供范围后,它不会被部署,只是可用,因此您可以访问 HttpServletResponse 类。

您的处理程序代码:

@Override
public void handleMessage( final Message message ) throws Fault
{
    if( shouldBlockMessage( message ) )
    {
        message.getInterceptorChain().abort();

        final HttpServletResponse response = (HttpServletResponse)message.get( AbstractHTTPDestination.HTTP_RESPONSE );

        // To redirect a user to a different Page
        response.setStatus( HttpServletResponse.SC_MOVED_TEMPORARILY );
        response.setHeader( "Location", "http://www.bla.blubb/redirectPage" );

        // Other possibility if a User provides faulty login data
        response.setStatus( HttpServletResponse.SC_FORBIDDEN );
    }
}
于 2014-06-06T08:52:36.677 回答
0

像这样的东西,没有必要玩拦截器链。

public void handleMessage(Message message) {
   //your logic
   Response response = Response.status(Status.UNAUTHORIZED).type(MediaType.APPLICATION_JSON).build();
   message.getExchange().put(Response.class, response);

}
于 2013-06-14T06:38:00.163 回答
0

abort您可以使用方法中止拦截器链的执行,包括 Web 服务

public void handleMessage(SoapMessage message) {

    InterceptorChain chain = message.getInterceptorChain();
    chain.abort();
}
于 2013-06-13T18:06:29.980 回答