diff --git a/tests/test-webapps/test-servlet-spec/test-spec-webapp/pom.xml b/tests/test-webapps/test-servlet-spec/test-spec-webapp/pom.xml index 7c685102c40..289964a8b78 100644 --- a/tests/test-webapps/test-servlet-spec/test-spec-webapp/pom.xml +++ b/tests/test-webapps/test-servlet-spec/test-spec-webapp/pom.xml @@ -231,7 +231,8 @@ org.eclipse.jetty jetty-util - ${project.version} + + 9.3.0.RC0 diff --git a/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/ClassLoaderServlet.java b/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/ClassLoaderServlet.java new file mode 100644 index 00000000000..e8e93f4414a --- /dev/null +++ b/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/ClassLoaderServlet.java @@ -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(""); + writer.println(""); + writer.println(""); + writer.println("

ClassLoader Isolation Test

"); + + 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("

Webapp loaded org.eclipse.jetty.util.IO(%s) from %s%n",webappVersion,webappURI); + writer.printf("
Server loaded org.eclipse.jetty.util.IO(%s) from %s%n",serverVersion, serverURI); + if (webappVersion.equals(serverVersion)) + writer.println("
Version Result: FAIL"); + else + writer.println("
Version Result: PASS"); + if (webappURI.equals(serverURI)) + writer.println("
URI Result: FAIL

"); + else + writer.println("
URI Result: PASS

"); + + writer.println(""); + writer.println(""); + 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); + } + } +} diff --git a/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/MultiPartTest.java b/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/MultiPartTest.java index 4fb60cc1ea4..ac6afa65f9e 100644 --- a/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/MultiPartTest.java +++ b/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/MultiPartTest.java @@ -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("

"); - 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("

"); } } diff --git a/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/SecuredServlet.java b/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/SecuredServlet.java index 81972a60ceb..4f188757f17 100644 --- a/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/SecuredServlet.java +++ b/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/SecuredServlet.java @@ -41,8 +41,8 @@ public class SecuredServlet extends HttpServlet PrintWriter writer = resp.getWriter(); writer.println( ""); writer.println(""); - writer.println("

@ServletSecurity

"); writer.println(""); + writer.println("

@ServletSecurity

"); writer.println("
");
         writer.println("@ServletSecurity");
         writer.println("public class SecuredServlet");
diff --git a/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/webapp/index.html b/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/webapp/index.html
index 8fe9f11ed93..0919b4bc2c1 100644
--- a/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/webapp/index.html
+++ b/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/webapp/index.html
@@ -65,10 +65,14 @@ Test of the annotation:
   
 
 
-
+

Test ClassPath Isolation

+

Click the link to test classpath isolation of system and server classes

+ClassPathServlet + +

-
+