在CXF Web服务项目中编写拦截器

时间:2013-12-04 08:59:29

标签: java spring cxf

如何根据某些特定条件编写CXF拦截器以防止执行几个Restful URL。我知道我需要编写一个扩展URIMappingInterceptor的拦截器。阻止必须基于将被调用的url或函数的名称来完成。我需要立即返回错误。

1 个答案:

答案 0 :(得分:2)

你可以扩展AbstractInDatabindingInterceptor并使用message.get,获取url或uri并进行验证以下是我长期使用的示例。

public class RestInInterceptor extends AbstractInDatabindingInterceptor {

    private static final Logger LOG = LoggerFactory
            .getLogger(RestInInterceptor.class);

    public RestInInterceptor(String phase) {
        super(Phase.USER_STREAM);
        // TODO Auto-generated constructor stub
    }

    public RestInInterceptor() {
        super(Phase.USER_STREAM);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void handleMessage(Message message) throws Fault {

        Endpoint endpoint = message.getExchange().getEndpoint();

        String output = "";

        // for uri key is org.apache.cxf.request.uri
        String url = (String) message.get("org.apache.cxf.request.url");

        if(!"".equals(url) && url.contains("KP1Service")){
            Response response = Response.status(Response.Status.METHOD_NOT_ALLOWED).entity("").type(MediaType.TEXT_PLAIN) .build();
            message.getExchange().put(Response.class, response);
            return;
        }
    }
}