Added core demo

This commit is contained in:
Greg Wilkins 2022-07-14 21:54:50 +10:00
parent cdc1b079ae
commit 235f34c2e2
8 changed files with 352 additions and 14 deletions

View File

@ -236,16 +236,16 @@
<artifactId>jetty-nosql</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.${ee.version}</groupId>
<artifactId>jetty-${ee.version}-websocket-jetty-client</artifactId>
<groupId>org.eclipse.jetty.ee9.websocket</groupId>
<artifactId>jetty-ee9-websocket-jetty-client</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.${ee.version}</groupId>
<artifactId>jetty-${ee.version}-websocket-jakarta-server</artifactId>
<groupId>org.eclipse.jetty.ee9.websocket</groupId>
<artifactId>jetty-ee9-websocket-jakarta-server</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.${ee.version}</groupId>
<artifactId>jetty-${ee.version}-websocket-jetty-server</artifactId>
<groupId>org.eclipse.jetty.ee9.websocket</groupId>
<artifactId>jetty-ee9-websocket-jetty-server</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -11,17 +11,20 @@
// ========================================================================
//
package org.eclipse.jetty.server.handler;
package org.eclipse.jetty.docs.programming.server;
import java.io.IOException;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.Flow;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.io.Content;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
@ -29,13 +32,15 @@ import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.util.Blocker;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.thread.SerializedInvoker;
public class HandlerDocs
{
// Work in progress
// TODO this class has been temporarily moved to jetty-core/jetty-demo
// move it back when documentation is re-enabled
public static class HelloHandler extends Handler.Abstract
public static class HelloHandler0 extends Handler.Abstract
{
@Override
public Request.Processor handle(Request request) throws Exception
@ -49,7 +54,7 @@ public class HandlerDocs
}
}
public static class HelloHandler2 extends Handler.Abstract
public static class HelloHandler1 extends Handler.Abstract
{
@Override
public Request.Processor handle(Request request) throws Exception
@ -65,7 +70,7 @@ public class HandlerDocs
}
}
public static class HelloHandler3 extends Handler.Processor.NonBlocking
public static class HelloHandler2 extends Handler.Processor.NonBlocking
{
@Override
public void process(Request request, Response response, Callback callback)
@ -76,7 +81,7 @@ public class HandlerDocs
}
}
public static class HelloHandler35 extends Handler.Processor.NonBlocking
public static class HelloHandler3 extends Handler.Processor.NonBlocking
{
@Override
public void process(Request request, Response response, Callback callback) throws IOException
@ -193,4 +198,39 @@ public class HandlerDocs
Content.copy(request, response, callback);
}
}
public static class RootHandler extends Handler.Collection
{
@Override
public Request.Processor handle(Request request) throws Exception
{
final StringBuilder index = new StringBuilder();
index.append("<h2>Handler Demos</h2>\n<ul>\n");
for (Handler handler : getHandlers())
{
String name = handler.getClass().getSimpleName().replace("Handler", "");
String path = "/" + name;
if (request.getPathInContext().equals(name))
{
Request.Processor processor = handler.handle(request);
if (processor != null)
return processor;
}
index.append("<li><a href=\"")
.append(URIUtil.addPaths(request.getContext().getContextPath(), path))
.append("\">")
.append(name)
.append("</a></li>\n");
}
index.append("</ul>");
return (req, res, callback) ->
{
res.setStatus(200);
res.getHeaders().add(HttpHeader.CONTENT_TYPE, MimeTypes.Type.TEXT_HTML_UTF_8.asString());
Content.Sink.write(res, true, index.toString(), callback);
};
}
}
}

View File

