diff --git a/embedded/ee10-file-server/pom.xml b/embedded/ee10-file-server/pom.xml new file mode 100644 index 0000000..777991d --- /dev/null +++ b/embedded/ee10-file-server/pom.xml @@ -0,0 +1,68 @@ + + + 4.0.0 + + org.eclipse.jetty.examples.embedded + jetty-embedded-examples + 12.0.x + + ee10-file-server + 12.0.x + jar + Jetty Examples :: Jetty 12.0.x :: Embedded :: EE10 File Server + + + + org.eclipse.jetty + jetty-server + ${jetty.version} + + + + org.eclipse.jetty.ee10 + jetty-ee10-servlet + ${jetty.version} + + + + org.eclipse.jetty + jetty-slf4j-impl + ${jetty.version} + + + org.eclipse.jetty.toolchain + jetty-test-helper + ${jetty-test-helper.version} + test + + + + + + + + org.apache.maven.plugins + maven-dependency-plugin + 3.6.1 + + + copy-wars + + copy-dependencies + + generate-resources + + hello-servlet-3 + runtime + war + true + true + true + ${project.build.directory}/webapps + + + + + + + diff --git a/embedded/file-server/src/main/java/examples/ServletFileServerMultipleLocations.java b/embedded/ee10-file-server/src/main/java/examples/ServletFileServerMultipleLocations.java similarity index 100% rename from embedded/file-server/src/main/java/examples/ServletFileServerMultipleLocations.java rename to embedded/ee10-file-server/src/main/java/examples/ServletFileServerMultipleLocations.java diff --git a/embedded/file-server/src/main/java/examples/ServletFileServerSingleLocation.java b/embedded/ee10-file-server/src/main/java/examples/ServletFileServerSingleLocation.java similarity index 100% rename from embedded/file-server/src/main/java/examples/ServletFileServerSingleLocation.java rename to embedded/ee10-file-server/src/main/java/examples/ServletFileServerSingleLocation.java diff --git a/embedded/ee10-file-server/src/main/resources/static-root/hello.html b/embedded/ee10-file-server/src/main/resources/static-root/hello.html new file mode 100644 index 0000000..aa97420 --- /dev/null +++ b/embedded/ee10-file-server/src/main/resources/static-root/hello.html @@ -0,0 +1,8 @@ + + +Hello from src/main/resources/static-root/ + + +

Hello from src/main/resources/static-root/

