Issue #3487 WebAppClassLoader does not definePackage

Added a test to the test-server-spec to demonstrate the issue
This commit is contained in:
Greg Wilkins 2019-03-26 13:56:00 +11:00
parent 8427c52c8f
commit ed03682071
5 changed files with 173 additions and 6 deletions

View File

@ -231,7 +231,8 @@
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>${project.version}</version>
<!-- deliberately old version to test classloading -->
<version>9.3.0.RC0</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,132 @@
//
// ========================================================================
// Copyright (c) 1995-2018 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 com.acme.test;
import java.io.PrintWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.util.IO;
@WebServlet(urlPatterns="/classloader")
public class ClassLoaderServlet extends HttpServlet
{
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException
{
try
{
PrintWriter writer = resp.getWriter();
writer.println("<html>");
writer.println("<HEAD><link rel=\"stylesheet\" type=\"text/css\" href=\"stylesheet.css\"/></HEAD>");
writer.println("<body>");
writer.println("<h1>ClassLoader Isolation Test</h1>");
Class<?> webappIO = IO.class;
URI webappURI = getLocationOfClass(webappIO);
String webappVersion = webappIO.getPackage().getImplementationVersion();
Class<?> serverIO = req.getServletContext().getClass().getClassLoader().loadClass("org.eclipse.jetty.util.IO");
URI serverURI = getLocationOfClass(serverIO);
String serverVersion = serverIO.getPackage().getImplementationVersion();
writer.printf("<p>Webapp loaded <code>org.eclipse.jetty.util.IO</code>(%s) from %s%n",webappVersion,webappURI);
writer.printf("<br/>Server loaded <code>org.eclipse.jetty.util.IO</code>(%s) from %s%n",serverVersion, serverURI);
if (webappVersion.equals(serverVersion))
writer.println("<br/><b>Version Result: <span class=\"fail\">FAIL</span></b>");
else
writer.println("<br/><b>Version Result: <span class=\"pass\">PASS</span></b>");
if (webappURI.equals(serverURI))
writer.println("<br/><b>URI Result: <span class=\"fail\">FAIL</span></b></p>");
else
writer.println("<br/><b>URI Result: <span class=\"pass\">PASS</span></b></p>");
writer.println("</body>");
writer.println("</html>");
writer.flush();
writer.close();
}
catch(Exception e)
{
throw new ServletException(e);
}
}
/* ------------------------------------------------------------ */
public static URI getLocationOfClass(Class<?> clazz)
{
try
{
ProtectionDomain domain = clazz.getProtectionDomain();
if (domain != null)
{
CodeSource source = domain.getCodeSource();
if (source != null)
{
URL location = source.getLocation();
if (location != null)
return location.toURI();
}
}
String resourceName = clazz.getName().replace('.', '/') + ".class";
ClassLoader loader = clazz.getClassLoader();
URL url = (loader == null ? ClassLoader.getSystemClassLoader() : loader).getResource(resourceName);
if (url != null)
{
return getJarSource(url.toURI());
}
}
catch (URISyntaxException e)
{
throw new RuntimeException(e);
}
return null;
}
public static URI getJarSource(URI uri)
{
try
{
if (!"jar".equals(uri.getScheme()))
return uri;
// Get SSP (retaining encoded form)
String s = uri.getRawSchemeSpecificPart();
int bang_slash = s.indexOf("!/");
if (bang_slash>=0)
s=s.substring(0,bang_slash);
return new URI(s);
}
catch(URISyntaxException e)
{
throw new IllegalArgumentException(e);
}
}
}

View File

@ -19,6 +19,7 @@
package com.acme.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import javax.servlet.ServletConfig;
@ -30,7 +31,6 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import org.eclipse.jetty.util.IO;
/**
* MultiPartTest
*
@ -42,9 +42,11 @@ import org.eclipse.jetty.util.IO;
@MultipartConfig(location="foo/bar", maxFileSize=10240, maxRequestSize=-1, fileSizeThreshold=2048)
public class MultiPartTest extends HttpServlet
{
private static int bufferSize = 4096;
private ServletConfig config;
public void init(ServletConfig config) throws ServletException
{
super.init(config);
@ -76,7 +78,35 @@ public class MultiPartTest extends HttpServlet
if (p.getContentType() == null || p.getContentType().startsWith("text/plain"))
{
out.println("<p>");
IO.copy(p.getInputStream(),out);
InputStream in = p.getInputStream();
long byteCount = -1;
byte buffer[] = new byte[bufferSize];
int len= bufferSize;
if (byteCount>=0)
{
while (byteCount>0)
{
int max = byteCount< bufferSize?(int)byteCount: bufferSize;
len=in.read(buffer,0,max);
if (len==-1)
break;
byteCount -= len;
out.write(buffer,0,len);
}
}
else
{
while (true)
{
len=in.read(buffer,0, bufferSize);
if (len<0 )
break;
out.write(buffer,0,len);
}
}
out.println("</p>");
}
}

View File

@ -41,8 +41,8 @@ public class SecuredServlet extends HttpServlet
PrintWriter writer = resp.getWriter();
writer.println( "<html>");
writer.println("<HEAD><link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheet.css\"/></HEAD>");
writer.println("<h1>@ServletSecurity</h1>");
writer.println("<body>");
writer.println("<h1>@ServletSecurity</h1>");
writer.println("<pre>");
writer.println("@ServletSecurity");
writer.println("public class SecuredServlet");

View File

@ -65,10 +65,14 @@ Test of the annotation:
<button type="submit">Test AsyncListener</button>
</form>
<center>
<h3>Test ClassPath Isolation </h3>
<p>Click the link to test classpath isolation of system and server classes</p>
<a href="classloader">ClassPathServlet</a>
<div style="text-align: center;">
<hr/>
<a href="http://www.eclipse.org/jetty"><img style="border:0" src="images/small_powered_by.gif"/></a>
</center>
</div>
</BODY>
</HTML>