From 5493c9e596c5fd0ebee0c26f4fe1106388cecd55 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Mon, 1 May 2017 16:38:55 +0200 Subject: [PATCH] Refactor ServletHandler to add getMappedServlet method (#1519) --- .../jetty/quickstart/TestQuickStart.java | 5 +- .../eclipse/jetty/servlet/DefaultServlet.java | 2 +- .../org/eclipse/jetty/servlet/Invoker.java | 6 +- .../eclipse/jetty/servlet/ServletHandler.java | 65 ++++++++++++------- .../jetty/servlet/ServletHandlerTest.java | 4 +- 5 files changed, 48 insertions(+), 34 deletions(-) diff --git a/jetty-quickstart/src/test/java/org/eclipse/jetty/quickstart/TestQuickStart.java b/jetty-quickstart/src/test/java/org/eclipse/jetty/quickstart/TestQuickStart.java index 35344441fce..c414bacaed5 100644 --- a/jetty-quickstart/src/test/java/org/eclipse/jetty/quickstart/TestQuickStart.java +++ b/jetty-quickstart/src/test/java/org/eclipse/jetty/quickstart/TestQuickStart.java @@ -22,14 +22,11 @@ package org.eclipse.jetty.quickstart; import static org.junit.Assert.*; import java.io.File; -import java.nio.file.Path; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.toolchain.test.FS; import org.eclipse.jetty.toolchain.test.MavenTestingUtils; -import org.eclipse.jetty.util.resource.Resource; -import org.eclipse.jetty.webapp.WebAppContext; import org.junit.Before; import org.junit.Test; @@ -90,7 +87,7 @@ public class TestQuickStart server.start(); //verify that FooServlet is now mapped to / and not the DefaultServlet - ServletHolder sh = webapp.getServletHandler().getHolderEntry("/").getResource(); + ServletHolder sh = webapp.getServletHandler().getMappedServlet("/").getResource(); assertNotNull(sh); assertEquals("foo", sh.getName()); server.stop(); diff --git a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/DefaultServlet.java b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/DefaultServlet.java index ff36a22c1f5..51f7d8abb5c 100644 --- a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/DefaultServlet.java +++ b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/DefaultServlet.java @@ -513,7 +513,7 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory, Welc if ((_welcomeServlets || _welcomeExactServlets) && welcome_servlet==null) { - MappedResource entry=_servletHandler.getHolderEntry(welcome_in_context); + MappedResource entry=_servletHandler.getMappedServlet(welcome_in_context); if (entry!=null && entry.getResource()!=_defaultHolder && (_welcomeServlets || (_welcomeExactServlets && entry.getPathSpec().getDeclaration().equals(welcome_in_context)))) welcome_servlet=welcome_in_context; diff --git a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/Invoker.java b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/Invoker.java index 1d1a2d016df..82e58e6c3ea 100644 --- a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/Invoker.java +++ b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/Invoker.java @@ -32,7 +32,6 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletResponse; -import org.eclipse.jetty.http.PathMap.MappedEntry; import org.eclipse.jetty.http.pathmap.MappedResource; import org.eclipse.jetty.server.Dispatcher; import org.eclipse.jetty.server.Handler; @@ -66,6 +65,7 @@ import org.eclipse.jetty.util.log.Logger; * @version $Id: Invoker.java 4780 2009-03-17 15:36:08Z jesse $ * */ +@SuppressWarnings("serial") public class Invoker extends HttpServlet { private static final Logger LOG = Log.getLogger(Invoker.class); @@ -168,11 +168,11 @@ public class Invoker extends HttpServlet synchronized(_servletHandler) { // find the entry for the invoker (me) - _invokerEntry=_servletHandler.getHolderEntry(servlet_path); + _invokerEntry=_servletHandler.getMappedServlet(servlet_path); // Check for existing mapping (avoid threaded race). String path=URIUtil.addPaths(servlet_path,servlet); - MappedResource entry = _servletHandler.getHolderEntry(path); + MappedResource entry = _servletHandler.getMappedServlet(path); if (entry!=null && !entry.equals(_invokerEntry)) { diff --git a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletHandler.java b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletHandler.java index 11a2c36f314..92dabbd00c2 100644 --- a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletHandler.java +++ b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletHandler.java @@ -24,7 +24,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.EnumSet; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.ListIterator; import java.util.Map; @@ -53,8 +52,6 @@ import org.eclipse.jetty.http.pathmap.MappedResource; import org.eclipse.jetty.http.pathmap.PathMappings; import org.eclipse.jetty.http.pathmap.PathSpec; import org.eclipse.jetty.http.pathmap.ServletPathSpec; -import org.eclipse.jetty.http.pathmap.PathSpec; -import org.eclipse.jetty.http.pathmap.ServletPathSpec; import org.eclipse.jetty.security.IdentityService; import org.eclipse.jetty.security.SecurityHandler; import org.eclipse.jetty.server.Request; @@ -361,14 +358,16 @@ public class ServletHandler extends ScopedHandler /** * ServletHolder matching path. * - * @param pathInContext Path within _context. + * @param target Path within _context or servlet name * @return PathMap Entries pathspec to ServletHolder + * @deprecated Use {@link #getMappedServlet(String)} */ - public MappedResource getHolderEntry(String pathInContext) + @Deprecated + public MappedResource getHolderEntry(String target) { - if (_servletPathMap==null) - return null; - return _servletPathMap.getMatch(pathInContext); + if (target.startsWith("/")) + return getMappedServlet(target); + return null; } /* ------------------------------------------------------------ */ @@ -438,16 +437,14 @@ public class ServletHandler extends ScopedHandler ServletHolder servlet_holder=null; UserIdentity.Scope old_scope=null; - // find the servlet - if (target.startsWith("/")) + MappedResource mapping=getMappedServlet(target); + if (mapping!=null) { - // Look for the servlet by path - MappedResource entry=getHolderEntry(target); - if (entry!=null) + servlet_holder = mapping.getResource(); + + if (mapping.getPathSpec()!=null) { - PathSpec pathSpec = entry.getPathSpec(); - servlet_holder=entry.getResource(); - + PathSpec pathSpec = mapping.getPathSpec(); String servlet_path=pathSpec.getPathMatch(target); String path_info=pathSpec.getPathInfo(target); @@ -463,12 +460,7 @@ public class ServletHandler extends ScopedHandler } } } - else - { - // look for a servlet by name! - servlet_holder= _servletNameMap.get(target); - } - + if (LOG.isDebugEnabled()) LOG.debug("servlet {}|{}|{} -> {}",baseRequest.getContextPath(),baseRequest.getServletPath(),baseRequest.getPathInfo(),servlet_holder); @@ -549,7 +541,33 @@ public class ServletHandler extends ScopedHandler baseRequest.setHandled(true); } } + + /* ------------------------------------------------------------ */ + /** + * ServletHolder matching path. + * + * @param target Path within _context or servlet name + * @return MappedResource to the ServletHolder. Named servlets have a null PathSpec + */ + public MappedResource getMappedServlet(String target) + { + if (target.startsWith("/")) + { + if (_servletPathMap==null) + return null; + return _servletPathMap.getMatch(target); + } + + if (_servletNameMap==null) + return null; + ServletHolder holder = _servletNameMap.get(target); + if (holder==null) + return null; + return new MappedResource<>(null,holder); + } + + /* ------------------------------------------------------------ */ protected FilterChain getFilterChain(Request baseRequest, String pathInContext, ServletHolder servletHolder) { String key=pathInContext==null?servletHolder.getName():pathInContext; @@ -703,8 +721,6 @@ public class ServletHandler extends ScopedHandler return _startWithUnavailable; } - - /* ------------------------------------------------------------ */ /** Initialize filters and load-on-startup servlets. * @throws Exception if unable to initialize @@ -1765,6 +1781,7 @@ public class ServletHandler extends ScopedHandler /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ + @SuppressWarnings("serial") public static class Default404Servlet extends HttpServlet { @Override diff --git a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/ServletHandlerTest.java b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/ServletHandlerTest.java index 22edca076e9..c7710b49427 100644 --- a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/ServletHandlerTest.java +++ b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/ServletHandlerTest.java @@ -267,7 +267,7 @@ public class ServletHandlerTest handler.updateMappings(); - MappedResource entry=handler.getHolderEntry("/foo/*"); + MappedResource entry=handler.getMappedServlet("/foo/*"); assertNotNull(entry); assertEquals("s1", entry.getResource().getName()); } @@ -312,7 +312,7 @@ public class ServletHandlerTest handler.addServletMapping(sm2); handler.updateMappings(); - MappedResource entry=handler.getHolderEntry("/foo/*"); + MappedResource entry=handler.getMappedServlet("/foo/*"); assertNotNull(entry); assertEquals("s2", entry.getResource().getName()); }