-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor out ee10 specifics from file-server example
- Loading branch information
Showing
11 changed files
with
240 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions
8
embedded/ee10-file-server/src/main/resources/static-root/hello.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
44 changes: 44 additions & 0 deletions
44
embedded/ee10-file-server/src/test/java/examples/ByteCountingOutputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
embedded/ee10-file-server/src/test/java/examples/StaticFileGen.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters