Java笔记-Spring Boot Webservice中对指定namespace及localpart进行拦截

在外网中有这样的一个例子:

    @Override
    public void addInterceptors(List interceptors) {

        // register global interceptor
        interceptors.add(new GlobalEndpointInterceptor());

        // register endpoint specific interceptor
        interceptors.add(new PayloadRootSmartSoapEndpointInterceptor(
                new CustomEndpointInterceptor(),
                BeerEndpoint.NAMESPACE,
                BeerEndpoint.BEER_REQUEST_LOCAL_PART));
    }

其中PayloadRootSmartSoapEndpointInterceptor可以对指定namespace和localpart进行拦截。

但是给出的例子,并不能实现这个效果。也就是把拦截到的调用CustomEndpointIntecetor中。下面稍微看下这个类。

public class PayloadRootSmartSoapEndpointInterceptor extends DelegatingSmartSoapEndpointInterceptor {
    private TransformerHelper transformerHelper = new TransformerHelper();
    private final String namespaceUri;
    private final String localPart;

    public PayloadRootSmartSoapEndpointInterceptor(EndpointInterceptor delegate, String namespaceUri, String localPart) {
        super(delegate);
        Assert.hasLength(namespaceUri, "namespaceUri can not be empty");
        this.namespaceUri = namespaceUri;
        this.localPart = localPart;
    }

    public void setTransformerHelper(TransformerHelper transformerHelper) {
        this.transformerHelper = transformerHelper;
    }

    protected boolean shouldIntercept(WebServiceMessage request, Object endpoint) {
        try {
            QName payloadRootName = PayloadRootUtils.getPayloadRootQName(request.getPayloadSource(), this.transformerHelper);
            if (payloadRootName != null && this.namespaceUri.equals(payloadRootName.getNamespaceURI())) {
                return !StringUtils.hasLength(this.localPart) || this.localPart.equals(payloadRootName.getLocalPart());
            } else {
                return false;
            }
        } catch (TransformerException var4) {
            return false;
        }
    }
}

可见最关键的是shouldIntercept,是不是判断其要拦截。这里我们可以仿照一个smart拦截器。

构建如下包:

源码如下:

MySmartInterceptor.java

public class MySmartInterceptor implements EndpointInterceptor {

    String namespaceUri = null;
    String localPart = null;

    public MySmartInterceptor(String namespaceUri, String localPart){

        this.namespaceUri = namespaceUri;
        this.localPart = localPart;
    }

    public boolean shouldInterceptor(MessageContext messageContext){

        SoapMessage request = (SoapMessage)messageContext.getRequest();
        String namespaceAdnLocalPart = request.toString();

        List ls = new ArrayList();
        Pattern pattern = Pattern.compile("(?<=\\{)(.+?)(?=\\})");
        Matcher matcher = pattern.matcher(namespaceAdnLocalPart);
        while(matcher.find()) {

            ls.add(matcher.group());
        }
        String namespaceStr = ls.get(0);
        String localPartStr = namespaceAdnLocalPart.split(namespaceStr + "}")[1];

        if(namespaceStr.equals(this.namespaceUri) && localPartStr.equals(this.localPart)){

            return true;
        }

        return false;

    }


    @Override
    public boolean handleRequest(MessageContext messageContext, Object o) throws Exception {
        return false;
    }

    @Override
    public boolean handleResponse(MessageContext messageContext, Object o) throws Exception {
        return false;
    }

    @Override
    public boolean handleFault(MessageContext messageContext, Object o) throws Exception {
        return false;
    }

    @Override
    public void afterCompletion(MessageContext messageContext, Object o, Exception e) throws Exception {

    }
}

SmartInterceptor1.java

public class SmartInterceptor1 extends MySmartInterceptor {

    public SmartInterceptor1(String namespaceUri, String localPart) {
        super(namespaceUri, localPart);
    }

    @Override
    public boolean handleRequest(MessageContext messageContext, Object o) throws Exception {

        boolean should = super.shouldInterceptor(messageContext);
        if(!should)
            return true;

        System.out.println("SmartInterceptor1 handleRequest");
        return true;
    }

    @Override
    public boolean handleResponse(MessageContext messageContext, Object o) throws Exception {

        boolean should = super.shouldInterceptor(messageContext);
        if(!should)
            return true;

        System.out.println("SmartInterceptor1 handleResponse");
        return true;
    }

    @Override
    public boolean handleFault(MessageContext messageContext, Object o) throws Exception {

        boolean should = super.shouldInterceptor(messageContext);
        if(!should)
            return true;

        System.out.println("SmartInterceptor1 handleFault");
        return true;
    }

    @Override
    public void afterCompletion(MessageContext messageContext, Object o, Exception e) throws Exception {

    }
}

这里也就是在拦截器中调用父类的shoudInterceptor,看看namespace和localPart是否正确,正确就拦截,不正确就放过去。其中添加的时候和以前的那个差不多。

    @Override
    public void addInterceptors(List interceptors) {
        // register global interceptor
        interceptors.add(new GlobalEndpointInterceptor());

        // register endpoint specific interceptor
        interceptors.add(new SmartInterceptor1(
                BeerEndpoint.NAMESPACE ,
                BeerEndpoint.BEER_REQUEST_LOCAL_PART));
    }

 

你可能感兴趣的:(webservice,Java,Spring,Boot,spring,boot,webservice,java,interceptor)