Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/9.4.x' into 10.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
joakime committed Feb 1, 2024
2 parents 6c80d29 + c568d13 commit f06b4a4
Show file tree
Hide file tree
Showing 9 changed files with 433 additions and 34 deletions.
7 changes: 7 additions & 0 deletions embedded/file-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
<artifactId>jetty-webapp</artifactId>
<version>${jetty.version}</version>
</dependency>

<dependency>
<groupId>org.eclipse.jetty.toolchain</groupId>
<artifactId>jetty-test-helper</artifactId>
<version>${jetty-test-helper.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package examples;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

Expand All @@ -24,8 +25,6 @@ public class ResourceHandlerFromClasspath
{
public static void main(String[] args) throws Exception
{
Server server = new Server(8080);

// Figure out what path to serve content from
ClassLoader cl = ResourceHandlerFromClasspath.class.getClassLoader();
// We look for a file, as ClassLoader.getResource() is not
Expand All @@ -40,13 +39,20 @@ public static void main(String[] args) throws Exception
URI webRootUri = f.toURI().resolve("./").normalize();
System.err.println("WebRoot is " + webRootUri);

Server server = ResourceHandlerFromClasspath.newServer(8080, webRootUri);
server.start();
server.join();
}

public static Server newServer(int port, URI resourcesRoot) throws MalformedURLException
{
Server server = new Server(port);

ResourceHandler handler = new ResourceHandler();
handler.setBaseResource(Resource.newResource(webRootUri));
handler.setBaseResource(Resource.newResource(resourcesRoot));
handler.setDirectoriesListed(true);

server.setHandler(handler);

server.start();
server.join();
return server;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public class ResourceHandlerFromFileSystem
{
public static void main(String[] args) throws Exception
{
Server server = new Server(8080);

Path webRootPath = Paths.get("webapps/alt-root/").toAbsolutePath().normalize();
if (!Files.isDirectory(webRootPath))
{
Expand All @@ -35,13 +33,20 @@ public static void main(String[] args) throws Exception
}
System.err.println("WebRoot is " + webRootPath);

Server server = ResourceHandlerFromFileSystem.newServer(8080, webRootPath);
server.start();
server.join();
}

public static Server newServer(int port, Path resourcesRoot)
{
Server server = new Server(port);

ResourceHandler handler = new ResourceHandler();
handler.setBaseResource(new PathResource(webRootPath));
handler.setBaseResource(new PathResource(resourcesRoot));
handler.setDirectoriesListed(true);

server.setHandler(handler);

server.start();
server.join();
return server;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@

package examples;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -41,11 +42,19 @@ public class ServletFileServerMultipleLocations
{
public static void main(String[] args) throws Exception
{
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(8080);
server.addConnector(connector);
URI webRootUri = findDefaultBaseResource();
System.err.println("Default Base Resource is " + webRootUri);

Path altPath = Paths.get("webapps/alt-root").toRealPath();
System.err.println("Alt Base Resource is " + altPath);

Server server = ServletFileServerMultipleLocations.newServer(8080, webRootUri, altPath);
server.start();
server.join();
}

public static URI findDefaultBaseResource() throws URISyntaxException
{
// Figure out what path to serve content from
ClassLoader cl = ServletFileServerMultipleLocations.class.getClassLoader();
// We look for a file, as ClassLoader.getResource() is not
Expand All @@ -57,21 +66,24 @@ public static void main(String[] args) throws Exception
}

// Resolve file to directory
URI webRootUri = f.toURI().resolve("./").normalize();
System.err.println("Main Base Resource is " + webRootUri);
return f.toURI().resolve("./").normalize();
}

public static Server newServer(int port, URI mainResourceBase, Path altPath) throws MalformedURLException
{
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(port);
server.addConnector(connector);

// Setup the basic application "context" for this application at "/"
// This is also known as the handler tree (in jetty speak)
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.setBaseResource(Resource.newResource(webRootUri));
context.setBaseResource(Resource.newResource(mainResourceBase));
context.setWelcomeFiles(new String[]{"index.html", "index.htm", "alt-index.html"});
server.setHandler(context);

// Find altPath
Path altPath = Paths.get("webapps/alt-root").toRealPath();
System.err.println("Alt Base Resource is " + altPath);

// add special pathspec of "/alt/" content mapped to the altPath
ServletHolder holderAlt = new ServletHolder("static-alt", DefaultServlet.class);
holderAlt.setInitParameter("resourceBase", altPath.toUri().toASCIIString());
Expand All @@ -85,7 +97,6 @@ public static void main(String[] args) throws Exception
holderDef.setInitParameter("dirAllowed", "true");
context.addServlet(holderDef, "/");

server.start();
server.join();
return server;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package examples;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

Expand All @@ -30,11 +31,6 @@ public class ServletFileServerSingleLocation
{
public static void main(String[] args) throws Exception
{
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(8080);
server.addConnector(connector);

// Figure out what path to serve content from
ClassLoader cl = ServletFileServerSingleLocation.class.getClassLoader();
// We look for a file, as ClassLoader.getResource() is not
Expand All @@ -49,16 +45,27 @@ public static void main(String[] args) throws Exception
URI webRootUri = f.toURI().resolve("./").normalize();
System.err.println("WebRoot is " + webRootUri);

Server server = ServletFileServerSingleLocation.newServer(8080, webRootUri);
server.start();
server.join();
}

public static Server newServer(int port, URI resourcesRoot) throws MalformedURLException
{
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(port);
server.addConnector(connector);

ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.setBaseResource(Resource.newResource(webRootUri));
context.setBaseResource(Resource.newResource(resourcesRoot));
server.setHandler(context);

ServletHolder holderPwd = new ServletHolder("default", DefaultServlet.class);
holderPwd.setInitParameter("dirAllowed", "true");
context.addServlet(holderPwd, "/");

server.start();
server.join();
return server;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package examples;

import java.io.OutputStream;

public class ByteCountingOutputStream extends OutputStream
{
private long count = 0;

public long getCount()
{
return count;
}

@Override
public void write(int b)
{
count++;
}

@Override
public void write(byte[] b)
{
count += b.length;
}

@Override
public void write(byte[] b, int off, int len)
{
count += len;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package examples;

import java.net.HttpURLConnection;
import java.nio.file.Path;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.MavenPaths;
import org.eclipse.jetty.util.component.LifeCycle;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static examples.StaticFileGen.GB;
import static examples.StaticFileGen.MB;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

public class ResourceHandlerFromFileSystemTest
{
private final long exampleSize = 2 * MB;
private final long largeSize = 2 * GB;
private Server server;
private String exampleSha;
private String largeSha;

@BeforeEach
public void startServer() throws Exception
{
Path resourcesRoot = MavenPaths.targetTestDir(ResourceHandlerFromFileSystemTest.class.getSimpleName());
FS.ensureDirExists(resourcesRoot);

exampleSha = StaticFileGen.generate(resourcesRoot.resolve("example.png"), exampleSize);
largeSha = StaticFileGen.generate(resourcesRoot.resolve("large.mkv"), largeSize);

server = ResourceHandlerFromFileSystem.newServer(0, resourcesRoot);
server.start();
}

@AfterEach
public void stopServer()
{
LifeCycle.stop(server);
}

/**
* Get small file
*/
@Test
public void testGetSmall() throws Exception
{
HttpURLConnection http = (HttpURLConnection)server.getURI().resolve("/example.png").toURL().openConnection();
http.connect();
dumpRequestResponse(http);
assertEquals(HttpURLConnection.HTTP_OK, http.getResponseCode());
String contentLengthResponse = http.getHeaderField("Content-Length");
assertNotNull(contentLengthResponse);
long contentLengthLong = Long.parseLong(contentLengthResponse);
assertEquals(2 * MB, contentLengthLong);
assertEquals("image/png", http.getHeaderField("Content-Type"));

StaticFileGen.verify(http.getInputStream(), exampleSize, exampleSha);
}

/**
* Get large file
*/
@Test
public void testGetLarge() throws Exception
{
HttpURLConnection http = (HttpURLConnection)server.getURI().resolve("/large.mkv").toURL().openConnection();
http.connect();
dumpRequestResponse(http);
assertEquals(HttpURLConnection.HTTP_OK, http.getResponseCode());
String contentLengthResponse = http.getHeaderField("Content-Length");
assertNotNull(contentLengthResponse);
long contentLengthLong = Long.parseLong(contentLengthResponse);
assertEquals(2 * GB, contentLengthLong);
assertNull(http.getHeaderField("Content-Type"), "Not a recognized mime-type by Jetty");

StaticFileGen.verify(http.getInputStream(), largeSize, largeSha);
}

private static void dumpRequestResponse(HttpURLConnection http)
{
System.out.println();
System.out.println("----");
System.out.printf("%s %s HTTP/1.1%n", http.getRequestMethod(), http.getURL());
System.out.println("----");
System.out.printf("%s%n", http.getHeaderField(null));
http.getHeaderFields().entrySet().stream()
.filter(entry -> entry.getKey() != null)
.forEach((entry) -> System.out.printf("%s: %s%n", entry.getKey(), http.getHeaderField(entry.getKey())));
}
}
Loading

0 comments on commit f06b4a4

Please sign in to comment.