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.
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;
23 import javax.servlet.GenericServlet;
24 import javax.servlet.ServletException;
25 import javax.servlet.ServletRequest;
26 import javax.servlet.ServletResponse;
27 import javax.servlet.http.HttpServletRequest;
29 import org.eclipse.jetty.server.Dispatcher;
30 import org.eclipse.jetty.server.handler.ContextHandler;
31 import org.eclipse.jetty.util.URIUtil;
32 import org.eclipse.jetty.util.resource.Resource;
35 /* ------------------------------------------------------------ */
36 /** Servlet handling JSP Property Group mappings
38 * This servlet is mapped to by any URL pattern for a JSP property group.
39 * Resources handled by this servlet that are not directories will be passed
40 * directly to the JSP servlet. Resources that are directories will be
41 * passed directly to the default servlet.
43 public class JspPropertyGroupServlet extends GenericServlet
45 private static final long serialVersionUID = 3681783214726776945L;
47 public final static String NAME = "__org.eclipse.jetty.servlet.JspPropertyGroupServlet__";
48 private final ServletHandler _servletHandler;
49 private final ContextHandler _contextHandler;
50 private ServletHolder _dftServlet;
51 private ServletHolder _jspServlet;
52 private boolean _starJspMapped;
54 public JspPropertyGroupServlet(ContextHandler context, ServletHandler servletHandler)
56 _contextHandler=context;
57 _servletHandler=servletHandler;
61 public void init() throws ServletException
63 String jsp_name = "jsp";
64 ServletMapping servlet_mapping =_servletHandler.getServletMapping("*.jsp");
65 if (servlet_mapping!=null)
69 //now find the jsp servlet, ignoring the mapping that is for ourself
70 ServletMapping[] mappings = _servletHandler.getServletMappings();
71 for (ServletMapping m:mappings)
73 String[] paths = m.getPathSpecs();
76 for (String path:paths)
78 if ("*.jsp".equals(path) && !NAME.equals(m.getServletName()))
84 jsp_name=servlet_mapping.getServletName();
86 _jspServlet=_servletHandler.getServlet(jsp_name);
88 String dft_name="default";
89 ServletMapping default_mapping=_servletHandler.getServletMapping("/");
90 if (default_mapping!=null)
91 dft_name=default_mapping.getServletName();
92 _dftServlet=_servletHandler.getServlet(dft_name);
96 public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
98 HttpServletRequest request = null;
99 if (req instanceof HttpServletRequest)
100 request = (HttpServletRequest)req;
102 throw new ServletException("Request not HttpServletRequest");
104 String servletPath=null;
105 String pathInfo=null;
106 if (request.getAttribute(Dispatcher.INCLUDE_REQUEST_URI)!=null)
108 servletPath=(String)request.getAttribute(Dispatcher.INCLUDE_SERVLET_PATH);
109 pathInfo=(String)request.getAttribute(Dispatcher.INCLUDE_PATH_INFO);
110 if (servletPath==null)
112 servletPath=request.getServletPath();
113 pathInfo=request.getPathInfo();
118 servletPath = request.getServletPath();
119 pathInfo = request.getPathInfo();
122 String pathInContext=URIUtil.addPaths(servletPath,pathInfo);
124 if (pathInContext.endsWith("/"))
126 _dftServlet.getServlet().service(req,res);
128 else if (_starJspMapped && pathInContext.toLowerCase().endsWith(".jsp"))
130 _jspServlet.getServlet().service(req,res);
135 Resource resource = _contextHandler.getResource(pathInContext);
136 if (resource!=null && resource.isDirectory())
137 _dftServlet.getServlet().service(req,res);
139 _jspServlet.getServlet().service(req,res);