From 6ee9527ed9f853a9446c7636a1279482f3a83d47 Mon Sep 17 00:00:00 2001 From: Redkale <8730487+redkale@users.noreply.github.com> Date: Wed, 9 Sep 2020 19:17:04 +0800 Subject: [PATCH] =?UTF-8?q?HttpServlet=20=E4=B8=8D=E5=86=8D=E6=94=AF?= =?UTF-8?q?=E6=8C=81/aa/bbcc=20=E8=83=BD=E5=8C=B9=E9=85=8D=E5=88=B0=20/aa/?= =?UTF-8?q?bb?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/org/redkale/net/http/HttpServlet.java | 47 ++++++++++++++--------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/src/org/redkale/net/http/HttpServlet.java b/src/org/redkale/net/http/HttpServlet.java index 0a8e912d8..2096e6070 100644 --- a/src/org/redkale/net/http/HttpServlet.java +++ b/src/org/redkale/net/http/HttpServlet.java @@ -39,6 +39,8 @@ public class HttpServlet extends Servlet private Map.Entry[] mappings; //字段名Rest有用到 + private Map mappingMap; + //这里不能直接使用HttpServlet,会造成死循环初始化HttpServlet private final Servlet authSuccessServlet = new Servlet() { @Override @@ -66,28 +68,34 @@ 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) { - if (request.getRequestURI().startsWith(en.getKey())) { - InnerActionEntry entry = en.getValue(); - if (!entry.checkMethod(request.getMethod())) { - response.finishJson(new RetResult(RET_METHOD_ERROR, "Method(" + request.getMethod() + ") Error")); - return; + InnerActionEntry entry = mappingMap.get(request.getRequestURI()); + if (entry == null) { + for (Map.Entry en : mappings) { + if (request.getRequestURI().startsWith(en.getKey() + "/")) { + entry = en.getValue(); + break; } - request.attachment = entry; - request.moduleid = entry.moduleid; - request.actionid = entry.actionid; - request.annotations = entry.annotations; - if (entry.auth) { - response.thenEvent(authSuccessServlet); - authenticate(request, response); - } else { - authSuccessServlet.execute(request, response); - } - return; } } - response.finish404(); - //throw new IOException(this.getClass().getName() + " not found method for URI(" + request.getRequestURI() + ")"); + if (entry == null) { + response.finish404(); + //throw new IOException(this.getClass().getName() + " not found method for URI(" + request.getRequestURI() + ")"); + return; + } + if (!entry.checkMethod(request.getMethod())) { + response.finishJson(new RetResult(RET_METHOD_ERROR, "Method(" + request.getMethod() + ") Error")); + return; + } + request.attachment = entry; + request.moduleid = entry.moduleid; + request.actionid = entry.actionid; + request.annotations = entry.annotations; + if (entry.auth) { + response.thenEvent(authSuccessServlet); + authenticate(request, response); + } else { + authSuccessServlet.execute(request, response); + } } }; @@ -98,6 +106,7 @@ public class HttpServlet extends Servlet WebServlet ws = this.getClass().getAnnotation(WebServlet.class); if (ws != null && !ws.repair()) path = ""; HashMap map = this._tmpentrys != null ? this._tmpentrys : loadActionEntry(); + this.mappingMap = map; this.mappings = new Map.Entry[map.size()]; int i = -1; for (Map.Entry en : map.entrySet()) {