Skip to content

Commit

Permalink
Issue #1032
Browse files Browse the repository at this point in the history
  • Loading branch information
janbartel committed Oct 26, 2016
1 parent 81e2bfd commit 4776b4c
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 23 deletions.
25 changes: 20 additions & 5 deletions apache-jsp/pom.xml
Expand Up @@ -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>
Expand All @@ -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>
Expand Up @@ -29,17 +29,17 @@
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;

/**
* JettyJasperInitializer
*/
public class JettyJasperInitializer extends JasperInitializer
{
private static final Logger LOG = Log.getLogger(JettyJasperInitializer.class);

private static final Log LOG = LogFactory.getLog(JasperInitializer.class);
/**
* NullTldScanner
*
Expand Down Expand Up @@ -111,6 +111,4 @@ public TldScanner newTldScanner(ServletContext context, boolean namespaceAware,
if (LOG.isDebugEnabled()) LOG.debug("Defaulting to jasper tld scanning");
return super.newTldScanner(context, namespaceAware, validate, blockExternal);
}


}
42 changes: 30 additions & 12 deletions apache-jsp/src/main/java/org/eclipse/jetty/jsp/JettyJspServlet.java
Expand Up @@ -19,16 +19,17 @@
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;
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
Expand Down Expand Up @@ -76,15 +77,15 @@ public void service(HttpServletRequest req, HttpServletResponse resp) throws Ser
pathInfo = request.getPathInfo();
}

String pathInContext = URIUtil.addPaths(servletPath,pathInfo);
String pathInContext = addPaths(servletPath,pathInfo);

String jspFile = getInitParameter("jspFile");

//if this is a forced-path from a jsp-file, we want the jsp servlet to handle it,
//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);
Expand All @@ -93,13 +94,16 @@ public void service(HttpServletRequest req, HttpServletResponse resp) throws Ser
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;
}
}
}
}
Expand All @@ -108,5 +112,19 @@ public void service(HttpServletRequest req, HttpServletResponse resp) throws Ser
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;
}
}
@@ -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."));
}

}
23 changes: 23 additions & 0 deletions apache-jsp/src/test/resources/base/foo.jsp
@@ -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>

0 comments on commit 4776b4c

Please sign in to comment.