Skip to content

Commit

Permalink
Refactor out ee10 specifics from file-server example
Browse files Browse the repository at this point in the history
  • Loading branch information
joakime committed Feb 1, 2024
1 parent 08c15d6 commit 6114cbc
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 12 deletions.
68 changes: 68 additions & 0 deletions embedded/ee10-file-server/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.eclipse.jetty.examples.embedded</groupId>
<artifactId>jetty-embedded-examples</artifactId>
<version>12.0.x</version>
</parent>
<artifactId>ee10-file-server</artifactId>
<version>12.0.x</version>
<packaging>jar</packaging>
<name>Jetty Examples :: Jetty 12.0.x :: Embedded :: EE10 File Server</name>

<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
</dependency>

<dependency>
<groupId>org.eclipse.jetty.ee10</groupId>
<artifactId>jetty-ee10-servlet</artifactId>
<version>${jetty.version}</version>
</dependency>

<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-slf4j-impl</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>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.6.1</version>
<executions>
<execution>
<id>copy-wars</id>
<goals>
<goal>copy-dependencies</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<includeArtifactIds>hello-servlet-3</includeArtifactIds>
<includeScope>runtime</includeScope>
<includeTypes>war</includeTypes>
<overWriteSnapshots>true</overWriteSnapshots>
<overWriteReleases>true</overWriteReleases>
<stripVersion>true</stripVersion>
<outputDirectory>${project.build.directory}/webapps</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>Hello from src/main/resources/static-root/</title>
</head>
<body>
<h4>Hello from <code></code>src/main/resources/static-root/</h4>
</body>
</html>
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
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
8 changes: 8 additions & 0 deletions embedded/ee10-file-server/webapps/alt-root/alt-hello.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>Alt Hello from webapps/alt-root/</title>
</head>
<body>
<h4>Alt Hello from <code></code>webapps/alt-root/</h4>
</body>
</html>
8 changes: 8 additions & 0 deletions embedded/ee10-file-server/webapps/alt-root/index.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<head>
<head>
<title>ALT Static Content</title>
</head>
<body>
<h4>Nothing to see here, move along...</h4>
</body>
</head>
6 changes: 0 additions & 6 deletions embedded/file-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@
<version>${jetty.version}</version>
</dependency>

<dependency>
<groupId>org.eclipse.jetty.ee10</groupId>
<artifactId>jetty-ee10-webapp</artifactId>
<version>${jetty.version}</version>
</dependency>

<dependency>
<groupId>org.eclipse.jetty.toolchain</groupId>
<artifactId>jetty-test-helper</artifactId>
Expand Down
1 change: 1 addition & 0 deletions embedded/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<module>connectors</module>
<module>deploying</module>
<module>ee8-webapp-context</module>
<module>ee10-file-server</module>
<module>ee10-webapp-context</module>
<module>error-handling</module>
<module>file-server</module>
Expand Down

0 comments on commit 6114cbc

Please sign in to comment.