Просмотр исходного кода

api 侦听处理时 加入方法的限制,防止一个serviceCode 用不通的Method调,如果不是用指定的Method掉直接 不进相应的侦听类

wuxw7 лет назад: 7
Родитель
Сommit
2db3a4ca1a

+ 6 - 4
Api/src/main/java/com/java110/api/listener/CheckLoginServiceListener.java

@@ -13,10 +13,7 @@ import com.java110.entity.center.AppService;
 import com.java110.event.service.api.ServiceDataFlowEvent;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.http.HttpEntity;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
+import org.springframework.http.*;
 
 import javax.naming.AuthenticationException;
 import java.util.HashMap;
@@ -38,6 +35,11 @@ public class CheckLoginServiceListener extends AbstractServiceApiDataFlowListene
         return ServiceCodeConstant.SERVICE_CODE_CHECK_SERVICE_LOGIN;
     }
 
+    @Override
+    public HttpMethod getHttpMethod() {
+        return HttpMethod.POST;
+    }
+
 
     @Override
     public int getOrder() {

+ 6 - 0
Api/src/main/java/com/java110/api/listener/OrderServiceListener.java

@@ -12,6 +12,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.http.HttpEntity;
 import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
 
 /**
  * 订单类信息处理 侦听
@@ -29,6 +30,11 @@ public class OrderServiceListener extends AbstractServiceApiDataFlowListener{
         return ServiceCodeConstant.SERVICE_CODE_DO_SERVICE_ORDER;
     }
 
+    @Override
+    public HttpMethod getHttpMethod() {
+        return HttpMethod.POST;
+    }
+
 
     @Override
     public int getOrder() {

+ 5 - 0
Api/src/main/java/com/java110/api/listener/TransferServiceListener.java

@@ -32,6 +32,11 @@ public class TransferServiceListener extends AbstractServiceApiDataFlowListener{
         return ServiceCodeConstant.SERVICE_CODE_DO_SERVICE_TRANSFER;
     }
 
+    @Override
+    public HttpMethod getHttpMethod() {
+        return null;
+    }
+
 
     @Override
     public int getOrder() {

+ 5 - 0
Api/src/main/java/com/java110/api/listener/UserLoginServiceListener.java

@@ -37,6 +37,11 @@ public class UserLoginServiceListener extends AbstractServiceApiDataFlowListener
         return ServiceCodeConstant.SERVICE_CODE_USER_SERVICE_LOGIN;
     }
 
+    @Override
+    public HttpMethod getHttpMethod() {
+        return HttpMethod.POST;
+    }
+
 
     @Override
     public int getOrder() {

+ 6 - 4
Api/src/main/java/com/java110/api/listener/UserRegisterServiceListener.java

@@ -16,10 +16,7 @@ import io.swagger.annotations.ApiImplicitParam;
 import io.swagger.annotations.ApiOperation;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.http.HttpEntity;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
+import org.springframework.http.*;
 
 import java.util.Map;
 
@@ -39,6 +36,11 @@ public class UserRegisterServiceListener extends AbstractServiceApiDataFlowListe
         return ServiceCodeConstant.SERVICE_CODE_USER_SERVICE_REGISTER;
     }
 
+    @Override
+    public HttpMethod getHttpMethod() {
+        return HttpMethod.POST;
+    }
+
 
     @Override
     public int getOrder() {

+ 6 - 0
Api/src/main/java/com/java110/api/listener/users/UserLogoutServiceListener.java

@@ -11,6 +11,7 @@ import com.java110.entity.center.AppService;
 import com.java110.event.service.api.ServiceDataFlowEvent;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpMethod;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 
@@ -33,6 +34,11 @@ public class UserLogoutServiceListener extends AbstractServiceApiDataFlowListene
         return ServiceCodeConstant.SERVICE_CODE_USER_SERVICE_LOGOUT;
     }
 
+    @Override
+    public HttpMethod getHttpMethod() {
+        return HttpMethod.POST;
+    }
+
 
     @Override
     public int getOrder() {

+ 28 - 7
java110-event/src/main/java/com/java110/event/service/api/ServiceDataFlowEventPublishing.java

@@ -2,7 +2,9 @@ package com.java110.event.service.api;
 
 import com.java110.common.constant.CommonConstant;
 import com.java110.common.constant.ResponseConstant;
+import com.java110.common.constant.ServiceCodeConstant;
 import com.java110.common.exception.BusinessException;
+import com.java110.common.exception.ListenerExecuteException;
 import com.java110.common.factory.ApplicationContextFactory;
 import com.java110.common.log.LoggerEngine;
 import com.java110.common.util.Assert;
@@ -11,6 +13,7 @@ import com.java110.entity.center.AppService;
 import com.java110.event.center.DataFlowListenerOrderComparator;
 import com.java110.event.service.BusinessServiceDataFlowEvent;
 import com.java110.event.service.BusinessServiceDataFlowListener;
+import org.springframework.http.HttpMethod;
 
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -62,19 +65,25 @@ public class ServiceDataFlowEventPublishing extends LoggerEngine {
      * @since 1.8
      * @return
      */
-    public static List<ServiceDataFlowListener> getListeners(String serviceCode){
+    public static List<ServiceDataFlowListener> getListeners(String serviceCode,String httpMethod){
 
         Assert.hasLength(serviceCode,"获取需要发布的事件处理侦听时,传递事件为空,请检查");
 
+        String needCachedServiceCode = serviceCode+httpMethod;
         //先从缓存中获取,为了提升效率
-        if(cacheListenersMap.containsKey(serviceCode)){
-            return cacheListenersMap.get(serviceCode);
+        if(cacheListenersMap.containsKey(needCachedServiceCode)){
+            return cacheListenersMap.get(needCachedServiceCode);
         }
 
         List<ServiceDataFlowListener> dataFlowListeners = new ArrayList<ServiceDataFlowListener>();
         for(String listenerBeanName : getListeners()){
             ServiceDataFlowListener listener = ApplicationContextFactory.getBean(listenerBeanName,ServiceDataFlowListener.class);
-            if(serviceCode.equals(listener.getServiceCode())){
+            if(serviceCode.equals(listener.getServiceCode())
+                    && listener.getHttpMethod() == HttpMethod.valueOf(httpMethod)){
+                dataFlowListeners.add(listener);
+            }
+            //特殊处理 透传类接口
+            if(ServiceCodeConstant.SERVICE_CODE_DO_SERVICE_TRANSFER.equals(listener.getServiceCode())){
                 dataFlowListeners.add(listener);
             }
         }
@@ -83,7 +92,7 @@ public class ServiceDataFlowEventPublishing extends LoggerEngine {
         DataFlowListenerOrderComparator.sort(dataFlowListeners);
 
         //将数据放入缓存中
-        cacheListenersMap.put(serviceCode,dataFlowListeners);
+        cacheListenersMap.put(needCachedServiceCode,dataFlowListeners);
         return dataFlowListeners;
     }
 
@@ -118,7 +127,8 @@ public class ServiceDataFlowEventPublishing extends LoggerEngine {
 
             multicastEvent(serviceCode,targetDataFlowEvent, asyn);
         }catch (Exception e){
-            throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR,"发布侦听失败,失败原因为:"+e);
+            logger.error("发布侦听失败,失败原因为:",e);
+            throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR,"发布侦听失败,失败原因为:"+e.getMessage());
         }
 
     }
@@ -130,7 +140,16 @@ public class ServiceDataFlowEventPublishing extends LoggerEngine {
      * @param asyn A 表示异步处理
      */
     public static void multicastEvent(String serviceCode,final ServiceDataFlowEvent event, String asyn) {
-        for (final ServiceDataFlowListener listener : getListeners(serviceCode)) {
+        String httpMethod = event.getDataFlowContext().getRequestCurrentHeaders().get(CommonConstant.HTTP_METHOD);
+        for (final ServiceDataFlowListener listener : getListeners(serviceCode,httpMethod)) {
+            //如果是透传类 请求方式必须与接口提供方调用方式一致
+            if(ServiceCodeConstant.SERVICE_CODE_DO_SERVICE_TRANSFER.equals(serviceCode)){
+                AppService appService = event.getAppService();
+                if(!appService.getMethod().equals(httpMethod)) {
+                    throw new ListenerExecuteException(ResponseConstant.RESULT_CODE_ERROR,
+                            "服务【" + serviceCode + "】调用方式不对请检查,当前请求方式为:"+httpMethod);
+                }
+            }
 
             if(CommonConstant.PROCESS_ORDER_ASYNCHRONOUS.equals(asyn)){ //异步处理
 
@@ -150,6 +169,8 @@ public class ServiceDataFlowEventPublishing extends LoggerEngine {
         }
     }
 
+
+
     /**
      * Return the current task executor for this multicaster.
      */

+ 7 - 0
java110-event/src/main/java/com/java110/event/service/api/ServiceDataFlowListener.java

@@ -2,6 +2,7 @@ package com.java110.event.service.api;
 
 import com.java110.event.app.order.Ordered;
 import com.java110.event.service.BusinessServiceDataFlowEvent;
+import org.springframework.http.HttpMethod;
 
 import java.util.EventListener;
 
@@ -17,5 +18,11 @@ public interface ServiceDataFlowListener extends EventListener,Ordered {
      */
     public String getServiceCode();
 
+    /**
+     * 获取调用时的方法
+     * @return 接口对外提供方式 如HttpMethod.POST
+     */
+    public HttpMethod getHttpMethod();
+
     public void soService(ServiceDataFlowEvent event);
 }