1// 2// ======================================================================== 3// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. 4// ------------------------------------------------------------------------ 5// All rights reserved. This program and the accompanying materials 6// are made available under the terms of the Eclipse Public License v1.0 7// and Apache License v2.0 which accompanies this distribution. 8// 9// The Eclipse Public License is available at 10// http://www.eclipse.org/legal/epl-v10.html 11// 12// The Apache License v2.0 is available at 13// http://www.opensource.org/licenses/apache2.0.php 14// 15// You may elect to redistribute this code under either of these licenses. 16// ======================================================================== 17// 18 19package org.eclipse.jetty.servlet; 20 21import java.io.IOException; 22 23import javax.servlet.GenericServlet; 24import javax.servlet.ServletException; 25import javax.servlet.ServletRequest; 26import javax.servlet.ServletResponse; 27import javax.servlet.http.HttpServletRequest; 28 29import org.eclipse.jetty.server.Dispatcher; 30import org.eclipse.jetty.server.AbstractHttpConnection; 31import org.eclipse.jetty.server.Request; 32import org.eclipse.jetty.server.handler.ContextHandler; 33import org.eclipse.jetty.util.URIUtil; 34import org.eclipse.jetty.util.resource.Resource; 35 36 37/* ------------------------------------------------------------ */ 38/** Servlet handling JSP Property Group mappings 39 * <p> 40 * This servlet is mapped to by any URL pattern for a JSP property group. 41 * Resources handled by this servlet that are not directories will be passed 42 * directly to the JSP servlet. Resources that are directories will be 43 * passed directly to the default servlet. 44 */ 45public class JspPropertyGroupServlet extends GenericServlet 46{ 47 private static final long serialVersionUID = 3681783214726776945L; 48 49 public final static String NAME = "__org.eclipse.jetty.servlet.JspPropertyGroupServlet__"; 50 private final ServletHandler _servletHandler; 51 private final ContextHandler _contextHandler; 52 private ServletHolder _dftServlet; 53 private ServletHolder _jspServlet; 54 private boolean _starJspMapped; 55 56 public JspPropertyGroupServlet(ContextHandler context, ServletHandler servletHandler) 57 { 58 _contextHandler=context; 59 _servletHandler=servletHandler; 60 } 61 62 @Override 63 public void init() throws ServletException 64 { 65 String jsp_name = "jsp"; 66 ServletMapping servlet_mapping =_servletHandler.getServletMapping("*.jsp"); 67 if (servlet_mapping!=null) 68 { 69 _starJspMapped=true; 70 71 //now find the jsp servlet, ignoring the mapping that is for ourself 72 ServletMapping[] mappings = _servletHandler.getServletMappings(); 73 for (ServletMapping m:mappings) 74 { 75 String[] paths = m.getPathSpecs(); 76 if (paths!=null) 77 { 78 for (String path:paths) 79 { 80 if ("*.jsp".equals(path) && !NAME.equals(m.getServletName())) 81 servlet_mapping = m; 82 } 83 } 84 } 85 86 jsp_name=servlet_mapping.getServletName(); 87 } 88 _jspServlet=_servletHandler.getServlet(jsp_name); 89 90 String dft_name="default"; 91 ServletMapping default_mapping=_servletHandler.getServletMapping("/"); 92 if (default_mapping!=null) 93 dft_name=default_mapping.getServletName(); 94 _dftServlet=_servletHandler.getServlet(dft_name); 95 } 96 97 @Override 98 public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException 99 { 100 HttpServletRequest request = null; 101 if (req instanceof HttpServletRequest) 102 request = (HttpServletRequest)req; 103 else 104 throw new ServletException("Request not HttpServletRequest"); 105 106 String servletPath=null; 107 String pathInfo=null; 108 if (request.getAttribute(Dispatcher.INCLUDE_REQUEST_URI)!=null) 109 { 110 servletPath=(String)request.getAttribute(Dispatcher.INCLUDE_SERVLET_PATH); 111 pathInfo=(String)request.getAttribute(Dispatcher.INCLUDE_PATH_INFO); 112 if (servletPath==null) 113 { 114 servletPath=request.getServletPath(); 115 pathInfo=request.getPathInfo(); 116 } 117 } 118 else 119 { 120 servletPath = request.getServletPath(); 121 pathInfo = request.getPathInfo(); 122 } 123 124 String pathInContext=URIUtil.addPaths(servletPath,pathInfo); 125 126 if (pathInContext.endsWith("/")) 127 { 128 _dftServlet.getServlet().service(req,res); 129 } 130 else if (_starJspMapped && pathInContext.toLowerCase().endsWith(".jsp")) 131 { 132 _jspServlet.getServlet().service(req,res); 133 } 134 else 135 { 136 137 Resource resource = _contextHandler.getResource(pathInContext); 138 if (resource!=null && resource.isDirectory()) 139 _dftServlet.getServlet().service(req,res); 140 else 141 _jspServlet.getServlet().service(req,res); 142 } 143 144 } 145 146} 147