+ + \ No newline at end of file diff --git a/embedded/ee10-file-server/src/test/java/examples/ByteCountingOutputStream.java b/embedded/ee10-file-server/src/test/java/examples/ByteCountingOutputStream.java new file mode 100644 index 0000000..53be4b1 --- /dev/null +++ b/embedded/ee10-file-server/src/test/java/examples/ByteCountingOutputStream.java @@ -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; + } +} diff --git a/embedded/file-server/src/test/java/examples/ServletFileServerMultipleLocationsTest.java b/embedded/ee10-file-server/src/test/java/examples/ServletFileServerMultipleLocationsTest.java similarity index 92% rename from embedded/file-server/src/test/java/examples/ServletFileServerMultipleLocationsTest.java rename to embedded/ee10-file-server/src/test/java/examples/ServletFileServerMultipleLocationsTest.java index bc3b9a2..3b90add 100644 --- a/embedded/file-server/src/test/java/examples/ServletFileServerMultipleLocationsTest.java +++ b/embedded/ee10-file-server/src/test/java/examples/ServletFileServerMultipleLocationsTest.java @@ -22,19 +22,18 @@ 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.Assertions; 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 ServletFileServerMultipleLocationsTest { - private final long exampleSize = 2 * MB; - private final long largeSize = 2 * GB; + private final long exampleSize = 2 * StaticFileGen.MB; + private final long largeSize = 2 * StaticFileGen.GB; private Server server; private String exampleSha; private String largeSha; @@ -73,7 +72,7 @@ public void testGetSmall() throws Exception String contentLengthResponse = http.getHeaderField("Content-Length"); assertNotNull(contentLengthResponse); long contentLengthLong = Long.parseLong(contentLengthResponse); - assertEquals(2 * MB, contentLengthLong); + Assertions.assertEquals(2 * StaticFileGen.MB, contentLengthLong); assertEquals("image/png", http.getHeaderField("Content-Type")); StaticFileGen.verify(http.getInputStream(), exampleSize, exampleSha); @@ -92,7 +91,7 @@ public void testGetLarge() throws Exception String contentLengthResponse = http.getHeaderField("Content-Length"); assertNotNull(contentLengthResponse); long contentLengthLong = Long.parseLong(contentLengthResponse); - assertEquals(2 * GB, contentLengthLong); + Assertions.assertEquals(2 * StaticFileGen.GB, contentLengthLong); assertNull(http.getHeaderField("Content-Type"), "Not a recognized mime-type by Jetty"); StaticFileGen.verify(http.getInputStream(), largeSize, largeSha); diff --git a/embedded/ee10-file-server/src/test/java/examples/StaticFileGen.java b/embedded/ee10-file-server/src/test/java/examples/StaticFileGen.java new file mode 100644 index 0000000..f01286d --- /dev/null +++ b/embedded/ee10-file-server/src/test/java/examples/StaticFileGen.java @@ -0,0 +1,98 @@ +// +// ======================================================================== +// 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.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.channels.SeekableByteChannel; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; +import java.security.DigestOutputStream; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Arrays; + +import org.eclipse.jetty.toolchain.test.Hex; +import org.eclipse.jetty.toolchain.test.Sha1Sum; +import org.eclipse.jetty.util.IO; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class StaticFileGen +{ + public static final long KB = 1024; + public static final long MB = 1024 * KB; + public static final long GB = 1024 * MB; + + /** + * Generate a static file. + * + * @param staticFile the path of the file to create + * @param size the size of the file to create + * @return the SHA1 hash of the static file + * @throws IOException if unable to create the file + * @throws NoSuchAlgorithmException if unable to find the SHA1 algorithm + */ + public static String generate(Path staticFile, long size) throws IOException, NoSuchAlgorithmException + { + byte[] buf = new byte[(int)MB]; + Arrays.fill(buf, (byte)'x'); + ByteBuffer src = ByteBuffer.wrap(buf); + + if (Files.exists(staticFile) && Files.size(staticFile) == size) + { + // all done, nothing left to do. + System.err.printf("File Exists Already: %s (%,d bytes)%n", staticFile, Files.size(staticFile)); + return Sha1Sum.calculate(staticFile); + } + + System.err.printf("Creating %,d byte file: %s ...%n", size, staticFile); + try (SeekableByteChannel channel = Files.newByteChannel(staticFile, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING)) + { + long remaining = size; + while (remaining > 0) + { + ByteBuffer slice = src.slice(); + int len = buf.length; + if (remaining < Integer.MAX_VALUE) + { + len = Math.min(buf.length, (int)remaining); + slice.limit(len); + } + + channel.write(slice); + remaining -= len; + } + } + System.err.println(" Done"); + return Sha1Sum.calculate(staticFile); + } + + public static void verify(InputStream inputStream, long expectedSize, String expectedSha1) throws NoSuchAlgorithmException, IOException + { + MessageDigest digest = MessageDigest.getInstance("SHA1"); + try(ByteCountingOutputStream byteCountingOutputStream = new ByteCountingOutputStream(); + DigestOutputStream digestOut = new DigestOutputStream(byteCountingOutputStream, digest)) + { + IO.copy(inputStream, digestOut); + String actualSha1 = Hex.asHex(digestOut.getMessageDigest().digest()); + assertEquals(expectedSha1, actualSha1); + + long actualSize = byteCountingOutputStream.getCount(); + assertEquals(expectedSize, actualSize); + } + } +} diff --git a/embedded/ee10-file-server/webapps/alt-root/alt-hello.html b/embedded/ee10-file-server/webapps/alt-root/alt-hello.html new file mode 100644 index 0000000..848ec93 --- /dev/null +++ b/embedded/ee10-file-server/webapps/alt-root/alt-hello.html @@ -0,0 +1,8 @@ + + +Alt Hello from webapps/alt-root/ + + +

Alt Hello from webapps/alt-root/

+ + \ No newline at end of file diff --git a/embedded/ee10-file-server/webapps/alt-root/index.htm b/embedded/ee10-file-server/webapps/alt-root/index.htm new file mode 100644 index 0000000..20fccd2 --- /dev/null +++ b/embedded/ee10-file-server/webapps/alt-root/index.htm @@ -0,0 +1,8 @@ + + + ALT Static Content + + +

Nothing to see here, move along...

+ + \ No newline at end of file diff --git a/embedded/file-server/pom.xml b/embedded/file-server/pom.xml index 122538d..44c0583 100644 --- a/embedded/file-server/pom.xml +++ b/embedded/file-server/pom.xml @@ -24,12 +24,6 @@ ${jetty.version} - - org.eclipse.jetty.ee10 - jetty-ee10-webapp - ${jetty.version} - - org.eclipse.jetty.toolchain jetty-test-helper diff --git a/embedded/pom.xml b/embedded/pom.xml index f805143..b9d84c7 100644 --- a/embedded/pom.xml +++ b/embedded/pom.xml @@ -19,6 +19,7 @@ connectors deploying ee8-webapp-context + ee10-file-server ee10-webapp-context error-handling file-server