From 63fa90da05f07a37f6a3717865c8dbffafe7df4c Mon Sep 17 00:00:00 2001 From: zhengjw22 Date: Mon, 12 Aug 2024 19:35:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/ApplicationFilterRegistration.java | 16 ++++++++-------- .../impl/ApplicationServletRegistration.java | 7 ++----- .../servlet/impl/ServletContextImpl.java | 6 +++--- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/servlet-core/src/main/java/tech/smartboot/servlet/impl/ApplicationFilterRegistration.java b/servlet-core/src/main/java/tech/smartboot/servlet/impl/ApplicationFilterRegistration.java index 02579d5..c03f8d5 100644 --- a/servlet-core/src/main/java/tech/smartboot/servlet/impl/ApplicationFilterRegistration.java +++ b/servlet-core/src/main/java/tech/smartboot/servlet/impl/ApplicationFilterRegistration.java @@ -12,16 +12,15 @@ import jakarta.servlet.DispatcherType; import jakarta.servlet.FilterRegistration; +import tech.smartboot.servlet.conf.DeploymentInfo; import tech.smartboot.servlet.conf.FilterInfo; import tech.smartboot.servlet.conf.FilterMappingInfo; import tech.smartboot.servlet.enums.FilterMappingType; -import java.util.ArrayList; import java.util.Collection; import java.util.EnumSet; import java.util.HashMap; import java.util.HashSet; -import java.util.List; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; @@ -34,34 +33,35 @@ public class ApplicationFilterRegistration implements FilterRegistration.Dynamic private final FilterInfo filterDef; - private final List filterMappings = new ArrayList<>(); + private DeploymentInfo deploymentInfo; - public ApplicationFilterRegistration(FilterInfo filterDef) { + public ApplicationFilterRegistration(DeploymentInfo deploymentInfo, FilterInfo filterDef) { + this.deploymentInfo = deploymentInfo; this.filterDef = filterDef; } @Override public void addMappingForServletNames(EnumSet dispatcherTypes, boolean isMatchAfter, String... servletNames) { for (String servletName : servletNames) { - filterMappings.add(new FilterMappingInfo(filterDef.getFilterName(), FilterMappingType.SERVLET, servletName, null, dispatcherTypes)); + deploymentInfo.addFilterMapping(new FilterMappingInfo(filterDef.getFilterName(), FilterMappingType.SERVLET, servletName, null, dispatcherTypes)); } } @Override public void addMappingForUrlPatterns(EnumSet dispatcherTypes, boolean isMatchAfter, String... urlPatterns) { for (String urlPattern : urlPatterns) { - filterMappings.add(new FilterMappingInfo(filterDef.getFilterName(), FilterMappingType.URL, null, urlPattern, dispatcherTypes)); + deploymentInfo.addFilterMapping(new FilterMappingInfo(filterDef.getFilterName(), FilterMappingType.URL, null, urlPattern, dispatcherTypes)); } } @Override public Collection getServletNameMappings() { - return filterMappings.stream().filter(filterMappingInfo -> filterMappingInfo.getMappingType() == FilterMappingType.SERVLET).map(FilterMappingInfo::getServletNameMapping).collect(Collectors.toList()); + return deploymentInfo.getFilterMappings().stream().filter(filterMappingInfo -> filterMappingInfo.getFilterName().equals(filterDef.getFilterName()) && filterMappingInfo.getMappingType() == FilterMappingType.SERVLET).map(FilterMappingInfo::getServletNameMapping).collect(Collectors.toList()); } @Override public Collection getUrlPatternMappings() { - return filterMappings.stream().filter(filterMappingInfo -> filterMappingInfo.getMappingType() == FilterMappingType.URL).map(FilterMappingInfo::getUrlPattern).collect(Collectors.toList()); + return deploymentInfo.getFilterMappings().stream().filter(filterMappingInfo -> filterMappingInfo.getFilterName().equals(filterDef.getFilterName()) && filterMappingInfo.getMappingType() == FilterMappingType.URL).map(FilterMappingInfo::getUrlPattern).collect(Collectors.toList()); } @Override diff --git a/servlet-core/src/main/java/tech/smartboot/servlet/impl/ApplicationServletRegistration.java b/servlet-core/src/main/java/tech/smartboot/servlet/impl/ApplicationServletRegistration.java index 03c6805..b7836ee 100644 --- a/servlet-core/src/main/java/tech/smartboot/servlet/impl/ApplicationServletRegistration.java +++ b/servlet-core/src/main/java/tech/smartboot/servlet/impl/ApplicationServletRegistration.java @@ -19,13 +19,11 @@ import tech.smartboot.servlet.conf.ServletInfo; import tech.smartboot.servlet.conf.ServletMappingInfo; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; -import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Set; @@ -38,7 +36,6 @@ public class ApplicationServletRegistration implements ServletRegistration.Dynamic { private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationServletRegistration.class); private final ServletInfo servletInfo; - private final List servletMappings = new ArrayList<>(); private final DeploymentInfo deploymentInfo; public ApplicationServletRegistration(DeploymentInfo deploymentInfo, ServletInfo servletInfo) { @@ -77,14 +74,14 @@ public Set addMapping(String... urlPatterns) { return existingMapping; } for (String urlPattern : urlPatterns) { - servletMappings.add(new ServletMappingInfo(servletInfo.getServletName(), urlPattern)); + deploymentInfo.addServletMapping(new ServletMappingInfo(servletInfo.getServletName(), urlPattern)); } return Collections.emptySet(); } @Override public Collection getMappings() { - return servletMappings.stream().map(ServletMappingInfo::getMapping).collect(Collectors.toList()); + return deploymentInfo.getServletMappings().stream().filter(mapping -> mapping.getServletName().equals(servletInfo.getServletName())).map(ServletMappingInfo::getMapping).collect(Collectors.toList()); } @Override diff --git a/servlet-core/src/main/java/tech/smartboot/servlet/impl/ServletContextImpl.java b/servlet-core/src/main/java/tech/smartboot/servlet/impl/ServletContextImpl.java index da4c131..d6782e1 100644 --- a/servlet-core/src/main/java/tech/smartboot/servlet/impl/ServletContextImpl.java +++ b/servlet-core/src/main/java/tech/smartboot/servlet/impl/ServletContextImpl.java @@ -456,7 +456,7 @@ public FilterRegistration.Dynamic addFilter(String filterName, Filter filter) { filterInfo.setFilterClass(filter.getClass().getName()); filterInfo.setDynamic(true); deploymentInfo.addFilter(filterInfo); - return new ApplicationFilterRegistration(filterInfo); + return new ApplicationFilterRegistration(deploymentInfo, filterInfo); } private void checkServletContextState() { @@ -483,14 +483,14 @@ public T createFilter(Class clazz) throws ServletException public FilterRegistration getFilterRegistration(String filterName) { checkContextInitializeState(); Optional optional = deploymentInfo.getFilters().values().stream().filter(filter -> filterName.equals(filter.getFilterName())).findFirst(); - return optional.map(ApplicationFilterRegistration::new).orElse(null); + return optional.map(filterInfo -> new ApplicationFilterRegistration(deploymentInfo, filterInfo)).orElse(null); } @Override public Map getFilterRegistrations() { checkContextInitializeState(); Map filterMap = new HashMap<>(); - deploymentInfo.getFilters().values().forEach(filterInfo -> filterMap.put(filterInfo.getFilterName(), new ApplicationFilterRegistration(filterInfo))); + deploymentInfo.getFilters().values().forEach(filterInfo -> filterMap.put(filterInfo.getFilterName(), new ApplicationFilterRegistration(deploymentInfo, filterInfo))); return filterMap; }