View Javadoc

1   //
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.
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  
19  package org.eclipse.jetty.servlet;
20  
21  import java.io.IOException;
22  import java.util.Locale;
23  
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;
29  
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;
34  
35  
36  /* ------------------------------------------------------------ */
37  /** Servlet handling JSP Property Group mappings
38   * <p>
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.
43   */
44  public class JspPropertyGroupServlet extends GenericServlet
45  {
46      private static final long serialVersionUID = 3681783214726776945L;
47  
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;
54  
55      public JspPropertyGroupServlet(ContextHandler context, ServletHandler servletHandler)
56      {
57          _contextHandler=context;
58          _servletHandler=servletHandler;
59      }
60  
61      @Override
62      public void init() throws ServletException
63      {
64          String jsp_name = "jsp";
65          ServletMapping servlet_mapping =_servletHandler.getServletMapping("*.jsp");
66          if (servlet_mapping!=null)
67          {
68              _starJspMapped=true;
69  
70              //now find the jsp servlet, ignoring the mapping that is for ourself
71              ServletMapping[] mappings = _servletHandler.getServletMappings();
72              for (ServletMapping m:mappings)
73              {
74                  String[] paths = m.getPathSpecs();
75                  if (paths!=null)
76                  {
77                      for (String path:paths)
78                      {
79                          if ("*.jsp".equals(path) && !NAME.equals(m.getServletName()))
80                              servlet_mapping = m;
81                      }
82                  }
83              }
84  
85              jsp_name=servlet_mapping.getServletName();
86          }
87          _jspServlet=_servletHandler.getServlet(jsp_name);
88  
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);
94      }
95  
96      @Override
97      public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
98      {
99          HttpServletRequest request = null;
100         if (req instanceof HttpServletRequest)
101             request = (HttpServletRequest)req;
102         else
103             throw new ServletException("Request not HttpServletRequest");
104 
105         String servletPath=null;
106         String pathInfo=null;
107         if (request.getAttribute(Dispatcher.INCLUDE_REQUEST_URI)!=null)
108         {
109             servletPath=(String)request.getAttribute(Dispatcher.INCLUDE_SERVLET_PATH);
110             pathInfo=(String)request.getAttribute(Dispatcher.INCLUDE_PATH_INFO);
111             if (servletPath==null)
112             {
113                 servletPath=request.getServletPath();
114                 pathInfo=request.getPathInfo();
115             }
116         }
117         else
118         {
119             servletPath = request.getServletPath();
120             pathInfo = request.getPathInfo();
121         }
122 
123         String pathInContext=URIUtil.addPaths(servletPath,pathInfo);
124 
125         if (pathInContext.endsWith("/"))
126         {
127             _dftServlet.getServlet().service(req,res);
128         }
129         else if (_starJspMapped && pathInContext.toLowerCase(Locale.ENGLISH).endsWith(".jsp"))
130         {
131             _jspServlet.getServlet().service(req,res);
132         }
133         else
134         {
135 
136             Resource resource = _contextHandler.getResource(pathInContext);
137             if (resource!=null && resource.isDirectory())
138                 _dftServlet.getServlet().service(req,res);
139             else
140                 _jspServlet.getServlet().service(req,res);
141         }
142 
143     }
144 
145 }