From 7d6897fa3684044a5e08de289e2c20d5271af809 Mon Sep 17 00:00:00 2001 From: Redkale <8730487+redkale@users.noreply.github.com> Date: Thu, 6 Sep 2018 14:33:53 +0800 Subject: [PATCH] --- src/org/redkale/net/http/HttpServlet.java | 151 ++++++++++++---------- 1 file changed, 83 insertions(+), 68 deletions(-) diff --git a/src/org/redkale/net/http/HttpServlet.java b/src/org/redkale/net/http/HttpServlet.java index 423b48414..9bfb885ec 100644 --- a/src/org/redkale/net/http/HttpServlet.java +++ b/src/org/redkale/net/http/HttpServlet.java @@ -34,13 +34,15 @@ public class HttpServlet extends Servlet String _prefix = ""; //当前HttpServlet的path前缀 - private Map.Entry[] mappings; + HashMap _tmpentrys; //Rest生成时赋值, 字段名Rest有用到 + + private Map.Entry[] mappings; //字段名Rest有用到 //这里不能直接使用HttpServlet,会造成死循环初始化HttpServlet private final Servlet authSuccessServlet = new Servlet() { @Override public void execute(HttpRequest request, HttpResponse response) throws IOException { - Entry entry = (Entry) request.attachment; + InnerActionEntry entry = (InnerActionEntry) request.attachment; if (entry.cacheseconds > 0) {//有缓存设置 CacheEntry ce = entry.cache.get(request.getRequestURI()); if (ce != null && ce.time + entry.cacheseconds > System.currentTimeMillis()) { //缓存有效 @@ -59,9 +61,9 @@ public class HttpServlet extends Servlet private final Servlet preSuccessServlet = new Servlet() { @Override public void execute(HttpRequest request, HttpResponse response) throws IOException { - for (Map.Entry en : mappings) { + for (Map.Entry en : mappings) { if (request.getRequestURI().startsWith(en.getKey())) { - Entry entry = en.getValue(); + InnerActionEntry entry = en.getValue(); if (!entry.checkMethod(request.getMethod())) { response.finishJson(new RetResult(RET_METHOD_ERROR, "Method(" + request.getMethod() + ") Error")); return; @@ -69,11 +71,11 @@ public class HttpServlet extends Servlet request.attachment = entry; request.moduleid = entry.moduleid; request.actionid = entry.actionid; - if (entry.ignore) { - authSuccessServlet.execute(request, response); - } else { + if (entry.auth) { response.thenEvent(authSuccessServlet); authenticate(request, response); + } else { + authSuccessServlet.execute(request, response); } return; } @@ -88,10 +90,10 @@ public class HttpServlet extends Servlet String path = _prefix == null ? "" : _prefix; WebServlet ws = this.getClass().getAnnotation(WebServlet.class); if (ws != null && !ws.repair()) path = ""; - HashMap map = load(); + HashMap map = this._tmpentrys != null ? this._tmpentrys : loadActionEntry(); this.mappings = new Map.Entry[map.size()]; int i = -1; - for (Map.Entry en : map.entrySet()) { + for (Map.Entry en : map.entrySet()) { mappings[++i] = new AbstractMap.SimpleEntry<>(path + en.getKey(), en.getValue()); } //必须要倒排序, /query /query1 /query12 确保含子集的优先匹配 /query12 /query1 /query @@ -164,10 +166,10 @@ public class HttpServlet extends Servlet preExecute(request, response); } - private HashMap load() { + private HashMap loadActionEntry() { WebServlet module = this.getClass().getAnnotation(WebServlet.class); final int serviceid = module == null ? 0 : module.moduleid(); - final HashMap map = new HashMap<>(); + final HashMap map = new HashMap<>(); HashMap nameset = new HashMap<>(); final Class selfClz = this.getClass(); Class clz = this.getClass(); @@ -198,13 +200,81 @@ public class HttpServlet extends Servlet throw new RuntimeException(this.getClass().getSimpleName() + " have two same " + HttpMapping.class.getSimpleName() + "(" + name + ")"); } nameset.put(name, clz); - map.put(name, new Entry(serviceid, actionid, name, methods, method, createHttpServlet(method))); + map.put(name, new InnerActionEntry(serviceid, actionid, name, methods, method, createActionServlet(method))); } } while ((clz = clz.getSuperclass()) != HttpServlet.class); return map; } - private HttpServlet createHttpServlet(final Method method) { + protected static final class InnerActionEntry { + + InnerActionEntry(int moduleid, int actionid, String name, String[] methods, Method method, HttpServlet servlet) { + this(moduleid, actionid, name, methods, method, auth(method), cacheseconds(method), servlet); + } + + public InnerActionEntry(int moduleid, int actionid, String name, String[] methods, Method method, boolean auth, int cacheseconds, HttpServlet servlet) { + this.moduleid = moduleid; + this.actionid = actionid; + this.name = name; + this.methods = methods; + this.method = method; //rest构建会为null + this.servlet = servlet; + this.auth = auth; + this.cacheseconds = cacheseconds; + this.cache = cacheseconds > 0 ? new ConcurrentHashMap<>() : null; + this.cacheHandler = cacheseconds > 0 ? (HttpResponse response, ByteBuffer[] buffers) -> { + int status = response.getStatus(); + if (status != 200) return null; + CacheEntry ce = new CacheEntry(response.getStatus(), response.getContentType(), buffers); + cache.put(response.getRequest().getRequestURI(), ce); + return ce.getBuffers(); + } : null; + } + + private static boolean auth(Method method) { + HttpMapping mapping = method.getAnnotation(HttpMapping.class); + return mapping == null || mapping.auth(); + } + + private static int cacheseconds(Method method) { + HttpMapping mapping = method.getAnnotation(HttpMapping.class); + return mapping == null ? 0 : mapping.cacheseconds(); + } + + boolean isNeedCheck() { + return this.moduleid != 0 || this.actionid != 0; + } + + boolean checkMethod(final String reqMethod) { + if (methods.length == 0) return true; + for (String m : methods) { + if (reqMethod.equalsIgnoreCase(m)) return true; + } + return false; + } + + final BiFunction cacheHandler; + + final ConcurrentHashMap cache; + + final int cacheseconds; + + final boolean auth; + + final int moduleid; + + final int actionid; + + final String name; + + final String[] methods; + + final Method method; + + final HttpServlet servlet; + } + + private HttpServlet createActionServlet(final Method method) { //------------------------------------------------------------------------------ final String supDynName = HttpServlet.class.getName().replace('.', '/'); final String interName = this.getClass().getName().replace('.', '/'); @@ -283,61 +353,6 @@ public class HttpServlet extends Servlet } } - private static final class Entry { - - public Entry(int moduleid, int actionid, String name, String[] methods, Method method, HttpServlet servlet) { - this.moduleid = moduleid; - this.actionid = actionid; - this.name = name; - this.methods = methods; - this.method = method; - this.servlet = servlet; - HttpMapping mapping = method.getAnnotation(HttpMapping.class); - this.ignore = mapping == null || !mapping.auth(); - this.cacheseconds = mapping == null ? 0 : mapping.cacheseconds(); - this.cache = cacheseconds > 0 ? new ConcurrentHashMap<>() : null; - this.cacheHandler = cacheseconds > 0 ? (HttpResponse response, ByteBuffer[] buffers) -> { - int status = response.getStatus(); - if (status != 200) return null; - CacheEntry ce = new CacheEntry(response.getStatus(), response.getContentType(), buffers); - cache.put(response.getRequest().getRequestURI(), ce); - return ce.getBuffers(); - } : null; - } - - public boolean isNeedCheck() { - return this.moduleid != 0 || this.actionid != 0; - } - - public boolean checkMethod(final String reqMethod) { - if (methods.length == 0) return true; - for (String m : methods) { - if (reqMethod.equalsIgnoreCase(m)) return true; - } - return false; - } - - public final BiFunction cacheHandler; - - public final ConcurrentHashMap cache; - - public final int cacheseconds; - - public final boolean ignore; - - public final int moduleid; - - public final int actionid; - - public final String name; - - public final String[] methods; - - public final Method method; - - public final HttpServlet servlet; - } - private static final class CacheEntry { public final long time = System.currentTimeMillis();