@ -0,0 +1,52 @@
<?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">
<parent>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-core</artifactId>
<version>12.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>jetty-demo</artifactId>
<name>Jetty Core :: Demo</name>
<properties>
<bundle-symbolic-name>${project.groupId}.demo</bundle-symbolic-name>
<spotbugs.onlyAnalyze>org.eclipse.jetty.demo.*</spotbugs.onlyAnalyze>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>
@{argLine} ${jetty.surefire.argLine} --add-reads org.eclipse.jetty.io=org.eclipse.jetty.logging
</argLine>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jmx</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-slf4j-impl</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.toolchain</groupId>
<artifactId>jetty-test-helper</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
<!-- ==================================================================
Configure and deploy the test web application
===================================================================== -->
<Configure id="coreDemo" class="org.eclipse.jetty.server.handler.ContextHandler">
<Set name="handler">
<New class="org.eclipse.jetty.core.demo.HandlerDocs.RootHandler">
<Call name="addHandler"><Arg><New class="org.eclipse.jetty.core.demo.HandlerDocs.HelloHandler0"/></Arg></Call>
<Call name="addHandler"><Arg><New class="org.eclipse.jetty.core.demo.HandlerDocs.HelloHandler1"/></Arg></Call>
</New>
</Set>
</Configure>

View File

