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.jsp;
20  
21  import java.io.IOException;
22  
23  import javax.servlet.RequestDispatcher;
24  import javax.servlet.ServletException;
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  import org.apache.jasper.servlet.JspServlet;
29  import org.eclipse.jetty.server.handler.ContextHandler;
30  import org.eclipse.jetty.util.URIUtil;
31  import org.eclipse.jetty.util.resource.Resource;
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      
49      
50      
51      
52      @Override
53      public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
54      {
55          HttpServletRequest request = null;
56          if (req instanceof HttpServletRequest)
57              request = (HttpServletRequest)req;
58          else
59              throw new ServletException("Request not HttpServletRequest");
60  
61          String servletPath=null;
62          String pathInfo=null;
63          if (request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI)!=null)
64          {
65              servletPath=(String)request.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH);
66              pathInfo=(String)request.getAttribute(RequestDispatcher.INCLUDE_PATH_INFO);
67              if (servletPath==null)
68              {
69                  servletPath=request.getServletPath();
70                  pathInfo=request.getPathInfo();
71              }
72          }
73          else
74          {
75              servletPath = request.getServletPath();
76              pathInfo = request.getPathInfo();
77          }
78          
79          String pathInContext = URIUtil.addPaths(servletPath,pathInfo);
80      
81          String jspFile = getInitParameter("jspFile");
82  
83          //if this is a forced-path from a jsp-file, we want the jsp servlet to handle it,
84          //otherwise the default servlet might handle it
85          if (jspFile == null)
86          {
87              if (pathInContext.endsWith("/"))
88              {
89                  //dispatch via forward to the default servlet
90                  getServletContext().getNamedDispatcher("default").forward(req, resp);
91                  return;
92              }
93              else
94              {      
95                  //check if it resolves to a directory
96                  Resource resource = ((ContextHandler.Context)getServletContext()).getContextHandler().getResource(pathInContext);    
97  
98                  if (resource!=null && resource.isDirectory())
99                  {
100                     //dispatch via forward to the default servlet
101                     getServletContext().getNamedDispatcher("default").forward(req, resp);
102                     return;
103                 }
104             }
105         }
106         
107         //fall through to the normal jsp servlet handling
108         super.service(req, resp);
109     }
110 
111     
112 }