View Javadoc

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.jsp;
20  
21  import java.io.IOException;
22  
23  import javax.servlet.ServletException;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.apache.jasper.servlet.JspServlet;
28  import org.eclipse.jetty.server.handler.ContextHandler;
29  import org.eclipse.jetty.util.URIUtil;
30  import org.eclipse.jetty.util.resource.Resource;
31  
32  
33  /**
34   * JettyJspServlet
35   *
36   * Wrapper for the jsp servlet that handles receiving requests mapped from 
37   * jsp-property-groups. Mappings could be wildcard urls like "/*", which would
38   * include welcome files, but we need those to be handled by the DefaultServlet.
39   */
40  public class JettyJspServlet extends JspServlet
41  {
42  
43      /**
44       * 
45       */
46      private static final long serialVersionUID = -5387857473125086791L;
47  
48      @Override
49      public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
50      {
51          
52          
53          HttpServletRequest request = null;
54          if (req instanceof HttpServletRequest)
55              request = (HttpServletRequest)req;
56          else
57              throw new ServletException("Request not HttpServletRequest");
58  
59          String servletPath=null;
60          String pathInfo=null;
61          if (request.getAttribute("javax.servlet.include.request_uri")!=null)
62          {
63              servletPath=(String)request.getAttribute("javax.servlet.include.servlet_path");
64              pathInfo=(String)request.getAttribute("javax.servlet.include.path_info");
65              if (servletPath==null)
66              {
67                  servletPath=request.getServletPath();
68                  pathInfo=request.getPathInfo();
69              }
70          }
71          else
72          {
73              servletPath = request.getServletPath();
74              pathInfo = request.getPathInfo();
75          }
76          
77          String pathInContext = URIUtil.addPaths(servletPath,pathInfo);
78          String jspFile = getInitParameter("jspFile");
79          
80          //if the request is for a jsp file then fall through to the jsp servlet
81          if (jspFile == null)
82          {
83              if (pathInContext.endsWith("/"))
84              {
85                  //dispatch via forward to the default servlet
86                  getServletContext().getNamedDispatcher("default").forward(req, resp);
87                  return;
88              }
89              else
90              {      
91                  //check if it resolves to a directory
92                  Resource resource = ((ContextHandler.Context)getServletContext()).getContextHandler().getResource(pathInContext);           
93                  if (resource!=null && resource.isDirectory())
94                  {
95                      //dispatch via forward to the default servlet
96                      getServletContext().getNamedDispatcher("default").forward(req, resp);
97                      return;
98                  }
99              }
100         }
101 
102         //fall through to the normal jsp servlet handling
103         super.service(req, resp);
104     }
105 
106     
107 }