From a65adcdd38330433dda43c70b813c52e550e4b98 Mon Sep 17 00:00:00 2001 From: Jan Bartel Date: Mon, 25 Feb 2013 17:45:22 +1100 Subject: [PATCH] 401531 StringIndexOutOfBoundsException for "/*" of --- .../servlet/JspPropertyGroupServlet.java | 120 ++++++++++++++++++ .../webapp/StandardDescriptorProcessor.java | 25 ++-- 2 files changed, 132 insertions(+), 13 deletions(-) create mode 100644 jetty-servlet/src/main/java/org/eclipse/jetty/servlet/JspPropertyGroupServlet.java diff --git a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/JspPropertyGroupServlet.java b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/JspPropertyGroupServlet.java new file mode 100644 index 00000000000..58f65b68798 --- /dev/null +++ b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/JspPropertyGroupServlet.java @@ -0,0 +1,120 @@ +// +// ======================================================================== +// Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.servlet; + +import java.io.IOException; + +import javax.servlet.GenericServlet; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; + +import org.eclipse.jetty.server.AbstractHttpConnection; +import org.eclipse.jetty.server.Dispatcher; +import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.handler.ContextHandler; +import org.eclipse.jetty.util.URIUtil; +import org.eclipse.jetty.util.resource.Resource; + + +/* ------------------------------------------------------------ */ +/** Servlet handling JSP Property Group mappings + *

+ * This servlet is mapped to by any URL pattern for a JSP property group. + * Resources handled by this servlet that are not directories will be passed + * directly to the JSP servlet. Resources that are directories will be + * passed directly to the default servlet. + */ +public class JspPropertyGroupServlet extends GenericServlet +{ + private static final long serialVersionUID = 3681783214726776945L; + + public final static String NAME = "__org.eclipse.jetty.servlet.JspPropertyGroupServlet__"; + private final ServletHandler _servletHandler; + private final ContextHandler _contextHandler; + private ServletHolder _dftServlet; + private ServletHolder _jspServlet; + private boolean _starJspMapped; + + public JspPropertyGroupServlet(ContextHandler context, ServletHandler servletHandler) + { + _contextHandler=context; + _servletHandler=servletHandler; + } + + @Override + public void init() throws ServletException + { + String jsp_name = "jsp"; + ServletMapping servlet_mapping=_servletHandler.getServletMapping("*.jsp"); + if (servlet_mapping!=null) + { + _starJspMapped=true; + jsp_name=servlet_mapping.getServletName(); + } + _jspServlet=_servletHandler.getServlet(jsp_name); + + String dft_name="default"; + ServletMapping default_mapping=_servletHandler.getServletMapping("/"); + if (default_mapping!=null) + dft_name=default_mapping.getServletName(); + _dftServlet=_servletHandler.getServlet(dft_name); + } + + @Override + public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException + { + Request request=(req instanceof Request)?(Request)req:AbstractHttpConnection.getCurrentConnection().getRequest(); + + String servletPath=null; + String pathInfo=null; + if (request.getAttribute(Dispatcher.INCLUDE_REQUEST_URI)!=null) + { + servletPath=(String)request.getAttribute(Dispatcher.INCLUDE_SERVLET_PATH); + pathInfo=(String)request.getAttribute(Dispatcher.INCLUDE_PATH_INFO); + if (servletPath==null) + { + servletPath=request.getServletPath(); + pathInfo=request.getPathInfo(); + } + } + else + { + servletPath = request.getServletPath(); + pathInfo = request.getPathInfo(); + } + + String pathInContext=URIUtil.addPaths(servletPath,pathInfo); + + if (pathInContext.endsWith("/")) + _dftServlet.getServlet().service(req,res); + else if (_starJspMapped && pathInContext.toLowerCase().endsWith(".jsp")) + _jspServlet.getServlet().service(req,res); + else + { + Resource resource = _contextHandler.getResource(pathInContext); + if (resource!=null && resource.isDirectory()) + _dftServlet.getServlet().service(req,res); + else + _jspServlet.getServlet().service(req,res); + } + + } + +} diff --git a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/StandardDescriptorProcessor.java b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/StandardDescriptorProcessor.java index f47d31ee76c..5fc3be0af72 100644 --- a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/StandardDescriptorProcessor.java +++ b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/StandardDescriptorProcessor.java @@ -39,7 +39,9 @@ import org.eclipse.jetty.server.DispatcherType; import org.eclipse.jetty.servlet.ErrorPageErrorHandler; import org.eclipse.jetty.servlet.FilterHolder; import org.eclipse.jetty.servlet.FilterMapping; +import org.eclipse.jetty.servlet.JspPropertyGroupServlet; import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.servlet.ServletHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.servlet.ServletMapping; import org.eclipse.jetty.util.LazyList; @@ -866,21 +868,18 @@ public class StandardDescriptorProcessor extends IterativeDescriptorProcessor if (paths.size() > 0) { - String jspName = "jsp"; - Map.Entry entry = context.getServletHandler().getHolderEntry("test.jsp"); - if (entry != null) + ServletHandler handler = context.getServletHandler(); + ServletHolder jsp_pg_servlet = handler.getServlet(JspPropertyGroupServlet.NAME); + if (jsp_pg_servlet==null) { - ServletHolder holder = (ServletHolder) entry.getValue(); - jspName = holder.getName(); - } - - if (jspName != null) - { - ServletMapping mapping = new ServletMapping(); - mapping.setServletName(jspName); - mapping.setPathSpecs(paths.toArray(new String[paths.size()])); - context.getServletHandler().addServletMapping(mapping); + jsp_pg_servlet=new ServletHolder(JspPropertyGroupServlet.NAME,new JspPropertyGroupServlet(context,handler)); + handler.addServlet(jsp_pg_servlet); } + + ServletMapping mapping = new ServletMapping(); + mapping.setServletName(JspPropertyGroupServlet.NAME); + mapping.setPathSpecs(paths.toArray(new String[paths.size()])); + context.getServletHandler().addServletMapping(mapping); } }