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  import java.nio.file.Files;
23  import java.nio.file.Path;
24  import java.nio.file.Paths;
25  
26  import javax.servlet.RequestDispatcher;
27  import javax.servlet.ServletException;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  import org.apache.jasper.servlet.JspServlet;
32  
33  
34  /**
35   * JettyJspServlet
36   *
37   * Wrapper for the jsp servlet that handles receiving requests mapped from 
38   * jsp-property-groups. Mappings could be wildcard urls like "/*", which would
39   * include welcome files, but we need those to be handled by the DefaultServlet.
40   */
41  public class JettyJspServlet extends JspServlet
42  {
43  
44      /**
45       * 
46       */
47      private static final long serialVersionUID = -5387857473125086791L;
48  
49      
50      
51      
52      
53      @Override
54      public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
55      {
56          HttpServletRequest request = null;
57          if (req instanceof HttpServletRequest)
58              request = (HttpServletRequest)req;
59          else
60              throw new ServletException("Request not HttpServletRequest");
61  
62          String servletPath=null;
63          String pathInfo=null;
64          if (request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI)!=null)
65          {
66              servletPath=(String)request.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH);
67              pathInfo=(String)request.getAttribute(RequestDispatcher.INCLUDE_PATH_INFO);
68              if (servletPath==null)
69              {
70                  servletPath=request.getServletPath();
71                  pathInfo=request.getPathInfo();
72              }
73          }
74          else
75          {
76              servletPath = request.getServletPath();
77              pathInfo = request.getPathInfo();
78          }
79          
80          String pathInContext = addPaths(servletPath,pathInfo);
81      
82          String jspFile = getInitParameter("jspFile");
83  
84          //if this is a forced-path from a jsp-file, we want the jsp servlet to handle it,
85          //otherwise the default servlet might handle it
86          if (jspFile == null)
87          {
88              if (pathInContext != null && pathInContext.endsWith("/"))
89              {
90                  //dispatch via forward to the default servlet
91                  getServletContext().getNamedDispatcher("default").forward(req, resp);
92                  return;
93              }
94              else
95              {      
96                  //check if it resolves to a directory
97                  String realPath = getServletContext().getRealPath(pathInContext);
98                  if (realPath != null)
99                  {
100                     Path asPath = Paths.get(realPath);
101                     if (Files.exists(asPath) && Files.isDirectory(asPath))
102                     {
103                         //dispatch via forward to the default servlet
104                         getServletContext().getNamedDispatcher("default").forward(req, resp);
105                         return;
106                     }
107                 }
108             }
109         }
110         
111         //fall through to the normal jsp servlet handling
112         super.service(req, resp);
113     }
114 
115     /**
116      * @param servletPath the servletPath of the request
117      * @param pathInfo the pathInfo of the request
118      * @return servletPath with pathInfo appended
119      */
120     private String addPaths(String servletPath, String pathInfo)
121     {
122         if (servletPath.length()==0)
123             return pathInfo;
124        
125         if (pathInfo==null)
126             return servletPath;
127         
128         return servletPath+pathInfo;
129     }
130 }