-
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.
Finish adding examples from old embedded-jetty-cookbook
- Loading branch information
Showing
43 changed files
with
11,716 additions
and
0 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,27 @@ | ||
<?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>compressed-encoding</artifactId> | ||
<version>9.4.x</version> | ||
<packaging>jar</packaging> | ||
<name>Jetty Examples :: Jetty 9.4.x :: Embedded :: HTML Compressed Encoding</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-servlet</artifactId> | ||
<version>${jetty.version}</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
69 changes: 69 additions & 0 deletions
69
embedded/compressed-encoding/src/main/java/examples/GzipRequestResponseExample.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,69 @@ | ||
// | ||
// ======================================================================== | ||
// 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.File; | ||
import java.net.URI; | ||
import java.net.URL; | ||
import java.nio.file.Path; | ||
|
||
import org.eclipse.jetty.server.Server; | ||
import org.eclipse.jetty.server.ServerConnector; | ||
import org.eclipse.jetty.server.handler.gzip.GzipHandler; | ||
import org.eclipse.jetty.servlet.DefaultServlet; | ||
import org.eclipse.jetty.servlet.ServletContextHandler; | ||
import org.eclipse.jetty.util.resource.PathResource; | ||
|
||
public class GzipRequestResponseExample | ||
{ | ||
public static void main(String[] args) throws Exception | ||
{ | ||
Server server = new Server(); | ||
ServerConnector connector = new ServerConnector(server); | ||
connector.setPort(8080); | ||
server.addConnector(connector); | ||
|
||
GzipHandler gzip = new GzipHandler(); | ||
gzip.setIncludedMethods("GET", "POST"); | ||
gzip.setMinGzipSize(245); // Enable Request Decompression | ||
gzip.setIncludedMimeTypes("text/plain", "text/css", "text/html", | ||
"application/javascript"); | ||
server.setHandler(gzip); | ||
|
||
ClassLoader cl = GzipRequestResponseExample.class.getClassLoader(); | ||
// We look for a file, as ClassLoader.getResource() is not | ||
// designed to look for directories (we resolve the directory later) | ||
URL f = cl.getResource("static-root/hello.html"); | ||
if (f == null) | ||
{ | ||
throw new RuntimeException("Unable to find resource directory"); | ||
} | ||
|
||
// Resolve file to directory | ||
URI webRootUri = f.toURI().resolve("./").normalize(); | ||
System.err.println("WebRoot is " + webRootUri); | ||
|
||
ServletContextHandler context = new ServletContextHandler(); | ||
gzip.setHandler(context); | ||
context.setContextPath("/"); | ||
context.setBaseResource(new PathResource(webRootUri)); | ||
context.setWelcomeFiles(new String[]{"index.html"}); | ||
|
||
// Adding Servlets | ||
context.addServlet(DefaultServlet.class, "/"); // always last, always on "/" | ||
|
||
server.start(); | ||
server.join(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
embedded/compressed-encoding/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> |
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,40 @@ | ||
<?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>deploying</artifactId> | ||
<version>9.4.x</version> | ||
<packaging>jar</packaging> | ||
<name>Jetty Examples :: Jetty 9.4.x :: Embedded :: Deploying</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-deploy</artifactId> | ||
<version>${jetty.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.eclipse.jetty</groupId> | ||
<artifactId>jetty-xml</artifactId> | ||
<version>${jetty.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.eclipse.jetty</groupId> | ||
<artifactId>jetty-servlet</artifactId> | ||
<version>${jetty.version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
136 changes: 136 additions & 0 deletions
136
embedded/deploying/src/main/java/examples/DelayedWebAppDeployExample.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,136 @@ | ||
// | ||
// ======================================================================== | ||
// 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.File; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.concurrent.TimeUnit; | ||
import javax.servlet.ServletContextEvent; | ||
import javax.servlet.ServletContextListener; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.eclipse.jetty.server.Request; | ||
import org.eclipse.jetty.server.Server; | ||
import org.eclipse.jetty.server.handler.AbstractHandler; | ||
import org.eclipse.jetty.server.handler.ContextHandlerCollection; | ||
import org.eclipse.jetty.server.handler.HandlerList; | ||
import org.eclipse.jetty.webapp.WebAppContext; | ||
|
||
/** | ||
* Demonstrate an Embedded Jetty server where the server | ||
* is available to answer requests immediately, but only return HTTP status code 503 | ||
* until the webapps themselves are available to respond to requests. | ||
*/ | ||
public class DelayedWebAppDeployExample | ||
{ | ||
public static void main(String[] args) throws Exception | ||
{ | ||
Server server = new Server(8080); | ||
|
||
// where all webapps will be deployed to. | ||
ContextHandlerCollection contexts = new ContextHandlerCollection(); | ||
|
||
// The server handler list | ||
HandlerList handlers = new HandlerList(); | ||
handlers.addHandler(contexts); | ||
handlers.addHandler(new UnavailableHandler()); | ||
|
||
server.setHandler(handlers); | ||
server.start(); | ||
|
||
Path warPath = new File("webapps/hello.war").toPath().toRealPath(); | ||
System.err.println("WAR File is " + warPath); | ||
|
||
server.getThreadPool().execute(new DelayedDeploymentTask(contexts, warPath, "/")); | ||
|
||
server.join(); | ||
} | ||
|
||
public static class UnavailableHandler extends AbstractHandler | ||
{ | ||
@Override | ||
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException | ||
{ | ||
// Indicate a 503 status | ||
response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE); | ||
// tell jetty that this request was handled (making others handlers not run for this request) | ||
baseRequest.setHandled(true); | ||
} | ||
} | ||
|
||
public static class DelayedDeploymentTask implements Runnable | ||
{ | ||
private final ContextHandlerCollection contexts; | ||
private final Path warPath; | ||
private final String contextPath; | ||
|
||
public DelayedDeploymentTask(ContextHandlerCollection contexts, Path warPath, String contextPath) | ||
{ | ||
this.contexts = contexts; | ||
this.warPath = warPath; | ||
this.contextPath = contextPath; | ||
} | ||
|
||
@Override | ||
public void run() | ||
{ | ||
try | ||
{ | ||
WebAppContext webapp = new WebAppContext(); | ||
webapp.setContextPath(contextPath); | ||
webapp.setWar(warPath.toUri().toASCIIString()); | ||
|
||
// Simulate a slow to initialize listener - this is for demo purposes only | ||
webapp.addEventListener(new SlowInitListener()); | ||
|
||
contexts.addHandler(webapp); | ||
webapp.start(); | ||
} | ||
catch (Exception e) | ||
{ | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
public static class SlowInitListener implements ServletContextListener | ||
{ | ||
@Override | ||
public void contextInitialized(ServletContextEvent sce) | ||
{ | ||
try | ||
{ | ||
// simulate a listener that takes a while to initialize | ||
// this should give the server some time to reply with 503 | ||
// until such time that this listener completes and the | ||
// rest of the webapp can finish its context init successfully. | ||
TimeUnit.SECONDS.sleep(10); | ||
System.out.printf("%s is done, try loading the %s webapp now%n", this.getClass().getName(), sce.getServletContext().getContextPath()); | ||
} | ||
catch (InterruptedException e) | ||
{ | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public void contextDestroyed(ServletContextEvent sce) | ||
{ | ||
|
||
} | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
embedded/deploying/src/main/java/examples/WebAppsHotDeployExample.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,104 @@ | ||
// | ||
// ======================================================================== | ||
// 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.File; | ||
import java.io.FileNotFoundException; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.eclipse.jetty.deploy.App; | ||
import org.eclipse.jetty.deploy.AppLifeCycle; | ||
import org.eclipse.jetty.deploy.DeploymentManager; | ||
import org.eclipse.jetty.deploy.graph.Node; | ||
import org.eclipse.jetty.deploy.providers.WebAppProvider; | ||
import org.eclipse.jetty.server.Handler; | ||
import org.eclipse.jetty.server.Server; | ||
import org.eclipse.jetty.server.handler.ContextHandler; | ||
import org.eclipse.jetty.server.handler.ContextHandlerCollection; | ||
import org.eclipse.jetty.server.handler.DefaultHandler; | ||
import org.eclipse.jetty.server.handler.HandlerCollection; | ||
|
||
public class WebAppsHotDeployExample | ||
{ | ||
public static void main(String[] args) throws Exception | ||
{ | ||
Server server = new Server(8080); | ||
|
||
ContextHandlerCollection contexts = new ContextHandlerCollection(); | ||
HandlerCollection handlers = new HandlerCollection(); | ||
|
||
handlers.setHandlers(new Handler[]{contexts, new DefaultHandler()}); | ||
|
||
server.setHandler(handlers); | ||
|
||
Path confFile = Paths.get(System.getProperty("user.dir"), "example.conf"); | ||
|
||
ContextAttributeCustomizer contextAttributeCustomizer = new ContextAttributeCustomizer(); | ||
contextAttributeCustomizer.setAttribute("common.conf", confFile); | ||
|
||
DeploymentManager deploymentManager = new DeploymentManager(); | ||
deploymentManager.setContexts(contexts); | ||
deploymentManager.addLifeCycleBinding(contextAttributeCustomizer); | ||
|
||
String jettyBaseProp = System.getProperty("jetty.base"); | ||
if (jettyBaseProp == null) | ||
{ | ||
throw new FileNotFoundException("Missing System Property 'jetty.base'"); | ||
} | ||
Path jettyBase = new File(jettyBaseProp).toPath().toAbsolutePath(); | ||
|
||
WebAppProvider webAppProvider = new WebAppProvider(); | ||
webAppProvider.setMonitoredDirName(jettyBase.resolve("webapps").toString()); | ||
|
||
deploymentManager.addAppProvider(webAppProvider); | ||
server.addBean(deploymentManager); | ||
|
||
// Lets dump the server after start. | ||
// We can look for the deployed contexts, along with an example of the | ||
// result of ContextAttributesCustomizer in the dump section for "Handler attributes" | ||
server.setDumpAfterStart(true); | ||
server.start(); | ||
server.join(); | ||
} | ||
|
||
public static class ContextAttributeCustomizer implements AppLifeCycle.Binding | ||
{ | ||
public final Map<String, Object> attributes = new HashMap<>(); | ||
|
||
public void setAttribute(String name, Object value) | ||
{ | ||
this.attributes.put(name, value); | ||
} | ||
|
||
@Override | ||
public String[] getBindingTargets() | ||
{ | ||
return new String[]{AppLifeCycle.DEPLOYING}; | ||
} | ||
|
||
@Override | ||
public void processBinding(Node node, App app) throws Exception | ||
{ | ||
ContextHandler handler = app.getContextHandler(); | ||
if (handler == null) | ||
{ | ||
throw new NullPointerException("No Handler created for App: " + app); | ||
} | ||
attributes.forEach((name, value) -> handler.setAttribute(name, value)); | ||
} | ||
} | ||
} |
Oops, something went wrong.