View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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  
23  import javax.servlet.GenericServlet;
24  import javax.servlet.ServletException;
25  import javax.servlet.ServletRequest;
26  import javax.servlet.ServletResponse;
27  
28  import org.eclipse.jetty.server.Dispatcher;
29  import org.eclipse.jetty.server.HttpConnection;
30  import org.eclipse.jetty.server.Request;
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          Request request=(req instanceof Request)?(Request)req:HttpConnection.getCurrentConnection().getHttpChannel().getRequest();
100 
101         String servletPath=null;
102         String pathInfo=null;
103         if (request.getAttribute(Dispatcher.INCLUDE_REQUEST_URI)!=null)
104         {
105             servletPath=(String)request.getAttribute(Dispatcher.INCLUDE_SERVLET_PATH);
106             pathInfo=(String)request.getAttribute(Dispatcher.INCLUDE_PATH_INFO);
107             if (servletPath==null)
108             {
109                 servletPath=request.getServletPath();
110                 pathInfo=request.getPathInfo();
111             }
112         }
113         else
114         {
115             servletPath = request.getServletPath();
116             pathInfo = request.getPathInfo();
117         }
118         
119         String pathInContext=URIUtil.addPaths(servletPath,pathInfo);
120         
121         if (pathInContext.endsWith("/"))
122         {
123             _dftServlet.getServlet().service(req,res);
124         }
125         else if (_starJspMapped && pathInContext.toLowerCase().endsWith(".jsp"))
126         {
127             _jspServlet.getServlet().service(req,res);
128         }
129         else
130         {
131          
132             Resource resource = _contextHandler.getResource(pathInContext);
133             if (resource!=null && resource.isDirectory())
134                 _dftServlet.getServlet().service(req,res);
135             else
136                 _jspServlet.getServlet().service(req,res);
137         }
138         
139     }
140 
141 }