From 2db3a4ca1a12d8f78effff493d46447f97dd41f9 Mon Sep 17 00:00:00 2001
From: wuxw7 <wuxw7@asiainfo.com>
Date: 星期二, 04 十二月 2018 15:36:45 +0800
Subject: [PATCH] api 侦听处理时 加入方法的限制,防止一个serviceCode 用不通的Method调,如果不是用指定的Method掉直接 不进相应的侦听类
---
java110-event/src/main/java/com/java110/event/service/api/ServiceDataFlowEventPublishing.java | 35 ++++++++++++++++++++++++++++-------
1 files changed, 28 insertions(+), 7 deletions(-)
diff --git a/java110-event/src/main/java/com/java110/event/service/api/ServiceDataFlowEventPublishing.java b/java110-event/src/main/java/com/java110/event/service/api/ServiceDataFlowEventPublishing.java
index 36d2d3d..1bba5c6 100644
--- a/java110-event/src/main/java/com/java110/event/service/api/ServiceDataFlowEventPublishing.java
+++ b/java110-event/src/main/java/com/java110/event/service/api/ServiceDataFlowEventPublishing.java
@@ -2,7 +2,9 @@
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.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 @@
* @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 @@
DataFlowListenerOrderComparator.sort(dataFlowListeners);
//灏嗘暟鎹斁鍏ョ紦瀛樹腑
- cacheListenersMap.put(serviceCode,dataFlowListeners);
+ cacheListenersMap.put(needCachedServiceCode,dataFlowListeners);
return dataFlowListeners;
}
@@ -118,7 +127,8 @@
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 @@
* @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)) {
+ //濡傛灉鏄�忎紶绫� 璇锋眰鏂瑰紡蹇呴』涓庢帴鍙f彁渚涙柟璋冪敤鏂瑰紡涓�鑷�
+ 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 @@
}
}
+
+
/**
* Return the current task executor for this multicaster.
*/
--
Gitblit v1.8.0