Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/10.0.x' into 11.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
joakime committed Feb 5, 2024
2 parents 4d738a9 + 5a865a0 commit 6211e0e
Show file tree
Hide file tree
Showing 17 changed files with 876 additions and 4 deletions.
Binary file added .mvn/wrapper/maven-wrapper.jar
Binary file not shown.
18 changes: 18 additions & 0 deletions .mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.6/apache-maven-3.9.6-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.component.LifeCycle;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -41,7 +40,7 @@ public class ResourceHandlerFromFileSystemTest
@BeforeEach
public void startServer() throws Exception
{
Path resourcesRoot = MavenTestingUtils.getTargetTestingPath(ResourceHandlerFromFileSystemTest.class.getSimpleName());
Path resourcesRoot = StaticFileGen.tempDir("static-huge");
FS.ensureDirExists(resourcesRoot);

exampleSha = StaticFileGen.generate(resourcesRoot.resolve("example.png"), exampleSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.component.LifeCycle;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -42,7 +41,7 @@ public class ServletFileServerMultipleLocationsTest
@BeforeEach
public void startServer() throws Exception
{
Path resourcesRoot = MavenTestingUtils.getTargetTestingPath(ServletFileServerMultipleLocations.class.getSimpleName());
Path resourcesRoot = StaticFileGen.tempDir("static-huge");
FS.ensureDirExists(resourcesRoot);

exampleSha = StaticFileGen.generate(resourcesRoot.resolve("example.png"), exampleSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.Hex;
import org.eclipse.jetty.toolchain.test.Sha1Sum;
import org.eclipse.jetty.util.IO;
Expand Down Expand Up @@ -81,6 +82,14 @@ public static String generate(Path staticFile, long size) throws IOException, No
return Sha1Sum.calculate(staticFile);
}

public static Path tempDir(String directoryName)
{
Path systemTemp = Path.of(System.getProperty("java.io.tmpdir"));
Path tempDir = systemTemp.resolve(directoryName);
FS.ensureDirExists(tempDir);
return tempDir;
}

public static void verify(InputStream inputStream, long expectedSize, String expectedSha1) throws NoSuchAlgorithmException, IOException
{
MessageDigest digest = MessageDigest.getInstance("SHA1");
Expand Down
1 change: 1 addition & 0 deletions embedded/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<module>rewrite</module>
<module>servlet-config</module>
<module>servlet-security</module>
<module>servlet-server</module>
<module>simple-server</module>
<module>virtual-hosts</module>
<module>webapp-context</module>
Expand Down
47 changes: 47 additions & 0 deletions embedded/servlet-server/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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>11.0.x</version>
</parent>
<artifactId>servlet-server</artifactId>
<version>11.0.x</version>
<packaging>jar</packaging>
<name>Jetty Examples :: Jetty 11.0.x :: Embedded :: Servlet Server</name>

<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</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</groupId>
<artifactId>jetty-webapp</artifactId>
<version>${jetty.version}</version>
</dependency>

<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
<version>${jetty.version}</version>
<scope>test</scope>
</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>
118 changes: 118 additions & 0 deletions embedded/servlet-server/src/main/java/examples/EmbedMe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
//
// ========================================================================
// 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.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.webapp.WebAppContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EmbedMe
{
private static final Logger LOG = LoggerFactory.getLogger(EmbedMe.class);

public static void main(String[] args) throws Exception
{
int port = 8080;
Server server = newServer(port);
server.start();
server.join();
}

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

WebAppContext context = new WebAppContext();
Resource baseResource = findBaseResource(context);
LOG.info("Using BaseResource: {}", baseResource);
context.setBaseResource(baseResource);
context.setContextPath("/");
context.setWelcomeFiles(new String[]{"index.html", "welcome.html"});
context.setParentLoaderPriority(true);
server.setHandler(context);
return server;
}

private static Resource findBaseResource(WebAppContext context)
{
try
{
// Look for resource in classpath (this is the best choice when working with a jar/war archive)
ClassLoader classLoader = context.getClass().getClassLoader();
URL webXml = classLoader.getResource("/WEB-INF/web.xml");
if (webXml != null)
{
URI uri = webXml.toURI().resolve("../..").normalize();
LOG.info("Found WebResourceBase (Using ClassLoader reference) {}", uri);
return Resource.newResource(uri);
}
}
catch (URISyntaxException e)
{
throw new RuntimeException("Bad ClassPath reference for: WEB-INF", e);
}
catch (MalformedURLException e)
{
throw new RuntimeException("Bad ClassPath reference for: WEB-INF", e);
}

// Look for resource in common file system paths
try
{
Path pwd = Paths.get(System.getProperty("user.dir")).toAbsolutePath();
Path targetDir = pwd.resolve("target");
if (Files.isDirectory(targetDir))
{
try (Stream<Path> listing = Files.list(targetDir))
{
Path embeddedServletServerDir = listing
.filter(Files::isDirectory)
.filter((path) -> path.getFileName().toString().startsWith("embedded-servlet-server-"))
.findFirst()
.orElse(null);
if (embeddedServletServerDir != null)
{
LOG.info("Found WebResourceBase (Using /target/ Path) {}", embeddedServletServerDir);
return Resource.newResource(embeddedServletServerDir);
}
}
}

// Try the source path next
Path srcWebapp = pwd.resolve("src/main/webapp/");
if (Files.exists(srcWebapp))
{
LOG.info("WebResourceBase (Using /src/main/webapp/ Path) {}", srcWebapp);
return Resource.newResource(srcWebapp);
}
}
catch (Throwable t)
{
throw new RuntimeException("Unable to find web resource in file system", t);
}

throw new RuntimeException("Unable to find web resource ref");
}
}
31 changes: 31 additions & 0 deletions embedded/servlet-server/src/main/java/examples/TestServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// ========================================================================
// 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 javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
@WebServlet(urlPatterns = {"/test"})
public class TestServlet extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
request.getRequestDispatcher("/WEB-INF/html/index.html").forward(request, response);
}
}
32 changes: 32 additions & 0 deletions embedded/servlet-server/src/main/java/examples/TimeServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// ========================================================================
// 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.util.Date;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
@WebServlet(urlPatterns = {"/time"})
public class TimeServlet extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
{
response.setContentType("text/plain");
response.getWriter().write(new Date().toString());
}
}
11 changes: 11 additions & 0 deletions embedded/servlet-server/src/main/webapp/WEB-INF/html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<head>
<title>Content from WEB-INF/html</title>
</head>
<body>
<h1>Content from WEB-INF/html</h1>
<p>
This content from <code>{war}/WEB-INF/html/index.html</code>
</p>
</body>
</html>
8 changes: 8 additions & 0 deletions embedded/servlet-server/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Demo of Servlet 3.1</display-name>
</web-app>
17 changes: 17 additions & 0 deletions embedded/servlet-server/src/main/webapp/welcome.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<html>
<head>
<title>Welcome File</title>
</head>
<body>
<h1>Welcome</h1>
<p>
This is the <code>src/main/webapp/welcome.html</code>
</p>
<h2>Other servlets</h2>
<ul>
<li><a href="/test">/test</a> - a simple servlet that just returns content from <code>{war}/WEB-INF/html/</code> directory
</li>
<li><a href="/time">/time</a> - a simple servlet that just shows the server time</li>
</ul>
</body>
</html>
Loading

0 comments on commit 6211e0e

Please sign in to comment.