Skip to content

Commit

Permalink
Adding embedded-servlet-server 9.4.x
Browse files Browse the repository at this point in the history
  • Loading branch information
joakime committed Feb 5, 2024
1 parent 6e9ead8 commit cd07145
Show file tree
Hide file tree
Showing 10 changed files with 328 additions and 0 deletions.
1 change: 1 addition & 0 deletions embedded/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,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
41 changes: 41 additions & 0 deletions embedded/servlet-server/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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>9.4.x</version>
</parent>
<artifactId>servlet-server</artifactId>
<version>9.4.x</version>
<packaging>jar</packaging>
<name>Jetty Examples :: Jetty 9.4.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-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.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.webapp.WebAppContext;

public class EmbedMe
{
private static final Logger LOG = Log.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>
67 changes: 67 additions & 0 deletions embedded/servlet-server/src/test/java/examples/EmbedMeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// ========================================================================
// 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.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Request;
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;
private HttpClient client;

@BeforeEach
public void startServer() throws Exception
{
server = EmbedMe.newServer(0);
server.start();
}

@BeforeEach
public void startClient() throws Exception
{
client = new HttpClient();
client.start();
}

@AfterEach
public void stopAll()
{
LifeCycle.stop(client);
LifeCycle.stop(server);
}

@Test
public void testGetWelcome() throws InterruptedException, ExecutionException, TimeoutException
{
Request request = client.newRequest(server.getURI().resolve("/"))
.method("GET");
ContentResponse response = request.send();
assertThat(response.getStatus(), is(200));
assertThat(response.getContentAsString(), containsString("<title>Welcome File</title>"));
}
}
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

0 comments on commit cd07145

Please sign in to comment.