@ -0,0 +1,231 @@
//
// ========================================================================
// Copyright (c) 1995-2022 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 org.eclipse.jetty.core.demo;
import java.io.IOException;
import java.io.PrintStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.Flow;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.io.Content;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.util.Blocker;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.thread.SerializedInvoker;
public class HandlerDocs
{
public static class HelloHandler0 extends Handler.Abstract
{
@Override
public Request.Processor handle(Request request) throws Exception
{
return (req, response, callback) ->
{
response.setStatus(200);
response.getHeaders().add(HttpHeader.CONTENT_LENGTH, "text/plain");
response.write(true, BufferUtil.toBuffer("Hello World\n"), callback);
};
}
}
public static class HelloHandler1 extends Handler.Abstract
{
@Override
public Request.Processor handle(Request request) throws Exception
{
return this::process;
}
private void process(Request request, Response response, Callback callback)
{
response.setStatus(200);
response.getHeaders().add(HttpHeader.CONTENT_LENGTH, "text/plain");
Content.Sink.write(response, true, "Hello World", callback);
}
}
public static class HelloHandler2 extends Handler.Processor.NonBlocking
{
@Override
public void process(Request request, Response response, Callback callback)
{
response.setStatus(200);
response.getHeaders().add(HttpHeader.CONTENT_LENGTH, "text/plain");
response.write(true, BufferUtil.toBuffer("Hello World\n"), callback);
}
}
public static class HelloHandler3 extends Handler.Processor.NonBlocking
{
@Override
public void process(Request request, Response response, Callback callback) throws IOException
{
response.setStatus(200);
response.getHeaders().add(HttpHeader.CONTENT_LENGTH, "text/plain");
Blocker.Shared blocker = new Blocker.Shared();
try (Blocker.Callback cb = blocker.callback())
{
response.write(true, BufferUtil.toBuffer("Hello "), callback);
cb.block();
}
try (Blocker.Callback cb = blocker.callback())
{
response.write(true, BufferUtil.toBuffer("World\n"), callback);
cb.block();
}
callback.succeeded();
}
}
public static class HelloHandler4 extends Handler.Processor.Blocking
{
@Override
public void process(Request request, Response response, Callback callback) throws IOException
{
response.setStatus(200);
response.getHeaders().add(HttpHeader.CONTENT_LENGTH, "text/plain");
try (PrintStream out = new PrintStream(Content.Sink.asOutputStream(response)))
{
out.print("Hello ");
out.println("World");
callback.succeeded();
}
catch (Throwable t)
{
callback.failed(t);
}
}
}
public static class HelloHandler5 extends Handler.Processor.NonBlocking
{
@Override
public void process(Request request, Response response, Callback callback) throws IOException
{
response.setStatus(200);
response.getHeaders().add(HttpHeader.CONTENT_LENGTH, "text/plain");
new HelloWorldPublisher().subscribe(Content.Sink.asSubscriber(response, callback));
}
}
public static class HelloWorldPublisher implements Flow.Publisher<Content.Chunk>
{
@Override
public void subscribe(Flow.Subscriber<? super Content.Chunk> subscriber)
{
final SerializedInvoker invoker = new SerializedInvoker();
final Queue<Content.Chunk> chunks = new LinkedList<>(List.of(
Content.Chunk.from(BufferUtil.toBuffer("Hello "), false),
Content.Chunk.from(BufferUtil.toBuffer("World "), false),
Content.Chunk.EOF));
subscriber.onSubscribe(new Flow.Subscription()
{
@Override
public void request(long n)
{
while (n-- > 0 && !chunks.isEmpty())
invoker.run(() -> subscriber.onNext(chunks.poll()));
}
@Override
public void cancel()
{
subscriber.onNext(Content.Chunk.from(new IOException("Cancelled")));
}
});
}
}
public static class DescriminatingGreeterHandler extends Handler.Processor.NonBlocking
{
@Override
public Request.Processor handle(Request request) throws Exception
{
if (HttpMethod.GET.is(request.getMethod()) &&
"greeting".equals(request.getPathInContext()))
return this;
return null;
}
@Override
public void process(Request request, Response response, Callback callback)
{
response.setStatus(200);
response.getHeaders().add(HttpHeader.CONTENT_LENGTH, "text/plain");
response.write(true, BufferUtil.toBuffer("Hello World\n"), callback);
}
}
public static class EchoHandler extends Handler.Processor.NonBlocking
{
@Override
public void process(Request request, Response response, Callback callback)
{
response.setStatus(200);
response.getHeaders().put(HttpHeader.CONTENT_TYPE, request.getHeaders().get(HttpHeader.CONTENT_TYPE));
long contentLength = request.getHeaders().getLongField(HttpHeader.CONTENT_LENGTH);
if (contentLength >= 0)
response.getHeaders().putLongField(HttpHeader.CONTENT_LENGTH, contentLength);
Content.copy(request, response, callback);
}
}
public static class RootHandler extends Handler.Collection
{
@Override
public Request.Processor handle(Request request) throws Exception
{
final StringBuilder index = new StringBuilder();
index.append("<h2>Handler Demos</h2>\n<ul>\n");
for (Handler handler : getHandlers())
{
String name = handler.getClass().getSimpleName().replace("Handler", "");
String path = "/" + name;
if (request.getPathInContext().equals(name))
{
Request.Processor processor = handler.handle(request);
if (processor != null)
return processor;
}
index.append("<li><a href=\"")
.append(URIUtil.addPaths(request.getContext().getContextPath(), path))
.append("\">")
.append(name)
.append("</a></li>\n");
}
index.append("</ul>");
return (req, res, callback) ->
{
res.setStatus(200);
res.getHeaders().add(HttpHeader.CONTENT_TYPE, MimeTypes.Type.TEXT_HTML_UTF_8.asString());
Content.Sink.write(res, true, index.toString(), callback);
};
}
}
}

View File

@ -347,7 +347,6 @@ public class ContextProvider extends ScanningAppProvider
if (!Files.exists(path))
throw new IllegalStateException("App resource does not exist " + path);
// prepare properties
Map<String, String> properties = new HashMap<>();
properties.putAll(getProperties());

View File

@ -16,6 +16,7 @@
<module>jetty-bom</module>
<module>jetty-client</module>
<module>jetty-deploy</module>
<module>jetty-demo</module>
<module>jetty-ee</module>
<module>jetty-fcgi</module>
<module>jetty-http</module>

View File

@ -193,7 +193,7 @@
<module>jetty-home</module>
<module>tests</module>
<!-- <module>jetty-p2</module> -->
<!-- <module>documentation</module> -->
<!-- <module>documentation</module>-->
</modules>
<build>