2 // ========================================================================
3 // Copyright (c) 1995-2016 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.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.servlet;
21 import java.io.IOException;
22 import java.util.Locale;
24 import javax.servlet.GenericServlet;
25 import javax.servlet.ServletException;
26 import javax.servlet.ServletRequest;
27 import javax.servlet.ServletResponse;
28 import javax.servlet.http.HttpServletRequest;
30 import org.eclipse.jetty.server.Dispatcher;
31 import org.eclipse.jetty.server.handler.ContextHandler;
32 import org.eclipse.jetty.util.URIUtil;
33 import org.eclipse.jetty.util.resource.Resource;
36 /* ------------------------------------------------------------ */
37 /** Servlet handling JSP Property Group mappings
39 * This servlet is mapped to by any URL pattern for a JSP property group.
40 * Resources handled by this servlet that are not directories will be passed
41 * directly to the JSP servlet. Resources that are directories will be
42 * passed directly to the default servlet.
44 public class JspPropertyGroupServlet extends GenericServlet
46 private static final long serialVersionUID = 3681783214726776945L;
48 public final static String NAME = "__org.eclipse.jetty.servlet.JspPropertyGroupServlet__";
49 private final ServletHandler _servletHandler;
50 private final ContextHandler _contextHandler;
51 private ServletHolder _dftServlet;
52 private ServletHolder _jspServlet;
53 private boolean _starJspMapped;
55 public JspPropertyGroupServlet(ContextHandler context, ServletHandler servletHandler)
57 _contextHandler=context;
58 _servletHandler=servletHandler;
62 public void init() throws ServletException
64 String jsp_name = "jsp";
65 ServletMapping servlet_mapping =_servletHandler.getServletMapping("*.jsp");
66 if (servlet_mapping!=null)
70 //now find the jsp servlet, ignoring the mapping that is for ourself
71 ServletMapping[] mappings = _servletHandler.getServletMappings();
72 for (ServletMapping m:mappings)
74 String[] paths = m.getPathSpecs();
77 for (String path:paths)
79 if ("*.jsp".equals(path) && !NAME.equals(m.getServletName()))
85 jsp_name=servlet_mapping.getServletName();
87 _jspServlet=_servletHandler.getServlet(jsp_name);
89 String dft_name="default";
90 ServletMapping default_mapping=_servletHandler.getServletMapping("/");
91 if (default_mapping!=null)
92 dft_name=default_mapping.getServletName();
93 _dftServlet=_servletHandler.getServlet(dft_name);
97 public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
99 HttpServletRequest request = null;
100 if (req instanceof HttpServletRequest)
101 request = (HttpServletRequest)req;
103 throw new ServletException("Request not HttpServletRequest");
105 String servletPath=null;
106 String pathInfo=null;
107 if (request.getAttribute(Dispatcher.INCLUDE_REQUEST_URI)!=null)
109 servletPath=(String)request.getAttribute(Dispatcher.INCLUDE_SERVLET_PATH);
110 pathInfo=(String)request.getAttribute(Dispatcher.INCLUDE_PATH_INFO);
111 if (servletPath==null)
113 servletPath=request.getServletPath();
114 pathInfo=request.getPathInfo();
119 servletPath = request.getServletPath();
120 pathInfo = request.getPathInfo();
123 String pathInContext=URIUtil.addPaths(servletPath,pathInfo);
125 if (pathInContext.endsWith("/"))
127 _dftServlet.getServlet().service(req,res);
129 else if (_starJspMapped && pathInContext.toLowerCase(Locale.ENGLISH).endsWith(".jsp"))
131 _jspServlet.getServlet().service(req,res);
136 Resource resource = _contextHandler.getResource(pathInContext);
137 if (resource!=null && resource.isDirectory())
138 _dftServlet.getServlet().service(req,res);
140 _jspServlet.getServlet().service(req,res);