From 42d6df69e71675cca932ef51dde80db65f65e923 Mon Sep 17 00:00:00 2001 From: Wendal Chen Date: Thu, 23 May 2019 21:52:00 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E6=B7=BB=E5=8A=A0=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E5=B8=AE=E5=8A=A9=E7=B1=BBWebFilterReg=E5=92=8CWebServletReg?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/nutz/boot/annotation/PropDoc.java | 4 + .../org/nutz/boot/starter/WebServletFace.java | 17 ++- .../nutz/boot/starter/impl/WebFilterReg.java | 104 ++++++++++++++++++ .../nutz/boot/starter/impl/WebServletReg.java | 95 ++++++++++++++++ .../servlet3/NbServletContextListener.java | 6 + 5 files changed, 224 insertions(+), 2 deletions(-) create mode 100644 nutzboot-core/src/main/java/org/nutz/boot/starter/impl/WebFilterReg.java create mode 100644 nutzboot-core/src/main/java/org/nutz/boot/starter/impl/WebServletReg.java diff --git a/nutzboot-core/src/main/java/org/nutz/boot/annotation/PropDoc.java b/nutzboot-core/src/main/java/org/nutz/boot/annotation/PropDoc.java index 6980f7a0..eed6374d 100644 --- a/nutzboot-core/src/main/java/org/nutz/boot/annotation/PropDoc.java +++ b/nutzboot-core/src/main/java/org/nutz/boot/annotation/PropDoc.java @@ -6,6 +6,10 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +/** + * 配置文档自动生成 + * + */ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD}) @Documented diff --git a/nutzboot-core/src/main/java/org/nutz/boot/starter/WebServletFace.java b/nutzboot-core/src/main/java/org/nutz/boot/starter/WebServletFace.java index 2a79991c..c2b31942 100644 --- a/nutzboot-core/src/main/java/org/nutz/boot/starter/WebServletFace.java +++ b/nutzboot-core/src/main/java/org/nutz/boot/starter/WebServletFace.java @@ -3,6 +3,7 @@ package org.nutz.boot.starter; import java.util.HashMap; import java.util.Map; +import javax.servlet.MultipartConfigElement; import javax.servlet.Servlet; import javax.servlet.ServletContext; @@ -11,9 +12,9 @@ public interface WebServletFace { String getName(); String getPathSpec(); - + default String[] getPathSpecs() { - return new String[] {getPathSpec()}; + return new String[]{getPathSpec()}; } Servlet getServlet(); @@ -25,4 +26,16 @@ public interface WebServletFace { default void setServletContext(ServletContext sc) { } + + default boolean isAsyncSupported() { + return false; + } + + default MultipartConfigElement getMultipartConfig() { + return null; + } + + default int getLoadOnStartup() { + return 1; + } } diff --git a/nutzboot-core/src/main/java/org/nutz/boot/starter/impl/WebFilterReg.java b/nutzboot-core/src/main/java/org/nutz/boot/starter/impl/WebFilterReg.java new file mode 100644 index 00000000..5bf6aa4b --- /dev/null +++ b/nutzboot-core/src/main/java/org/nutz/boot/starter/impl/WebFilterReg.java @@ -0,0 +1,104 @@ +package org.nutz.boot.starter.impl; + +import java.util.EnumSet; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import javax.servlet.DispatcherType; +import javax.servlet.Filter; + +import org.nutz.boot.starter.WebFilterFace; + +/** + * 写个@IocBean工厂方法, 配置并返回本对象, 就可以了 + * + */ +public class WebFilterReg implements WebFilterFace { + + protected String name; + + protected String[] pathSpecs; + + protected Set dispatcheTypes = new HashSet<>(); + + protected Filter filter; + + protected Map initParameters = new HashMap<>(); + + protected int order; + + public WebFilterReg() { + dispatcheTypes.add(DispatcherType.REQUEST); + } + + public WebFilterReg(String name, Filter filter, String pathSpec) { + this(); + this.name = name; + this.filter = filter; + this.pathSpecs = new String[]{pathSpec}; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String[] getPathSpecs() { + return pathSpecs; + } + + public void setPathSpecs(String[] pathSpecs) { + this.pathSpecs = pathSpecs; + } + + public void setDispatcheTypes(Set dispatcheTypes) { + this.dispatcheTypes = dispatcheTypes; + } + + public Set getDispatcheTypes() { + return dispatcheTypes; + } + + public Filter getFilter() { + return filter; + } + + public void setFilter(Filter filter) { + this.filter = filter; + } + + public Map getInitParameters() { + return initParameters; + } + + public void setInitParameters(Map initParameters) { + this.initParameters = initParameters; + } + + public int getOrder() { + return order; + } + + public void setOrder(int order) { + this.order = order; + } + + @Override + public EnumSet getDispatches() { + return EnumSet.copyOf(dispatcheTypes); + } + + public void addInitParameters(String key, String value) { + initParameters.put(key, value); + } + + @Override + public String getPathSpec() { + return pathSpecs == null || pathSpecs.length == 0 ? null : pathSpecs[0]; + } +} diff --git a/nutzboot-core/src/main/java/org/nutz/boot/starter/impl/WebServletReg.java b/nutzboot-core/src/main/java/org/nutz/boot/starter/impl/WebServletReg.java new file mode 100644 index 00000000..8c12e72b --- /dev/null +++ b/nutzboot-core/src/main/java/org/nutz/boot/starter/impl/WebServletReg.java @@ -0,0 +1,95 @@ +package org.nutz.boot.starter.impl; + +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.MultipartConfigElement; +import javax.servlet.Servlet; + +import org.nutz.boot.starter.WebServletFace; + +public class WebServletReg implements WebServletFace { + + protected String name; + + protected String pathSpec; + + protected Servlet servlet; + + protected Map initParameters = new HashMap<>(); + + protected boolean asyncSupported; + + protected MultipartConfigElement multipartConfig; + + protected int loadOnStartup = 1; + + public WebServletReg() { + } + + public WebServletReg(String name, Servlet servlet, String pathSpec) { + this.name = name; + this.servlet = servlet; + this.pathSpec = pathSpec; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPathSpec() { + return pathSpec; + } + + public void setPathSpec(String pathSpec) { + this.pathSpec = pathSpec; + } + + public Servlet getServlet() { + return servlet; + } + + public void setServlet(Servlet servlet) { + this.servlet = servlet; + } + + public Map getInitParameters() { + return initParameters; + } + + public void setInitParameters(Map initParameters) { + this.initParameters = initParameters; + } + + public void addInitParameters(String key, String value) { + initParameters.put(key, value); + } + + public boolean isAsyncSupported() { + return asyncSupported; + } + + public void setAsyncSupported(boolean asyncSupported) { + this.asyncSupported = asyncSupported; + } + + public MultipartConfigElement getMultipartConfig() { + return multipartConfig; + } + + public void setMultipartConfig(MultipartConfigElement multipartConfig) { + this.multipartConfig = multipartConfig; + } + + public int getLoadOnStartup() { + return loadOnStartup; + } + + public void setLoadOnStartup(int loadOnStartup) { + this.loadOnStartup = loadOnStartup; + } +} diff --git a/nutzboot-starter/nutzboot-servlet3/src/main/java/org/nutz/boot/starter/servlet3/NbServletContextListener.java b/nutzboot-starter/nutzboot-servlet3/src/main/java/org/nutz/boot/starter/servlet3/NbServletContextListener.java index 6bc257c0..0c509bc8 100644 --- a/nutzboot-starter/nutzboot-servlet3/src/main/java/org/nutz/boot/starter/servlet3/NbServletContextListener.java +++ b/nutzboot-starter/nutzboot-servlet3/src/main/java/org/nutz/boot/starter/servlet3/NbServletContextListener.java @@ -7,6 +7,7 @@ import java.util.EventListener; import java.util.LinkedList; import java.util.List; +import javax.servlet.MultipartConfigElement; import javax.servlet.Servlet; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; @@ -99,6 +100,11 @@ public class NbServletContextListener implements ServletContextListener { dyna.addMapping(pathSpec); } dyna.setInitParameters(face.getInitParameters()); + dyna.setAsyncSupported(face.isAsyncSupported()); + MultipartConfigElement multipartConfig = face.getMultipartConfig(); + if (multipartConfig != null) + dyna.setMultipartConfig(multipartConfig); + dyna.setLoadOnStartup(face.getLoadOnStartup()); }); // 注册监听器 appContext.getBeans(WebEventListenerFace.class).forEach((face) -> {