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