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