Skip to content

Commit

Permalink
代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
smthing committed Aug 12, 2024
1 parent f51c941 commit 63fa90d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,34 +33,35 @@ public class ApplicationFilterRegistration implements FilterRegistration.Dynamic


private final FilterInfo filterDef;
private final List<FilterMappingInfo> 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<DispatcherType> 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<DispatcherType> 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<String> 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<String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<ServletMappingInfo> servletMappings = new ArrayList<>();
private final DeploymentInfo deploymentInfo;

public ApplicationServletRegistration(DeploymentInfo deploymentInfo, ServletInfo servletInfo) {
Expand Down Expand Up @@ -77,14 +74,14 @@ public Set<String> 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<String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -483,14 +483,14 @@ public <T extends Filter> T createFilter(Class<T> clazz) throws ServletException
public FilterRegistration getFilterRegistration(String filterName) {
checkContextInitializeState();
Optional<FilterInfo> 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<String, ? extends FilterRegistration> getFilterRegistrations() {
checkContextInitializeState();
Map<String, ApplicationFilterRegistration> 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;
}

Expand Down

0 comments on commit 63fa90d

Please sign in to comment.