Java笔记-Spring Boot Webservice中对指定namespace及localpart进行拦截
生活随笔
收集整理的這篇文章主要介紹了
Java笔记-Spring Boot Webservice中对指定namespace及localpart进行拦截
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在外網中有這樣的一個例子:
@Overridepublic void addInterceptors(List<EndpointInterceptor> interceptors) {// register global interceptorinterceptors.add(new GlobalEndpointInterceptor());// register endpoint specific interceptorinterceptors.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<String> ls = new ArrayList<String>();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;}@Overridepublic boolean handleRequest(MessageContext messageContext, Object o) throws Exception {return false;}@Overridepublic boolean handleResponse(MessageContext messageContext, Object o) throws Exception {return false;}@Overridepublic boolean handleFault(MessageContext messageContext, Object o) throws Exception {return false;}@Overridepublic 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);}@Overridepublic boolean handleRequest(MessageContext messageContext, Object o) throws Exception {boolean should = super.shouldInterceptor(messageContext);if(!should)return true;System.out.println("SmartInterceptor1 handleRequest");return true;}@Overridepublic boolean handleResponse(MessageContext messageContext, Object o) throws Exception {boolean should = super.shouldInterceptor(messageContext);if(!should)return true;System.out.println("SmartInterceptor1 handleResponse");return true;}@Overridepublic boolean handleFault(MessageContext messageContext, Object o) throws Exception {boolean should = super.shouldInterceptor(messageContext);if(!should)return true;System.out.println("SmartInterceptor1 handleFault");return true;}@Overridepublic void afterCompletion(MessageContext messageContext, Object o, Exception e) throws Exception {} }這里也就是在攔截器中調用父類的shoudInterceptor,看看namespace和localPart是否正確,正確就攔截,不正確就放過去。其中添加的時候和以前的那個差不多。
@Overridepublic void addInterceptors(List<EndpointInterceptor> interceptors) {// register global interceptorinterceptors.add(new GlobalEndpointInterceptor());// register endpoint specific interceptorinterceptors.add(new SmartInterceptor1(BeerEndpoint.NAMESPACE ,BeerEndpoint.BEER_REQUEST_LOCAL_PART));}?
總結
以上是生活随笔為你收集整理的Java笔记-Spring Boot Webservice中对指定namespace及localpart进行拦截的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++笔记-基于邻接表的BFS(宽度优先
- 下一篇: Linux笔记-bash中执行命令并将执