This commit is contained in:
Jan Bartel 2016-10-26 15:51:11 +11:00
parent 81e2bfd0c7
commit 4776b4c877
5 changed files with 200 additions and 23 deletions

View File

@ -69,11 +69,6 @@
<artifactId>jetty-util</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${project.version}</version>
</dependency>
<!-- Schemas -->
<dependency>
@ -98,5 +93,25 @@
<groupId>org.eclipse.jdt.core.compiler</groupId>
<artifactId>ecj</artifactId>
</dependency>
<!-- tests -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${project.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.toolchain</groupId>
<artifactId>jetty-test-helper</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -29,8 +29,9 @@ import javax.servlet.ServletContext;
import org.apache.jasper.servlet.JasperInitializer;
import org.apache.jasper.servlet.TldPreScanned;
import org.apache.jasper.servlet.TldScanner;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.xml.sax.SAXException;
/**
@ -38,8 +39,7 @@ import org.xml.sax.SAXException;
*/
public class JettyJasperInitializer extends JasperInitializer
{
private static final Logger LOG = Log.getLogger(JettyJasperInitializer.class);
private static final Log LOG = LogFactory.getLog(JasperInitializer.class);
/**
* NullTldScanner
*
@ -111,6 +111,4 @@ public class JettyJasperInitializer extends JasperInitializer
if (LOG.isDebugEnabled()) LOG.debug("Defaulting to jasper tld scanning");
return super.newTldScanner(context, namespaceAware, validate, blockExternal);
}
}

View File

@ -19,6 +19,9 @@
package org.eclipse.jetty.jsp;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
@ -26,9 +29,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.jasper.servlet.JspServlet;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.resource.Resource;
/**
* JettyJspServlet
@ -76,7 +77,7 @@ public class JettyJspServlet extends JspServlet
pathInfo = request.getPathInfo();
}
String pathInContext = URIUtil.addPaths(servletPath,pathInfo);
String pathInContext = addPaths(servletPath,pathInfo);
String jspFile = getInitParameter("jspFile");
@ -84,7 +85,7 @@ public class JettyJspServlet extends JspServlet
//otherwise the default servlet might handle it
if (jspFile == null)
{
if (pathInContext.endsWith("/"))
if (pathInContext != null && pathInContext.endsWith("/"))
{
//dispatch via forward to the default servlet
getServletContext().getNamedDispatcher("default").forward(req, resp);
@ -93,13 +94,16 @@ public class JettyJspServlet extends JspServlet
else
{
//check if it resolves to a directory
Resource resource = ((ContextHandler.Context)getServletContext()).getContextHandler().getResource(pathInContext);
if (resource!=null && resource.isDirectory())
String realPath = getServletContext().getRealPath(pathInContext);
if (realPath != null)
{
//dispatch via forward to the default servlet
getServletContext().getNamedDispatcher("default").forward(req, resp);
return;
Path asPath = Paths.get(realPath);
if (Files.exists(asPath) && Files.isDirectory(asPath))
{
//dispatch via forward to the default servlet
getServletContext().getNamedDispatcher("default").forward(req, resp);
return;
}
}
}
}
@ -108,5 +112,19 @@ public class JettyJspServlet extends JspServlet
super.service(req, resp);
}
/**
* @param servletPath the servletPath of the request
* @param pathInfo the pathInfo of the request
* @return servletPath with pathInfo appended
*/
private String addPaths(String servletPath, String pathInfo)
{
if (servletPath.length()==0)
return pathInfo;
if (pathInfo==null)
return servletPath;
return servletPath+pathInfo;
}
}

View File

@ -0,0 +1,123 @@
//
// ========================================================================
// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.jsp;
import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.servlet.ServletTester;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.apache.jasper.runtime.JspFactoryImpl;
import org.apache.tomcat.InstanceManager;
import org.apache.tomcat.SimpleInstanceManager;
import org.eclipse.jetty.servlet.ServletContextHandler;
public class TestJettyJspServlet
{
File _dir;
ServletTester _tester;
public static class DfltServlet extends HttpServlet
{
public DfltServlet()
{
super();
}
/**
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
resp.setContentType("html/text");
resp.getOutputStream().println("This.Is.The.Default.");
}
}
@Before
public void setUp () throws Exception
{
JspFactory.setDefaultFactory(new JspFactoryImpl());
_dir = MavenTestingUtils.getTestResourceDir("base");
_tester = new ServletTester("/context");
_tester.getContext().setClassLoader(new URLClassLoader(new URL[0], Thread.currentThread().getContextClassLoader()));
ServletHolder jspHolder = _tester.getContext().addServlet(JettyJspServlet.class, "/*");
jspHolder.setInitParameter("scratchdir", MavenTestingUtils.getTargetTestingDir().getAbsolutePath());
_tester.getContext().setResourceBase(_dir.getAbsolutePath());
_tester.getContext().setAttribute(InstanceManager.class.getName(), new SimpleInstanceManager());
ServletHolder dfltHolder = new ServletHolder();
dfltHolder.setName("default");
dfltHolder.setHeldClass( DfltServlet.class);
_tester.getContext().addServlet(dfltHolder, "/");
_tester.start();
}
@After
public void tearDown() throws Exception
{
if (_tester != null)
_tester.stop();
}
@Test
public void testWithJsp() throws Exception
{
//test that an ordinary jsp is served by jsp servlet
String request = "" +
"GET /context/foo.jsp HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"Connection: close\r\n" +
"\r\n";
String response = _tester.getResponses(request);
assertTrue(!response.contains("This.Is.The.Default."));
}
@Test
public void testWithDirectory() throws Exception
{
//test that a dir is served by the default servlet
String request = "" +
"GET /context/dir HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"Connection: close\r\n" +
"\r\n";
String response = _tester.getResponses(request);
assertTrue(response.contains("This.Is.The.Default."));
}
}

View File

@ -0,0 +1,23 @@
<html><head>
<%@ page import="java.util.Enumeration" %>
</head><body>
<h1>JSP Dump</h1>
<table border="1">
<tr><th>Request URI:</th><td><%= request.getRequestURI() %></td></tr>
<tr><th>ServletPath:</th><td><%= request.getServletPath() %></td></tr>
<tr><th>PathInfo:</th><td><%= request.getPathInfo() %></td></tr>
<%
Enumeration e =request.getParameterNames();
while(e.hasMoreElements())
{
String name = (String)e.nextElement();
%>
<tr>
<th>getParameter("<%= name %>")</th>
<td><%= request.getParameter(name) %></td></tr>
<% } %>
</table>
</body></html>