-
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.
Merge remote-tracking branch 'origin/11.0.x' into 12.0.x
- Loading branch information
Showing
18 changed files
with
878 additions
and
4 deletions.
There are no files selected for viewing
Binary file not shown.
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,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 |
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
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,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>12.0.x</version> | ||
</parent> | ||
<artifactId>ee10-servlet-server</artifactId> | ||
<version>12.0.x</version> | ||
<packaging>jar</packaging> | ||
<name>Jetty Examples :: Jetty 12.0.x :: Embedded :: EE10 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.ee10</groupId> | ||
<artifactId>jetty-ee10-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> |
117 changes: 117 additions & 0 deletions
117
embedded/ee10-servlet-server/src/main/java/examples/EmbedMe.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,117 @@ | ||
// | ||
// ======================================================================== | ||
// 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.ee10.webapp.WebAppContext; | ||
import org.eclipse.jetty.util.resource.ResourceFactory; | ||
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) | ||
{ | ||
ResourceFactory resourceFactory = ResourceFactory.of(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 resourceFactory.newResource(uri); | ||
} | ||
} | ||
catch (URISyntaxException e) | ||
{ | ||
throw new RuntimeException("Bad ClassPath reference for: WEB-INF", e); | ||
} | ||
|
||
// Look for resource in common file system paths | ||
try | ||
{ | ||
Path pwd = Path.of(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 resourceFactory.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 resourceFactory.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
31
embedded/ee10-servlet-server/src/main/java/examples/TestServlet.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,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 jakarta.servlet.ServletException; | ||
import jakarta.servlet.annotation.WebServlet; | ||
import jakarta.servlet.http.HttpServlet; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.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
32
embedded/ee10-servlet-server/src/main/java/examples/TimeServlet.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,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 jakarta.servlet.annotation.WebServlet; | ||
import jakarta.servlet.http.HttpServlet; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.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
11
embedded/ee10-servlet-server/src/main/webapp/WEB-INF/html/index.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,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> |
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 @@ | ||
<?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> |
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,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> |
61 changes: 61 additions & 0 deletions
61
embedded/ee10-servlet-server/src/test/java/examples/EmbedMeTest.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,61 @@ | ||
// | ||
// ======================================================================== | ||
// 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.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import org.eclipse.jetty.server.Server; | ||
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 org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
public class EmbedMeTest | ||
{ | ||
private Server server; | ||
|
||
@BeforeEach | ||
public void startServer() throws Exception | ||
{ | ||
server = EmbedMe.newServer(0); | ||
server.start(); | ||
} | ||
|
||
@AfterEach | ||
public void stopServer() | ||
{ | ||
LifeCycle.stop(server); | ||
} | ||
|
||
@Test | ||
public void testGetWelcome() throws IOException, InterruptedException | ||
{ | ||
HttpClient client = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).build(); | ||
HttpRequest request = HttpRequest.newBuilder() | ||
.uri(server.getURI().resolve("/")) | ||
.GET() | ||
.build(); | ||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8)); | ||
assertThat(response.statusCode(), is(200)); | ||
assertThat(response.body(), containsString("<title>Welcome File</title>")); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
embedded/ee10-servlet-server/src/test/resources/jetty-logging.properties
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,2 @@ | ||
# Jetty Logging using jetty-slf4j-impl | ||
org.eclipse.jetty.LEVEL=INFO |
Oops, something went wrong.