Skip to content

Commit

Permalink
增加渠道匹配,
Browse files Browse the repository at this point in the history
可以不用引用jstl.jar
DyanmicUrlPattern 增加 ignore方法
  • Loading branch information
allon2 committed May 29, 2020
1 parent 863c2e7 commit 06eb037
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 32 deletions.
13 changes: 11 additions & 2 deletions dactor/src/main/java/cn/ymotel/dactor/action/ViewResolveActor.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import cn.ymotel.dactor.async.web.AsyncContextWrapper;
import cn.ymotel.dactor.async.web.view.CustomHttpView;
import cn.ymotel.dactor.async.web.view.HttpView;
import cn.ymotel.dactor.core.ActorChainCfg;
import cn.ymotel.dactor.core.ActorTransactionCfg;
import cn.ymotel.dactor.message.ServletMessage;
import cn.ymotel.dactor.message.Message;
import cn.ymotel.dactor.pattern.PatternLookUpMatch;
Expand Down Expand Up @@ -114,10 +116,17 @@ public Message HandleMessage(ServletMessage message) throws Exception {
* @param message
* @return 执行返回true,不执行 返回false
*/
public boolean UrlPathViewExecute(ServletMessage message){
public boolean UrlPathViewExecute(ServletMessage message) {

String UrlPath = urlPathHelper.getLookupPathForRequest(message.getRequest());
String chain=message.getControlMessage().getProcessStructure().getActorTransactionCfg().getChain().getId();
String chain=null;
{
ActorTransactionCfg transactionCfg= message.getControlMessage().getProcessStructure().getActorTransactionCfg();
ActorChainCfg chainCfg=transactionCfg.getChain();
if(chain!=null) {
chain = chainCfg.getId();
}
}
HttpView httpView= patternLookUpMatch.lookupMatchBean(UrlPath,message.getRequest().getMethod(),message.getRequest().getServerName(),chain,message.getRequest());
if(httpView==null){
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,11 @@ protected MultipartResolver lookupMultipartResolver() {
private long timeout =30000 ;
private UrlPathHelper urlPathHelper = new UrlPathHelper();
private DefaultResolveMessage defaultResolveMessage = null;
public static String messageSourceId = "messageSource";
private int errorcode = HttpStatus.TOO_MANY_REQUESTS.value();//请求数量太多
private HttpStatus errstatus= HttpStatus.TOO_MANY_REQUESTS;
public UrlPathHelper getUrlPathHelper() {
return urlPathHelper;
}
// public UrlPathHelper getUrlPathHelper() {
// return urlPathHelper;
// }

@Override
public void init(FilterConfig filterConfig) throws ServletException {
Expand Down Expand Up @@ -352,11 +351,14 @@ private MatchPair UrlPatternHandler(String UrlPath, HttpServletRequest request)
UrlMapping.getDynamicMapping().forEach((dyanmicUrlPattern, actorTransactionCfg) -> {
String[] patterns= null;
try {
if(dyanmicUrlPattern.ignore()){
return;
}
patterns= dyanmicUrlPattern.getPatterns(request);

if(patterns==null||patterns.length==0){
return;
}
if(patterns==null||patterns.length==0){
return;
}
PatternMatcher patternMatcher=new PatternMatcher(patterns,dyanmicUrlPattern.getExcludePatterns(request),new String[]{actorTransactionCfg.getChain().getId()},actorTransactionCfg);
lookUpMatch.add(patternMatcher);
} catch (java.lang.Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@
public interface DyanmicUrlPattern<T> {
public default String[] getPatterns(T request){return null;};
public default String[] getExcludePatterns(T request){return null;};

/**
* 此规则是否忽略
* @return false 不忽略,true 忽略
*/
public default boolean ignore() { return false;}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ public int compare(MatchPair pair1, MatchPair pair2) {
if(DispatcherType.REQUEST.name().equals(pair1.getDispatcherType())&&DispatcherType.ERROR.name().equals(pair2.getDispatcherType())) {
return 1;
}
if(pair1.getMatchPattern()==null&&pair2.getMatchPattern()==null){
return compareServerNameAndMethod(pair1,pair2);
}
if(pair1.getMatchPattern()!=null&&pair2.getMatchPattern()==null){
return -1;
}
if(pair1.getMatchPattern()==null&&pair2.getMatchPattern()!=null){
return 1;
}
if(pair1.getMatchPattern().equals(pair2.getMatchPattern())){
return compareServerNameAndMethod(pair1, pair2);
}
Expand Down Expand Up @@ -74,6 +83,13 @@ private int compareServerNameAndMethod(MatchPair pair1, MatchPair pair2) {
if (pair2.getMethod() != null) {
right++;
}
if(pair1.getChain()!=null){
left++;
}

if(pair2.getChain()!=null){
right++;
}
int rtn = right - left;
if (rtn <= 0) {
return -1;
Expand Down
38 changes: 22 additions & 16 deletions dactor/src/main/java/cn/ymotel/dactor/pattern/PatternMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,29 +237,35 @@ public MatchPair matchePatterns(String lookupPath, PathMatcher pathMatcher,Strin
}
}
}

if(this.includePatterns!=null&&this.includePatterns.length>0){
List rtnList=new ArrayList();
/*
非Error,IncludePattern还是必须的
*/
if(!DispatcherType.ERROR.name().equals(dispatcherType)){
List rtnList=new ArrayList();
if(this.includePatterns==null||this.includePatterns.length==0){
return null;
}
// if(this.includePatterns!=null&&this.includePatterns.length>0){
for (String pattern : this.includePatterns) {
if (pathMatcherToUse.match(pattern, lookupPath)) {
rtnList.add(pattern);
}
}

if (rtnList.isEmpty()) {
return null;
}
if(comparator!=null) {
if (rtnList.size() > 1) {
rtnList.sort(comparator);
}
}
if(rtnList!=null&&rtnList.size()>0) {
pair.setMatchPattern((String) rtnList.get(0));
// }
if (rtnList.isEmpty()) {
return null;
}
if(comparator!=null) {
if (rtnList.size() > 1) {
rtnList.sort(comparator);
}
pair.setMatchPatterns(rtnList);

}
if(rtnList!=null&&rtnList.size()>0) {
pair.setMatchPattern((String) rtnList.get(0));
}
pair.setMatchPatterns(rtnList);
}

// if(!methods.isEmpty()){
// pair.setMethod(method);
// }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cn.ymotel.dactor.springboot;

import cn.ymotel.dactor.action.ViewResolveActor;
import cn.ymotel.dactor.async.web.AsyncServletFilter;
import cn.ymotel.dactor.async.web.MessageSourceFilter;
import cn.ymotel.dactor.async.web.view.*;
import cn.ymotel.dactor.response.ResponseViewType;
import cn.ymotel.dactor.response.TransportResponseViewActor;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(javax.servlet.jsp.jstl.core.Config.class)
@AutoConfigureAfter({DactorAutoConfiguration.class})
@ConditionalOnProperty(name = "dactor.enabled", matchIfMissing = true)
public class DactorMessageFilterConfiguration {

@Bean
@ConditionalOnClass(javax.servlet.jsp.jstl.core.Config.class)
public MessageSourceFilter MessageSourceFilter() {
MessageSourceFilter filter=new MessageSourceFilter();
return filter;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,7 @@ public AsyncServletFilter asyncServletFilter() {
AsyncServletFilter filter=new AsyncServletFilter();
return filter;
}
@Bean
@ConditionalOnClass(javax.servlet.jsp.jstl.core.Config.class)
public MessageSourceFilter MessageSourceFilter() {
MessageSourceFilter filter=new MessageSourceFilter();
return filter;
}

@Bean
@ConditionalOnMissingBean
public ViewResolveActor getViewResolveActor() {
Expand Down
3 changes: 2 additions & 1 deletion dactor/src/main/resources/META-INF/spring.factories
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.ymotel.dactor.springboot.DactorAutoConfiguration,\
cn.ymotel.dactor.springboot.DactorServletAutoConfiguration
cn.ymotel.dactor.springboot.DactorServletAutoConfiguration,\
cn.ymotel.dactor.springboot.DactorMessageFilterConfiguration

0 comments on commit 06eb037

Please sign in to comment.