migrate documentation to Antora
This commit is contained in:
parent
1995b5ea59
commit
f3bc547622
|
@ -0,0 +1,2 @@
|
||||||
|
/.asciidoctorconfig
|
||||||
|
/provided-antora-playbook.yml
|
|
@ -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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Jetty Documentation
|
||||||
|
|
||||||
|
This project is the root of the Jetty documentation.
|
||||||
|
The content files in this project get sourced by the Antora playbook in the playbook repository that builds the website.
|
||||||
|
|
||||||
|
In order to build the documentation locally, you first need to prepare a jetty-home directory by running the following command from the top-level folder of the Jetty project:
|
||||||
|
|
||||||
|
$ mvn install -Dcollector -Pfast -am -pl documentation/jetty
|
||||||
|
|
||||||
|
Then you can use the following command from this directory to prepare and run Antora using a preview profile:
|
||||||
|
|
||||||
|
$ mvn antora -N
|
||||||
|
|
||||||
|
If you don't run the first command, the Antora build will still succeed, but you will get warnings about missing includes for files taken from jetty-home.
|
||||||
|
|
||||||
|
The `antora:antora` goal, which the `antora` lifecycle invokes, takes advantage of the playbook provider feature so the playbook for this branch can be centrally managed in the playbook repository.
|
||||||
|
|
||||||
|
Note that this preview profile does not run the jetty blocks, so you will only see the configuration for those runs in the preview site.
|
||||||
|
If you want to build the full site, use the build in the playbook repository.
|
|
@ -0,0 +1,25 @@
|
||||||
|
name: jetty
|
||||||
|
version: '10'
|
||||||
|
title: Eclipse Jetty
|
||||||
|
asciidoc:
|
||||||
|
attributes:
|
||||||
|
javadoc-url: https://eclipse.dev/jetty/javadoc/jetty-10
|
||||||
|
jdurl: '{javadoc-url}'
|
||||||
|
jetty-home: ${jetty.home}@
|
||||||
|
version: 10.0.21-SNAPSHOT
|
||||||
|
idprefix: ''
|
||||||
|
idseparator: ''
|
||||||
|
run-jetty-classpath: ${settings.localRepository}/org/eclipse/jetty/tests/jetty-home-tester/${project.version}/jetty-home-tester-${project.version}.jar${path.separator}${run.jetty.classpath}
|
||||||
|
nav:
|
||||||
|
- modules/operations-guide/nav.adoc
|
||||||
|
- modules/programming-guide/nav.adoc
|
||||||
|
ext:
|
||||||
|
collector:
|
||||||
|
- run:
|
||||||
|
command: mvn install -ntp -B -Dcollector -Pfast -am -pl documentation/jetty
|
||||||
|
scan:
|
||||||
|
dir: documentation/jetty/target/collector
|
||||||
|
- scan:
|
||||||
|
dir: jetty-server/src/main/java
|
||||||
|
files: org/eclipse/jetty/server/CustomRequestLog.java
|
||||||
|
base: modules/code/partials
|
|
@ -0,0 +1,24 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Eclipse Jetty
|
||||||
|
|
||||||
|
This section of the site contains the documentation for {page-component-title} {page-version}.
|
||||||
|
|
||||||
|
== xref:operations-guide:index.adoc[]
|
||||||
|
|
||||||
|
The Eclipse Jetty Operations Guide targets sysops, devops, and developers who want to install Eclipse Jetty as a standalone server to deploy web applications.
|
||||||
|
|
||||||
|
== xref:programming-guide:index.adoc[]
|
||||||
|
|
||||||
|
The Eclipse Jetty Programming Guide targets developers who want to use the Eclipse Jetty libraries in their applications, and advanced sysops/devops that want to customize the deployment of web applications.
|
|
@ -0,0 +1,8 @@
|
||||||
|
[description]
|
||||||
|
JPMS Configuration Module
|
||||||
|
|
||||||
|
[ini]
|
||||||
|
--jpms
|
||||||
|
|
||||||
|
[jpms]
|
||||||
|
# Additional JPMS configuration.
|
|
@ -0,0 +1,6 @@
|
||||||
|
[description]
|
||||||
|
JVM Options Module
|
||||||
|
|
||||||
|
[exec]
|
||||||
|
-Xmx1g
|
||||||
|
-Xlog:gc*,gc+stats=off:file=logs/gc.log:time,level,tags
|
|
@ -0,0 +1,15 @@
|
||||||
|
[description]
|
||||||
|
Postgres JDBC Driver Module
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
lib/postgresql-${postgresql-version}.jar
|
||||||
|
|
||||||
|
[files]
|
||||||
|
maven://org.postgresql/postgresql/${postgresql-version}|lib/postgresql-${postgresql-version}.jar
|
||||||
|
|
||||||
|
[ini]
|
||||||
|
postgresql-version?=42.2.18
|
||||||
|
|
||||||
|
[ini-template]
|
||||||
|
## Postgres JDBC version.
|
||||||
|
# postgresql-version=42.2.18
|
|
@ -0,0 +1,5 @@
|
||||||
|
[description]
|
||||||
|
Enables remote debugging
|
||||||
|
|
||||||
|
[exec]
|
||||||
|
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005
|
|
@ -0,0 +1,203 @@
|
||||||
|
<?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.documentation</groupId>
|
||||||
|
<artifactId>documentation-parent</artifactId>
|
||||||
|
<version>10.0.21-SNAPSHOT</version>
|
||||||
|
<relativePath>../../../../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
<artifactId>code-examples</artifactId>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
<name>Documentation :: Code Examples</name>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<jacoco.skip>true</jacoco.skip>
|
||||||
|
<javadoc.skip>true</javadoc.skip>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>infinispan-embedded-query</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>infinispan-remote-query</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-alpn-server</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-client</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-jmx</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-nosql</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-rewrite</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-server</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-servlets</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-unixdomain-server</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-util-ajax</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty.fcgi</groupId>
|
||||||
|
<artifactId>fcgi-client</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty.gcloud</groupId>
|
||||||
|
<artifactId>jetty-gcloud-session-manager</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty.http2</groupId>
|
||||||
|
<artifactId>http2-http-client-transport</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty.http2</groupId>
|
||||||
|
<artifactId>http2-server</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty.http3</groupId>
|
||||||
|
<artifactId>http3-http-client-transport</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty.http3</groupId>
|
||||||
|
<artifactId>http3-server</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty.memcached</groupId>
|
||||||
|
<artifactId>jetty-memcached-sessions</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!--<dependency>
|
||||||
|
<groupId>org.eclipse.jetty.toolchain</groupId>
|
||||||
|
<artifactId>jetty-jakarta-servlet-api</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty.websocket</groupId>
|
||||||
|
<artifactId>websocket-jakarta-server</artifactId>
|
||||||
|
</dependency>-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty.websocket</groupId>
|
||||||
|
<artifactId>websocket-javax-server</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty.websocket</groupId>
|
||||||
|
<artifactId>websocket-jetty-client</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty.websocket</groupId>
|
||||||
|
<artifactId>websocket-jetty-server</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>compile-code-examples</id>
|
||||||
|
<goals>
|
||||||
|
<goal>compile</goal>
|
||||||
|
</goals>
|
||||||
|
<phase>compile</phase>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-enforcer-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<fail>false</fail>
|
||||||
|
<rules>
|
||||||
|
<requireJavaVersion>
|
||||||
|
<version>[21,)</version>
|
||||||
|
<message>[ERROR] OLD JDK [${java.version}] in use. Jetty documentation ${project.version} MUST use JDK 21 or newer</message>
|
||||||
|
</requireJavaVersion>
|
||||||
|
</rules>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<profiles>
|
||||||
|
<profile>
|
||||||
|
<id>jdk17-18</id>
|
||||||
|
<activation>
|
||||||
|
<jdk>[17,19)</jdk>
|
||||||
|
</activation>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<excludes>
|
||||||
|
<exclude>**/ArchitectureDocs.java</exclude>
|
||||||
|
</excludes>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</profile>
|
||||||
|
<profile>
|
||||||
|
<id>jdk19-20</id>
|
||||||
|
<activation>
|
||||||
|
<jdk>[19,21)</jdk>
|
||||||
|
</activation>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>${java.specification.version}</source>
|
||||||
|
<target>${java.specification.version}</target>
|
||||||
|
<release>${java.specification.version}</release>
|
||||||
|
<enablePreview>true</enablePreview>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</profile>
|
||||||
|
<profile>
|
||||||
|
<id>jdk21+</id>
|
||||||
|
<activation>
|
||||||
|
<jdk>[21,)</jdk>
|
||||||
|
</activation>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>${java.specification.version}</source>
|
||||||
|
<target>${java.specification.version}</target>
|
||||||
|
<release>${java.specification.version}</release>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</profile>
|
||||||
|
</profiles>
|
||||||
|
</project>
|
|
@ -0,0 +1,284 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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 org.eclipse.jetty.docs.programming;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.server.Server;
|
||||||
|
import org.eclipse.jetty.util.component.AbstractLifeCycle;
|
||||||
|
import org.eclipse.jetty.util.component.Container;
|
||||||
|
import org.eclipse.jetty.util.component.ContainerLifeCycle;
|
||||||
|
import org.eclipse.jetty.util.component.LifeCycle;
|
||||||
|
|
||||||
|
import static java.lang.System.Logger.Level.INFO;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class ComponentDocs
|
||||||
|
{
|
||||||
|
public void start() throws Exception
|
||||||
|
{
|
||||||
|
// tag::start[]
|
||||||
|
class Monitor extends AbstractLifeCycle
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
class Root extends ContainerLifeCycle
|
||||||
|
{
|
||||||
|
// Monitor is an internal component.
|
||||||
|
private final Monitor monitor = new Monitor();
|
||||||
|
|
||||||
|
public Root()
|
||||||
|
{
|
||||||
|
// The Monitor life cycle is managed by Root.
|
||||||
|
addManaged(monitor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Service extends ContainerLifeCycle
|
||||||
|
{
|
||||||
|
// An instance of the Java scheduler service.
|
||||||
|
private ScheduledExecutorService scheduler;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doStart() throws Exception
|
||||||
|
{
|
||||||
|
// Java's schedulers cannot be restarted, so they must
|
||||||
|
// be created anew every time their container is started.
|
||||||
|
scheduler = Executors.newSingleThreadScheduledExecutor();
|
||||||
|
// Even if Java scheduler does not implement
|
||||||
|
// LifeCycle, make it part of the component tree.
|
||||||
|
addBean(scheduler);
|
||||||
|
// Start all the children beans.
|
||||||
|
super.doStart();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doStop() throws Exception
|
||||||
|
{
|
||||||
|
// Perform the opposite operations that were
|
||||||
|
// performed in doStart(), in reverse order.
|
||||||
|
super.doStop();
|
||||||
|
removeBean(scheduler);
|
||||||
|
scheduler.shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a Root instance.
|
||||||
|
Root root = new Root();
|
||||||
|
|
||||||
|
// Create a Service instance.
|
||||||
|
Service service = new Service();
|
||||||
|
|
||||||
|
// Link the components.
|
||||||
|
root.addBean(service);
|
||||||
|
|
||||||
|
// Start the root component to
|
||||||
|
// start the whole component tree.
|
||||||
|
root.start();
|
||||||
|
// end::start[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void restart() throws Exception
|
||||||
|
{
|
||||||
|
// tag::restart[]
|
||||||
|
class Root extends ContainerLifeCycle
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
class Service extends ContainerLifeCycle
|
||||||
|
{
|
||||||
|
// An instance of the Java scheduler service.
|
||||||
|
private ScheduledExecutorService scheduler;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doStart() throws Exception
|
||||||
|
{
|
||||||
|
// Java's schedulers cannot be restarted, so they must
|
||||||
|
// be created anew every time their container is started.
|
||||||
|
scheduler = Executors.newSingleThreadScheduledExecutor();
|
||||||
|
// Even if Java scheduler does not implement
|
||||||
|
// LifeCycle, make it part of the component tree.
|
||||||
|
addBean(scheduler);
|
||||||
|
// Start all the children beans.
|
||||||
|
super.doStart();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doStop() throws Exception
|
||||||
|
{
|
||||||
|
// Perform the opposite operations that were
|
||||||
|
// performed in doStart(), in reverse order.
|
||||||
|
super.doStop();
|
||||||
|
removeBean(scheduler);
|
||||||
|
scheduler.shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Root root = new Root();
|
||||||
|
Service service = new Service();
|
||||||
|
root.addBean(service);
|
||||||
|
|
||||||
|
// Start the Root component.
|
||||||
|
root.start();
|
||||||
|
|
||||||
|
// Stop temporarily Service without stopping the Root.
|
||||||
|
service.stop();
|
||||||
|
|
||||||
|
// Restart Service.
|
||||||
|
service.start();
|
||||||
|
// end::restart[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void getBeans() throws Exception
|
||||||
|
{
|
||||||
|
// tag::getBeans[]
|
||||||
|
class Root extends ContainerLifeCycle
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
class Service extends ContainerLifeCycle
|
||||||
|
{
|
||||||
|
private ScheduledExecutorService scheduler;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doStart() throws Exception
|
||||||
|
{
|
||||||
|
scheduler = Executors.newSingleThreadScheduledExecutor();
|
||||||
|
addBean(scheduler);
|
||||||
|
super.doStart();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doStop() throws Exception
|
||||||
|
{
|
||||||
|
super.doStop();
|
||||||
|
removeBean(scheduler);
|
||||||
|
scheduler.shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Root root = new Root();
|
||||||
|
Service service = new Service();
|
||||||
|
root.addBean(service);
|
||||||
|
|
||||||
|
// Start the Root component.
|
||||||
|
root.start();
|
||||||
|
|
||||||
|
// Find all the direct children of root.
|
||||||
|
Collection<Object> children = root.getBeans();
|
||||||
|
// children contains only service
|
||||||
|
|
||||||
|
// Find all descendants of root that are instance of a particular class.
|
||||||
|
Collection<ScheduledExecutorService> schedulers = root.getContainedBeans(ScheduledExecutorService.class);
|
||||||
|
// schedulers contains the service scheduler.
|
||||||
|
// end::getBeans[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void lifecycleListener()
|
||||||
|
{
|
||||||
|
// tag::lifecycleListener[]
|
||||||
|
Server server = new Server();
|
||||||
|
|
||||||
|
// Add an event listener of type LifeCycle.Listener.
|
||||||
|
server.addEventListener(new LifeCycle.Listener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void lifeCycleStarted(LifeCycle lifeCycle)
|
||||||
|
{
|
||||||
|
System.getLogger("server").log(INFO, "Server {0} has been started", lifeCycle);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void lifeCycleFailure(LifeCycle lifeCycle, Throwable failure)
|
||||||
|
{
|
||||||
|
System.getLogger("server").log(INFO, "Server {0} failed to start", lifeCycle, failure);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void lifeCycleStopped(LifeCycle lifeCycle)
|
||||||
|
{
|
||||||
|
System.getLogger("server").log(INFO, "Server {0} has been stopped", lifeCycle);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// end::lifecycleListener[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void containerListener()
|
||||||
|
{
|
||||||
|
// tag::containerListener[]
|
||||||
|
Server server = new Server();
|
||||||
|
|
||||||
|
// Add an event listener of type LifeCycle.Listener.
|
||||||
|
server.addEventListener(new Container.Listener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void beanAdded(Container parent, Object child)
|
||||||
|
{
|
||||||
|
System.getLogger("server").log(INFO, "Added bean {1} to {0}", parent, child);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void beanRemoved(Container parent, Object child)
|
||||||
|
{
|
||||||
|
System.getLogger("server").log(INFO, "Removed bean {1} from {0}", parent, child);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// end::containerListener[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void containerSiblings()
|
||||||
|
{
|
||||||
|
// tag::containerSiblings[]
|
||||||
|
class Parent extends ContainerLifeCycle
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
class Child
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
// The older child takes care of its siblings.
|
||||||
|
class OlderChild extends Child implements Container.Listener
|
||||||
|
{
|
||||||
|
private Set<Object> siblings = new HashSet<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void beanAdded(Container parent, Object child)
|
||||||
|
{
|
||||||
|
siblings.add(child);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void beanRemoved(Container parent, Object child)
|
||||||
|
{
|
||||||
|
siblings.remove(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Parent parent = new Parent();
|
||||||
|
|
||||||
|
Child older = new OlderChild();
|
||||||
|
// The older child is a child bean _and_ a listener.
|
||||||
|
parent.addBean(older);
|
||||||
|
|
||||||
|
Child younger = new Child();
|
||||||
|
// Adding a younger child will notify the older child.
|
||||||
|
parent.addBean(younger);
|
||||||
|
// end::containerSiblings[]
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,92 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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 org.eclipse.jetty.docs.programming;
|
||||||
|
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.SocketAddress;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.util.Queue;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.http.HttpFields;
|
||||||
|
import org.eclipse.jetty.http.HttpHeader;
|
||||||
|
import org.eclipse.jetty.http.HttpURI;
|
||||||
|
import org.eclipse.jetty.http.HttpVersion;
|
||||||
|
import org.eclipse.jetty.http.MetaData;
|
||||||
|
import org.eclipse.jetty.http2.api.Session;
|
||||||
|
import org.eclipse.jetty.http2.api.Stream;
|
||||||
|
import org.eclipse.jetty.http2.client.HTTP2Client;
|
||||||
|
import org.eclipse.jetty.http2.frames.DataFrame;
|
||||||
|
import org.eclipse.jetty.http2.frames.HeadersFrame;
|
||||||
|
import org.eclipse.jetty.util.Callback;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class HTTP2Docs
|
||||||
|
{
|
||||||
|
public void dataDemanded() throws Exception
|
||||||
|
{
|
||||||
|
HTTP2Client http2Client = new HTTP2Client();
|
||||||
|
http2Client.start();
|
||||||
|
SocketAddress serverAddress = new InetSocketAddress("localhost", 8080);
|
||||||
|
CompletableFuture<Session> sessionCF = http2Client.connect(serverAddress, new Session.Listener.Adapter());
|
||||||
|
Session session = sessionCF.get();
|
||||||
|
|
||||||
|
HttpFields requestHeaders = HttpFields.build()
|
||||||
|
.put(HttpHeader.USER_AGENT, "Jetty HTTP2Client {version}");
|
||||||
|
MetaData.Request request = new MetaData.Request("GET", HttpURI.from("http://localhost:8080/path"), HttpVersion.HTTP_2, requestHeaders);
|
||||||
|
HeadersFrame headersFrame = new HeadersFrame(request, null, true);
|
||||||
|
|
||||||
|
// tag::dataDemanded[]
|
||||||
|
class Chunk
|
||||||
|
{
|
||||||
|
private final ByteBuffer buffer;
|
||||||
|
private final Callback callback;
|
||||||
|
|
||||||
|
Chunk(ByteBuffer buffer, Callback callback)
|
||||||
|
{
|
||||||
|
this.buffer = buffer;
|
||||||
|
this.callback = callback;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A queue that consumers poll to consume content asynchronously.
|
||||||
|
Queue<Chunk> dataQueue = new ConcurrentLinkedQueue<>();
|
||||||
|
|
||||||
|
// Implementation of Stream.Listener.onDataDemanded(...)
|
||||||
|
// in case of asynchronous content consumption and demand.
|
||||||
|
Stream.Listener listener = new Stream.Listener.Adapter()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onDataDemanded(Stream stream, DataFrame frame, Callback callback)
|
||||||
|
{
|
||||||
|
// Get the content buffer.
|
||||||
|
ByteBuffer buffer = frame.getData();
|
||||||
|
|
||||||
|
// Store buffer to consume it asynchronously, and wrap the callback.
|
||||||
|
dataQueue.offer(new Chunk(buffer, Callback.from(() ->
|
||||||
|
{
|
||||||
|
// When the buffer has been consumed, then:
|
||||||
|
// A) succeed the nested callback.
|
||||||
|
callback.succeeded();
|
||||||
|
// B) demand more DATA frames.
|
||||||
|
stream.demand(1);
|
||||||
|
}, callback::failed)));
|
||||||
|
|
||||||
|
// Do not demand more content here, to avoid to overflow the queue.
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// end::dataDemanded[]
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,271 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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 org.eclipse.jetty.docs.programming;
|
||||||
|
|
||||||
|
import java.lang.management.ManagementFactory;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import javax.management.ObjectName;
|
||||||
|
import javax.management.remote.JMXConnector;
|
||||||
|
import javax.management.remote.JMXConnectorFactory;
|
||||||
|
import javax.management.remote.JMXServiceURL;
|
||||||
|
import javax.rmi.ssl.SslRMIClientSocketFactory;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.client.HttpClient;
|
||||||
|
import org.eclipse.jetty.jmx.ConnectorServer;
|
||||||
|
import org.eclipse.jetty.jmx.MBeanContainer;
|
||||||
|
import org.eclipse.jetty.jmx.ObjectMBean;
|
||||||
|
import org.eclipse.jetty.server.Server;
|
||||||
|
import org.eclipse.jetty.util.annotation.ManagedAttribute;
|
||||||
|
import org.eclipse.jetty.util.annotation.ManagedObject;
|
||||||
|
import org.eclipse.jetty.util.annotation.ManagedOperation;
|
||||||
|
import org.eclipse.jetty.util.annotation.Name;
|
||||||
|
import org.eclipse.jetty.util.ssl.SslContextFactory;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class JMXDocs
|
||||||
|
{
|
||||||
|
public void server()
|
||||||
|
{
|
||||||
|
// tag::server[]
|
||||||
|
Server server = new Server();
|
||||||
|
|
||||||
|
// Create an MBeanContainer with the platform MBeanServer.
|
||||||
|
MBeanContainer mbeanContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
|
||||||
|
|
||||||
|
// Add MBeanContainer to the root component.
|
||||||
|
server.addBean(mbeanContainer);
|
||||||
|
// end::server[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void client()
|
||||||
|
{
|
||||||
|
// tag::client[]
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
|
||||||
|
// Create an MBeanContainer with the platform MBeanServer.
|
||||||
|
MBeanContainer mbeanContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
|
||||||
|
|
||||||
|
// Add MBeanContainer to the root component.
|
||||||
|
httpClient.addBean(mbeanContainer);
|
||||||
|
// end::client[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void remote() throws Exception
|
||||||
|
{
|
||||||
|
// tag::remote[]
|
||||||
|
Server server = new Server();
|
||||||
|
|
||||||
|
// Setup Jetty JMX.
|
||||||
|
MBeanContainer mbeanContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
|
||||||
|
server.addBean(mbeanContainer);
|
||||||
|
|
||||||
|
// Setup ConnectorServer.
|
||||||
|
|
||||||
|
// Bind the RMI server to the wildcard address and port 1999.
|
||||||
|
// Bind the RMI registry to the wildcard address and port 1099.
|
||||||
|
JMXServiceURL jmxURL = new JMXServiceURL("rmi", null, 1999, "/jndi/rmi:///jmxrmi");
|
||||||
|
ConnectorServer jmxServer = new ConnectorServer(jmxURL, "org.eclipse.jetty.jmx:name=rmiconnectorserver");
|
||||||
|
|
||||||
|
// Add ConnectorServer as a bean, so it is started
|
||||||
|
// with the Server and also exported as MBean.
|
||||||
|
server.addBean(jmxServer);
|
||||||
|
|
||||||
|
server.start();
|
||||||
|
// end::remote[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception
|
||||||
|
{
|
||||||
|
new JMXDocs().remote();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void remoteAuthorization() throws Exception
|
||||||
|
{
|
||||||
|
// tag::remoteAuthorization[]
|
||||||
|
Server server = new Server();
|
||||||
|
|
||||||
|
// Setup Jetty JMX.
|
||||||
|
MBeanContainer mbeanContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
|
||||||
|
server.addBean(mbeanContainer);
|
||||||
|
|
||||||
|
// Setup ConnectorServer.
|
||||||
|
JMXServiceURL jmxURL = new JMXServiceURL("rmi", null, 1099, "/jndi/rmi:///jmxrmi");
|
||||||
|
Map<String, Object> env = new HashMap<>();
|
||||||
|
env.put("com.sun.management.jmxremote.access.file", "/path/to/users.access");
|
||||||
|
env.put("com.sun.management.jmxremote.password.file", "/path/to/users.password");
|
||||||
|
ConnectorServer jmxServer = new ConnectorServer(jmxURL, env, "org.eclipse.jetty.jmx:name=rmiconnectorserver");
|
||||||
|
server.addBean(jmxServer);
|
||||||
|
|
||||||
|
server.start();
|
||||||
|
// end::remoteAuthorization[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void tlsRemote() throws Exception
|
||||||
|
{
|
||||||
|
// tag::tlsRemote[]
|
||||||
|
Server server = new Server();
|
||||||
|
|
||||||
|
// Setup Jetty JMX.
|
||||||
|
MBeanContainer mbeanContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
|
||||||
|
server.addBean(mbeanContainer);
|
||||||
|
|
||||||
|
// Setup SslContextFactory.
|
||||||
|
SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
|
||||||
|
sslContextFactory.setKeyStorePath("/path/to/keystore");
|
||||||
|
sslContextFactory.setKeyStorePassword("secret");
|
||||||
|
|
||||||
|
// Setup ConnectorServer with SslContextFactory.
|
||||||
|
JMXServiceURL jmxURL = new JMXServiceURL("rmi", null, 1099, "/jndi/rmi:///jmxrmi");
|
||||||
|
ConnectorServer jmxServer = new ConnectorServer(jmxURL, null, "org.eclipse.jetty.jmx:name=rmiconnectorserver", sslContextFactory);
|
||||||
|
server.addBean(jmxServer);
|
||||||
|
|
||||||
|
server.start();
|
||||||
|
// end::tlsRemote[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void tlsJMXConnector() throws Exception
|
||||||
|
{
|
||||||
|
// tag::tlsJMXConnector[]
|
||||||
|
// System properties necessary for an RMI client to trust a self-signed certificate.
|
||||||
|
System.setProperty("javax.net.ssl.trustStore", "/path/to/trustStore");
|
||||||
|
System.setProperty("javax.net.ssl.trustStorePassword", "secret");
|
||||||
|
|
||||||
|
JMXServiceURL jmxURL = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://domain.com:1100/jmxrmi");
|
||||||
|
|
||||||
|
Map<String, Object> clientEnv = new HashMap<>();
|
||||||
|
// Required to connect to the RMI registry via TLS.
|
||||||
|
clientEnv.put(ConnectorServer.RMI_REGISTRY_CLIENT_SOCKET_FACTORY_ATTRIBUTE, new SslRMIClientSocketFactory());
|
||||||
|
|
||||||
|
try (JMXConnector client = JMXConnectorFactory.connect(jmxURL, clientEnv))
|
||||||
|
{
|
||||||
|
Set<ObjectName> names = client.getMBeanServerConnection().queryNames(null, null);
|
||||||
|
}
|
||||||
|
// end::tlsJMXConnector[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void jmxAnnotation() throws Exception
|
||||||
|
{
|
||||||
|
// tag::jmxAnnotation[]
|
||||||
|
// Annotate the class with @ManagedObject and provide a description.
|
||||||
|
@ManagedObject("Services that provide useful features")
|
||||||
|
class Services
|
||||||
|
{
|
||||||
|
private final Map<String, Object> services = new ConcurrentHashMap<>();
|
||||||
|
private boolean enabled = true;
|
||||||
|
|
||||||
|
// A read-only attribute with description.
|
||||||
|
@ManagedAttribute(value = "The number of services", readonly = true)
|
||||||
|
public int getServiceCount()
|
||||||
|
{
|
||||||
|
return services.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
// A read-write attribute with description.
|
||||||
|
// Only the getter is annotated.
|
||||||
|
@ManagedAttribute(value = "Whether the services are enabled")
|
||||||
|
public boolean isEnabled()
|
||||||
|
{
|
||||||
|
return enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
// There is no need to annotate the setter.
|
||||||
|
public void setEnabled(boolean enabled)
|
||||||
|
{
|
||||||
|
this.enabled = enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
// An operation with description and impact.
|
||||||
|
// The @Name annotation is used to annotate parameters
|
||||||
|
// for example to display meaningful parameter names.
|
||||||
|
@ManagedOperation(value = "Retrieves the service with the given name", impact = "INFO")
|
||||||
|
public Object getService(@Name(value = "serviceName") String n)
|
||||||
|
{
|
||||||
|
return services.get(n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// end::jmxAnnotation[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void jmxCustomMBean()
|
||||||
|
{
|
||||||
|
// tag::jmxCustomMBean[]
|
||||||
|
//package com.acme;
|
||||||
|
@ManagedObject
|
||||||
|
class Service
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
//package com.acme.jmx;
|
||||||
|
class ServiceMBean extends ObjectMBean
|
||||||
|
{
|
||||||
|
ServiceMBean(Object service)
|
||||||
|
{
|
||||||
|
super(service);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// end::jmxCustomMBean[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void jmxCustomMBeanOverride()
|
||||||
|
{
|
||||||
|
// tag::jmxCustomMBeanOverride[]
|
||||||
|
//package com.acme;
|
||||||
|
// No Jetty JMX annotations.
|
||||||
|
class CountService
|
||||||
|
{
|
||||||
|
private int count;
|
||||||
|
|
||||||
|
public int getCount()
|
||||||
|
{
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addCount(int value)
|
||||||
|
{
|
||||||
|
count += value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//package com.acme.jmx;
|
||||||
|
@ManagedObject("the count service")
|
||||||
|
class CountServiceMBean extends ObjectMBean
|
||||||
|
{
|
||||||
|
public CountServiceMBean(Object service)
|
||||||
|
{
|
||||||
|
super(service);
|
||||||
|
}
|
||||||
|
|
||||||
|
private CountService getCountService()
|
||||||
|
{
|
||||||
|
return (CountService)super.getManagedObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
@ManagedAttribute("the current service count")
|
||||||
|
public int getCount()
|
||||||
|
{
|
||||||
|
return getCountService().getCount();
|
||||||
|
}
|
||||||
|
|
||||||
|
@ManagedOperation(value = "adds the given value to the service count", impact = "ACTION")
|
||||||
|
public void addCount(@Name("count delta") int value)
|
||||||
|
{
|
||||||
|
getCountService().addCount(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// end::jmxCustomMBeanOverride[]
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,253 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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 org.eclipse.jetty.docs.programming;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.channels.ServerSocketChannel;
|
||||||
|
import java.nio.channels.SocketChannel;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.io.AbstractConnection;
|
||||||
|
import org.eclipse.jetty.io.EndPoint;
|
||||||
|
import org.eclipse.jetty.io.SelectorManager;
|
||||||
|
import org.eclipse.jetty.util.BufferUtil;
|
||||||
|
import org.eclipse.jetty.util.Callback;
|
||||||
|
import org.eclipse.jetty.util.IteratingCallback;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class SelectorManagerDocs
|
||||||
|
{
|
||||||
|
// tag::connect[]
|
||||||
|
public void connect(SelectorManager selectorManager, Map<String, Object> context) throws IOException
|
||||||
|
{
|
||||||
|
String host = "host";
|
||||||
|
int port = 8080;
|
||||||
|
|
||||||
|
// Create an unconnected SocketChannel.
|
||||||
|
SocketChannel socketChannel = SocketChannel.open();
|
||||||
|
socketChannel.configureBlocking(false);
|
||||||
|
|
||||||
|
// Connect and register to Jetty.
|
||||||
|
if (socketChannel.connect(new InetSocketAddress(host, port)))
|
||||||
|
selectorManager.accept(socketChannel, context);
|
||||||
|
else
|
||||||
|
selectorManager.connect(socketChannel, context);
|
||||||
|
}
|
||||||
|
// end::connect[]
|
||||||
|
|
||||||
|
// tag::accept[]
|
||||||
|
public void accept(ServerSocketChannel acceptor, SelectorManager selectorManager) throws IOException
|
||||||
|
{
|
||||||
|
// Wait until a client connects.
|
||||||
|
SocketChannel socketChannel = acceptor.accept();
|
||||||
|
socketChannel.configureBlocking(false);
|
||||||
|
|
||||||
|
// Accept and register to Jetty.
|
||||||
|
Object attachment = null;
|
||||||
|
selectorManager.accept(socketChannel, attachment);
|
||||||
|
}
|
||||||
|
// end::accept[]
|
||||||
|
|
||||||
|
public void connection()
|
||||||
|
{
|
||||||
|
// tag::connection[]
|
||||||
|
// Extend AbstractConnection to inherit basic implementation.
|
||||||
|
class MyConnection extends AbstractConnection
|
||||||
|
{
|
||||||
|
public MyConnection(EndPoint endPoint, Executor executor)
|
||||||
|
{
|
||||||
|
super(endPoint, executor);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onOpen()
|
||||||
|
{
|
||||||
|
super.onOpen();
|
||||||
|
|
||||||
|
// Declare interest for fill events.
|
||||||
|
fillInterested();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFillable()
|
||||||
|
{
|
||||||
|
// Called when a fill event happens.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// end::connection[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void echoWrong()
|
||||||
|
{
|
||||||
|
// tag::echo-wrong[]
|
||||||
|
class WrongEchoConnection extends AbstractConnection implements Callback
|
||||||
|
{
|
||||||
|
public WrongEchoConnection(EndPoint endPoint, Executor executor)
|
||||||
|
{
|
||||||
|
super(endPoint, executor);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onOpen()
|
||||||
|
{
|
||||||
|
super.onOpen();
|
||||||
|
|
||||||
|
// Declare interest for fill events.
|
||||||
|
fillInterested();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFillable()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ByteBuffer buffer = BufferUtil.allocate(1024);
|
||||||
|
int filled = getEndPoint().fill(buffer);
|
||||||
|
if (filled > 0)
|
||||||
|
{
|
||||||
|
// Filled some bytes, echo them back.
|
||||||
|
getEndPoint().write(this, buffer);
|
||||||
|
}
|
||||||
|
else if (filled == 0)
|
||||||
|
{
|
||||||
|
// No more bytes to fill, declare
|
||||||
|
// again interest for fill events.
|
||||||
|
fillInterested();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// The other peer closed the
|
||||||
|
// connection, close it back.
|
||||||
|
getEndPoint().close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception x)
|
||||||
|
{
|
||||||
|
getEndPoint().close(x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void succeeded()
|
||||||
|
{
|
||||||
|
// The write is complete, fill again.
|
||||||
|
onFillable();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void failed(Throwable x)
|
||||||
|
{
|
||||||
|
getEndPoint().close(x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// end::echo-wrong[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void echoCorrect()
|
||||||
|
{
|
||||||
|
// tag::echo-correct[]
|
||||||
|
class EchoConnection extends AbstractConnection
|
||||||
|
{
|
||||||
|
private final IteratingCallback callback = new EchoIteratingCallback();
|
||||||
|
|
||||||
|
public EchoConnection(EndPoint endp, Executor executor)
|
||||||
|
{
|
||||||
|
super(endp, executor);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onOpen()
|
||||||
|
{
|
||||||
|
super.onOpen();
|
||||||
|
|
||||||
|
// Declare interest for fill events.
|
||||||
|
fillInterested();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFillable()
|
||||||
|
{
|
||||||
|
// Start the iteration loop that reads and echoes back.
|
||||||
|
callback.iterate();
|
||||||
|
}
|
||||||
|
|
||||||
|
class EchoIteratingCallback extends IteratingCallback
|
||||||
|
{
|
||||||
|
private ByteBuffer buffer;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Action process() throws Throwable
|
||||||
|
{
|
||||||
|
// Obtain a buffer if we don't already have one.
|
||||||
|
if (buffer == null)
|
||||||
|
buffer = BufferUtil.allocate(1024);
|
||||||
|
|
||||||
|
int filled = getEndPoint().fill(buffer);
|
||||||
|
if (filled > 0)
|
||||||
|
{
|
||||||
|
// We have filled some bytes, echo them back.
|
||||||
|
getEndPoint().write(this, buffer);
|
||||||
|
|
||||||
|
// Signal that the iteration should resume
|
||||||
|
// when the write() operation is completed.
|
||||||
|
return Action.SCHEDULED;
|
||||||
|
}
|
||||||
|
else if (filled == 0)
|
||||||
|
{
|
||||||
|
// We don't need the buffer anymore, so
|
||||||
|
// don't keep it around while we are idle.
|
||||||
|
buffer = null;
|
||||||
|
|
||||||
|
// No more bytes to read, declare
|
||||||
|
// again interest for fill events.
|
||||||
|
fillInterested();
|
||||||
|
|
||||||
|
// Signal that the iteration is now IDLE.
|
||||||
|
return Action.IDLE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// The other peer closed the connection,
|
||||||
|
// the iteration completed successfully.
|
||||||
|
return Action.SUCCEEDED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCompleteSuccess()
|
||||||
|
{
|
||||||
|
// The iteration completed successfully.
|
||||||
|
getEndPoint().close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCompleteFailure(Throwable cause)
|
||||||
|
{
|
||||||
|
// The iteration completed with a failure.
|
||||||
|
getEndPoint().close(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InvocationType getInvocationType()
|
||||||
|
{
|
||||||
|
return InvocationType.NON_BLOCKING;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// end::echo-correct[]
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,492 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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 org.eclipse.jetty.docs.programming;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.Reader;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.time.Duration;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.util.IteratingCallback;
|
||||||
|
import org.eclipse.jetty.util.NanoTime;
|
||||||
|
import org.eclipse.jetty.websocket.api.RemoteEndpoint;
|
||||||
|
import org.eclipse.jetty.websocket.api.Session;
|
||||||
|
import org.eclipse.jetty.websocket.api.StatusCode;
|
||||||
|
import org.eclipse.jetty.websocket.api.WebSocketListener;
|
||||||
|
import org.eclipse.jetty.websocket.api.WebSocketPartialListener;
|
||||||
|
import org.eclipse.jetty.websocket.api.WebSocketPingPongListener;
|
||||||
|
import org.eclipse.jetty.websocket.api.WriteCallback;
|
||||||
|
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
|
||||||
|
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
|
||||||
|
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError;
|
||||||
|
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
|
||||||
|
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class WebSocketDocs
|
||||||
|
{
|
||||||
|
@SuppressWarnings("InnerClassMayBeStatic")
|
||||||
|
// tag::listenerEndpoint[]
|
||||||
|
public class ListenerEndPoint implements WebSocketListener // <1>
|
||||||
|
{
|
||||||
|
private Session session;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onWebSocketConnect(Session session)
|
||||||
|
{
|
||||||
|
// The WebSocket connection is established.
|
||||||
|
|
||||||
|
// Store the session to be able to send data to the remote peer.
|
||||||
|
this.session = session;
|
||||||
|
|
||||||
|
// You may configure the session.
|
||||||
|
session.setMaxTextMessageSize(16 * 1024);
|
||||||
|
|
||||||
|
// You may immediately send a message to the remote peer.
|
||||||
|
session.getRemote().sendString("connected", WriteCallback.NOOP);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onWebSocketClose(int statusCode, String reason)
|
||||||
|
{
|
||||||
|
// The WebSocket connection is closed.
|
||||||
|
|
||||||
|
// You may dispose resources.
|
||||||
|
disposeResources();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onWebSocketError(Throwable cause)
|
||||||
|
{
|
||||||
|
// The WebSocket connection failed.
|
||||||
|
|
||||||
|
// You may log the error.
|
||||||
|
cause.printStackTrace();
|
||||||
|
|
||||||
|
// You may dispose resources.
|
||||||
|
disposeResources();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onWebSocketText(String message)
|
||||||
|
{
|
||||||
|
// A WebSocket textual message is received.
|
||||||
|
|
||||||
|
// You may echo it back if it matches certain criteria.
|
||||||
|
if (message.startsWith("echo:"))
|
||||||
|
session.getRemote().sendString(message.substring("echo:".length()), WriteCallback.NOOP);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onWebSocketBinary(byte[] payload, int offset, int length)
|
||||||
|
{
|
||||||
|
// A WebSocket binary message is received.
|
||||||
|
|
||||||
|
// Save only PNG images.
|
||||||
|
byte[] pngBytes = new byte[]{(byte)0x89, 'P', 'N', 'G'};
|
||||||
|
for (int i = 0; i < pngBytes.length; ++i)
|
||||||
|
{
|
||||||
|
if (pngBytes[i] != payload[offset + i])
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
savePNGImage(payload, offset, length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// end::listenerEndpoint[]
|
||||||
|
|
||||||
|
@SuppressWarnings("InnerClassMayBeStatic")
|
||||||
|
// tag::streamingListenerEndpoint[]
|
||||||
|
public class StreamingListenerEndpoint implements WebSocketPartialListener
|
||||||
|
{
|
||||||
|
private Path textPath;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onWebSocketPartialText(String payload, boolean fin)
|
||||||
|
{
|
||||||
|
// Forward chunks to external REST service.
|
||||||
|
forwardToREST(payload, fin);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onWebSocketPartialBinary(ByteBuffer payload, boolean fin)
|
||||||
|
{
|
||||||
|
// Save chunks to file.
|
||||||
|
appendToFile(payload, fin);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// end::streamingListenerEndpoint[]
|
||||||
|
|
||||||
|
@SuppressWarnings("InnerClassMayBeStatic")
|
||||||
|
// tag::annotatedEndpoint[]
|
||||||
|
@WebSocket // <1>
|
||||||
|
public class AnnotatedEndPoint
|
||||||
|
{
|
||||||
|
private Session session;
|
||||||
|
|
||||||
|
@OnWebSocketConnect // <2>
|
||||||
|
public void onConnect(Session session)
|
||||||
|
{
|
||||||
|
// The WebSocket connection is established.
|
||||||
|
|
||||||
|
// Store the session to be able to send data to the remote peer.
|
||||||
|
this.session = session;
|
||||||
|
|
||||||
|
// You may configure the session.
|
||||||
|
session.setMaxTextMessageSize(16 * 1024);
|
||||||
|
|
||||||
|
// You may immediately send a message to the remote peer.
|
||||||
|
session.getRemote().sendString("connected", WriteCallback.NOOP);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnWebSocketClose // <3>
|
||||||
|
public void onClose(int statusCode, String reason)
|
||||||
|
{
|
||||||
|
// The WebSocket connection is closed.
|
||||||
|
|
||||||
|
// You may dispose resources.
|
||||||
|
disposeResources();
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnWebSocketError // <4>
|
||||||
|
public void onError(Throwable cause)
|
||||||
|
{
|
||||||
|
// The WebSocket connection failed.
|
||||||
|
|
||||||
|
// You may log the error.
|
||||||
|
cause.printStackTrace();
|
||||||
|
|
||||||
|
// You may dispose resources.
|
||||||
|
disposeResources();
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnWebSocketMessage // <5>
|
||||||
|
public void onTextMessage(Session session, String message) // <3>
|
||||||
|
{
|
||||||
|
// A WebSocket textual message is received.
|
||||||
|
|
||||||
|
// You may echo it back if it matches certain criteria.
|
||||||
|
if (message.startsWith("echo:"))
|
||||||
|
session.getRemote().sendString(message.substring("echo:".length()), WriteCallback.NOOP);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnWebSocketMessage // <5>
|
||||||
|
public void onBinaryMessage(byte[] payload, int offset, int length)
|
||||||
|
{
|
||||||
|
// A WebSocket binary message is received.
|
||||||
|
|
||||||
|
// Save only PNG images.
|
||||||
|
byte[] pngBytes = new byte[]{(byte)0x89, 'P', 'N', 'G'};
|
||||||
|
for (int i = 0; i < pngBytes.length; ++i)
|
||||||
|
{
|
||||||
|
if (pngBytes[i] != payload[offset + i])
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
savePNGImage(payload, offset, length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// end::annotatedEndpoint[]
|
||||||
|
|
||||||
|
@SuppressWarnings("InnerClassMayBeStatic")
|
||||||
|
// tag::streamingAnnotatedEndpoint[]
|
||||||
|
@WebSocket
|
||||||
|
public class StreamingAnnotatedEndpoint
|
||||||
|
{
|
||||||
|
@OnWebSocketMessage
|
||||||
|
public void onTextMessage(Reader reader)
|
||||||
|
{
|
||||||
|
// Read chunks and forward.
|
||||||
|
forwardToREST(reader);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnWebSocketMessage
|
||||||
|
public void onBinaryMessage(InputStream stream)
|
||||||
|
{
|
||||||
|
// Save chunks to file.
|
||||||
|
appendToFile(stream);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// end::streamingAnnotatedEndpoint[]
|
||||||
|
|
||||||
|
@SuppressWarnings("InnerClassMayBeStatic")
|
||||||
|
// tag::sessionConfigure[]
|
||||||
|
public class ConfigureEndpoint implements WebSocketListener
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onWebSocketConnect(Session session)
|
||||||
|
{
|
||||||
|
// Configure the max length of incoming messages.
|
||||||
|
session.setMaxTextMessageSize(16 * 1024);
|
||||||
|
|
||||||
|
// Configure the idle timeout.
|
||||||
|
session.setIdleTimeout(Duration.ofSeconds(30));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// end::sessionConfigure[]
|
||||||
|
|
||||||
|
@SuppressWarnings("InnerClassMayBeStatic")
|
||||||
|
// tag::sendBlocking[]
|
||||||
|
@WebSocket
|
||||||
|
public class BlockingSendEndpoint
|
||||||
|
{
|
||||||
|
@OnWebSocketMessage
|
||||||
|
public void onText(Session session, String text)
|
||||||
|
{
|
||||||
|
// Obtain the RemoteEndpoint APIs.
|
||||||
|
RemoteEndpoint remote = session.getRemote();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Send textual data to the remote peer.
|
||||||
|
remote.sendString("data");
|
||||||
|
|
||||||
|
// Send binary data to the remote peer.
|
||||||
|
ByteBuffer bytes = readImageFromFile();
|
||||||
|
remote.sendBytes(bytes);
|
||||||
|
|
||||||
|
// Send a PING frame to the remote peer.
|
||||||
|
remote.sendPing(ByteBuffer.allocate(8).putLong(NanoTime.now()).flip());
|
||||||
|
}
|
||||||
|
catch (IOException x)
|
||||||
|
{
|
||||||
|
// No need to rethrow or close the session.
|
||||||
|
System.getLogger("websocket").log(System.Logger.Level.WARNING, "could not send data", x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// end::sendBlocking[]
|
||||||
|
|
||||||
|
@SuppressWarnings("InnerClassMayBeStatic")
|
||||||
|
// tag::sendNonBlocking[]
|
||||||
|
@WebSocket
|
||||||
|
public class NonBlockingSendEndpoint
|
||||||
|
{
|
||||||
|
@OnWebSocketMessage
|
||||||
|
public void onText(Session session, String text)
|
||||||
|
{
|
||||||
|
// Obtain the RemoteEndpoint APIs.
|
||||||
|
RemoteEndpoint remote = session.getRemote();
|
||||||
|
|
||||||
|
// Send textual data to the remote peer.
|
||||||
|
remote.sendString("data", new WriteCallback() // <1>
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void writeSuccess()
|
||||||
|
{
|
||||||
|
// Send binary data to the remote peer.
|
||||||
|
ByteBuffer bytes = readImageFromFile();
|
||||||
|
remote.sendBytes(bytes, new WriteCallback() // <2>
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void writeSuccess()
|
||||||
|
{
|
||||||
|
// Both sends succeeded.
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeFailed(Throwable x)
|
||||||
|
{
|
||||||
|
System.getLogger("websocket").log(System.Logger.Level.WARNING, "could not send binary data", x);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeFailed(Throwable x)
|
||||||
|
{
|
||||||
|
// No need to rethrow or close the session.
|
||||||
|
System.getLogger("websocket").log(System.Logger.Level.WARNING, "could not send textual data", x);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// remote.sendString("wrong", WriteCallback.NOOP); // May throw WritePendingException! <3>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// end::sendNonBlocking[]
|
||||||
|
|
||||||
|
@SuppressWarnings("InnerClassMayBeStatic")
|
||||||
|
// tag::streamSendBlocking[]
|
||||||
|
@WebSocket
|
||||||
|
public class StreamSendBlockingEndpoint
|
||||||
|
{
|
||||||
|
@OnWebSocketMessage
|
||||||
|
public void onText(Session session, String text)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
RemoteEndpoint remote = session.getRemote();
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
ByteBuffer chunk = readChunkToSend();
|
||||||
|
if (chunk == null)
|
||||||
|
{
|
||||||
|
// No more bytes, finish the WebSocket message.
|
||||||
|
remote.sendPartialBytes(ByteBuffer.allocate(0), true);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Send the chunk.
|
||||||
|
remote.sendPartialBytes(chunk, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (IOException x)
|
||||||
|
{
|
||||||
|
x.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// end::streamSendBlocking[]
|
||||||
|
|
||||||
|
@SuppressWarnings("InnerClassMayBeStatic")
|
||||||
|
// tag::streamSendNonBlocking[]
|
||||||
|
@WebSocket
|
||||||
|
public class StreamSendNonBlockingEndpoint
|
||||||
|
{
|
||||||
|
@OnWebSocketMessage
|
||||||
|
public void onText(Session session, String text)
|
||||||
|
{
|
||||||
|
RemoteEndpoint remote = session.getRemote();
|
||||||
|
new Sender(remote).iterate();
|
||||||
|
}
|
||||||
|
|
||||||
|
private class Sender extends IteratingCallback implements WriteCallback // <1>
|
||||||
|
{
|
||||||
|
private final RemoteEndpoint remote;
|
||||||
|
private boolean finished;
|
||||||
|
|
||||||
|
private Sender(RemoteEndpoint remote)
|
||||||
|
{
|
||||||
|
this.remote = remote;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Action process() throws Throwable // <2>
|
||||||
|
{
|
||||||
|
if (finished)
|
||||||
|
return Action.SUCCEEDED;
|
||||||
|
|
||||||
|
ByteBuffer chunk = readChunkToSend();
|
||||||
|
if (chunk == null)
|
||||||
|
{
|
||||||
|
// No more bytes, finish the WebSocket message.
|
||||||
|
remote.sendPartialBytes(ByteBuffer.allocate(0), true, this); // <3>
|
||||||
|
finished = true;
|
||||||
|
return Action.SCHEDULED;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Send the chunk.
|
||||||
|
remote.sendPartialBytes(ByteBuffer.allocate(0), false, this); // <3>
|
||||||
|
return Action.SCHEDULED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeSuccess()
|
||||||
|
{
|
||||||
|
// When the send succeeds, succeed this IteratingCallback.
|
||||||
|
succeeded();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeFailed(Throwable x)
|
||||||
|
{
|
||||||
|
// When the send fails, fail this IteratingCallback.
|
||||||
|
failed(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCompleteFailure(Throwable x)
|
||||||
|
{
|
||||||
|
x.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// end::streamSendNonBlocking[]
|
||||||
|
|
||||||
|
@SuppressWarnings("InnerClassMayBeStatic")
|
||||||
|
// tag::pingPongListener[]
|
||||||
|
public class RoundTripListenerEndpoint implements WebSocketPingPongListener // <1>
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onWebSocketConnect(Session session)
|
||||||
|
{
|
||||||
|
// Send to the remote peer the local nanoTime.
|
||||||
|
ByteBuffer buffer = ByteBuffer.allocate(8).putLong(NanoTime.now()).flip();
|
||||||
|
session.getRemote().sendPing(buffer, WriteCallback.NOOP);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onWebSocketPong(ByteBuffer payload)
|
||||||
|
{
|
||||||
|
// The remote peer echoed back the local nanoTime.
|
||||||
|
long start = payload.getLong();
|
||||||
|
|
||||||
|
// Calculate the round-trip time.
|
||||||
|
long roundTrip = NanoTime.since(start);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// end::pingPongListener[]
|
||||||
|
|
||||||
|
@SuppressWarnings("InnerClassMayBeStatic")
|
||||||
|
// tag::sessionClose[]
|
||||||
|
@WebSocket
|
||||||
|
public class CloseEndpoint
|
||||||
|
{
|
||||||
|
@OnWebSocketMessage
|
||||||
|
public void onText(Session session, String text)
|
||||||
|
{
|
||||||
|
if ("close".equalsIgnoreCase(text))
|
||||||
|
session.close(StatusCode.NORMAL, "bye");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// end::sessionClose[]
|
||||||
|
|
||||||
|
private static void forwardToREST(String payload, boolean fin)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void forwardToREST(Reader reader)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void appendToFile(ByteBuffer payload, boolean fin)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void appendToFile(InputStream stream)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void disposeResources()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void savePNGImage(byte[] payload, int offset, int length)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ByteBuffer readImageFromFile()
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ByteBuffer readChunkToSend()
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,439 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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 org.eclipse.jetty.docs.programming.client;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.SocketAddress;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.io.AbstractConnection;
|
||||||
|
import org.eclipse.jetty.io.ClientConnectionFactory;
|
||||||
|
import org.eclipse.jetty.io.ClientConnector;
|
||||||
|
import org.eclipse.jetty.io.EndPoint;
|
||||||
|
import org.eclipse.jetty.io.SelectorManager;
|
||||||
|
import org.eclipse.jetty.io.ssl.SslClientConnectionFactory;
|
||||||
|
import org.eclipse.jetty.io.ssl.SslConnection;
|
||||||
|
import org.eclipse.jetty.util.BufferUtil;
|
||||||
|
import org.eclipse.jetty.util.Callback;
|
||||||
|
import org.eclipse.jetty.util.Promise;
|
||||||
|
import org.eclipse.jetty.util.ssl.SslContextFactory;
|
||||||
|
import org.eclipse.jetty.util.thread.QueuedThreadPool;
|
||||||
|
import org.eclipse.jetty.util.thread.ScheduledExecutorScheduler;
|
||||||
|
import org.eclipse.jetty.util.thread.Scheduler;
|
||||||
|
|
||||||
|
import static java.lang.System.Logger.Level.INFO;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class ClientConnectorDocs
|
||||||
|
{
|
||||||
|
public void simplest() throws Exception
|
||||||
|
{
|
||||||
|
// tag::simplest[]
|
||||||
|
ClientConnector clientConnector = new ClientConnector();
|
||||||
|
clientConnector.start();
|
||||||
|
// end::simplest[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void typical() throws Exception
|
||||||
|
{
|
||||||
|
// tag::typical[]
|
||||||
|
// Create and configure the SslContextFactory.
|
||||||
|
SslContextFactory.Client sslContextFactory = new SslContextFactory.Client();
|
||||||
|
sslContextFactory.addExcludeProtocols("TLSv1", "TLSv1.1");
|
||||||
|
|
||||||
|
// Create and configure the thread pool.
|
||||||
|
QueuedThreadPool threadPool = new QueuedThreadPool();
|
||||||
|
threadPool.setName("client");
|
||||||
|
|
||||||
|
// Create and configure the ClientConnector.
|
||||||
|
ClientConnector clientConnector = new ClientConnector();
|
||||||
|
clientConnector.setSslContextFactory(sslContextFactory);
|
||||||
|
clientConnector.setExecutor(threadPool);
|
||||||
|
clientConnector.start();
|
||||||
|
// end::typical[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void advanced() throws Exception
|
||||||
|
{
|
||||||
|
// tag::advanced[]
|
||||||
|
class CustomClientConnector extends ClientConnector
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
protected SelectorManager newSelectorManager()
|
||||||
|
{
|
||||||
|
return new ClientSelectorManager(getExecutor(), getScheduler(), getSelectors())
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
protected void endPointOpened(EndPoint endpoint)
|
||||||
|
{
|
||||||
|
System.getLogger("endpoint").log(INFO, "opened %s", endpoint);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void endPointClosed(EndPoint endpoint)
|
||||||
|
{
|
||||||
|
System.getLogger("endpoint").log(INFO, "closed %s", endpoint);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create and configure the thread pool.
|
||||||
|
QueuedThreadPool threadPool = new QueuedThreadPool();
|
||||||
|
threadPool.setName("client");
|
||||||
|
|
||||||
|
// Create and configure the scheduler.
|
||||||
|
Scheduler scheduler = new ScheduledExecutorScheduler("scheduler-client", false);
|
||||||
|
|
||||||
|
// Create and configure the custom ClientConnector.
|
||||||
|
CustomClientConnector clientConnector = new CustomClientConnector();
|
||||||
|
clientConnector.setExecutor(threadPool);
|
||||||
|
clientConnector.setScheduler(scheduler);
|
||||||
|
clientConnector.start();
|
||||||
|
// end::advanced[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void connect() throws Exception
|
||||||
|
{
|
||||||
|
// tag::connect[]
|
||||||
|
class CustomConnection extends AbstractConnection
|
||||||
|
{
|
||||||
|
public CustomConnection(EndPoint endPoint, Executor executor)
|
||||||
|
{
|
||||||
|
super(endPoint, executor);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onOpen()
|
||||||
|
{
|
||||||
|
super.onOpen();
|
||||||
|
System.getLogger("connection").log(INFO, "Opened connection {0}", this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFillable()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ClientConnector clientConnector = new ClientConnector();
|
||||||
|
clientConnector.start();
|
||||||
|
|
||||||
|
String host = "serverHost";
|
||||||
|
int port = 8080;
|
||||||
|
SocketAddress address = new InetSocketAddress(host, port);
|
||||||
|
|
||||||
|
// The ClientConnectionFactory that creates CustomConnection instances.
|
||||||
|
ClientConnectionFactory connectionFactory = (endPoint, context) ->
|
||||||
|
{
|
||||||
|
System.getLogger("connection").log(INFO, "Creating connection for {0}", endPoint);
|
||||||
|
return new CustomConnection(endPoint, clientConnector.getExecutor());
|
||||||
|
};
|
||||||
|
|
||||||
|
// The Promise to notify of connection creation success or failure.
|
||||||
|
CompletableFuture<CustomConnection> connectionPromise = new Promise.Completable<>();
|
||||||
|
|
||||||
|
// Populate the context with the mandatory keys to create and obtain connections.
|
||||||
|
Map<String, Object> context = new HashMap<>();
|
||||||
|
context.put(ClientConnector.CLIENT_CONNECTION_FACTORY_CONTEXT_KEY, connectionFactory);
|
||||||
|
context.put(ClientConnector.CONNECTION_PROMISE_CONTEXT_KEY, connectionPromise);
|
||||||
|
clientConnector.connect(address, context);
|
||||||
|
|
||||||
|
// Use the Connection when it's available.
|
||||||
|
|
||||||
|
// Use it in a non-blocking way via CompletableFuture APIs.
|
||||||
|
connectionPromise.whenComplete((connection, failure) ->
|
||||||
|
{
|
||||||
|
System.getLogger("connection").log(INFO, "Created connection for {0}", connection);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Alternatively, you can block waiting for the connection (or a failure).
|
||||||
|
// CustomConnection connection = connectionPromise.get();
|
||||||
|
// end::connect[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void telnet() throws Exception
|
||||||
|
{
|
||||||
|
// tag::telnet[]
|
||||||
|
class TelnetConnection extends AbstractConnection
|
||||||
|
{
|
||||||
|
private final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||||
|
private Consumer<String> consumer;
|
||||||
|
|
||||||
|
public TelnetConnection(EndPoint endPoint, Executor executor)
|
||||||
|
{
|
||||||
|
super(endPoint, executor);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onOpen()
|
||||||
|
{
|
||||||
|
super.onOpen();
|
||||||
|
|
||||||
|
// Declare interest for fill events.
|
||||||
|
fillInterested();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFillable()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ByteBuffer buffer = BufferUtil.allocate(1024);
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
int filled = getEndPoint().fill(buffer);
|
||||||
|
if (filled > 0)
|
||||||
|
{
|
||||||
|
while (buffer.hasRemaining())
|
||||||
|
{
|
||||||
|
// Search for newline.
|
||||||
|
byte read = buffer.get();
|
||||||
|
if (read == '\n')
|
||||||
|
{
|
||||||
|
// Notify the consumer of the line.
|
||||||
|
consumer.accept(bytes.toString(StandardCharsets.UTF_8));
|
||||||
|
bytes.reset();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
bytes.write(read);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (filled == 0)
|
||||||
|
{
|
||||||
|
// No more bytes to fill, declare
|
||||||
|
// again interest for fill events.
|
||||||
|
fillInterested();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// The other peer closed the
|
||||||
|
// connection, close it back.
|
||||||
|
getEndPoint().close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception x)
|
||||||
|
{
|
||||||
|
getEndPoint().close(x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onLine(Consumer<String> consumer)
|
||||||
|
{
|
||||||
|
this.consumer = consumer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void writeLine(String line, Callback callback)
|
||||||
|
{
|
||||||
|
line = line + "\r\n";
|
||||||
|
getEndPoint().write(callback, ByteBuffer.wrap(line.getBytes(StandardCharsets.UTF_8)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ClientConnector clientConnector = new ClientConnector();
|
||||||
|
clientConnector.start();
|
||||||
|
|
||||||
|
String host = "wikipedia.org";
|
||||||
|
int port = 80;
|
||||||
|
SocketAddress address = new InetSocketAddress(host, port);
|
||||||
|
|
||||||
|
ClientConnectionFactory connectionFactory = (endPoint, context) ->
|
||||||
|
new TelnetConnection(endPoint, clientConnector.getExecutor());
|
||||||
|
|
||||||
|
CompletableFuture<TelnetConnection> connectionPromise = new Promise.Completable<>();
|
||||||
|
|
||||||
|
Map<String, Object> context = new HashMap<>();
|
||||||
|
context.put(ClientConnector.CLIENT_CONNECTION_FACTORY_CONTEXT_KEY, connectionFactory);
|
||||||
|
context.put(ClientConnector.CONNECTION_PROMISE_CONTEXT_KEY, connectionPromise);
|
||||||
|
clientConnector.connect(address, context);
|
||||||
|
|
||||||
|
connectionPromise.whenComplete((connection, failure) ->
|
||||||
|
{
|
||||||
|
if (failure == null)
|
||||||
|
{
|
||||||
|
// Register a listener that receives string lines.
|
||||||
|
connection.onLine(line -> System.getLogger("app").log(INFO, "line: {0}", line));
|
||||||
|
|
||||||
|
// Write a line.
|
||||||
|
connection.writeLine("" +
|
||||||
|
"GET / HTTP/1.0\r\n" +
|
||||||
|
"", Callback.NOOP);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
failure.printStackTrace();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// end::telnet[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void tlsTelnet() throws Exception
|
||||||
|
{
|
||||||
|
// tag::tlsTelnet[]
|
||||||
|
class TelnetConnection extends AbstractConnection
|
||||||
|
{
|
||||||
|
private final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||||
|
private Consumer<String> consumer;
|
||||||
|
|
||||||
|
public TelnetConnection(EndPoint endPoint, Executor executor)
|
||||||
|
{
|
||||||
|
super(endPoint, executor);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onOpen()
|
||||||
|
{
|
||||||
|
super.onOpen();
|
||||||
|
|
||||||
|
// Declare interest for fill events.
|
||||||
|
fillInterested();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFillable()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ByteBuffer buffer = BufferUtil.allocate(1024);
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
int filled = getEndPoint().fill(buffer);
|
||||||
|
if (filled > 0)
|
||||||
|
{
|
||||||
|
while (buffer.hasRemaining())
|
||||||
|
{
|
||||||
|
// Search for newline.
|
||||||
|
byte read = buffer.get();
|
||||||
|
if (read == '\n')
|
||||||
|
{
|
||||||
|
// Notify the consumer of the line.
|
||||||
|
consumer.accept(bytes.toString(StandardCharsets.UTF_8));
|
||||||
|
bytes.reset();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
bytes.write(read);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (filled == 0)
|
||||||
|
{
|
||||||
|
// No more bytes to fill, declare
|
||||||
|
// again interest for fill events.
|
||||||
|
fillInterested();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// The other peer closed the
|
||||||
|
// connection, close it back.
|
||||||
|
getEndPoint().close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception x)
|
||||||
|
{
|
||||||
|
getEndPoint().close(x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onLine(Consumer<String> consumer)
|
||||||
|
{
|
||||||
|
this.consumer = consumer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void writeLine(String line, Callback callback)
|
||||||
|
{
|
||||||
|
line = line + "\r\n";
|
||||||
|
getEndPoint().write(callback, ByteBuffer.wrap(line.getBytes(StandardCharsets.UTF_8)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ClientConnector clientConnector = new ClientConnector();
|
||||||
|
clientConnector.start();
|
||||||
|
|
||||||
|
// Use port 443 to contact the server using encrypted HTTP.
|
||||||
|
String host = "wikipedia.org";
|
||||||
|
int port = 443;
|
||||||
|
SocketAddress address = new InetSocketAddress(host, port);
|
||||||
|
|
||||||
|
ClientConnectionFactory connectionFactory = (endPoint, context) ->
|
||||||
|
new TelnetConnection(endPoint, clientConnector.getExecutor());
|
||||||
|
|
||||||
|
// Wrap the "telnet" ClientConnectionFactory with the SslClientConnectionFactory.
|
||||||
|
connectionFactory = new SslClientConnectionFactory(clientConnector.getSslContextFactory(),
|
||||||
|
clientConnector.getByteBufferPool(), clientConnector.getExecutor(), connectionFactory);
|
||||||
|
|
||||||
|
// We will obtain a SslConnection now.
|
||||||
|
CompletableFuture<SslConnection> connectionPromise = new Promise.Completable<>();
|
||||||
|
|
||||||
|
Map<String, Object> context = new HashMap<>();
|
||||||
|
context.put(ClientConnector.CLIENT_CONNECTION_FACTORY_CONTEXT_KEY, connectionFactory);
|
||||||
|
context.put(ClientConnector.CONNECTION_PROMISE_CONTEXT_KEY, connectionPromise);
|
||||||
|
clientConnector.connect(address, context);
|
||||||
|
|
||||||
|
connectionPromise.whenComplete((sslConnection, failure) ->
|
||||||
|
{
|
||||||
|
if (failure == null)
|
||||||
|
{
|
||||||
|
// Unwrap the SslConnection to access the "line" APIs in TelnetConnection.
|
||||||
|
TelnetConnection connection = (TelnetConnection)sslConnection.getDecryptedEndPoint().getConnection();
|
||||||
|
// Register a listener that receives string lines.
|
||||||
|
connection.onLine(line -> System.getLogger("app").log(INFO, "line: {0}", line));
|
||||||
|
|
||||||
|
// Write a line.
|
||||||
|
connection.writeLine("" +
|
||||||
|
"GET / HTTP/1.0\r\n" +
|
||||||
|
"", Callback.NOOP);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
failure.printStackTrace();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// end::tlsTelnet[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void unixDomain() throws Exception
|
||||||
|
{
|
||||||
|
// tag::unixDomain[]
|
||||||
|
// This is the path where the server "listens" on.
|
||||||
|
Path unixDomainPath = Path.of("/path/to/server.sock");
|
||||||
|
|
||||||
|
// Creates a ClientConnector that uses Unix-Domain
|
||||||
|
// sockets, not the network, to connect to the server.
|
||||||
|
ClientConnector clientConnector = ClientConnector.forUnixDomain(unixDomainPath);
|
||||||
|
clientConnector.start();
|
||||||
|
// end::unixDomain[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception
|
||||||
|
{
|
||||||
|
new ClientConnectorDocs().tlsTelnet();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,930 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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 org.eclipse.jetty.docs.programming.client.http;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.CookieStore;
|
||||||
|
import java.net.HttpCookie;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.function.LongConsumer;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.client.ConnectionPool;
|
||||||
|
import org.eclipse.jetty.client.HttpClient;
|
||||||
|
import org.eclipse.jetty.client.HttpClientTransport;
|
||||||
|
import org.eclipse.jetty.client.HttpDestination;
|
||||||
|
import org.eclipse.jetty.client.HttpProxy;
|
||||||
|
import org.eclipse.jetty.client.ProxyConfiguration;
|
||||||
|
import org.eclipse.jetty.client.RoundRobinConnectionPool;
|
||||||
|
import org.eclipse.jetty.client.Socks5;
|
||||||
|
import org.eclipse.jetty.client.Socks5Proxy;
|
||||||
|
import org.eclipse.jetty.client.api.Authentication;
|
||||||
|
import org.eclipse.jetty.client.api.AuthenticationStore;
|
||||||
|
import org.eclipse.jetty.client.api.ContentResponse;
|
||||||
|
import org.eclipse.jetty.client.api.Request;
|
||||||
|
import org.eclipse.jetty.client.api.Response;
|
||||||
|
import org.eclipse.jetty.client.api.Result;
|
||||||
|
import org.eclipse.jetty.client.dynamic.HttpClientTransportDynamic;
|
||||||
|
import org.eclipse.jetty.client.http.HttpClientConnectionFactory;
|
||||||
|
import org.eclipse.jetty.client.http.HttpClientTransportOverHTTP;
|
||||||
|
import org.eclipse.jetty.client.util.AsyncRequestContent;
|
||||||
|
import org.eclipse.jetty.client.util.BasicAuthentication;
|
||||||
|
import org.eclipse.jetty.client.util.BufferingResponseListener;
|
||||||
|
import org.eclipse.jetty.client.util.BytesRequestContent;
|
||||||
|
import org.eclipse.jetty.client.util.DigestAuthentication;
|
||||||
|
import org.eclipse.jetty.client.util.FutureResponseListener;
|
||||||
|
import org.eclipse.jetty.client.util.InputStreamRequestContent;
|
||||||
|
import org.eclipse.jetty.client.util.InputStreamResponseListener;
|
||||||
|
import org.eclipse.jetty.client.util.OutputStreamRequestContent;
|
||||||
|
import org.eclipse.jetty.client.util.PathRequestContent;
|
||||||
|
import org.eclipse.jetty.client.util.StringRequestContent;
|
||||||
|
import org.eclipse.jetty.fcgi.client.http.HttpClientTransportOverFCGI;
|
||||||
|
import org.eclipse.jetty.http.HttpHeader;
|
||||||
|
import org.eclipse.jetty.http.HttpMethod;
|
||||||
|
import org.eclipse.jetty.http.HttpStatus;
|
||||||
|
import org.eclipse.jetty.http.HttpVersion;
|
||||||
|
import org.eclipse.jetty.http2.client.HTTP2Client;
|
||||||
|
import org.eclipse.jetty.http2.client.http.ClientConnectionFactoryOverHTTP2;
|
||||||
|
import org.eclipse.jetty.http2.client.http.HttpClientTransportOverHTTP2;
|
||||||
|
import org.eclipse.jetty.http3.client.HTTP3Client;
|
||||||
|
import org.eclipse.jetty.http3.client.http.HttpClientTransportOverHTTP3;
|
||||||
|
import org.eclipse.jetty.io.ByteBufferPool;
|
||||||
|
import org.eclipse.jetty.io.ClientConnectionFactory;
|
||||||
|
import org.eclipse.jetty.io.ClientConnector;
|
||||||
|
import org.eclipse.jetty.util.Callback;
|
||||||
|
import org.eclipse.jetty.util.HttpCookieStore;
|
||||||
|
import org.eclipse.jetty.util.component.LifeCycle;
|
||||||
|
import org.eclipse.jetty.util.ssl.SslContextFactory;
|
||||||
|
|
||||||
|
import static java.lang.System.Logger.Level.INFO;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class HTTPClientDocs
|
||||||
|
{
|
||||||
|
public void start() throws Exception
|
||||||
|
{
|
||||||
|
// tag::start[]
|
||||||
|
// Instantiate HttpClient.
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
|
||||||
|
// Configure HttpClient, for example:
|
||||||
|
httpClient.setFollowRedirects(false);
|
||||||
|
|
||||||
|
// Start HttpClient.
|
||||||
|
httpClient.start();
|
||||||
|
// end::start[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
// tag::stop[]
|
||||||
|
// Stop HttpClient.
|
||||||
|
httpClient.stop();
|
||||||
|
// end::stop[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stopFromOtherThread() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
// tag::stopFromOtherThread[]
|
||||||
|
// Stop HttpClient from a new thread.
|
||||||
|
// Use LifeCycle.stop(...) to rethrow checked exceptions as unchecked.
|
||||||
|
new Thread(() -> LifeCycle.stop(httpClient)).start();
|
||||||
|
// end::stopFromOtherThread[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void tlsExplicit() throws Exception
|
||||||
|
{
|
||||||
|
// tag::tlsExplicit[]
|
||||||
|
SslContextFactory.Client sslContextFactory = new SslContextFactory.Client();
|
||||||
|
|
||||||
|
ClientConnector clientConnector = new ClientConnector();
|
||||||
|
clientConnector.setSslContextFactory(sslContextFactory);
|
||||||
|
|
||||||
|
HttpClient httpClient = new HttpClient(new HttpClientTransportDynamic(clientConnector));
|
||||||
|
httpClient.start();
|
||||||
|
// end::tlsExplicit[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void tlsNoValidation()
|
||||||
|
{
|
||||||
|
// tag::tlsNoValidation[]
|
||||||
|
SslContextFactory.Client sslContextFactory = new SslContextFactory.Client();
|
||||||
|
// Disable the validation of the server host name at the TLS level.
|
||||||
|
sslContextFactory.setEndpointIdentificationAlgorithm(null);
|
||||||
|
// end::tlsNoValidation[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void tlsAppValidation()
|
||||||
|
{
|
||||||
|
// tag::tlsAppValidation[]
|
||||||
|
SslContextFactory.Client sslContextFactory = new SslContextFactory.Client();
|
||||||
|
// Only allow to connect to subdomains of domain.com.
|
||||||
|
sslContextFactory.setHostnameVerifier((hostName, session) -> hostName.endsWith(".domain.com"));
|
||||||
|
// end::tlsAppValidation[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void simpleBlockingGet() throws Exception
|
||||||
|
{
|
||||||
|
// tag::simpleBlockingGet[]
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// Perform a simple GET and wait for the response.
|
||||||
|
ContentResponse response = httpClient.GET("http://domain.com/path?query");
|
||||||
|
// end::simpleBlockingGet[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void headFluent() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// tag::headFluent[]
|
||||||
|
ContentResponse response = httpClient.newRequest("http://domain.com/path?query")
|
||||||
|
.method(HttpMethod.HEAD)
|
||||||
|
.agent("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:17.0) Gecko/20100101 Firefox/17.0")
|
||||||
|
.send();
|
||||||
|
// end::headFluent[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void headNonFluent() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// tag::headNonFluent[]
|
||||||
|
Request request = httpClient.newRequest("http://domain.com/path?query");
|
||||||
|
request.method(HttpMethod.HEAD);
|
||||||
|
request.agent("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:17.0) Gecko/20100101 Firefox/17.0");
|
||||||
|
ContentResponse response = request.send();
|
||||||
|
// end::headNonFluent[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void postFluent() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// tag::postFluent[]
|
||||||
|
ContentResponse response = httpClient.POST("http://domain.com/entity/1")
|
||||||
|
.param("p", "value")
|
||||||
|
.send();
|
||||||
|
// end::postFluent[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void fileFluent() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// tag::fileFluent[]
|
||||||
|
ContentResponse response = httpClient.POST("http://domain.com/upload")
|
||||||
|
.file(Paths.get("file_to_upload.txt"), "text/plain")
|
||||||
|
.send();
|
||||||
|
// end::fileFluent[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void totalTimeout() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// tag::totalTimeout[]
|
||||||
|
ContentResponse response = httpClient.newRequest("http://domain.com/path?query")
|
||||||
|
.timeout(5, TimeUnit.SECONDS)
|
||||||
|
.send();
|
||||||
|
// end::totalTimeout[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void simpleNonBlocking() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// tag::simpleNonBlocking[]
|
||||||
|
httpClient.newRequest("http://domain.com/path")
|
||||||
|
.send(result ->
|
||||||
|
{
|
||||||
|
// Your logic here
|
||||||
|
});
|
||||||
|
// end::simpleNonBlocking[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void nonBlockingTotalTimeout() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// tag::nonBlockingTotalTimeout[]
|
||||||
|
httpClient.newRequest("http://domain.com/path")
|
||||||
|
.timeout(3, TimeUnit.SECONDS)
|
||||||
|
.send(result ->
|
||||||
|
{
|
||||||
|
/* Your logic here */
|
||||||
|
});
|
||||||
|
// end::nonBlockingTotalTimeout[]
|
||||||
|
}
|
||||||
|
|
||||||
|
// @checkstyle-disable-check : LeftCurly
|
||||||
|
public void listeners() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// tag::listeners[]
|
||||||
|
httpClient.newRequest("http://domain.com/path")
|
||||||
|
// Add request hooks.
|
||||||
|
.onRequestQueued(request -> { /* ... */ })
|
||||||
|
.onRequestBegin(request -> { /* ... */ })
|
||||||
|
.onRequestHeaders(request -> { /* ... */ })
|
||||||
|
.onRequestCommit(request -> { /* ... */ })
|
||||||
|
.onRequestContent((request, content) -> { /* ... */ })
|
||||||
|
.onRequestFailure((request, failure) -> { /* ... */ })
|
||||||
|
.onRequestSuccess(request -> { /* ... */ })
|
||||||
|
// Add response hooks.
|
||||||
|
.onResponseBegin(response -> { /* ... */ })
|
||||||
|
.onResponseHeader((response, field) -> true)
|
||||||
|
.onResponseHeaders(response -> { /* ... */ })
|
||||||
|
.onResponseContentAsync((response, buffer, callback) -> callback.succeeded())
|
||||||
|
.onResponseFailure((response, failure) -> { /* ... */ })
|
||||||
|
.onResponseSuccess(response -> { /* ... */ })
|
||||||
|
// Result hook.
|
||||||
|
.send(result -> { /* ... */ });
|
||||||
|
// end::listeners[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void pathRequestContent() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// tag::pathRequestContent[]
|
||||||
|
ContentResponse response = httpClient.POST("http://domain.com/upload")
|
||||||
|
.body(new PathRequestContent("text/plain", Paths.get("file_to_upload.txt")))
|
||||||
|
.send();
|
||||||
|
// end::pathRequestContent[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void inputStreamRequestContent() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// tag::inputStreamRequestContent[]
|
||||||
|
ContentResponse response = httpClient.POST("http://domain.com/upload")
|
||||||
|
.body(new InputStreamRequestContent("text/plain", new FileInputStream("file_to_upload.txt")))
|
||||||
|
.send();
|
||||||
|
// end::inputStreamRequestContent[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void bytesStringRequestContent() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
byte[] bytes = new byte[1024];
|
||||||
|
String string = new String(bytes);
|
||||||
|
// tag::bytesStringRequestContent[]
|
||||||
|
ContentResponse bytesResponse = httpClient.POST("http://domain.com/upload")
|
||||||
|
.body(new BytesRequestContent("text/plain", bytes))
|
||||||
|
.send();
|
||||||
|
|
||||||
|
ContentResponse stringResponse = httpClient.POST("http://domain.com/upload")
|
||||||
|
.body(new StringRequestContent("text/plain", string))
|
||||||
|
.send();
|
||||||
|
// end::bytesStringRequestContent[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void asyncRequestContent() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// tag::asyncRequestContent[]
|
||||||
|
AsyncRequestContent content = new AsyncRequestContent();
|
||||||
|
httpClient.POST("http://domain.com/upload")
|
||||||
|
.body(content)
|
||||||
|
.send(result ->
|
||||||
|
{
|
||||||
|
// Your logic here
|
||||||
|
});
|
||||||
|
|
||||||
|
// Content not available yet here.
|
||||||
|
|
||||||
|
// An event happens in some other class, in some other thread.
|
||||||
|
class ContentPublisher
|
||||||
|
{
|
||||||
|
void publish(ByteBufferPool bufferPool, byte[] bytes, boolean lastContent)
|
||||||
|
{
|
||||||
|
// Wrap the bytes into a new ByteBuffer.
|
||||||
|
ByteBuffer buffer = ByteBuffer.wrap(bytes);
|
||||||
|
|
||||||
|
// Offer the content, and release the ByteBuffer
|
||||||
|
// to the pool when the Callback is completed.
|
||||||
|
content.offer(buffer, Callback.from(() -> bufferPool.release(buffer)));
|
||||||
|
|
||||||
|
// Close AsyncRequestContent when all the content is arrived.
|
||||||
|
if (lastContent)
|
||||||
|
content.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// end::asyncRequestContent[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void outputStreamRequestContent() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// tag::outputStreamRequestContent[]
|
||||||
|
OutputStreamRequestContent content = new OutputStreamRequestContent();
|
||||||
|
|
||||||
|
// Use try-with-resources to close the OutputStream when all content is written.
|
||||||
|
try (OutputStream output = content.getOutputStream())
|
||||||
|
{
|
||||||
|
httpClient.POST("http://localhost:8080/")
|
||||||
|
.body(content)
|
||||||
|
.send(result ->
|
||||||
|
{
|
||||||
|
// Your logic here
|
||||||
|
});
|
||||||
|
|
||||||
|
// Content not available yet here.
|
||||||
|
|
||||||
|
// Content is now available.
|
||||||
|
byte[] bytes = new byte[]{'h', 'e', 'l', 'l', 'o'};
|
||||||
|
output.write(bytes);
|
||||||
|
}
|
||||||
|
// End of try-with-resource, output.close() called automatically to signal end of content.
|
||||||
|
// end::outputStreamRequestContent[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void futureResponseListener() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// tag::futureResponseListener[]
|
||||||
|
Request request = httpClient.newRequest("http://domain.com/path");
|
||||||
|
|
||||||
|
// Limit response content buffer to 512 KiB.
|
||||||
|
FutureResponseListener listener = new FutureResponseListener(request, 512 * 1024);
|
||||||
|
|
||||||
|
request.send(listener);
|
||||||
|
|
||||||
|
// Wait at most 5 seconds for request+response to complete.
|
||||||
|
ContentResponse response = listener.get(5, TimeUnit.SECONDS);
|
||||||
|
// end::futureResponseListener[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void bufferingResponseListener() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// tag::bufferingResponseListener[]
|
||||||
|
httpClient.newRequest("http://domain.com/path")
|
||||||
|
// Buffer response content up to 8 MiB
|
||||||
|
.send(new BufferingResponseListener(8 * 1024 * 1024)
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onComplete(Result result)
|
||||||
|
{
|
||||||
|
if (!result.isFailed())
|
||||||
|
{
|
||||||
|
byte[] responseContent = getContent();
|
||||||
|
// Your logic here
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// end::bufferingResponseListener[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void inputStreamResponseListener() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// tag::inputStreamResponseListener[]
|
||||||
|
InputStreamResponseListener listener = new InputStreamResponseListener();
|
||||||
|
httpClient.newRequest("http://domain.com/path")
|
||||||
|
.send(listener);
|
||||||
|
|
||||||
|
// Wait for the response headers to arrive.
|
||||||
|
Response response = listener.get(5, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
// Look at the response before streaming the content.
|
||||||
|
if (response.getStatus() == HttpStatus.OK_200)
|
||||||
|
{
|
||||||
|
// Use try-with-resources to close input stream.
|
||||||
|
try (InputStream responseContent = listener.getInputStream())
|
||||||
|
{
|
||||||
|
// Your logic here
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
response.abort(new IOException("Unexpected HTTP response"));
|
||||||
|
}
|
||||||
|
// end::inputStreamResponseListener[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void demandedContentListener() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
String host1 = "localhost";
|
||||||
|
String host2 = "localhost";
|
||||||
|
int port1 = 8080;
|
||||||
|
int port2 = 8080;
|
||||||
|
// tag::demandedContentListener[]
|
||||||
|
// Prepare a request to server1, the source.
|
||||||
|
Request request1 = httpClient.newRequest(host1, port1)
|
||||||
|
.path("/source");
|
||||||
|
|
||||||
|
// Prepare a request to server2, the sink.
|
||||||
|
AsyncRequestContent content2 = new AsyncRequestContent();
|
||||||
|
Request request2 = httpClient.newRequest(host2, port2)
|
||||||
|
.path("/sink")
|
||||||
|
.body(content2);
|
||||||
|
|
||||||
|
request1.onResponseContentDemanded(new Response.DemandedContentListener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onBeforeContent(Response response, LongConsumer demand)
|
||||||
|
{
|
||||||
|
request2.onRequestCommit(request ->
|
||||||
|
{
|
||||||
|
// Only when the request to server2 has been sent,
|
||||||
|
// then demand response content from server1.
|
||||||
|
demand.accept(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Send the request to server2.
|
||||||
|
request2.send(result -> System.getLogger("forwarder").log(INFO, "Forwarding to server2 complete"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onContent(Response response, LongConsumer demand, ByteBuffer content, Callback callback)
|
||||||
|
{
|
||||||
|
// When response content is received from server1, forward it to server2.
|
||||||
|
content2.offer(content, Callback.from(() ->
|
||||||
|
{
|
||||||
|
// When the request content to server2 is sent,
|
||||||
|
// succeed the callback to recycle the buffer.
|
||||||
|
callback.succeeded();
|
||||||
|
// Then demand more response content from server1.
|
||||||
|
demand.accept(1);
|
||||||
|
}, callback::failed));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// When the response content from server1 is complete,
|
||||||
|
// complete also the request content to server2.
|
||||||
|
request1.onResponseSuccess(response -> content2.close());
|
||||||
|
|
||||||
|
// Send the request to server1.
|
||||||
|
request1.send(result -> System.getLogger("forwarder").log(INFO, "Sourcing from server1 complete"));
|
||||||
|
// end::demandedContentListener[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void getCookies() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// tag::getCookies[]
|
||||||
|
CookieStore cookieStore = httpClient.getCookieStore();
|
||||||
|
List<HttpCookie> cookies = cookieStore.get(URI.create("http://domain.com/path"));
|
||||||
|
// end::getCookies[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCookie() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// tag::setCookie[]
|
||||||
|
CookieStore cookieStore = httpClient.getCookieStore();
|
||||||
|
HttpCookie cookie = new HttpCookie("foo", "bar");
|
||||||
|
cookie.setDomain("domain.com");
|
||||||
|
cookie.setPath("/");
|
||||||
|
cookie.setMaxAge(TimeUnit.DAYS.toSeconds(1));
|
||||||
|
cookieStore.add(URI.create("http://domain.com"), cookie);
|
||||||
|
// end::setCookie[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void requestCookie() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// tag::requestCookie[]
|
||||||
|
ContentResponse response = httpClient.newRequest("http://domain.com/path")
|
||||||
|
.cookie(new HttpCookie("foo", "bar"))
|
||||||
|
.send();
|
||||||
|
// end::requestCookie[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeCookie() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// tag::removeCookie[]
|
||||||
|
CookieStore cookieStore = httpClient.getCookieStore();
|
||||||
|
URI uri = URI.create("http://domain.com");
|
||||||
|
List<HttpCookie> cookies = cookieStore.get(uri);
|
||||||
|
for (HttpCookie cookie : cookies)
|
||||||
|
{
|
||||||
|
cookieStore.remove(uri, cookie);
|
||||||
|
}
|
||||||
|
// end::removeCookie[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void emptyCookieStore() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// tag::emptyCookieStore[]
|
||||||
|
httpClient.setCookieStore(new HttpCookieStore.Empty());
|
||||||
|
// end::emptyCookieStore[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void filteringCookieStore() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// tag::filteringCookieStore[]
|
||||||
|
class GoogleOnlyCookieStore extends HttpCookieStore
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void add(URI uri, HttpCookie cookie)
|
||||||
|
{
|
||||||
|
if (uri.getHost().endsWith("google.com"))
|
||||||
|
super.add(uri, cookie);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
httpClient.setCookieStore(new GoogleOnlyCookieStore());
|
||||||
|
// end::filteringCookieStore[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addAuthentication() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// tag::addAuthentication[]
|
||||||
|
// Add authentication credentials.
|
||||||
|
AuthenticationStore auth = httpClient.getAuthenticationStore();
|
||||||
|
|
||||||
|
URI uri1 = new URI("http://mydomain.com/secure");
|
||||||
|
auth.addAuthentication(new BasicAuthentication(uri1, "MyRealm", "userName1", "password1"));
|
||||||
|
|
||||||
|
URI uri2 = new URI("http://otherdomain.com/admin");
|
||||||
|
auth.addAuthentication(new BasicAuthentication(uri1, "AdminRealm", "admin", "password"));
|
||||||
|
// end::addAuthentication[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clearResults() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// tag::clearResults[]
|
||||||
|
httpClient.getAuthenticationStore().clearAuthenticationResults();
|
||||||
|
// end::clearResults[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void preemptedResult() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// tag::preemptedResult[]
|
||||||
|
AuthenticationStore auth = httpClient.getAuthenticationStore();
|
||||||
|
URI uri = URI.create("http://domain.com/secure");
|
||||||
|
auth.addAuthenticationResult(new BasicAuthentication.BasicResult(uri, "username", "password"));
|
||||||
|
// end::preemptedResult[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void requestPreemptedResult() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// tag::requestPreemptedResult[]
|
||||||
|
URI uri = URI.create("http://domain.com/secure");
|
||||||
|
Authentication.Result authn = new BasicAuthentication.BasicResult(uri, "username", "password");
|
||||||
|
Request request = httpClient.newRequest(uri);
|
||||||
|
authn.apply(request);
|
||||||
|
request.send();
|
||||||
|
// end::requestPreemptedResult[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void proxy() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// tag::proxy[]
|
||||||
|
HttpProxy proxy = new HttpProxy("proxyHost", 8888);
|
||||||
|
|
||||||
|
// Do not proxy requests for localhost:8080.
|
||||||
|
proxy.getExcludedAddresses().add("localhost:8080");
|
||||||
|
|
||||||
|
// Add the new proxy to the list of proxies already registered.
|
||||||
|
ProxyConfiguration proxyConfig = httpClient.getProxyConfiguration();
|
||||||
|
proxyConfig.addProxy(proxy);
|
||||||
|
|
||||||
|
ContentResponse response = httpClient.GET("http://domain.com/path");
|
||||||
|
// end::proxy[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void proxySocks5() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// tag::proxySocks5[]
|
||||||
|
Socks5Proxy proxy = new Socks5Proxy("proxyHost", 8888);
|
||||||
|
String socks5User = "jetty";
|
||||||
|
String socks5Pass = "secret";
|
||||||
|
var socks5AuthenticationFactory = new Socks5.UsernamePasswordAuthenticationFactory(socks5User, socks5Pass);
|
||||||
|
// Add the authentication method to the proxy.
|
||||||
|
proxy.putAuthenticationFactory(socks5AuthenticationFactory);
|
||||||
|
|
||||||
|
// Do not proxy requests for localhost:8080.
|
||||||
|
proxy.getExcludedAddresses().add("localhost:8080");
|
||||||
|
|
||||||
|
// Add the new proxy to the list of proxies already registered.
|
||||||
|
ProxyConfiguration proxyConfig = httpClient.getProxyConfiguration();
|
||||||
|
proxyConfig.addProxy(proxy);
|
||||||
|
|
||||||
|
ContentResponse response = httpClient.GET("http://domain.com/path");
|
||||||
|
// end::proxySocks5[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void proxyAuthentication() throws Exception
|
||||||
|
{
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// tag::proxyAuthentication[]
|
||||||
|
AuthenticationStore auth = httpClient.getAuthenticationStore();
|
||||||
|
|
||||||
|
// Proxy credentials.
|
||||||
|
URI proxyURI = new URI("http://proxy.net:8080");
|
||||||
|
auth.addAuthentication(new BasicAuthentication(proxyURI, "ProxyRealm", "proxyUser", "proxyPass"));
|
||||||
|
|
||||||
|
// Server credentials.
|
||||||
|
URI serverURI = new URI("http://domain.com/secure");
|
||||||
|
auth.addAuthentication(new DigestAuthentication(serverURI, "ServerRealm", "serverUser", "serverPass"));
|
||||||
|
|
||||||
|
// Proxy configuration.
|
||||||
|
ProxyConfiguration proxyConfig = httpClient.getProxyConfiguration();
|
||||||
|
HttpProxy proxy = new HttpProxy("proxy.net", 8080);
|
||||||
|
proxyConfig.addProxy(proxy);
|
||||||
|
|
||||||
|
ContentResponse response = httpClient.newRequest(serverURI).send();
|
||||||
|
// end::proxyAuthentication[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void defaultTransport() throws Exception
|
||||||
|
{
|
||||||
|
// tag::defaultTransport[]
|
||||||
|
// No transport specified, using default.
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
// end::defaultTransport[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void http11Transport() throws Exception
|
||||||
|
{
|
||||||
|
// tag::http11Transport[]
|
||||||
|
// Configure HTTP/1.1 transport.
|
||||||
|
HttpClientTransportOverHTTP transport = new HttpClientTransportOverHTTP();
|
||||||
|
transport.setHeaderCacheSize(16384);
|
||||||
|
|
||||||
|
HttpClient client = new HttpClient(transport);
|
||||||
|
client.start();
|
||||||
|
// end::http11Transport[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void http2Transport() throws Exception
|
||||||
|
{
|
||||||
|
// tag::http2Transport[]
|
||||||
|
// The HTTP2Client powers the HTTP/2 transport.
|
||||||
|
HTTP2Client h2Client = new HTTP2Client();
|
||||||
|
h2Client.setInitialSessionRecvWindow(64 * 1024 * 1024);
|
||||||
|
|
||||||
|
// Create and configure the HTTP/2 transport.
|
||||||
|
HttpClientTransportOverHTTP2 transport = new HttpClientTransportOverHTTP2(h2Client);
|
||||||
|
transport.setUseALPN(true);
|
||||||
|
|
||||||
|
HttpClient client = new HttpClient(transport);
|
||||||
|
client.start();
|
||||||
|
// end::http2Transport[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void http3Transport() throws Exception
|
||||||
|
{
|
||||||
|
// tag::http3Transport[]
|
||||||
|
// The HTTP3Client powers the HTTP/3 transport.
|
||||||
|
HTTP3Client h3Client = new HTTP3Client();
|
||||||
|
h3Client.getQuicConfiguration().setSessionRecvWindow(64 * 1024 * 1024);
|
||||||
|
|
||||||
|
// Create and configure the HTTP/3 transport.
|
||||||
|
HttpClientTransportOverHTTP3 transport = new HttpClientTransportOverHTTP3(h3Client);
|
||||||
|
|
||||||
|
HttpClient client = new HttpClient(transport);
|
||||||
|
client.start();
|
||||||
|
// end::http3Transport[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void fcgiTransport() throws Exception
|
||||||
|
{
|
||||||
|
// tag::fcgiTransport[]
|
||||||
|
String scriptRoot = "/var/www/wordpress";
|
||||||
|
HttpClientTransportOverFCGI transport = new HttpClientTransportOverFCGI(scriptRoot);
|
||||||
|
|
||||||
|
HttpClient client = new HttpClient(transport);
|
||||||
|
client.start();
|
||||||
|
// end::fcgiTransport[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void dynamicDefault() throws Exception
|
||||||
|
{
|
||||||
|
// tag::dynamicDefault[]
|
||||||
|
// Dynamic transport speaks HTTP/1.1 by default.
|
||||||
|
HttpClientTransportDynamic transport = new HttpClientTransportDynamic();
|
||||||
|
|
||||||
|
HttpClient client = new HttpClient(transport);
|
||||||
|
client.start();
|
||||||
|
// end::dynamicDefault[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void dynamicOneProtocol()
|
||||||
|
{
|
||||||
|
// tag::dynamicOneProtocol[]
|
||||||
|
ClientConnector connector = new ClientConnector();
|
||||||
|
|
||||||
|
// Equivalent to HttpClientTransportOverHTTP.
|
||||||
|
HttpClientTransportDynamic http11Transport = new HttpClientTransportDynamic(connector, HttpClientConnectionFactory.HTTP11);
|
||||||
|
|
||||||
|
// Equivalent to HttpClientTransportOverHTTP2.
|
||||||
|
HTTP2Client http2Client = new HTTP2Client(connector);
|
||||||
|
HttpClientTransportDynamic http2Transport = new HttpClientTransportDynamic(connector, new ClientConnectionFactoryOverHTTP2.HTTP2(http2Client));
|
||||||
|
// end::dynamicOneProtocol[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void dynamicH1H2() throws Exception
|
||||||
|
{
|
||||||
|
// tag::dynamicH1H2[]
|
||||||
|
ClientConnector connector = new ClientConnector();
|
||||||
|
|
||||||
|
ClientConnectionFactory.Info http1 = HttpClientConnectionFactory.HTTP11;
|
||||||
|
|
||||||
|
HTTP2Client http2Client = new HTTP2Client(connector);
|
||||||
|
ClientConnectionFactoryOverHTTP2.HTTP2 http2 = new ClientConnectionFactoryOverHTTP2.HTTP2(http2Client);
|
||||||
|
|
||||||
|
HttpClientTransportDynamic transport = new HttpClientTransportDynamic(connector, http1, http2);
|
||||||
|
|
||||||
|
HttpClient client = new HttpClient(transport);
|
||||||
|
client.start();
|
||||||
|
// end::dynamicH1H2[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void dynamicClearText() throws Exception
|
||||||
|
{
|
||||||
|
// tag::dynamicClearText[]
|
||||||
|
ClientConnector connector = new ClientConnector();
|
||||||
|
ClientConnectionFactory.Info http1 = HttpClientConnectionFactory.HTTP11;
|
||||||
|
HTTP2Client http2Client = new HTTP2Client(connector);
|
||||||
|
ClientConnectionFactoryOverHTTP2.HTTP2 http2 = new ClientConnectionFactoryOverHTTP2.HTTP2(http2Client);
|
||||||
|
HttpClientTransportDynamic transport = new HttpClientTransportDynamic(connector, http1, http2);
|
||||||
|
HttpClient client = new HttpClient(transport);
|
||||||
|
client.start();
|
||||||
|
|
||||||
|
// The server supports both HTTP/1.1 and HTTP/2 clear-text on port 8080.
|
||||||
|
|
||||||
|
// Make a clear-text request without explicit version.
|
||||||
|
// The first protocol specified to HttpClientTransportDynamic
|
||||||
|
// is picked, in this example will be HTTP/1.1.
|
||||||
|
ContentResponse http1Response = client.newRequest("host", 8080).send();
|
||||||
|
|
||||||
|
// Make a clear-text request with explicit version.
|
||||||
|
// Clear-text HTTP/2 is used for this request.
|
||||||
|
ContentResponse http2Response = client.newRequest("host", 8080)
|
||||||
|
// Specify the version explicitly.
|
||||||
|
.version(HttpVersion.HTTP_2)
|
||||||
|
.send();
|
||||||
|
|
||||||
|
// Make a clear-text upgrade request from HTTP/1.1 to HTTP/2.
|
||||||
|
// The request will start as HTTP/1.1, but the response will be HTTP/2.
|
||||||
|
ContentResponse upgradedResponse = client.newRequest("host", 8080)
|
||||||
|
.headers(headers -> headers
|
||||||
|
.put(HttpHeader.UPGRADE, "h2c")
|
||||||
|
.put(HttpHeader.HTTP2_SETTINGS, "")
|
||||||
|
.put(HttpHeader.CONNECTION, "Upgrade, HTTP2-Settings"))
|
||||||
|
.send();
|
||||||
|
// end::dynamicClearText[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void getConnectionPool() throws Exception
|
||||||
|
{
|
||||||
|
// tag::getConnectionPool[]
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
ConnectionPool connectionPool = httpClient.getDestinations().stream()
|
||||||
|
// Cast to HttpDestination.
|
||||||
|
.map(HttpDestination.class::cast)
|
||||||
|
// Find the destination by filtering on the Origin.
|
||||||
|
.filter(destination -> destination.getOrigin().getAddress().getHost().equals("domain.com"))
|
||||||
|
.findAny()
|
||||||
|
// Get the ConnectionPool.
|
||||||
|
.map(HttpDestination::getConnectionPool)
|
||||||
|
.orElse(null);
|
||||||
|
// end::getConnectionPool[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConnectionPool() throws Exception
|
||||||
|
{
|
||||||
|
// tag::setConnectionPool[]
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
httpClient.start();
|
||||||
|
|
||||||
|
// The max number of connections in the pool.
|
||||||
|
int maxConnectionsPerDestination = httpClient.getMaxConnectionsPerDestination();
|
||||||
|
|
||||||
|
// The max number of requests per connection (multiplexing).
|
||||||
|
// Start with 1, since this value is dynamically set to larger values if
|
||||||
|
// the transport supports multiplexing requests on the same connection.
|
||||||
|
int maxRequestsPerConnection = 1;
|
||||||
|
|
||||||
|
HttpClientTransport transport = httpClient.getTransport();
|
||||||
|
|
||||||
|
// Set the ConnectionPool.Factory using a lambda.
|
||||||
|
transport.setConnectionPoolFactory(destination ->
|
||||||
|
new RoundRobinConnectionPool(destination,
|
||||||
|
maxConnectionsPerDestination,
|
||||||
|
destination,
|
||||||
|
maxRequestsPerConnection));
|
||||||
|
// end::setConnectionPool[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void unixDomain() throws Exception
|
||||||
|
{
|
||||||
|
// tag::unixDomain[]
|
||||||
|
// This is the path where the server "listens" on.
|
||||||
|
Path unixDomainPath = Path.of("/path/to/server.sock");
|
||||||
|
|
||||||
|
// Creates a ClientConnector that uses Unix-Domain
|
||||||
|
// sockets, not the network, to connect to the server.
|
||||||
|
ClientConnector unixDomainClientConnector = ClientConnector.forUnixDomain(unixDomainPath);
|
||||||
|
|
||||||
|
// Use Unix-Domain for HTTP/1.1.
|
||||||
|
HttpClientTransportOverHTTP http1Transport = new HttpClientTransportOverHTTP(unixDomainClientConnector);
|
||||||
|
|
||||||
|
// You can use Unix-Domain also for HTTP/2.
|
||||||
|
HTTP2Client http2Client = new HTTP2Client(unixDomainClientConnector);
|
||||||
|
HttpClientTransportOverHTTP2 http2Transport = new HttpClientTransportOverHTTP2(http2Client);
|
||||||
|
|
||||||
|
// You can also use UnixDomain for the dynamic transport.
|
||||||
|
ClientConnectionFactory.Info http1 = HttpClientConnectionFactory.HTTP11;
|
||||||
|
ClientConnectionFactoryOverHTTP2.HTTP2 http2 = new ClientConnectionFactoryOverHTTP2.HTTP2(http2Client);
|
||||||
|
HttpClientTransportDynamic dynamicTransport = new HttpClientTransportDynamic(unixDomainClientConnector, http1, http2);
|
||||||
|
|
||||||
|
// Choose the transport you prefer for HttpClient, for example the dynamic transport.
|
||||||
|
HttpClient httpClient = new HttpClient(dynamicTransport);
|
||||||
|
httpClient.start();
|
||||||
|
// end::unixDomain[]
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,377 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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 org.eclipse.jetty.docs.programming.client.http2;
|
||||||
|
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.SocketAddress;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.http.HttpFields;
|
||||||
|
import org.eclipse.jetty.http.HttpHeader;
|
||||||
|
import org.eclipse.jetty.http.HttpURI;
|
||||||
|
import org.eclipse.jetty.http.HttpVersion;
|
||||||
|
import org.eclipse.jetty.http.MetaData;
|
||||||
|
import org.eclipse.jetty.http2.ErrorCode;
|
||||||
|
import org.eclipse.jetty.http2.api.Session;
|
||||||
|
import org.eclipse.jetty.http2.api.Stream;
|
||||||
|
import org.eclipse.jetty.http2.client.HTTP2Client;
|
||||||
|
import org.eclipse.jetty.http2.frames.DataFrame;
|
||||||
|
import org.eclipse.jetty.http2.frames.HeadersFrame;
|
||||||
|
import org.eclipse.jetty.http2.frames.PushPromiseFrame;
|
||||||
|
import org.eclipse.jetty.http2.frames.ResetFrame;
|
||||||
|
import org.eclipse.jetty.http2.frames.SettingsFrame;
|
||||||
|
import org.eclipse.jetty.io.ClientConnector;
|
||||||
|
import org.eclipse.jetty.util.Callback;
|
||||||
|
|
||||||
|
import static java.lang.System.Logger.Level.INFO;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class HTTP2ClientDocs
|
||||||
|
{
|
||||||
|
public void start() throws Exception
|
||||||
|
{
|
||||||
|
// tag::start[]
|
||||||
|
// Instantiate HTTP2Client.
|
||||||
|
HTTP2Client http2Client = new HTTP2Client();
|
||||||
|
|
||||||
|
// Configure HTTP2Client, for example:
|
||||||
|
http2Client.setStreamIdleTimeout(15000);
|
||||||
|
|
||||||
|
// Start HTTP2Client.
|
||||||
|
http2Client.start();
|
||||||
|
// end::start[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop() throws Exception
|
||||||
|
{
|
||||||
|
HTTP2Client http2Client = new HTTP2Client();
|
||||||
|
http2Client.start();
|
||||||
|
// tag::stop[]
|
||||||
|
// Stop HTTP2Client.
|
||||||
|
http2Client.stop();
|
||||||
|
// end::stop[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clearTextConnect() throws Exception
|
||||||
|
{
|
||||||
|
HTTP2Client http2Client = new HTTP2Client();
|
||||||
|
http2Client.start();
|
||||||
|
// tag::clearTextConnect[]
|
||||||
|
// Address of the server's clear-text port.
|
||||||
|
SocketAddress serverAddress = new InetSocketAddress("localhost", 8080);
|
||||||
|
|
||||||
|
// Connect to the server, the CompletableFuture will be
|
||||||
|
// notified when the connection is succeeded (or failed).
|
||||||
|
CompletableFuture<Session> sessionCF = http2Client.connect(serverAddress, new Session.Listener.Adapter());
|
||||||
|
|
||||||
|
// Block to obtain the Session.
|
||||||
|
// Alternatively you can use the CompletableFuture APIs to avoid blocking.
|
||||||
|
Session session = sessionCF.get();
|
||||||
|
// end::clearTextConnect[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void encryptedConnect() throws Exception
|
||||||
|
{
|
||||||
|
// tag::encryptedConnect[]
|
||||||
|
HTTP2Client http2Client = new HTTP2Client();
|
||||||
|
http2Client.start();
|
||||||
|
|
||||||
|
ClientConnector connector = http2Client.getClientConnector();
|
||||||
|
|
||||||
|
// Address of the server's encrypted port.
|
||||||
|
SocketAddress serverAddress = new InetSocketAddress("localhost", 8443);
|
||||||
|
|
||||||
|
// Connect to the server, the CompletableFuture will be
|
||||||
|
// notified when the connection is succeeded (or failed).
|
||||||
|
CompletableFuture<Session> sessionCF = http2Client.connect(connector.getSslContextFactory(), serverAddress, new Session.Listener.Adapter());
|
||||||
|
|
||||||
|
// Block to obtain the Session.
|
||||||
|
// Alternatively you can use the CompletableFuture APIs to avoid blocking.
|
||||||
|
Session session = sessionCF.get();
|
||||||
|
// end::encryptedConnect[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void configure() throws Exception
|
||||||
|
{
|
||||||
|
HTTP2Client http2Client = new HTTP2Client();
|
||||||
|
http2Client.start();
|
||||||
|
|
||||||
|
// tag::configure[]
|
||||||
|
SocketAddress serverAddress = new InetSocketAddress("localhost", 8080);
|
||||||
|
http2Client.connect(serverAddress, new Session.Listener.Adapter()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public Map<Integer, Integer> onPreface(Session session)
|
||||||
|
{
|
||||||
|
Map<Integer, Integer> configuration = new HashMap<>();
|
||||||
|
|
||||||
|
// Disable push from the server.
|
||||||
|
configuration.put(SettingsFrame.ENABLE_PUSH, 0);
|
||||||
|
|
||||||
|
// Override HTTP2Client.initialStreamRecvWindow for this session.
|
||||||
|
configuration.put(SettingsFrame.INITIAL_WINDOW_SIZE, 1024 * 1024);
|
||||||
|
|
||||||
|
return configuration;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// end::configure[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void newStream() throws Exception
|
||||||
|
{
|
||||||
|
HTTP2Client http2Client = new HTTP2Client();
|
||||||
|
http2Client.start();
|
||||||
|
// tag::newStream[]
|
||||||
|
SocketAddress serverAddress = new InetSocketAddress("localhost", 8080);
|
||||||
|
CompletableFuture<Session> sessionCF = http2Client.connect(serverAddress, new Session.Listener.Adapter());
|
||||||
|
Session session = sessionCF.get();
|
||||||
|
|
||||||
|
// Configure the request headers.
|
||||||
|
HttpFields requestHeaders = HttpFields.build()
|
||||||
|
.put(HttpHeader.USER_AGENT, "Jetty HTTP2Client {version}");
|
||||||
|
|
||||||
|
// The request metadata with method, URI and headers.
|
||||||
|
MetaData.Request request = new MetaData.Request("GET", HttpURI.from("http://localhost:8080/path"), HttpVersion.HTTP_2, requestHeaders);
|
||||||
|
|
||||||
|
// The HTTP/2 HEADERS frame, with endStream=true
|
||||||
|
// to signal that this request has no content.
|
||||||
|
HeadersFrame headersFrame = new HeadersFrame(request, null, true);
|
||||||
|
|
||||||
|
// Open a Stream by sending the HEADERS frame.
|
||||||
|
session.newStream(headersFrame, new Stream.Listener.Adapter());
|
||||||
|
// end::newStream[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void newStreamWithData() throws Exception
|
||||||
|
{
|
||||||
|
HTTP2Client http2Client = new HTTP2Client();
|
||||||
|
http2Client.start();
|
||||||
|
// tag::newStreamWithData[]
|
||||||
|
SocketAddress serverAddress = new InetSocketAddress("localhost", 8080);
|
||||||
|
CompletableFuture<Session> sessionCF = http2Client.connect(serverAddress, new Session.Listener.Adapter());
|
||||||
|
Session session = sessionCF.get();
|
||||||
|
|
||||||
|
// Configure the request headers.
|
||||||
|
HttpFields requestHeaders = HttpFields.build()
|
||||||
|
.put(HttpHeader.CONTENT_TYPE, "application/json");
|
||||||
|
|
||||||
|
// The request metadata with method, URI and headers.
|
||||||
|
MetaData.Request request = new MetaData.Request("POST", HttpURI.from("http://localhost:8080/path"), HttpVersion.HTTP_2, requestHeaders);
|
||||||
|
|
||||||
|
// The HTTP/2 HEADERS frame, with endStream=false to
|
||||||
|
// signal that there will be more frames in this stream.
|
||||||
|
HeadersFrame headersFrame = new HeadersFrame(request, null, false);
|
||||||
|
|
||||||
|
// Open a Stream by sending the HEADERS frame.
|
||||||
|
CompletableFuture<Stream> streamCF = session.newStream(headersFrame, new Stream.Listener.Adapter());
|
||||||
|
|
||||||
|
// Block to obtain the Stream.
|
||||||
|
// Alternatively you can use the CompletableFuture APIs to avoid blocking.
|
||||||
|
Stream stream = streamCF.get();
|
||||||
|
|
||||||
|
// The request content, in two chunks.
|
||||||
|
String content1 = "{\"greet\": \"hello world\"}";
|
||||||
|
ByteBuffer buffer1 = StandardCharsets.UTF_8.encode(content1);
|
||||||
|
String content2 = "{\"user\": \"jetty\"}";
|
||||||
|
ByteBuffer buffer2 = StandardCharsets.UTF_8.encode(content2);
|
||||||
|
|
||||||
|
// Send the first DATA frame on the stream, with endStream=false
|
||||||
|
// to signal that there are more frames in this stream.
|
||||||
|
CompletableFuture<Stream> dataCF1 = stream.data(new DataFrame(stream.getId(), buffer1, false));
|
||||||
|
|
||||||
|
// Only when the first chunk has been sent we can send the second,
|
||||||
|
// with endStream=true to signal that there are no more frames.
|
||||||
|
dataCF1.thenCompose(s -> s.data(new DataFrame(s.getId(), buffer2, true)));
|
||||||
|
// end::newStreamWithData[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void responseListener() throws Exception
|
||||||
|
{
|
||||||
|
HTTP2Client http2Client = new HTTP2Client();
|
||||||
|
http2Client.start();
|
||||||
|
SocketAddress serverAddress = new InetSocketAddress("localhost", 8080);
|
||||||
|
CompletableFuture<Session> sessionCF = http2Client.connect(serverAddress, new Session.Listener.Adapter());
|
||||||
|
Session session = sessionCF.get();
|
||||||
|
|
||||||
|
HttpFields requestHeaders = HttpFields.build()
|
||||||
|
.put(HttpHeader.USER_AGENT, "Jetty HTTP2Client {version}");
|
||||||
|
MetaData.Request request = new MetaData.Request("GET", HttpURI.from("http://localhost:8080/path"), HttpVersion.HTTP_2, requestHeaders);
|
||||||
|
HeadersFrame headersFrame = new HeadersFrame(request, null, true);
|
||||||
|
|
||||||
|
// tag::responseListener[]
|
||||||
|
// Open a Stream by sending the HEADERS frame.
|
||||||
|
session.newStream(headersFrame, new Stream.Listener.Adapter()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onHeaders(Stream stream, HeadersFrame frame)
|
||||||
|
{
|
||||||
|
MetaData metaData = frame.getMetaData();
|
||||||
|
|
||||||
|
// Is this HEADERS frame the response or the trailers?
|
||||||
|
if (metaData.isResponse())
|
||||||
|
{
|
||||||
|
MetaData.Response response = (MetaData.Response)metaData;
|
||||||
|
System.getLogger("http2").log(INFO, "Received response {0}", response);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
System.getLogger("http2").log(INFO, "Received trailers {0}", metaData.getFields());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onData(Stream stream, DataFrame frame, Callback callback)
|
||||||
|
{
|
||||||
|
// Get the content buffer.
|
||||||
|
ByteBuffer buffer = frame.getData();
|
||||||
|
|
||||||
|
// Consume the buffer, here - as an example - just log it.
|
||||||
|
System.getLogger("http2").log(INFO, "Consuming buffer {0}", buffer);
|
||||||
|
|
||||||
|
// Tell the implementation that the buffer has been consumed.
|
||||||
|
callback.succeeded();
|
||||||
|
|
||||||
|
// By returning from the method, implicitly tell the implementation
|
||||||
|
// to deliver to this method more DATA frames when they are available.
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// end::responseListener[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void reset() throws Exception
|
||||||
|
{
|
||||||
|
HTTP2Client http2Client = new HTTP2Client();
|
||||||
|
http2Client.start();
|
||||||
|
SocketAddress serverAddress = new InetSocketAddress("localhost", 8080);
|
||||||
|
CompletableFuture<Session> sessionCF = http2Client.connect(serverAddress, new Session.Listener.Adapter());
|
||||||
|
Session session = sessionCF.get();
|
||||||
|
|
||||||
|
HttpFields requestHeaders = HttpFields.build()
|
||||||
|
.put(HttpHeader.USER_AGENT, "Jetty HTTP2Client {version}");
|
||||||
|
MetaData.Request request = new MetaData.Request("GET", HttpURI.from("http://localhost:8080/path"), HttpVersion.HTTP_2, requestHeaders);
|
||||||
|
HeadersFrame headersFrame = new HeadersFrame(request, null, true);
|
||||||
|
|
||||||
|
// tag::reset[]
|
||||||
|
// Open a Stream by sending the HEADERS frame.
|
||||||
|
CompletableFuture<Stream> streamCF = session.newStream(headersFrame, new Stream.Listener.Adapter()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onReset(Stream stream, ResetFrame frame)
|
||||||
|
{
|
||||||
|
// The server reset this stream.
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Stream stream = streamCF.get();
|
||||||
|
|
||||||
|
// Reset this stream (for example, the user closed the application).
|
||||||
|
stream.reset(new ResetFrame(stream.getId(), ErrorCode.CANCEL_STREAM_ERROR.code), Callback.NOOP);
|
||||||
|
// end::reset[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void push() throws Exception
|
||||||
|
{
|
||||||
|
HTTP2Client http2Client = new HTTP2Client();
|
||||||
|
http2Client.start();
|
||||||
|
SocketAddress serverAddress = new InetSocketAddress("localhost", 8080);
|
||||||
|
CompletableFuture<Session> sessionCF = http2Client.connect(serverAddress, new Session.Listener.Adapter());
|
||||||
|
Session session = sessionCF.get();
|
||||||
|
|
||||||
|
HttpFields requestHeaders = HttpFields.build()
|
||||||
|
.put(HttpHeader.USER_AGENT, "Jetty HTTP2Client {version}");
|
||||||
|
MetaData.Request request = new MetaData.Request("GET", HttpURI.from("http://localhost:8080/path"), HttpVersion.HTTP_2, requestHeaders);
|
||||||
|
HeadersFrame headersFrame = new HeadersFrame(request, null, true);
|
||||||
|
|
||||||
|
// tag::push[]
|
||||||
|
// Open a Stream by sending the HEADERS frame.
|
||||||
|
CompletableFuture<Stream> streamCF = session.newStream(headersFrame, new Stream.Listener.Adapter()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public Stream.Listener onPush(Stream pushedStream, PushPromiseFrame frame)
|
||||||
|
{
|
||||||
|
// The "request" the client would make for the pushed resource.
|
||||||
|
MetaData.Request pushedRequest = frame.getMetaData();
|
||||||
|
// The pushed "request" URI.
|
||||||
|
HttpURI pushedURI = pushedRequest.getURI();
|
||||||
|
// The pushed "request" headers.
|
||||||
|
HttpFields pushedRequestHeaders = pushedRequest.getFields();
|
||||||
|
|
||||||
|
// If needed, retrieve the primary stream that triggered the push.
|
||||||
|
Stream primaryStream = pushedStream.getSession().getStream(frame.getStreamId());
|
||||||
|
|
||||||
|
// Return a Stream.Listener to listen for the pushed "response" events.
|
||||||
|
return new Stream.Listener.Adapter()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onHeaders(Stream stream, HeadersFrame frame)
|
||||||
|
{
|
||||||
|
// Handle the pushed stream "response".
|
||||||
|
|
||||||
|
MetaData metaData = frame.getMetaData();
|
||||||
|
if (metaData.isResponse())
|
||||||
|
{
|
||||||
|
// The pushed "response" headers.
|
||||||
|
HttpFields pushedResponseHeaders = metaData.getFields();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onData(Stream stream, DataFrame frame, Callback callback)
|
||||||
|
{
|
||||||
|
// Handle the pushed stream "response" content.
|
||||||
|
|
||||||
|
// The pushed stream "response" content bytes.
|
||||||
|
ByteBuffer buffer = frame.getData();
|
||||||
|
// Consume the buffer and complete the callback.
|
||||||
|
callback.succeeded();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// end::push[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void pushReset() throws Exception
|
||||||
|
{
|
||||||
|
HTTP2Client http2Client = new HTTP2Client();
|
||||||
|
http2Client.start();
|
||||||
|
SocketAddress serverAddress = new InetSocketAddress("localhost", 8080);
|
||||||
|
CompletableFuture<Session> sessionCF = http2Client.connect(serverAddress, new Session.Listener.Adapter());
|
||||||
|
Session session = sessionCF.get();
|
||||||
|
|
||||||
|
HttpFields requestHeaders = HttpFields.build()
|
||||||
|
.put(HttpHeader.USER_AGENT, "Jetty HTTP2Client {version}");
|
||||||
|
MetaData.Request request = new MetaData.Request("GET", HttpURI.from("http://localhost:8080/path"), HttpVersion.HTTP_2, requestHeaders);
|
||||||
|
HeadersFrame headersFrame = new HeadersFrame(request, null, true);
|
||||||
|
|
||||||
|
// tag::pushReset[]
|
||||||
|
// Open a Stream by sending the HEADERS frame.
|
||||||
|
CompletableFuture<Stream> streamCF = session.newStream(headersFrame, new Stream.Listener.Adapter()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public Stream.Listener onPush(Stream pushedStream, PushPromiseFrame frame)
|
||||||
|
{
|
||||||
|
// Reset the pushed stream to tell the server we are not interested.
|
||||||
|
pushedStream.reset(new ResetFrame(pushedStream.getId(), ErrorCode.CANCEL_STREAM_ERROR.code), Callback.NOOP);
|
||||||
|
|
||||||
|
// Not interested in listening to pushed response events.
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// end::pushReset[]
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,356 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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 org.eclipse.jetty.docs.programming.client.http3;
|
||||||
|
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.SocketAddress;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.channels.ClosedChannelException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.http.HttpFields;
|
||||||
|
import org.eclipse.jetty.http.HttpHeader;
|
||||||
|
import org.eclipse.jetty.http.HttpURI;
|
||||||
|
import org.eclipse.jetty.http.HttpVersion;
|
||||||
|
import org.eclipse.jetty.http.MetaData;
|
||||||
|
import org.eclipse.jetty.http3.api.Session;
|
||||||
|
import org.eclipse.jetty.http3.api.Stream;
|
||||||
|
import org.eclipse.jetty.http3.client.HTTP3Client;
|
||||||
|
import org.eclipse.jetty.http3.frames.DataFrame;
|
||||||
|
import org.eclipse.jetty.http3.frames.HeadersFrame;
|
||||||
|
import org.eclipse.jetty.http3.internal.HTTP3ErrorCode;
|
||||||
|
|
||||||
|
import static java.lang.System.Logger.Level.INFO;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class HTTP3ClientDocs
|
||||||
|
{
|
||||||
|
public void start() throws Exception
|
||||||
|
{
|
||||||
|
// tag::start[]
|
||||||
|
// Instantiate HTTP3Client.
|
||||||
|
HTTP3Client http3Client = new HTTP3Client();
|
||||||
|
|
||||||
|
// Configure HTTP3Client, for example:
|
||||||
|
http3Client.getHTTP3Configuration().setStreamIdleTimeout(15000);
|
||||||
|
|
||||||
|
// Start HTTP3Client.
|
||||||
|
http3Client.start();
|
||||||
|
// end::start[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop() throws Exception
|
||||||
|
{
|
||||||
|
HTTP3Client http3Client = new HTTP3Client();
|
||||||
|
http3Client.start();
|
||||||
|
// tag::stop[]
|
||||||
|
// Stop HTTP3Client.
|
||||||
|
http3Client.stop();
|
||||||
|
// end::stop[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void connect() throws Exception
|
||||||
|
{
|
||||||
|
HTTP3Client http3Client = new HTTP3Client();
|
||||||
|
http3Client.start();
|
||||||
|
// tag::connect[]
|
||||||
|
// Address of the server's port.
|
||||||
|
SocketAddress serverAddress = new InetSocketAddress("localhost", 8444);
|
||||||
|
|
||||||
|
// Connect to the server, the CompletableFuture will be
|
||||||
|
// notified when the connection is succeeded (or failed).
|
||||||
|
CompletableFuture<Session.Client> sessionCF = http3Client.connect(serverAddress, new Session.Client.Listener() {});
|
||||||
|
|
||||||
|
// Block to obtain the Session.
|
||||||
|
// Alternatively you can use the CompletableFuture APIs to avoid blocking.
|
||||||
|
Session session = sessionCF.get();
|
||||||
|
// end::connect[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void configure() throws Exception
|
||||||
|
{
|
||||||
|
HTTP3Client http3Client = new HTTP3Client();
|
||||||
|
http3Client.start();
|
||||||
|
|
||||||
|
// tag::configure[]
|
||||||
|
SocketAddress serverAddress = new InetSocketAddress("localhost", 8444);
|
||||||
|
http3Client.connect(serverAddress, new Session.Client.Listener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public Map<Long, Long> onPreface(Session session)
|
||||||
|
{
|
||||||
|
Map<Long, Long> configuration = new HashMap<>();
|
||||||
|
|
||||||
|
// Add here configuration settings.
|
||||||
|
|
||||||
|
return configuration;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// end::configure[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void newStream() throws Exception
|
||||||
|
{
|
||||||
|
HTTP3Client http3Client = new HTTP3Client();
|
||||||
|
http3Client.start();
|
||||||
|
// tag::newStream[]
|
||||||
|
SocketAddress serverAddress = new InetSocketAddress("localhost", 8444);
|
||||||
|
CompletableFuture<Session.Client> sessionCF = http3Client.connect(serverAddress, new Session.Client.Listener() {});
|
||||||
|
Session.Client session = sessionCF.get();
|
||||||
|
|
||||||
|
// Configure the request headers.
|
||||||
|
HttpFields requestHeaders = HttpFields.build()
|
||||||
|
.put(HttpHeader.USER_AGENT, "Jetty HTTP3Client {version}");
|
||||||
|
|
||||||
|
// The request metadata with method, URI and headers.
|
||||||
|
MetaData.Request request = new MetaData.Request("GET", HttpURI.from("http://localhost:8444/path"), HttpVersion.HTTP_3, requestHeaders);
|
||||||
|
|
||||||
|
// The HTTP/3 HEADERS frame, with endStream=true
|
||||||
|
// to signal that this request has no content.
|
||||||
|
HeadersFrame headersFrame = new HeadersFrame(request, true);
|
||||||
|
|
||||||
|
// Open a Stream by sending the HEADERS frame.
|
||||||
|
session.newRequest(headersFrame, new Stream.Client.Listener() {});
|
||||||
|
// end::newStream[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void newStreamWithData() throws Exception
|
||||||
|
{
|
||||||
|
HTTP3Client http3Client = new HTTP3Client();
|
||||||
|
http3Client.start();
|
||||||
|
// tag::newStreamWithData[]
|
||||||
|
SocketAddress serverAddress = new InetSocketAddress("localhost", 8444);
|
||||||
|
CompletableFuture<Session.Client> sessionCF = http3Client.connect(serverAddress, new Session.Client.Listener() {});
|
||||||
|
Session.Client session = sessionCF.get();
|
||||||
|
|
||||||
|
// Configure the request headers.
|
||||||
|
HttpFields requestHeaders = HttpFields.build()
|
||||||
|
.put(HttpHeader.CONTENT_TYPE, "application/json");
|
||||||
|
|
||||||
|
// The request metadata with method, URI and headers.
|
||||||
|
MetaData.Request request = new MetaData.Request("POST", HttpURI.from("http://localhost:8444/path"), HttpVersion.HTTP_3, requestHeaders);
|
||||||
|
|
||||||
|
// The HTTP/3 HEADERS frame, with endStream=false to
|
||||||
|
// signal that there will be more frames in this stream.
|
||||||
|
HeadersFrame headersFrame = new HeadersFrame(request, false);
|
||||||
|
|
||||||
|
// Open a Stream by sending the HEADERS frame.
|
||||||
|
CompletableFuture<Stream> streamCF = session.newRequest(headersFrame, new Stream.Client.Listener() {});
|
||||||
|
|
||||||
|
// Block to obtain the Stream.
|
||||||
|
// Alternatively you can use the CompletableFuture APIs to avoid blocking.
|
||||||
|
Stream stream = streamCF.get();
|
||||||
|
|
||||||
|
// The request content, in two chunks.
|
||||||
|
String content1 = "{\"greet\": \"hello world\"}";
|
||||||
|
ByteBuffer buffer1 = StandardCharsets.UTF_8.encode(content1);
|
||||||
|
String content2 = "{\"user\": \"jetty\"}";
|
||||||
|
ByteBuffer buffer2 = StandardCharsets.UTF_8.encode(content2);
|
||||||
|
|
||||||
|
// Send the first DATA frame on the stream, with endStream=false
|
||||||
|
// to signal that there are more frames in this stream.
|
||||||
|
CompletableFuture<Stream> dataCF1 = stream.data(new DataFrame(buffer1, false));
|
||||||
|
|
||||||
|
// Only when the first chunk has been sent we can send the second,
|
||||||
|
// with endStream=true to signal that there are no more frames.
|
||||||
|
dataCF1.thenCompose(s -> s.data(new DataFrame(buffer2, true)));
|
||||||
|
// end::newStreamWithData[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void responseListener() throws Exception
|
||||||
|
{
|
||||||
|
HTTP3Client http3Client = new HTTP3Client();
|
||||||
|
http3Client.start();
|
||||||
|
SocketAddress serverAddress = new InetSocketAddress("localhost", 8444);
|
||||||
|
CompletableFuture<Session.Client> sessionCF = http3Client.connect(serverAddress, new Session.Client.Listener() {});
|
||||||
|
Session.Client session = sessionCF.get();
|
||||||
|
|
||||||
|
HttpFields requestHeaders = HttpFields.build()
|
||||||
|
.put(HttpHeader.USER_AGENT, "Jetty HTTP3Client {version}");
|
||||||
|
MetaData.Request request = new MetaData.Request("GET", HttpURI.from("http://localhost:8444/path"), HttpVersion.HTTP_3, requestHeaders);
|
||||||
|
HeadersFrame headersFrame = new HeadersFrame(request, true);
|
||||||
|
|
||||||
|
// tag::responseListener[]
|
||||||
|
// Open a Stream by sending the HEADERS frame.
|
||||||
|
session.newRequest(headersFrame, new Stream.Client.Listener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onResponse(Stream.Client stream, HeadersFrame frame)
|
||||||
|
{
|
||||||
|
MetaData metaData = frame.getMetaData();
|
||||||
|
MetaData.Response response = (MetaData.Response)metaData;
|
||||||
|
System.getLogger("http3").log(INFO, "Received response {0}", response);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDataAvailable(Stream.Client stream)
|
||||||
|
{
|
||||||
|
// Read a chunk of the content.
|
||||||
|
Stream.Data data = stream.readData();
|
||||||
|
if (data == null)
|
||||||
|
{
|
||||||
|
// No data available now, demand to be called back.
|
||||||
|
stream.demand();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Process the content.
|
||||||
|
process(data.getByteBuffer());
|
||||||
|
|
||||||
|
// Notify the implementation that the content has been consumed.
|
||||||
|
data.complete();
|
||||||
|
|
||||||
|
if (!data.isLast())
|
||||||
|
{
|
||||||
|
// Demand to be called back.
|
||||||
|
stream.demand();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// end::responseListener[]
|
||||||
|
}
|
||||||
|
|
||||||
|
private void process(ByteBuffer byteBuffer)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public void reset() throws Exception
|
||||||
|
{
|
||||||
|
HTTP3Client http3Client = new HTTP3Client();
|
||||||
|
http3Client.start();
|
||||||
|
SocketAddress serverAddress = new InetSocketAddress("localhost", 8444);
|
||||||
|
CompletableFuture<Session.Client> sessionCF = http3Client.connect(serverAddress, new Session.Client.Listener() {});
|
||||||
|
Session.Client session = sessionCF.get();
|
||||||
|
|
||||||
|
HttpFields requestHeaders = HttpFields.build()
|
||||||
|
.put(HttpHeader.USER_AGENT, "Jetty HTTP3Client {version}");
|
||||||
|
MetaData.Request request = new MetaData.Request("GET", HttpURI.from("http://localhost:8080/path"), HttpVersion.HTTP_2, requestHeaders);
|
||||||
|
HeadersFrame headersFrame = new HeadersFrame(request, true);
|
||||||
|
|
||||||
|
// tag::reset[]
|
||||||
|
// Open a Stream by sending the HEADERS frame.
|
||||||
|
CompletableFuture<Stream> streamCF = session.newRequest(headersFrame, new Stream.Client.Listener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onFailure(Stream.Client stream, long error, Throwable failure)
|
||||||
|
{
|
||||||
|
// The server reset this stream.
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Stream stream = streamCF.get();
|
||||||
|
|
||||||
|
// Reset this stream (for example, the user closed the application).
|
||||||
|
stream.reset(HTTP3ErrorCode.REQUEST_CANCELLED_ERROR.code(), new ClosedChannelException());
|
||||||
|
// end::reset[]
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: push not yet implemented in HTTP/3
|
||||||
|
/*
|
||||||
|
public void push() throws Exception
|
||||||
|
{
|
||||||
|
HTTP3Client http3Client = new HTTP3Client();
|
||||||
|
http3Client.start();
|
||||||
|
SocketAddress serverAddress = new InetSocketAddress("localhost", 8444);
|
||||||
|
CompletableFuture<Session> sessionCF = http3Client.connect(serverAddress, new Session.Listener.Adapter());
|
||||||
|
Session session = sessionCF.get();
|
||||||
|
|
||||||
|
HttpFields requestHeaders = HttpFields.build()
|
||||||
|
.put(HttpHeader.USER_AGENT, "Jetty HTTP3Client {version}");
|
||||||
|
MetaData.Request request = new MetaData.Request("GET", HttpURI.from("http://localhost:8080/path"), HttpVersion.HTTP_2, requestHeaders);
|
||||||
|
HeadersFrame headersFrame = new HeadersFrame(request, null, true);
|
||||||
|
|
||||||
|
// tag::push[]
|
||||||
|
// Open a Stream by sending the HEADERS frame.
|
||||||
|
CompletableFuture<Stream> streamCF = session.newStream(headersFrame, new Stream.Listener.Adapter()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public Stream.Listener onPush(Stream pushedStream, PushPromiseFrame frame)
|
||||||
|
{
|
||||||
|
// The "request" the client would make for the pushed resource.
|
||||||
|
MetaData.Request pushedRequest = frame.getMetaData();
|
||||||
|
// The pushed "request" URI.
|
||||||
|
HttpURI pushedURI = pushedRequest.getURI();
|
||||||
|
// The pushed "request" headers.
|
||||||
|
HttpFields pushedRequestHeaders = pushedRequest.getFields();
|
||||||
|
|
||||||
|
// If needed, retrieve the primary stream that triggered the push.
|
||||||
|
Stream primaryStream = pushedStream.getSession().getStream(frame.getStreamId());
|
||||||
|
|
||||||
|
// Return a Stream.Listener to listen for the pushed "response" events.
|
||||||
|
return new Adapter()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onHeaders(Stream stream, HeadersFrame frame)
|
||||||
|
{
|
||||||
|
// Handle the pushed stream "response".
|
||||||
|
|
||||||
|
MetaData metaData = frame.getMetaData();
|
||||||
|
if (metaData.isResponse())
|
||||||
|
{
|
||||||
|
// The pushed "response" headers.
|
||||||
|
HttpFields pushedResponseHeaders = metaData.getFields();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onData(Stream stream, DataFrame frame, Callback callback)
|
||||||
|
{
|
||||||
|
// Handle the pushed stream "response" content.
|
||||||
|
|
||||||
|
// The pushed stream "response" content bytes.
|
||||||
|
ByteBuffer buffer = frame.getData();
|
||||||
|
// Consume the buffer and complete the callback.
|
||||||
|
callback.succeeded();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// end::push[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void pushReset() throws Exception
|
||||||
|
{
|
||||||
|
HTTP3Client http3Client = new HTTP3Client();
|
||||||
|
http3Client.start();
|
||||||
|
SocketAddress serverAddress = new InetSocketAddress("localhost", 8444);
|
||||||
|
CompletableFuture<Session> sessionCF = http3Client.connect(serverAddress, new Session.Listener.Adapter());
|
||||||
|
Session session = sessionCF.get();
|
||||||
|
|
||||||
|
HttpFields requestHeaders = HttpFields.build()
|
||||||
|
.put(HttpHeader.USER_AGENT, "Jetty HTTP3Client {version}");
|
||||||
|
MetaData.Request request = new MetaData.Request("GET", HttpURI.from("http://localhost:8080/path"), HttpVersion.HTTP_2, requestHeaders);
|
||||||
|
HeadersFrame headersFrame = new HeadersFrame(request, null, true);
|
||||||
|
|
||||||
|
// tag::pushReset[]
|
||||||
|
// Open a Stream by sending the HEADERS frame.
|
||||||
|
CompletableFuture<Stream> streamCF = session.newStream(headersFrame, new Stream.Listener.Adapter()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public Stream.Listener onPush(Stream pushedStream, PushPromiseFrame frame)
|
||||||
|
{
|
||||||
|
// Reset the pushed stream to tell the server we are not interested.
|
||||||
|
pushedStream.reset(new ResetFrame(pushedStream.getId(), ErrorCode.CANCEL_STREAM_ERROR.code), Callback.NOOP);
|
||||||
|
|
||||||
|
// Not interested in listening to pushed response events.
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// end::pushReset[]
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
|
@ -0,0 +1,193 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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 org.eclipse.jetty.docs.programming.client.websocket;
|
||||||
|
|
||||||
|
import java.net.HttpCookie;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.client.HttpClient;
|
||||||
|
import org.eclipse.jetty.client.HttpProxy;
|
||||||
|
import org.eclipse.jetty.client.HttpRequest;
|
||||||
|
import org.eclipse.jetty.client.HttpResponse;
|
||||||
|
import org.eclipse.jetty.client.dynamic.HttpClientTransportDynamic;
|
||||||
|
import org.eclipse.jetty.http2.client.HTTP2Client;
|
||||||
|
import org.eclipse.jetty.http2.client.http.ClientConnectionFactoryOverHTTP2;
|
||||||
|
import org.eclipse.jetty.http2.client.http.HttpClientTransportOverHTTP2;
|
||||||
|
import org.eclipse.jetty.util.component.LifeCycle;
|
||||||
|
import org.eclipse.jetty.websocket.api.Session;
|
||||||
|
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
|
||||||
|
import org.eclipse.jetty.websocket.client.JettyUpgradeListener;
|
||||||
|
import org.eclipse.jetty.websocket.client.WebSocketClient;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class WebSocketClientDocs
|
||||||
|
{
|
||||||
|
public void start() throws Exception
|
||||||
|
{
|
||||||
|
// tag::start[]
|
||||||
|
// Instantiate WebSocketClient.
|
||||||
|
WebSocketClient webSocketClient = new WebSocketClient();
|
||||||
|
|
||||||
|
// Configure WebSocketClient, for example:
|
||||||
|
webSocketClient.setMaxTextMessageSize(8 * 1024);
|
||||||
|
|
||||||
|
// Start WebSocketClient.
|
||||||
|
webSocketClient.start();
|
||||||
|
// end::start[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void startWithHttpClient() throws Exception
|
||||||
|
{
|
||||||
|
// tag::startWithHttpClient[]
|
||||||
|
// Instantiate and configure HttpClient.
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
// For example, configure a proxy.
|
||||||
|
httpClient.getProxyConfiguration().addProxy(new HttpProxy("localhost", 8888));
|
||||||
|
|
||||||
|
// Instantiate WebSocketClient, passing HttpClient to the constructor.
|
||||||
|
WebSocketClient webSocketClient = new WebSocketClient(httpClient);
|
||||||
|
// Configure WebSocketClient, for example:
|
||||||
|
webSocketClient.setMaxTextMessageSize(8 * 1024);
|
||||||
|
|
||||||
|
// Start WebSocketClient; this implicitly starts also HttpClient.
|
||||||
|
webSocketClient.start();
|
||||||
|
// end::startWithHttpClient[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop() throws Exception
|
||||||
|
{
|
||||||
|
WebSocketClient webSocketClient = new WebSocketClient();
|
||||||
|
webSocketClient.start();
|
||||||
|
// tag::stop[]
|
||||||
|
// Stop WebSocketClient.
|
||||||
|
// Use LifeCycle.stop(...) to rethrow checked exceptions as unchecked.
|
||||||
|
new Thread(() -> LifeCycle.stop(webSocketClient)).start();
|
||||||
|
// end::stop[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void connectHTTP11() throws Exception
|
||||||
|
{
|
||||||
|
// tag::connectHTTP11[]
|
||||||
|
// Use a standard, HTTP/1.1, HttpClient.
|
||||||
|
HttpClient httpClient = new HttpClient();
|
||||||
|
|
||||||
|
// Create and start WebSocketClient.
|
||||||
|
WebSocketClient webSocketClient = new WebSocketClient(httpClient);
|
||||||
|
webSocketClient.start();
|
||||||
|
|
||||||
|
// The client-side WebSocket EndPoint that
|
||||||
|
// receives WebSocket messages from the server.
|
||||||
|
ClientEndPoint clientEndPoint = new ClientEndPoint();
|
||||||
|
// The server URI to connect to.
|
||||||
|
URI serverURI = URI.create("ws://domain.com/path");
|
||||||
|
|
||||||
|
// Connect the client EndPoint to the server.
|
||||||
|
CompletableFuture<Session> clientSessionPromise = webSocketClient.connect(clientEndPoint, serverURI);
|
||||||
|
// end::connectHTTP11[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void connectHTTP2() throws Exception
|
||||||
|
{
|
||||||
|
// tag::connectHTTP2[]
|
||||||
|
// Use the HTTP/2 transport for HttpClient.
|
||||||
|
HTTP2Client http2Client = new HTTP2Client();
|
||||||
|
HttpClient httpClient = new HttpClient(new HttpClientTransportOverHTTP2(http2Client));
|
||||||
|
|
||||||
|
// Create and start WebSocketClient.
|
||||||
|
WebSocketClient webSocketClient = new WebSocketClient(httpClient);
|
||||||
|
webSocketClient.start();
|
||||||
|
|
||||||
|
// The client-side WebSocket EndPoint that
|
||||||
|
// receives WebSocket messages from the server.
|
||||||
|
ClientEndPoint clientEndPoint = new ClientEndPoint();
|
||||||
|
// The server URI to connect to.
|
||||||
|
URI serverURI = URI.create("wss://domain.com/path");
|
||||||
|
|
||||||
|
// Connect the client EndPoint to the server.
|
||||||
|
CompletableFuture<Session> clientSessionPromise = webSocketClient.connect(clientEndPoint, serverURI);
|
||||||
|
// end::connectHTTP2[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void connectHTTP2Dynamic() throws Exception
|
||||||
|
{
|
||||||
|
// tag::connectHTTP2Dynamic[]
|
||||||
|
// Use the dynamic HTTP/2 transport for HttpClient.
|
||||||
|
HTTP2Client http2Client = new HTTP2Client();
|
||||||
|
HttpClient httpClient = new HttpClient(new HttpClientTransportDynamic(new ClientConnectionFactoryOverHTTP2.HTTP2(http2Client)));
|
||||||
|
|
||||||
|
// Create and start WebSocketClient.
|
||||||
|
WebSocketClient webSocketClient = new WebSocketClient(httpClient);
|
||||||
|
webSocketClient.start();
|
||||||
|
|
||||||
|
ClientEndPoint clientEndPoint = new ClientEndPoint();
|
||||||
|
URI serverURI = URI.create("wss://domain.com/path");
|
||||||
|
|
||||||
|
// Connect the client EndPoint to the server.
|
||||||
|
CompletableFuture<Session> clientSessionPromise = webSocketClient.connect(clientEndPoint, serverURI);
|
||||||
|
// end::connectHTTP2Dynamic[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void customHTTPRequest() throws Exception
|
||||||
|
{
|
||||||
|
WebSocketClient webSocketClient = new WebSocketClient(new HttpClient());
|
||||||
|
webSocketClient.start();
|
||||||
|
|
||||||
|
// tag::customHTTPRequest[]
|
||||||
|
ClientEndPoint clientEndPoint = new ClientEndPoint();
|
||||||
|
URI serverURI = URI.create("ws://domain.com/path");
|
||||||
|
|
||||||
|
// Create a custom HTTP request.
|
||||||
|
ClientUpgradeRequest customRequest = new ClientUpgradeRequest();
|
||||||
|
// Specify a cookie.
|
||||||
|
customRequest.getCookies().add(new HttpCookie("name", "value"));
|
||||||
|
// Specify a custom header.
|
||||||
|
customRequest.setHeader("X-Token", "0123456789ABCDEF");
|
||||||
|
// Specify a custom sub-protocol.
|
||||||
|
customRequest.setSubProtocols("chat");
|
||||||
|
|
||||||
|
// Connect the client EndPoint to the server with a custom HTTP request.
|
||||||
|
CompletableFuture<Session> clientSessionPromise = webSocketClient.connect(clientEndPoint, serverURI, customRequest);
|
||||||
|
// end::customHTTPRequest[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void inspectHTTPResponse() throws Exception
|
||||||
|
{
|
||||||
|
WebSocketClient webSocketClient = new WebSocketClient(new HttpClient());
|
||||||
|
webSocketClient.start();
|
||||||
|
|
||||||
|
// tag::inspectHTTPResponse[]
|
||||||
|
ClientEndPoint clientEndPoint = new ClientEndPoint();
|
||||||
|
URI serverURI = URI.create("ws://domain.com/path");
|
||||||
|
|
||||||
|
// The listener to inspect the HTTP response.
|
||||||
|
JettyUpgradeListener listener = new JettyUpgradeListener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onHandshakeResponse(HttpRequest request, HttpResponse response)
|
||||||
|
{
|
||||||
|
// Inspect the HTTP response here.
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Connect the client EndPoint to the server with a custom HTTP request.
|
||||||
|
CompletableFuture<Session> clientSessionPromise = webSocketClient.connect(clientEndPoint, serverURI, null, listener);
|
||||||
|
// end::inspectHTTPResponse[]
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("InnerClassMayBeStatic")
|
||||||
|
public class ClientEndPoint
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,335 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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 org.eclipse.jetty.docs.programming.server;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.http.CookieCompliance;
|
||||||
|
import org.eclipse.jetty.http.HttpCompliance;
|
||||||
|
import org.eclipse.jetty.http.UriCompliance;
|
||||||
|
import org.eclipse.jetty.io.AbstractConnection;
|
||||||
|
import org.eclipse.jetty.io.Connection;
|
||||||
|
import org.eclipse.jetty.io.EndPoint;
|
||||||
|
import org.eclipse.jetty.server.AbstractConnectionFactory;
|
||||||
|
import org.eclipse.jetty.server.Connector;
|
||||||
|
import org.eclipse.jetty.server.DetectorConnectionFactory;
|
||||||
|
import org.eclipse.jetty.server.HttpConfiguration;
|
||||||
|
import org.eclipse.jetty.server.HttpConnectionFactory;
|
||||||
|
import org.eclipse.jetty.server.Server;
|
||||||
|
import org.eclipse.jetty.server.ServerConnector;
|
||||||
|
import org.eclipse.jetty.server.SslConnectionFactory;
|
||||||
|
import org.eclipse.jetty.unixdomain.server.UnixDomainServerConnector;
|
||||||
|
import org.eclipse.jetty.util.BufferUtil;
|
||||||
|
import org.eclipse.jetty.util.Callback;
|
||||||
|
import org.eclipse.jetty.util.IteratingCallback;
|
||||||
|
import org.eclipse.jetty.util.ajax.AsyncJSON;
|
||||||
|
import org.eclipse.jetty.util.ssl.SslContextFactory;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class ServerDocs
|
||||||
|
{
|
||||||
|
public void http() throws Exception
|
||||||
|
{
|
||||||
|
// tag::http[]
|
||||||
|
// Create the HTTP/1.1 ConnectionFactory.
|
||||||
|
HttpConnectionFactory http = new HttpConnectionFactory();
|
||||||
|
|
||||||
|
Server server = new Server();
|
||||||
|
|
||||||
|
// Create the connector with the ConnectionFactory.
|
||||||
|
ServerConnector connector = new ServerConnector(server, http);
|
||||||
|
connector.setPort(8080);
|
||||||
|
|
||||||
|
server.addConnector(connector);
|
||||||
|
server.start();
|
||||||
|
// end::http[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void httpUnix() throws Exception
|
||||||
|
{
|
||||||
|
// tag::httpUnix[]
|
||||||
|
// Create the HTTP/1.1 ConnectionFactory.
|
||||||
|
HttpConnectionFactory http = new HttpConnectionFactory();
|
||||||
|
|
||||||
|
Server server = new Server();
|
||||||
|
|
||||||
|
// Create the connector with the ConnectionFactory.
|
||||||
|
UnixDomainServerConnector connector = new UnixDomainServerConnector(server, http);
|
||||||
|
connector.setUnixDomainPath(Path.of("/tmp/jetty.sock"));
|
||||||
|
|
||||||
|
server.addConnector(connector);
|
||||||
|
server.start();
|
||||||
|
// end::httpUnix[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void tlsHttp() throws Exception
|
||||||
|
{
|
||||||
|
// tag::tlsHttp[]
|
||||||
|
// Create the HTTP/1.1 ConnectionFactory.
|
||||||
|
HttpConnectionFactory http = new HttpConnectionFactory();
|
||||||
|
|
||||||
|
// Create and configure the TLS context factory.
|
||||||
|
SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
|
||||||
|
sslContextFactory.setKeyStorePath("/path/to/keystore.p12");
|
||||||
|
sslContextFactory.setKeyStorePassword("secret");
|
||||||
|
|
||||||
|
// Create the TLS ConnectionFactory,
|
||||||
|
// setting HTTP/1.1 as the wrapped protocol.
|
||||||
|
SslConnectionFactory tls = new SslConnectionFactory(sslContextFactory, http.getProtocol());
|
||||||
|
|
||||||
|
Server server = new Server();
|
||||||
|
|
||||||
|
// Create the connector with both ConnectionFactories.
|
||||||
|
ServerConnector connector = new ServerConnector(server, tls, http);
|
||||||
|
connector.setPort(8443);
|
||||||
|
|
||||||
|
server.addConnector(connector);
|
||||||
|
server.start();
|
||||||
|
// end::tlsHttp[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void detector() throws Exception
|
||||||
|
{
|
||||||
|
// tag::detector[]
|
||||||
|
// Create the HTTP/1.1 ConnectionFactory.
|
||||||
|
HttpConnectionFactory http = new HttpConnectionFactory();
|
||||||
|
|
||||||
|
// Create and configure the TLS context factory.
|
||||||
|
SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
|
||||||
|
sslContextFactory.setKeyStorePath("/path/to/keystore.p12");
|
||||||
|
sslContextFactory.setKeyStorePassword("secret");
|
||||||
|
|
||||||
|
// Create the TLS ConnectionFactory,
|
||||||
|
// setting HTTP/1.1 as the wrapped protocol.
|
||||||
|
SslConnectionFactory tls = new SslConnectionFactory(sslContextFactory, http.getProtocol());
|
||||||
|
|
||||||
|
Server server = new Server();
|
||||||
|
|
||||||
|
// Create the detector ConnectionFactory to
|
||||||
|
// detect whether the initial bytes are TLS.
|
||||||
|
DetectorConnectionFactory tlsDetector = new DetectorConnectionFactory(tls); // <1>
|
||||||
|
|
||||||
|
// Create the connector with both ConnectionFactories.
|
||||||
|
ServerConnector connector = new ServerConnector(server, tlsDetector, http); // <2>
|
||||||
|
connector.setPort(8181);
|
||||||
|
|
||||||
|
server.addConnector(connector);
|
||||||
|
server.start();
|
||||||
|
// end::detector[]
|
||||||
|
}
|
||||||
|
|
||||||
|
// tag::jsonHttpConnectionFactory[]
|
||||||
|
public class JSONHTTPConnectionFactory extends AbstractConnectionFactory
|
||||||
|
{
|
||||||
|
public JSONHTTPConnectionFactory()
|
||||||
|
{
|
||||||
|
super("JSONHTTP");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Connection newConnection(Connector connector, EndPoint endPoint)
|
||||||
|
{
|
||||||
|
JSONHTTPConnection connection = new JSONHTTPConnection(endPoint, connector.getExecutor());
|
||||||
|
// Call configure() to apply configurations common to all connections.
|
||||||
|
return configure(connection, connector, endPoint);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// end::jsonHttpConnectionFactory[]
|
||||||
|
|
||||||
|
@SuppressWarnings("InnerClassMayBeStatic")
|
||||||
|
// tag::jsonHttpConnection[]
|
||||||
|
public class JSONHTTPConnection extends AbstractConnection
|
||||||
|
{
|
||||||
|
// The asynchronous JSON parser.
|
||||||
|
private final AsyncJSON parser = new AsyncJSON.Factory().newAsyncJSON();
|
||||||
|
private final IteratingCallback callback = new JSONHTTPIteratingCallback();
|
||||||
|
|
||||||
|
public JSONHTTPConnection(EndPoint endPoint, Executor executor)
|
||||||
|
{
|
||||||
|
super(endPoint, executor);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onOpen()
|
||||||
|
{
|
||||||
|
super.onOpen();
|
||||||
|
|
||||||
|
// Declare interest in being called back when
|
||||||
|
// there are bytes to read from the network.
|
||||||
|
fillInterested();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFillable()
|
||||||
|
{
|
||||||
|
callback.iterate();
|
||||||
|
}
|
||||||
|
|
||||||
|
private class JSONHTTPIteratingCallback extends IteratingCallback
|
||||||
|
{
|
||||||
|
private ByteBuffer buffer;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Action process() throws Throwable
|
||||||
|
{
|
||||||
|
if (buffer == null)
|
||||||
|
buffer = BufferUtil.allocate(getInputBufferSize(), true);
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
int filled = getEndPoint().fill(buffer);
|
||||||
|
if (filled > 0)
|
||||||
|
{
|
||||||
|
boolean parsed = parser.parse(buffer);
|
||||||
|
if (parsed)
|
||||||
|
{
|
||||||
|
Map<String, Object> request = parser.complete();
|
||||||
|
|
||||||
|
// Allow applications to process the request.
|
||||||
|
invokeApplication(request, this);
|
||||||
|
|
||||||
|
// Signal that the iteration should resume when
|
||||||
|
// the application completed the request processing.
|
||||||
|
return Action.SCHEDULED;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Did not receive enough JSON bytes,
|
||||||
|
// loop around to try to read more.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (filled == 0)
|
||||||
|
{
|
||||||
|
// We don't need the buffer anymore, so
|
||||||
|
// don't keep it around while we are idle.
|
||||||
|
buffer = null;
|
||||||
|
|
||||||
|
// No more bytes to read, declare
|
||||||
|
// again interest for fill events.
|
||||||
|
fillInterested();
|
||||||
|
|
||||||
|
// Signal that the iteration is now IDLE.
|
||||||
|
return Action.IDLE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// The other peer closed the connection,
|
||||||
|
// the iteration completed successfully.
|
||||||
|
return Action.SUCCEEDED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCompleteSuccess()
|
||||||
|
{
|
||||||
|
getEndPoint().close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCompleteFailure(Throwable cause)
|
||||||
|
{
|
||||||
|
getEndPoint().close(cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// end::jsonHttpConnection[]
|
||||||
|
|
||||||
|
private void invokeApplication(Map<String, Object> request, Callback callback)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
// tag::jsonHttpAPI[]
|
||||||
|
class JSONHTTPRequest
|
||||||
|
{
|
||||||
|
// Request APIs
|
||||||
|
}
|
||||||
|
|
||||||
|
class JSONHTTPResponse
|
||||||
|
{
|
||||||
|
// Response APIs
|
||||||
|
}
|
||||||
|
|
||||||
|
interface JSONHTTPService
|
||||||
|
{
|
||||||
|
void service(JSONHTTPRequest request, JSONHTTPResponse response, Callback callback);
|
||||||
|
}
|
||||||
|
// end::jsonHttpAPI[]
|
||||||
|
|
||||||
|
public void httpCompliance()
|
||||||
|
{
|
||||||
|
// tag::httpCompliance[]
|
||||||
|
HttpConfiguration httpConfiguration = new HttpConfiguration();
|
||||||
|
httpConfiguration.setHttpCompliance(HttpCompliance.RFC7230);
|
||||||
|
// end::httpCompliance[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void httpComplianceCustom()
|
||||||
|
{
|
||||||
|
// tag::httpComplianceCustom[]
|
||||||
|
HttpConfiguration httpConfiguration = new HttpConfiguration();
|
||||||
|
|
||||||
|
// RFC7230 compliance, but allow Violation.MULTIPLE_CONTENT_LENGTHS.
|
||||||
|
HttpCompliance customHttpCompliance = HttpCompliance.from("RFC7230,MULTIPLE_CONTENT_LENGTHS");
|
||||||
|
|
||||||
|
httpConfiguration.setHttpCompliance(customHttpCompliance);
|
||||||
|
// end::httpComplianceCustom[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void uriCompliance()
|
||||||
|
{
|
||||||
|
// tag::uriCompliance[]
|
||||||
|
HttpConfiguration httpConfiguration = new HttpConfiguration();
|
||||||
|
httpConfiguration.setUriCompliance(UriCompliance.RFC3986);
|
||||||
|
// end::uriCompliance[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void uriComplianceCustom()
|
||||||
|
{
|
||||||
|
// tag::uriComplianceCustom[]
|
||||||
|
HttpConfiguration httpConfiguration = new HttpConfiguration();
|
||||||
|
|
||||||
|
// RFC3986 compliance, but enforce Violation.AMBIGUOUS_PATH_SEPARATOR.
|
||||||
|
UriCompliance customUriCompliance = UriCompliance.from("RFC3986,-AMBIGUOUS_PATH_SEPARATOR");
|
||||||
|
|
||||||
|
httpConfiguration.setUriCompliance(customUriCompliance);
|
||||||
|
// end::uriComplianceCustom[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void cookieCompliance()
|
||||||
|
{
|
||||||
|
// tag::cookieCompliance[]
|
||||||
|
HttpConfiguration httpConfiguration = new HttpConfiguration();
|
||||||
|
httpConfiguration.setRequestCookieCompliance(CookieCompliance.RFC6265);
|
||||||
|
httpConfiguration.setResponseCookieCompliance(CookieCompliance.RFC6265);
|
||||||
|
// end::cookieCompliance[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void cookieComplianceCustom()
|
||||||
|
{
|
||||||
|
// tag::cookieComplianceCustom[]
|
||||||
|
HttpConfiguration httpConfiguration = new HttpConfiguration();
|
||||||
|
|
||||||
|
// RFC6265 compliance, but enforce Violation.RESERVED_NAMES_NOT_DOLLAR_PREFIXED.
|
||||||
|
CookieCompliance customUriCompliance = CookieCompliance.from("RFC6265,-RESERVED_NAMES_NOT_DOLLAR_PREFIXED");
|
||||||
|
httpConfiguration.setRequestCookieCompliance(customUriCompliance);
|
||||||
|
|
||||||
|
httpConfiguration.setResponseCookieCompliance(CookieCompliance.RFC6265);
|
||||||
|
|
||||||
|
// end::cookieComplianceCustom[]
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,310 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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 org.eclipse.jetty.docs.programming.server.http2;
|
||||||
|
|
||||||
|
import java.net.SocketAddress;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.http.HttpFields;
|
||||||
|
import org.eclipse.jetty.http.HttpMethod;
|
||||||
|
import org.eclipse.jetty.http.HttpStatus;
|
||||||
|
import org.eclipse.jetty.http.HttpURI;
|
||||||
|
import org.eclipse.jetty.http.HttpVersion;
|
||||||
|
import org.eclipse.jetty.http.MetaData;
|
||||||
|
import org.eclipse.jetty.http2.ErrorCode;
|
||||||
|
import org.eclipse.jetty.http2.api.Session;
|
||||||
|
import org.eclipse.jetty.http2.api.Stream;
|
||||||
|
import org.eclipse.jetty.http2.api.server.ServerSessionListener;
|
||||||
|
import org.eclipse.jetty.http2.frames.DataFrame;
|
||||||
|
import org.eclipse.jetty.http2.frames.HeadersFrame;
|
||||||
|
import org.eclipse.jetty.http2.frames.PushPromiseFrame;
|
||||||
|
import org.eclipse.jetty.http2.frames.ResetFrame;
|
||||||
|
import org.eclipse.jetty.http2.frames.SettingsFrame;
|
||||||
|
import org.eclipse.jetty.http2.server.RawHTTP2ServerConnectionFactory;
|
||||||
|
import org.eclipse.jetty.server.Server;
|
||||||
|
import org.eclipse.jetty.server.ServerConnector;
|
||||||
|
import org.eclipse.jetty.util.BufferUtil;
|
||||||
|
import org.eclipse.jetty.util.Callback;
|
||||||
|
import org.eclipse.jetty.util.resource.Resource;
|
||||||
|
|
||||||
|
import static java.lang.System.Logger.Level.INFO;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class HTTP2ServerDocs
|
||||||
|
{
|
||||||
|
public void setup() throws Exception
|
||||||
|
{
|
||||||
|
// tag::setup[]
|
||||||
|
// Create a Server instance.
|
||||||
|
Server server = new Server();
|
||||||
|
|
||||||
|
ServerSessionListener sessionListener = new ServerSessionListener.Adapter();
|
||||||
|
|
||||||
|
// Create a ServerConnector with RawHTTP2ServerConnectionFactory.
|
||||||
|
RawHTTP2ServerConnectionFactory http2 = new RawHTTP2ServerConnectionFactory(sessionListener);
|
||||||
|
|
||||||
|
// Configure RawHTTP2ServerConnectionFactory, for example:
|
||||||
|
|
||||||
|
// Configure the max number of concurrent requests.
|
||||||
|
http2.setMaxConcurrentStreams(128);
|
||||||
|
// Enable support for CONNECT.
|
||||||
|
http2.setConnectProtocolEnabled(true);
|
||||||
|
|
||||||
|
// Create the ServerConnector.
|
||||||
|
ServerConnector connector = new ServerConnector(server, http2);
|
||||||
|
|
||||||
|
// Add the Connector to the Server
|
||||||
|
server.addConnector(connector);
|
||||||
|
|
||||||
|
// Start the Server so it starts accepting connections from clients.
|
||||||
|
server.start();
|
||||||
|
// end::setup[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void accept()
|
||||||
|
{
|
||||||
|
// tag::accept[]
|
||||||
|
ServerSessionListener sessionListener = new ServerSessionListener.Adapter()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onAccept(Session session)
|
||||||
|
{
|
||||||
|
SocketAddress remoteAddress = session.getRemoteSocketAddress();
|
||||||
|
System.getLogger("http2").log(INFO, "Connection from {0}", remoteAddress);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// end::accept[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void preface()
|
||||||
|
{
|
||||||
|
// tag::preface[]
|
||||||
|
ServerSessionListener sessionListener = new ServerSessionListener.Adapter()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public Map<Integer, Integer> onPreface(Session session)
|
||||||
|
{
|
||||||
|
// Customize the settings, for example:
|
||||||
|
Map<Integer, Integer> settings = new HashMap<>();
|
||||||
|
|
||||||
|
// Tell the client that HTTP/2 push is disabled.
|
||||||
|
settings.put(SettingsFrame.ENABLE_PUSH, 0);
|
||||||
|
|
||||||
|
return settings;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// end::preface[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void request()
|
||||||
|
{
|
||||||
|
// tag::request[]
|
||||||
|
ServerSessionListener sessionListener = new ServerSessionListener.Adapter()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public Stream.Listener onNewStream(Stream stream, HeadersFrame frame)
|
||||||
|
{
|
||||||
|
// This is the "new stream" event, so it's guaranteed to be a request.
|
||||||
|
MetaData.Request request = (MetaData.Request)frame.getMetaData();
|
||||||
|
|
||||||
|
// Return a Stream.Listener to handle the request events,
|
||||||
|
// for example request content events or a request reset.
|
||||||
|
return new Stream.Listener.Adapter();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// end::request[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void requestContent()
|
||||||
|
{
|
||||||
|
// tag::requestContent[]
|
||||||
|
ServerSessionListener sessionListener = new ServerSessionListener.Adapter()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public Stream.Listener onNewStream(Stream stream, HeadersFrame frame)
|
||||||
|
{
|
||||||
|
MetaData.Request request = (MetaData.Request)frame.getMetaData();
|
||||||
|
// Return a Stream.Listener to handle the request events.
|
||||||
|
return new Stream.Listener.Adapter()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onData(Stream stream, DataFrame frame, Callback callback)
|
||||||
|
{
|
||||||
|
// Get the content buffer.
|
||||||
|
ByteBuffer buffer = frame.getData();
|
||||||
|
|
||||||
|
// Consume the buffer, here - as an example - just log it.
|
||||||
|
System.getLogger("http2").log(INFO, "Consuming buffer {0}", buffer);
|
||||||
|
|
||||||
|
// Tell the implementation that the buffer has been consumed.
|
||||||
|
callback.succeeded();
|
||||||
|
|
||||||
|
// By returning from the method, implicitly tell the implementation
|
||||||
|
// to deliver to this method more DATA frames when they are available.
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// end::requestContent[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void response()
|
||||||
|
{
|
||||||
|
// tag::response[]
|
||||||
|
ServerSessionListener sessionListener = new ServerSessionListener.Adapter()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public Stream.Listener onNewStream(Stream stream, HeadersFrame frame)
|
||||||
|
{
|
||||||
|
// Send a response after reading the request.
|
||||||
|
MetaData.Request request = (MetaData.Request)frame.getMetaData();
|
||||||
|
if (frame.isEndStream())
|
||||||
|
{
|
||||||
|
respond(stream, request);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return new Stream.Listener.Adapter()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onData(Stream stream, DataFrame frame, Callback callback)
|
||||||
|
{
|
||||||
|
// Consume the request content.
|
||||||
|
callback.succeeded();
|
||||||
|
if (frame.isEndStream())
|
||||||
|
respond(stream, request);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void respond(Stream stream, MetaData.Request request)
|
||||||
|
{
|
||||||
|
// Prepare the response HEADERS frame.
|
||||||
|
|
||||||
|
// The response HTTP status and HTTP headers.
|
||||||
|
MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, HttpFields.EMPTY);
|
||||||
|
|
||||||
|
if (HttpMethod.GET.is(request.getMethod()))
|
||||||
|
{
|
||||||
|
// The response content.
|
||||||
|
ByteBuffer resourceBytes = getResourceBytes(request);
|
||||||
|
|
||||||
|
// Send the HEADERS frame with the response status and headers,
|
||||||
|
// and a DATA frame with the response content bytes.
|
||||||
|
stream.headers(new HeadersFrame(stream.getId(), response, null, false))
|
||||||
|
.thenCompose(s -> s.data(new DataFrame(s.getId(), resourceBytes, true)));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Send just the HEADERS frame with the response status and headers.
|
||||||
|
stream.headers(new HeadersFrame(stream.getId(), response, null, true));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// tag::exclude[]
|
||||||
|
|
||||||
|
private ByteBuffer getResourceBytes(MetaData.Request request)
|
||||||
|
{
|
||||||
|
return ByteBuffer.allocate(1024);
|
||||||
|
}
|
||||||
|
// end::exclude[]
|
||||||
|
};
|
||||||
|
// end::response[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void reset()
|
||||||
|
{
|
||||||
|
float maxRequestRate = 0F;
|
||||||
|
// tag::reset[]
|
||||||
|
ServerSessionListener sessionListener = new ServerSessionListener.Adapter()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public Stream.Listener onNewStream(Stream stream, HeadersFrame frame)
|
||||||
|
{
|
||||||
|
float requestRate = calculateRequestRate();
|
||||||
|
|
||||||
|
if (requestRate > maxRequestRate)
|
||||||
|
{
|
||||||
|
stream.reset(new ResetFrame(stream.getId(), ErrorCode.REFUSED_STREAM_ERROR.code), Callback.NOOP);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// The request is accepted.
|
||||||
|
MetaData.Request request = (MetaData.Request)frame.getMetaData();
|
||||||
|
// Return a Stream.Listener to handle the request events.
|
||||||
|
return new Stream.Listener.Adapter();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// tag::exclude[]
|
||||||
|
|
||||||
|
private float calculateRequestRate()
|
||||||
|
{
|
||||||
|
return 0F;
|
||||||
|
}
|
||||||
|
// end::exclude[]
|
||||||
|
};
|
||||||
|
// end::reset[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void push() throws Exception
|
||||||
|
{
|
||||||
|
// tag::push[]
|
||||||
|
// The favicon bytes.
|
||||||
|
ByteBuffer faviconBuffer = BufferUtil.toBuffer(Resource.newResource("/path/to/favicon.ico"), true);
|
||||||
|
|
||||||
|
ServerSessionListener sessionListener = new ServerSessionListener.Adapter()
|
||||||
|
{
|
||||||
|
// By default, push is enabled.
|
||||||
|
private boolean pushEnabled = true;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSettings(Session session, SettingsFrame frame)
|
||||||
|
{
|
||||||
|
// Check whether the client sent an ENABLE_PUSH setting.
|
||||||
|
Map<Integer, Integer> settings = frame.getSettings();
|
||||||
|
Integer enablePush = settings.get(SettingsFrame.ENABLE_PUSH);
|
||||||
|
if (enablePush != null)
|
||||||
|
pushEnabled = enablePush == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Stream.Listener onNewStream(Stream stream, HeadersFrame frame)
|
||||||
|
{
|
||||||
|
MetaData.Request request = (MetaData.Request)frame.getMetaData();
|
||||||
|
if (pushEnabled && request.getURIString().endsWith("/index.html"))
|
||||||
|
{
|
||||||
|
// Push the favicon.
|
||||||
|
HttpURI pushedURI = HttpURI.build(request.getURI()).path("/favicon.ico");
|
||||||
|
MetaData.Request pushedRequest = new MetaData.Request("GET", pushedURI, HttpVersion.HTTP_2, HttpFields.EMPTY);
|
||||||
|
PushPromiseFrame promiseFrame = new PushPromiseFrame(stream.getId(), 0, pushedRequest);
|
||||||
|
stream.push(promiseFrame, new Stream.Listener.Adapter())
|
||||||
|
.thenCompose(pushedStream ->
|
||||||
|
{
|
||||||
|
// Send the favicon "response".
|
||||||
|
MetaData.Response pushedResponse = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, HttpFields.EMPTY);
|
||||||
|
return pushedStream.headers(new HeadersFrame(pushedStream.getId(), pushedResponse, null, false))
|
||||||
|
.thenCompose(pushed -> pushed.data(new DataFrame(pushed.getId(), faviconBuffer, true)));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// Return a Stream.Listener to handle the request events.
|
||||||
|
return new Stream.Listener.Adapter();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// end::push[]
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,334 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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 org.eclipse.jetty.docs.programming.server.http3;
|
||||||
|
|
||||||
|
import java.net.SocketAddress;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.RejectedExecutionException;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.http.HttpFields;
|
||||||
|
import org.eclipse.jetty.http.HttpMethod;
|
||||||
|
import org.eclipse.jetty.http.HttpStatus;
|
||||||
|
import org.eclipse.jetty.http.HttpVersion;
|
||||||
|
import org.eclipse.jetty.http.MetaData;
|
||||||
|
import org.eclipse.jetty.http3.api.Session;
|
||||||
|
import org.eclipse.jetty.http3.api.Stream;
|
||||||
|
import org.eclipse.jetty.http3.frames.DataFrame;
|
||||||
|
import org.eclipse.jetty.http3.frames.HeadersFrame;
|
||||||
|
import org.eclipse.jetty.http3.internal.HTTP3ErrorCode;
|
||||||
|
import org.eclipse.jetty.http3.server.HTTP3ServerConnector;
|
||||||
|
import org.eclipse.jetty.http3.server.RawHTTP3ServerConnectionFactory;
|
||||||
|
import org.eclipse.jetty.server.Server;
|
||||||
|
import org.eclipse.jetty.util.ssl.SslContextFactory;
|
||||||
|
|
||||||
|
import static java.lang.System.Logger.Level.INFO;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class HTTP3ServerDocs
|
||||||
|
{
|
||||||
|
public void setup() throws Exception
|
||||||
|
{
|
||||||
|
// tag::setup[]
|
||||||
|
// Create a Server instance.
|
||||||
|
Server server = new Server();
|
||||||
|
|
||||||
|
// HTTP/3 is always secure, so it always need a SslContextFactory.
|
||||||
|
SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
|
||||||
|
sslContextFactory.setKeyStorePath("/path/to/keystore");
|
||||||
|
sslContextFactory.setKeyStorePassword("secret");
|
||||||
|
|
||||||
|
// The listener for session events.
|
||||||
|
Session.Server.Listener sessionListener = new Session.Server.Listener() {};
|
||||||
|
|
||||||
|
// Create and configure the RawHTTP3ServerConnectionFactory.
|
||||||
|
RawHTTP3ServerConnectionFactory http3 = new RawHTTP3ServerConnectionFactory(sessionListener);
|
||||||
|
http3.getHTTP3Configuration().setStreamIdleTimeout(15000);
|
||||||
|
|
||||||
|
// Create and configure the HTTP3ServerConnector.
|
||||||
|
HTTP3ServerConnector connector = new HTTP3ServerConnector(server, sslContextFactory, http3);
|
||||||
|
// Configure the max number of requests per QUIC connection.
|
||||||
|
connector.getQuicConfiguration().setMaxBidirectionalRemoteStreams(1024);
|
||||||
|
|
||||||
|
// Add the Connector to the Server.
|
||||||
|
server.addConnector(connector);
|
||||||
|
|
||||||
|
// Start the Server so it starts accepting connections from clients.
|
||||||
|
server.start();
|
||||||
|
// end::setup[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void accept()
|
||||||
|
{
|
||||||
|
// tag::accept[]
|
||||||
|
Session.Server.Listener sessionListener = new Session.Server.Listener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onAccept(Session session)
|
||||||
|
{
|
||||||
|
SocketAddress remoteAddress = session.getRemoteSocketAddress();
|
||||||
|
System.getLogger("http3").log(INFO, "Connection from {0}", remoteAddress);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// end::accept[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void preface()
|
||||||
|
{
|
||||||
|
// tag::preface[]
|
||||||
|
Session.Server.Listener sessionListener = new Session.Server.Listener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public Map<Long, Long> onPreface(Session session)
|
||||||
|
{
|
||||||
|
Map<Long, Long> settings = new HashMap<>();
|
||||||
|
|
||||||
|
// Customize the settings
|
||||||
|
|
||||||
|
return settings;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// end::preface[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void request()
|
||||||
|
{
|
||||||
|
// tag::request[]
|
||||||
|
Session.Server.Listener sessionListener = new Session.Server.Listener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public Stream.Server.Listener onRequest(Stream.Server stream, HeadersFrame frame)
|
||||||
|
{
|
||||||
|
MetaData.Request request = (MetaData.Request)frame.getMetaData();
|
||||||
|
|
||||||
|
// Return a Stream.Server.Listener to handle the request events,
|
||||||
|
// for example request content events or a request reset.
|
||||||
|
return new Stream.Server.Listener() {};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// end::request[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void requestContent()
|
||||||
|
{
|
||||||
|
// tag::requestContent[]
|
||||||
|
Session.Server.Listener sessionListener = new Session.Server.Listener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public Stream.Server.Listener onRequest(Stream.Server stream, HeadersFrame frame)
|
||||||
|
{
|
||||||
|
MetaData.Request request = (MetaData.Request)frame.getMetaData();
|
||||||
|
|
||||||
|
// Demand to be called back when data is available.
|
||||||
|
stream.demand();
|
||||||
|
|
||||||
|
// Return a Stream.Server.Listener to handle the request content.
|
||||||
|
return new Stream.Server.Listener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onDataAvailable(Stream.Server stream)
|
||||||
|
{
|
||||||
|
// Read a chunk of the request content.
|
||||||
|
Stream.Data data = stream.readData();
|
||||||
|
|
||||||
|
if (data == null)
|
||||||
|
{
|
||||||
|
// No data available now, demand to be called back.
|
||||||
|
stream.demand();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Get the content buffer.
|
||||||
|
ByteBuffer buffer = data.getByteBuffer();
|
||||||
|
|
||||||
|
// Consume the buffer, here - as an example - just log it.
|
||||||
|
System.getLogger("http3").log(INFO, "Consuming buffer {0}", buffer);
|
||||||
|
|
||||||
|
// Tell the implementation that the buffer has been consumed.
|
||||||
|
data.complete();
|
||||||
|
|
||||||
|
if (!data.isLast())
|
||||||
|
{
|
||||||
|
// Demand to be called back.
|
||||||
|
stream.demand();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// end::requestContent[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void response()
|
||||||
|
{
|
||||||
|
// tag::response[]
|
||||||
|
Session.Server.Listener sessionListener = new Session.Server.Listener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public Stream.Server.Listener onRequest(Stream.Server stream, HeadersFrame frame)
|
||||||
|
{
|
||||||
|
// Send a response after reading the request.
|
||||||
|
MetaData.Request request = (MetaData.Request)frame.getMetaData();
|
||||||
|
if (frame.isLast())
|
||||||
|
{
|
||||||
|
respond(stream, request);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Demand to be called back when data is available.
|
||||||
|
stream.demand();
|
||||||
|
return new Stream.Server.Listener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onDataAvailable(Stream.Server stream)
|
||||||
|
{
|
||||||
|
Stream.Data data = stream.readData();
|
||||||
|
if (data == null)
|
||||||
|
{
|
||||||
|
stream.demand();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Consume the request content.
|
||||||
|
data.complete();
|
||||||
|
if (data.isLast())
|
||||||
|
respond(stream, request);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void respond(Stream.Server stream, MetaData.Request request)
|
||||||
|
{
|
||||||
|
// Prepare the response HEADERS frame.
|
||||||
|
|
||||||
|
// The response HTTP status and HTTP headers.
|
||||||
|
MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_3, HttpStatus.OK_200, HttpFields.EMPTY);
|
||||||
|
|
||||||
|
if (HttpMethod.GET.is(request.getMethod()))
|
||||||
|
{
|
||||||
|
// The response content.
|
||||||
|
ByteBuffer resourceBytes = getResourceBytes(request);
|
||||||
|
|
||||||
|
// Send the HEADERS frame with the response status and headers,
|
||||||
|
// and a DATA frame with the response content bytes.
|
||||||
|
stream.respond(new HeadersFrame(response, false))
|
||||||
|
.thenCompose(s -> s.data(new DataFrame(resourceBytes, true)));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Send just the HEADERS frame with the response status and headers.
|
||||||
|
stream.respond(new HeadersFrame(response, true));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// tag::exclude[]
|
||||||
|
|
||||||
|
private ByteBuffer getResourceBytes(MetaData.Request request)
|
||||||
|
{
|
||||||
|
return ByteBuffer.allocate(1024);
|
||||||
|
}
|
||||||
|
// end::exclude[]
|
||||||
|
};
|
||||||
|
// end::response[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void reset()
|
||||||
|
{
|
||||||
|
float maxRequestRate = 0F;
|
||||||
|
// tag::reset[]
|
||||||
|
Session.Server.Listener sessionListener = new Session.Server.Listener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public Stream.Server.Listener onRequest(Stream.Server stream, HeadersFrame frame)
|
||||||
|
{
|
||||||
|
float requestRate = calculateRequestRate();
|
||||||
|
|
||||||
|
if (requestRate > maxRequestRate)
|
||||||
|
{
|
||||||
|
stream.reset(HTTP3ErrorCode.REQUEST_REJECTED_ERROR.code(), new RejectedExecutionException());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// The request is accepted.
|
||||||
|
MetaData.Request request = (MetaData.Request)frame.getMetaData();
|
||||||
|
// Return a Stream.Listener to handle the request events.
|
||||||
|
return new Stream.Server.Listener() {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// tag::exclude[]
|
||||||
|
|
||||||
|
private float calculateRequestRate()
|
||||||
|
{
|
||||||
|
return 0F;
|
||||||
|
}
|
||||||
|
// end::exclude[]
|
||||||
|
};
|
||||||
|
// end::reset[]
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: push not yet implemented in HTTP/3.
|
||||||
|
/*
|
||||||
|
public void push() throws Exception
|
||||||
|
{
|
||||||
|
// tag::push[]
|
||||||
|
// The favicon bytes.
|
||||||
|
ByteBuffer faviconBuffer = BufferUtil.toBuffer(Resource.newResource("/path/to/favicon.ico"), true);
|
||||||
|
|
||||||
|
ServerSessionListener sessionListener = new ServerSessionListener.Adapter()
|
||||||
|
{
|
||||||
|
// By default, push is enabled.
|
||||||
|
private boolean pushEnabled = true;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSettings(Session session, SettingsFrame frame)
|
||||||
|
{
|
||||||
|
// Check whether the client sent an ENABLE_PUSH setting.
|
||||||
|
Map<Integer, Integer> settings = frame.getSettings();
|
||||||
|
Integer enablePush = settings.get(SettingsFrame.ENABLE_PUSH);
|
||||||
|
if (enablePush != null)
|
||||||
|
pushEnabled = enablePush == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Stream.Listener onNewStream(Stream stream, HeadersFrame frame)
|
||||||
|
{
|
||||||
|
MetaData.Request request = (MetaData.Request)frame.getMetaData();
|
||||||
|
if (pushEnabled && request.getURIString().endsWith("/index.html"))
|
||||||
|
{
|
||||||
|
// Push the favicon.
|
||||||
|
HttpURI pushedURI = HttpURI.build(request.getURI()).path("/favicon.ico");
|
||||||
|
MetaData.Request pushedRequest = new MetaData.Request("GET", pushedURI, HttpVersion.HTTP_2, HttpFields.EMPTY);
|
||||||
|
PushPromiseFrame promiseFrame = new PushPromiseFrame(stream.getId(), 0, pushedRequest);
|
||||||
|
stream.push(promiseFrame, new Stream.Listener.Adapter())
|
||||||
|
.thenCompose(pushedStream ->
|
||||||
|
{
|
||||||
|
// Send the favicon "response".
|
||||||
|
MetaData.Response pushedResponse = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, HttpFields.EMPTY);
|
||||||
|
return pushedStream.headers(new HeadersFrame(pushedStream.getId(), pushedResponse, null, false))
|
||||||
|
.thenCompose(pushed -> pushed.data(new DataFrame(pushed.getId(), faviconBuffer, true)));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// Return a Stream.Listener to handle the request events.
|
||||||
|
return new Stream.Listener.Adapter();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// end::push[]
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
|
@ -0,0 +1,307 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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 org.eclipse.jetty.docs.programming.server.session;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.memcached.session.MemcachedSessionDataMapFactory;
|
||||||
|
import org.eclipse.jetty.nosql.mongodb.MongoSessionDataStoreFactory;
|
||||||
|
import org.eclipse.jetty.server.Server;
|
||||||
|
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
|
||||||
|
import org.eclipse.jetty.server.session.CachingSessionDataStoreFactory;
|
||||||
|
import org.eclipse.jetty.server.session.DatabaseAdaptor;
|
||||||
|
import org.eclipse.jetty.server.session.DefaultSessionCache;
|
||||||
|
import org.eclipse.jetty.server.session.DefaultSessionCacheFactory;
|
||||||
|
import org.eclipse.jetty.server.session.DefaultSessionIdManager;
|
||||||
|
import org.eclipse.jetty.server.session.FileSessionDataStore;
|
||||||
|
import org.eclipse.jetty.server.session.FileSessionDataStoreFactory;
|
||||||
|
import org.eclipse.jetty.server.session.HouseKeeper;
|
||||||
|
import org.eclipse.jetty.server.session.NullSessionCache;
|
||||||
|
import org.eclipse.jetty.server.session.NullSessionCacheFactory;
|
||||||
|
import org.eclipse.jetty.server.session.NullSessionDataStore;
|
||||||
|
import org.eclipse.jetty.server.session.SessionCache;
|
||||||
|
import org.eclipse.jetty.server.session.SessionHandler;
|
||||||
|
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||||
|
import org.eclipse.jetty.webapp.WebAppContext;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class SessionDocs
|
||||||
|
{
|
||||||
|
public void minimumDefaultSessionIdManager()
|
||||||
|
{
|
||||||
|
//tag::default[]
|
||||||
|
Server server = new Server();
|
||||||
|
DefaultSessionIdManager idMgr = new DefaultSessionIdManager(server);
|
||||||
|
//you must set the workerName unless you set the env viable JETTY_WORKER_NAME
|
||||||
|
idMgr.setWorkerName("server3");
|
||||||
|
server.setSessionIdManager(idMgr);
|
||||||
|
//end::default[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void defaultSessionIdManagerWithHouseKeeper()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//tag::housekeeper[]
|
||||||
|
Server server = new Server();
|
||||||
|
DefaultSessionIdManager idMgr = new DefaultSessionIdManager(server);
|
||||||
|
idMgr.setWorkerName("server7");
|
||||||
|
server.setSessionIdManager(idMgr);
|
||||||
|
|
||||||
|
HouseKeeper houseKeeper = new HouseKeeper();
|
||||||
|
houseKeeper.setSessionIdManager(idMgr);
|
||||||
|
//set the frequency of scavenge cycles
|
||||||
|
houseKeeper.setIntervalSec(600L);
|
||||||
|
idMgr.setSessionHouseKeeper(houseKeeper);
|
||||||
|
//end::housekeeper[]
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void servletContextWithSessionHandler()
|
||||||
|
{
|
||||||
|
//tag:schsession[]
|
||||||
|
Server server = new Server();
|
||||||
|
|
||||||
|
ServletContextHandler context = new ServletContextHandler(server, "/foo", ServletContextHandler.SESSIONS);
|
||||||
|
SessionHandler sessions = context.getSessionHandler();
|
||||||
|
//make idle sessions valid for only 5mins
|
||||||
|
sessions.setMaxInactiveInterval(300);
|
||||||
|
//turn off use of cookies
|
||||||
|
sessions.setUsingCookies(false);
|
||||||
|
|
||||||
|
server.setHandler(context);
|
||||||
|
//end::schsession[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void webAppWithSessionHandler()
|
||||||
|
{
|
||||||
|
//tag:wacsession[]
|
||||||
|
Server server = new Server();
|
||||||
|
|
||||||
|
WebAppContext context = new WebAppContext();
|
||||||
|
SessionHandler sessions = context.getSessionHandler();
|
||||||
|
//make idle sessions valid for only 5mins
|
||||||
|
sessions.setMaxInactiveInterval(300);
|
||||||
|
//turn off use of cookies
|
||||||
|
sessions.setUsingCookies(false);
|
||||||
|
|
||||||
|
server.setHandler(context);
|
||||||
|
//end::wacsession[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void defaultSessionCache()
|
||||||
|
{
|
||||||
|
//tag::defaultsessioncache[]
|
||||||
|
Server server = new Server();
|
||||||
|
|
||||||
|
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
|
||||||
|
//EVICT_ON_INACTIVE: evict a session after 60sec inactivity
|
||||||
|
cacheFactory.setEvictionPolicy(60);
|
||||||
|
//Only useful with the EVICT_ON_INACTIVE policy
|
||||||
|
cacheFactory.setSaveOnInactiveEvict(true);
|
||||||
|
cacheFactory.setFlushOnResponseCommit(true);
|
||||||
|
cacheFactory.setInvalidateOnShutdown(false);
|
||||||
|
cacheFactory.setRemoveUnloadableSessions(true);
|
||||||
|
cacheFactory.setSaveOnCreate(true);
|
||||||
|
|
||||||
|
//Add the factory as a bean to the server, now whenever a
|
||||||
|
//SessionHandler starts it will consult the bean to create a new DefaultSessionCache
|
||||||
|
server.addBean(cacheFactory);
|
||||||
|
//end::defaultsessioncache[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void nullSessionCache()
|
||||||
|
{
|
||||||
|
//tag::nullsessioncache[]
|
||||||
|
Server server = new Server();
|
||||||
|
NullSessionCacheFactory cacheFactory = new NullSessionCacheFactory();
|
||||||
|
cacheFactory.setFlushOnResponseCommit(true);
|
||||||
|
cacheFactory.setRemoveUnloadableSessions(true);
|
||||||
|
cacheFactory.setSaveOnCreate(true);
|
||||||
|
|
||||||
|
//Add the factory as a bean to the server, now whenever a
|
||||||
|
//SessionHandler starts it will consult the bean to create a new NullSessionCache
|
||||||
|
server.addBean(cacheFactory);
|
||||||
|
//end::nullsessioncache[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void mixedSessionCache()
|
||||||
|
{
|
||||||
|
//tag::mixedsessioncache[]
|
||||||
|
Server server = new Server();
|
||||||
|
|
||||||
|
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
|
||||||
|
//NEVER_EVICT
|
||||||
|
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
|
||||||
|
cacheFactory.setFlushOnResponseCommit(true);
|
||||||
|
cacheFactory.setInvalidateOnShutdown(false);
|
||||||
|
cacheFactory.setRemoveUnloadableSessions(true);
|
||||||
|
cacheFactory.setSaveOnCreate(true);
|
||||||
|
|
||||||
|
//Add the factory as a bean to the server, now whenever a
|
||||||
|
//SessionHandler starts it will consult the bean to create a new DefaultSessionCache
|
||||||
|
server.addBean(cacheFactory);
|
||||||
|
|
||||||
|
ContextHandlerCollection contexts = new ContextHandlerCollection();
|
||||||
|
server.setHandler(contexts);
|
||||||
|
|
||||||
|
//Add a webapp that will use a DefaultSessionCache via the DefaultSessionCacheFactory
|
||||||
|
WebAppContext app1 = new WebAppContext();
|
||||||
|
app1.setContextPath("/app1");
|
||||||
|
contexts.addHandler(app1);
|
||||||
|
|
||||||
|
//Add a webapp that uses an explicit NullSessionCache instead
|
||||||
|
WebAppContext app2 = new WebAppContext();
|
||||||
|
app2.setContextPath("/app2");
|
||||||
|
NullSessionCache nullSessionCache = new NullSessionCache(app2.getSessionHandler());
|
||||||
|
nullSessionCache.setFlushOnResponseCommit(true);
|
||||||
|
nullSessionCache.setRemoveUnloadableSessions(true);
|
||||||
|
nullSessionCache.setSaveOnCreate(true);
|
||||||
|
//If we pass an existing SessionCache instance to the SessionHandler, it must be
|
||||||
|
//fully configured: this means we must also provide SessionDataStore
|
||||||
|
nullSessionCache.setSessionDataStore(new NullSessionDataStore());
|
||||||
|
app2.getSessionHandler().setSessionCache(nullSessionCache);
|
||||||
|
//end::mixedsessioncache[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void fileSessionDataStoreFactory()
|
||||||
|
{
|
||||||
|
//tag::filesessiondatastorefactory[]
|
||||||
|
Server server = new Server();
|
||||||
|
|
||||||
|
//First lets configure a DefaultSessionCacheFactory
|
||||||
|
DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
|
||||||
|
//NEVER_EVICT
|
||||||
|
cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
|
||||||
|
cacheFactory.setFlushOnResponseCommit(true);
|
||||||
|
cacheFactory.setInvalidateOnShutdown(false);
|
||||||
|
cacheFactory.setRemoveUnloadableSessions(true);
|
||||||
|
cacheFactory.setSaveOnCreate(true);
|
||||||
|
|
||||||
|
//Add the factory as a bean to the server, now whenever a
|
||||||
|
//SessionHandler starts it will consult the bean to create a new DefaultSessionCache
|
||||||
|
server.addBean(cacheFactory);
|
||||||
|
|
||||||
|
//Now, lets configure a FileSessionDataStoreFactory
|
||||||
|
FileSessionDataStoreFactory storeFactory = new FileSessionDataStoreFactory();
|
||||||
|
storeFactory.setStoreDir(new File("/tmp/sessions"));
|
||||||
|
storeFactory.setGracePeriodSec(3600);
|
||||||
|
storeFactory.setSavePeriodSec(0);
|
||||||
|
|
||||||
|
//Add the factory as a bean on the server, now whenever a
|
||||||
|
//SessionHandler starts, it will consult the bean to create a new FileSessionDataStore
|
||||||
|
//for use by the DefaultSessionCache
|
||||||
|
server.addBean(storeFactory);
|
||||||
|
//end::filesessiondatastorefactory[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void fileSessionDataStore()
|
||||||
|
{
|
||||||
|
//tag::filesessiondatastore[]
|
||||||
|
|
||||||
|
//create a context
|
||||||
|
WebAppContext app1 = new WebAppContext();
|
||||||
|
app1.setContextPath("/app1");
|
||||||
|
|
||||||
|
//First, we create a DefaultSessionCache
|
||||||
|
DefaultSessionCache cache = new DefaultSessionCache(app1.getSessionHandler());
|
||||||
|
cache.setEvictionPolicy(SessionCache.NEVER_EVICT);
|
||||||
|
cache.setFlushOnResponseCommit(true);
|
||||||
|
cache.setInvalidateOnShutdown(false);
|
||||||
|
cache.setRemoveUnloadableSessions(true);
|
||||||
|
cache.setSaveOnCreate(true);
|
||||||
|
|
||||||
|
//Now, we configure a FileSessionDataStore
|
||||||
|
FileSessionDataStore store = new FileSessionDataStore();
|
||||||
|
store.setStoreDir(new File("/tmp/sessions"));
|
||||||
|
store.setGracePeriodSec(3600);
|
||||||
|
store.setSavePeriodSec(0);
|
||||||
|
|
||||||
|
//Tell the cache to use the store
|
||||||
|
cache.setSessionDataStore(store);
|
||||||
|
|
||||||
|
//Tell the contex to use the cache/store combination
|
||||||
|
app1.getSessionHandler().setSessionCache(cache);
|
||||||
|
|
||||||
|
//end::filesessiondatastore[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void cachingSessionDataStore()
|
||||||
|
{
|
||||||
|
//tag::cachingsds[]
|
||||||
|
Server server = new Server();
|
||||||
|
|
||||||
|
//Make a factory for memcached L2 caches for SessionData
|
||||||
|
MemcachedSessionDataMapFactory mapFactory = new MemcachedSessionDataMapFactory();
|
||||||
|
mapFactory.setExpirySec(0); //items in memcached don't expire
|
||||||
|
mapFactory.setHeartbeats(true); //tell memcached to use heartbeats
|
||||||
|
mapFactory.setAddresses(new InetSocketAddress("localhost", 11211)); //use a local memcached instance
|
||||||
|
mapFactory.setWeights(new int[] {100}); //set the weighting
|
||||||
|
|
||||||
|
|
||||||
|
//Make a FileSessionDataStoreFactory for creating FileSessionDataStores
|
||||||
|
//to persist the session data
|
||||||
|
FileSessionDataStoreFactory storeFactory = new FileSessionDataStoreFactory();
|
||||||
|
storeFactory.setStoreDir(new File("/tmp/sessions"));
|
||||||
|
storeFactory.setGracePeriodSec(3600);
|
||||||
|
storeFactory.setSavePeriodSec(0);
|
||||||
|
|
||||||
|
//Make a factory that plugs the L2 cache into the SessionDataStore
|
||||||
|
CachingSessionDataStoreFactory cachingSessionDataStoreFactory = new CachingSessionDataStoreFactory();
|
||||||
|
cachingSessionDataStoreFactory.setSessionDataMapFactory(mapFactory);
|
||||||
|
cachingSessionDataStoreFactory.setSessionStoreFactory(storeFactory);
|
||||||
|
|
||||||
|
//Register it as a bean so that all SessionHandlers will use it
|
||||||
|
//to make FileSessionDataStores that use memcached as an L2 SessionData cache.
|
||||||
|
server.addBean(cachingSessionDataStoreFactory);
|
||||||
|
//end::cachingsds[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void jdbcSessionDataStore()
|
||||||
|
{
|
||||||
|
//tag::dbaDatasource[]
|
||||||
|
DatabaseAdaptor datasourceAdaptor = new DatabaseAdaptor();
|
||||||
|
datasourceAdaptor.setDatasourceName("/jdbc/myDS");
|
||||||
|
//end::dbaDatasource[]
|
||||||
|
|
||||||
|
//tag::dbaDriver[]
|
||||||
|
DatabaseAdaptor driverAdaptor = new DatabaseAdaptor();
|
||||||
|
driverAdaptor.setDriverInfo("com.mysql.jdbc.Driver", "jdbc:mysql://127.0.0.1:3306/sessions?user=sessionsadmin");
|
||||||
|
//end::dbaDriver[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void mongoSessionDataStore()
|
||||||
|
{
|
||||||
|
//tag::mongosdfactory[]
|
||||||
|
Server server = new Server();
|
||||||
|
|
||||||
|
MongoSessionDataStoreFactory mongoSessionDataStoreFactory = new MongoSessionDataStoreFactory();
|
||||||
|
mongoSessionDataStoreFactory.setGracePeriodSec(3600);
|
||||||
|
mongoSessionDataStoreFactory.setSavePeriodSec(0);
|
||||||
|
mongoSessionDataStoreFactory.setDbName("HttpSessions");
|
||||||
|
mongoSessionDataStoreFactory.setCollectionName("JettySessions");
|
||||||
|
|
||||||
|
// Either set the connectionString
|
||||||
|
mongoSessionDataStoreFactory.setConnectionString("mongodb:://localhost:27017");
|
||||||
|
// or alternatively set the host and port.
|
||||||
|
mongoSessionDataStoreFactory.setHost("localhost");
|
||||||
|
mongoSessionDataStoreFactory.setPort(27017);
|
||||||
|
//end::mongosdfactory[]
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,388 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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 org.eclipse.jetty.docs.programming.server.websocket;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import javax.websocket.DeploymentException;
|
||||||
|
import javax.websocket.server.ServerContainer;
|
||||||
|
import javax.websocket.server.ServerEndpoint;
|
||||||
|
import javax.websocket.server.ServerEndpointConfig;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.http.pathmap.PathSpec;
|
||||||
|
import org.eclipse.jetty.http.pathmap.UriTemplatePathSpec;
|
||||||
|
import org.eclipse.jetty.server.Server;
|
||||||
|
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||||
|
import org.eclipse.jetty.webapp.WebAppContext;
|
||||||
|
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
|
||||||
|
import org.eclipse.jetty.websocket.javax.server.config.JavaxWebSocketServletContainerInitializer;
|
||||||
|
import org.eclipse.jetty.websocket.server.JettyWebSocketCreator;
|
||||||
|
import org.eclipse.jetty.websocket.server.JettyWebSocketServerContainer;
|
||||||
|
import org.eclipse.jetty.websocket.server.JettyWebSocketServlet;
|
||||||
|
import org.eclipse.jetty.websocket.server.JettyWebSocketServletFactory;
|
||||||
|
import org.eclipse.jetty.websocket.server.config.JettyWebSocketServletContainerInitializer;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class WebSocketServerDocs
|
||||||
|
{
|
||||||
|
public void standardContainerWebAppContext() throws Exception
|
||||||
|
{
|
||||||
|
// tag::standardContainerWebAppContext[]
|
||||||
|
// Create a Server with a ServerConnector listening on port 8080.
|
||||||
|
Server server = new Server(8080);
|
||||||
|
|
||||||
|
// Create a WebAppContext with the given context path.
|
||||||
|
WebAppContext handler = new WebAppContext("/path/to/webapp", "/ctx");
|
||||||
|
server.setHandler(handler);
|
||||||
|
|
||||||
|
// Starting the Server will start the WebAppContext.
|
||||||
|
server.start();
|
||||||
|
// end::standardContainerWebAppContext[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void standardContainerServletContextHandler() throws Exception
|
||||||
|
{
|
||||||
|
// tag::standardContainerServletContextHandler[]
|
||||||
|
// Create a Server with a ServerConnector listening on port 8080.
|
||||||
|
Server server = new Server(8080);
|
||||||
|
|
||||||
|
// Create a ServletContextHandler with the given context path.
|
||||||
|
ServletContextHandler handler = new ServletContextHandler(server, "/ctx");
|
||||||
|
server.setHandler(handler);
|
||||||
|
|
||||||
|
// Ensure that JavaxWebSocketServletContainerInitializer is initialized,
|
||||||
|
// to setup the ServerContainer for this web application context.
|
||||||
|
JavaxWebSocketServletContainerInitializer.configure(handler, null);
|
||||||
|
|
||||||
|
// Starting the Server will start the ServletContextHandler.
|
||||||
|
server.start();
|
||||||
|
// end::standardContainerServletContextHandler[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void standardEndpointsInitialization() throws Exception
|
||||||
|
{
|
||||||
|
// tag::standardEndpointsInitialization[]
|
||||||
|
// Create a Server with a ServerConnector listening on port 8080.
|
||||||
|
Server server = new Server(8080);
|
||||||
|
|
||||||
|
// Create a ServletContextHandler with the given context path.
|
||||||
|
ServletContextHandler handler = new ServletContextHandler(server, "/ctx");
|
||||||
|
server.setHandler(handler);
|
||||||
|
|
||||||
|
// Ensure that JavaxWebSocketServletContainerInitializer is initialized,
|
||||||
|
// to setup the ServerContainer for this web application context.
|
||||||
|
JavaxWebSocketServletContainerInitializer.configure(handler, null);
|
||||||
|
|
||||||
|
// Add a WebSocket-initializer Servlet to register WebSocket endpoints.
|
||||||
|
handler.addServlet(MyJavaxWebSocketInitializerServlet.class, "/*");
|
||||||
|
|
||||||
|
// Starting the Server will start the ServletContextHandler.
|
||||||
|
server.start();
|
||||||
|
// end::standardEndpointsInitialization[]
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("InnerClassMayBeStatic")
|
||||||
|
// tag::standardWebSocketInitializerServlet[]
|
||||||
|
public class MyJavaxWebSocketInitializerServlet extends HttpServlet
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void init() throws ServletException
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Retrieve the ServerContainer from the ServletContext attributes.
|
||||||
|
ServerContainer container = (ServerContainer)getServletContext().getAttribute(ServerContainer.class.getName());
|
||||||
|
|
||||||
|
// Configure the ServerContainer.
|
||||||
|
container.setDefaultMaxTextMessageBufferSize(128 * 1024);
|
||||||
|
|
||||||
|
// Simple registration of your WebSocket endpoints.
|
||||||
|
container.addEndpoint(MyJavaxWebSocketEndPoint.class);
|
||||||
|
|
||||||
|
// Advanced registration of your WebSocket endpoints.
|
||||||
|
container.addEndpoint(
|
||||||
|
ServerEndpointConfig.Builder.create(MyJavaxWebSocketEndPoint.class, "/ws")
|
||||||
|
.subprotocols(List.of("my-ws-protocol"))
|
||||||
|
.build()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
catch (DeploymentException x)
|
||||||
|
{
|
||||||
|
throw new ServletException(x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// end::standardWebSocketInitializerServlet[]
|
||||||
|
|
||||||
|
public void standardContainerAndEndpoints() throws Exception
|
||||||
|
{
|
||||||
|
// tag::standardContainerAndEndpoints[]
|
||||||
|
// Create a Server with a ServerConnector listening on port 8080.
|
||||||
|
Server server = new Server(8080);
|
||||||
|
|
||||||
|
// Create a ServletContextHandler with the given context path.
|
||||||
|
ServletContextHandler handler = new ServletContextHandler(server, "/ctx");
|
||||||
|
server.setHandler(handler);
|
||||||
|
|
||||||
|
// Setup the ServerContainer and the WebSocket endpoints for this web application context.
|
||||||
|
JavaxWebSocketServletContainerInitializer.configure(handler, (servletContext, container) ->
|
||||||
|
{
|
||||||
|
// Configure the ServerContainer.
|
||||||
|
container.setDefaultMaxTextMessageBufferSize(128 * 1024);
|
||||||
|
|
||||||
|
// Simple registration of your WebSocket endpoints.
|
||||||
|
container.addEndpoint(MyJavaxWebSocketEndPoint.class);
|
||||||
|
|
||||||
|
// Advanced registration of your WebSocket endpoints.
|
||||||
|
container.addEndpoint(
|
||||||
|
ServerEndpointConfig.Builder.create(MyJavaxWebSocketEndPoint.class, "/ws")
|
||||||
|
.subprotocols(List.of("my-ws-protocol"))
|
||||||
|
.build()
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Starting the Server will start the ServletContextHandler.
|
||||||
|
server.start();
|
||||||
|
// end::standardContainerAndEndpoints[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void jettyContainerServletContextHandler() throws Exception
|
||||||
|
{
|
||||||
|
// tag::jettyContainerServletContextHandler[]
|
||||||
|
// Create a Server with a ServerConnector listening on port 8080.
|
||||||
|
Server server = new Server(8080);
|
||||||
|
|
||||||
|
// Create a ServletContextHandler with the given context path.
|
||||||
|
ServletContextHandler handler = new ServletContextHandler(server, "/ctx");
|
||||||
|
server.setHandler(handler);
|
||||||
|
|
||||||
|
// Ensure that JettyWebSocketServletContainerInitializer is initialized,
|
||||||
|
// to setup the JettyWebSocketServerContainer for this web application context.
|
||||||
|
JettyWebSocketServletContainerInitializer.configure(handler, null);
|
||||||
|
|
||||||
|
// Starting the Server will start the ServletContextHandler.
|
||||||
|
server.start();
|
||||||
|
// end::jettyContainerServletContextHandler[]
|
||||||
|
}
|
||||||
|
|
||||||
|
public void jettyEndpointsInitialization() throws Exception
|
||||||
|
{
|
||||||
|
// tag::jettyEndpointsInitialization[]
|
||||||
|
// Create a Server with a ServerConnector listening on port 8080.
|
||||||
|
Server server = new Server(8080);
|
||||||
|
|
||||||
|
// Create a ServletContextHandler with the given context path.
|
||||||
|
ServletContextHandler handler = new ServletContextHandler(server, "/ctx");
|
||||||
|
server.setHandler(handler);
|
||||||
|
|
||||||
|
// Ensure that JettyWebSocketServletContainerInitializer is initialized,
|
||||||
|
// to setup the JettyWebSocketServerContainer for this web application context.
|
||||||
|
JettyWebSocketServletContainerInitializer.configure(handler, null);
|
||||||
|
|
||||||
|
// Add a WebSocket-initializer Servlet to register WebSocket endpoints.
|
||||||
|
handler.addServlet(MyJettyWebSocketInitializerServlet.class, "/*");
|
||||||
|
|
||||||
|
// Starting the Server will start the ServletContextHandler.
|
||||||
|
server.start();
|
||||||
|
// end::jettyEndpointsInitialization[]
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("InnerClassMayBeStatic")
|
||||||
|
// tag::jettyWebSocketInitializerServlet[]
|
||||||
|
public class MyJettyWebSocketInitializerServlet extends HttpServlet
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void init() throws ServletException
|
||||||
|
{
|
||||||
|
// Retrieve the JettyWebSocketServerContainer.
|
||||||
|
JettyWebSocketServerContainer container = JettyWebSocketServerContainer.getContainer(getServletContext());
|
||||||
|
|
||||||
|
// Configure the JettyWebSocketServerContainer.
|
||||||
|
container.setMaxTextMessageSize(128 * 1024);
|
||||||
|
|
||||||
|
// Simple registration of your WebSocket endpoints.
|
||||||
|
container.addMapping("/ws/myURI", MyJettyWebSocketEndPoint.class);
|
||||||
|
|
||||||
|
// Advanced registration of your WebSocket endpoints.
|
||||||
|
container.addMapping("/ws/myOtherURI", (upgradeRequest, upgradeResponse) ->
|
||||||
|
new MyOtherJettyWebSocketEndPoint()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// end::jettyWebSocketInitializerServlet[]
|
||||||
|
|
||||||
|
public void jettyContainerAndEndpoints() throws Exception
|
||||||
|
{
|
||||||
|
// tag::jettyContainerAndEndpoints[]
|
||||||
|
// Create a Server with a ServerConnector listening on port 8080.
|
||||||
|
Server server = new Server(8080);
|
||||||
|
|
||||||
|
// Create a ServletContextHandler with the given context path.
|
||||||
|
ServletContextHandler handler = new ServletContextHandler(server, "/ctx");
|
||||||
|
server.setHandler(handler);
|
||||||
|
|
||||||
|
// Setup the JettyWebSocketServerContainer and the WebSocket endpoints for this web application context.
|
||||||
|
JettyWebSocketServletContainerInitializer.configure(handler, (servletContext, container) ->
|
||||||
|
{
|
||||||
|
// Configure the ServerContainer.
|
||||||
|
container.setMaxTextMessageSize(128 * 1024);
|
||||||
|
|
||||||
|
// Add your WebSocket endpoint(s) to the JettyWebSocketServerContainer.
|
||||||
|
container.addMapping("/ws/myURI", MyJettyWebSocketEndPoint.class);
|
||||||
|
|
||||||
|
// Use JettyWebSocketCreator to have more control on the WebSocket endpoint creation.
|
||||||
|
container.addMapping("/ws/myOtherURI", (upgradeRequest, upgradeResponse) ->
|
||||||
|
{
|
||||||
|
// Possibly inspect the upgrade request and modify the upgrade response.
|
||||||
|
upgradeResponse.setAcceptedSubProtocol("my-ws-protocol");
|
||||||
|
|
||||||
|
// Create the new WebSocket endpoint.
|
||||||
|
return new MyOtherJettyWebSocketEndPoint();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Starting the Server will start the ServletContextHandler.
|
||||||
|
server.start();
|
||||||
|
// end::jettyContainerAndEndpoints[]
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("InnerClassMayBeStatic")
|
||||||
|
// tag::jettyContainerUpgrade[]
|
||||||
|
public class ProgrammaticWebSocketUpgradeServlet extends HttpServlet
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
|
||||||
|
{
|
||||||
|
if (requiresWebSocketUpgrade(request))
|
||||||
|
{
|
||||||
|
// Retrieve the JettyWebSocketServerContainer.
|
||||||
|
JettyWebSocketServerContainer container = JettyWebSocketServerContainer.getContainer(getServletContext());
|
||||||
|
|
||||||
|
// Use a JettyWebSocketCreator to inspect the upgrade request,
|
||||||
|
// possibly modify the upgrade response, and create the WebSocket endpoint.
|
||||||
|
JettyWebSocketCreator creator = (upgradeRequest, upgradeResponse) -> new MyJettyWebSocketEndPoint();
|
||||||
|
|
||||||
|
// Perform the direct WebSocket upgrade.
|
||||||
|
container.upgrade(creator, request, response);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Normal handling of the HTTP request/response.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// end::jettyContainerUpgrade[]
|
||||||
|
|
||||||
|
private boolean requiresWebSocketUpgrade(HttpServletRequest request)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void jettyWebSocketServletMain() throws Exception
|
||||||
|
{
|
||||||
|
// tag::jettyWebSocketServletMain[]
|
||||||
|
// Create a Server with a ServerConnector listening on port 8080.
|
||||||
|
Server server = new Server(8080);
|
||||||
|
|
||||||
|
// Create a ServletContextHandler with the given context path.
|
||||||
|
ServletContextHandler handler = new ServletContextHandler(server, "/ctx");
|
||||||
|
server.setHandler(handler);
|
||||||
|
|
||||||
|
// Setup the JettyWebSocketServerContainer to initialize WebSocket components.
|
||||||
|
JettyWebSocketServletContainerInitializer.configure(handler, null);
|
||||||
|
|
||||||
|
// Add your WebSocketServlet subclass to the ServletContextHandler.
|
||||||
|
handler.addServlet(MyJettyWebSocketServlet.class, "/ws/*");
|
||||||
|
|
||||||
|
// Starting the Server will start the ServletContextHandler.
|
||||||
|
server.start();
|
||||||
|
// end::jettyWebSocketServletMain[]
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("InnerClassMayBeStatic")
|
||||||
|
// tag::jettyWebSocketServlet[]
|
||||||
|
public class MyJettyWebSocketServlet extends JettyWebSocketServlet
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
protected void configure(JettyWebSocketServletFactory factory)
|
||||||
|
{
|
||||||
|
// At most 1 MiB text messages.
|
||||||
|
factory.setMaxTextMessageSize(1048576);
|
||||||
|
|
||||||
|
// Add the WebSocket endpoint.
|
||||||
|
factory.addMapping("/ws/someURI", (upgradeRequest, upgradeResponse) ->
|
||||||
|
{
|
||||||
|
// Possibly inspect the upgrade request and modify the upgrade response.
|
||||||
|
|
||||||
|
// Create the new WebSocket endpoint.
|
||||||
|
return new MyJettyWebSocketEndPoint();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// end::jettyWebSocketServlet[]
|
||||||
|
|
||||||
|
@ServerEndpoint("/ws")
|
||||||
|
private static class MyJavaxWebSocketEndPoint
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
@WebSocket
|
||||||
|
private static class MyJettyWebSocketEndPoint
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
@WebSocket
|
||||||
|
private static class MyOtherJettyWebSocketEndPoint
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public void uriTemplatePathSpec()
|
||||||
|
{
|
||||||
|
Server server = new Server(8080);
|
||||||
|
|
||||||
|
// tag::uriTemplatePathSpec[]
|
||||||
|
ServletContextHandler handler = new ServletContextHandler(server, "/ctx");
|
||||||
|
|
||||||
|
// Configure the JettyWebSocketServerContainer.
|
||||||
|
JettyWebSocketServletContainerInitializer.configure(handler, (servletContext, container) ->
|
||||||
|
{
|
||||||
|
container.addMapping("/ws/chat/{room}", (upgradeRequest, upgradeResponse) ->
|
||||||
|
{
|
||||||
|
// Retrieve the URI template.
|
||||||
|
UriTemplatePathSpec pathSpec = (UriTemplatePathSpec)upgradeRequest.getServletAttribute(PathSpec.class.getName());
|
||||||
|
|
||||||
|
// Match the URI template.
|
||||||
|
Map<String, String> params = pathSpec.getPathParams(upgradeRequest.getRequestPath());
|
||||||
|
String room = params.get("room");
|
||||||
|
|
||||||
|
// Create the new WebSocket endpoint with the URI template information.
|
||||||
|
return new MyWebSocketRoomEndPoint(room);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
// end::uriTemplatePathSpec[]
|
||||||
|
}
|
||||||
|
|
||||||
|
@WebSocket
|
||||||
|
private static class MyWebSocketRoomEndPoint
|
||||||
|
{
|
||||||
|
public MyWebSocketRoomEndPoint(String room)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
After Width: | Height: | Size: 262 KiB |
|
@ -0,0 +1,39 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
.xref:operations-guide:index.adoc[]
|
||||||
|
* xref:begin/index.adoc[]
|
||||||
|
* xref:features/index.adoc[]
|
||||||
|
* xref:howtos/index.adoc[]
|
||||||
|
* xref:arch/index.adoc[]
|
||||||
|
* xref:start/index.adoc[]
|
||||||
|
** xref:start/start-jpms.adoc[]
|
||||||
|
* xref:modules/index.adoc[]
|
||||||
|
** xref:modules/custom.adoc[]
|
||||||
|
** xref:modules/standard.adoc[]
|
||||||
|
* xref:deploy/index.adoc[]
|
||||||
|
* xref:server/index.adoc[]
|
||||||
|
* xref:protocols/index.adoc[]
|
||||||
|
* xref:keystore/index.adoc[]
|
||||||
|
* xref:session/index.adoc[]
|
||||||
|
* xref:quickstart/index.adoc[]
|
||||||
|
* xref:annotations/index.adoc[]
|
||||||
|
* xref:jsp/index.adoc[]
|
||||||
|
* xref:jstl/index.adoc[]
|
||||||
|
* xref:jsf-taglibs/index.adoc[]
|
||||||
|
* xref:jndi/index.adoc[]
|
||||||
|
* xref:jaas/index.adoc[]
|
||||||
|
* xref:jaspi/index.adoc[]
|
||||||
|
* xref:jmx/index.adoc[]
|
||||||
|
* xref:troubleshooting/index.adoc[]
|
||||||
|
* xref:xml/index.adoc[]
|
|
@ -0,0 +1,219 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Annotations
|
||||||
|
|
||||||
|
Enable the `annotations` module if your webapp - or any of its third party libraries - uses any of the following:
|
||||||
|
|
||||||
|
* Annotations:
|
||||||
|
** @Resource
|
||||||
|
** @Resources
|
||||||
|
** @PostConstruct
|
||||||
|
** @PreDestroy
|
||||||
|
** @DeclaredRoles
|
||||||
|
** @RunAs
|
||||||
|
** @MultipartConfig
|
||||||
|
** @WebServlet
|
||||||
|
** @WebFilter
|
||||||
|
** @WebListener
|
||||||
|
** @WebInitParam
|
||||||
|
** @ServletSecurity, @HttpConstraint, @HttpMethodConstraint
|
||||||
|
** @HandlesTypes
|
||||||
|
* javax.servlet.ServletContainerInitializers
|
||||||
|
* JSP
|
||||||
|
|
||||||
|
|
||||||
|
[[scanning]]
|
||||||
|
== Annotation Scanning
|
||||||
|
|
||||||
|
According to more recent versions of the Servlet Specification, the web.xml file can contain the attribute `metadata-complete`.
|
||||||
|
If this is set to `true`, then _no_ annotation scanning takes place, and your descriptor must contain the equivalent xml statements of any annotations.
|
||||||
|
|
||||||
|
If it is `metadata-complete=false`, or your web.xml predates the inclusion of this attribute, annotation scanning is required to take place.
|
||||||
|
|
||||||
|
To prevent annotation scanning you can use the `WebAppContext.setConfigurationDiscovered(false)` method.
|
||||||
|
Here's an example context xml file that calls this method:
|
||||||
|
|
||||||
|
[,xml,subs=verbatim]
|
||||||
|
----
|
||||||
|
<?xml version="1.0"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure class="org.eclipse.jetty.webapp.WebAppContext"> <1>
|
||||||
|
<Set name="configurationDiscovered">false</Set> <2>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
<1> Configures a link:{javadoc-url}/org/eclipse/jetty/webapp/WebAppContext.html[`WebAppContext`], which is the Jetty component that represents a standard Servlet web application.
|
||||||
|
<2> Specifies that scanning should not take place.
|
||||||
|
|
||||||
|
However, despite `metadata-complete=true`, scanning of classes may _still_ occur because of http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContainerInitializer.html[javax.servlet.ServletContainerInitializer]s.
|
||||||
|
Classes implementing this interface are found by Jetty using the http://docs.oracle.com/javase/6/docs/api/java/util/ServiceLoader.html[javax.util.ServiceLoader] mechanism, and if one is present _and_ it includes the @HandlesTypes annotation, then Jetty must scan the class hierarchy of the web application.
|
||||||
|
This may be very time-consuming if you have many jars.
|
||||||
|
|
||||||
|
We will now look at ways to limit the jars that are scanned.
|
||||||
|
|
||||||
|
[[og-container-include-jar-pattern]]
|
||||||
|
=== The container classpath
|
||||||
|
|
||||||
|
By default, Jetty will _not_ scan any classes that are on the container's classpath.
|
||||||
|
|
||||||
|
Sometimes, you may have third party libraries on the container's classpath that you need to be scanned.
|
||||||
|
In this case, use the `org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern` context attribute to define which container jars and class directories to scan.
|
||||||
|
The value of this attribute is a regular expression.
|
||||||
|
|
||||||
|
Here's an example from a context xml file that includes any jar whose name starts with "foo-" or "bar-", or a directory named "classes":
|
||||||
|
|
||||||
|
[,xml,subs=verbatim]
|
||||||
|
----
|
||||||
|
<?xml version="1.0"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure class="org.eclipse.jetty.webapp.WebAppContext"> <1>
|
||||||
|
<Call name="setAttribute"> <2>
|
||||||
|
<Arg>org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern</Arg> <3>
|
||||||
|
<Arg>.*/foo-[^/]*\.jar$|.*/bar-[^/]*\.jar$|.*/classes/.*</Arg> <4>
|
||||||
|
</Call>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
<1> Configures a link:{javadoc-url}/org/eclipse/jetty/webapp/WebAppContext.html[`WebAppContext`], which is the Jetty component that represents a standard Servlet web application.
|
||||||
|
<2> Specifies a context attribute.
|
||||||
|
<3> Specifies the name of the context attribute.
|
||||||
|
<4> Specifies the value of the context attribute.
|
||||||
|
|
||||||
|
Note that the order of the patterns defines the ordering of the scanning of the jars or class directories.
|
||||||
|
|
||||||
|
[[og-web-inf-include-jar-pattern]]
|
||||||
|
=== The webapp classpath
|
||||||
|
|
||||||
|
By default Jetty will scan __all__ classes from `WEB-INF/classes` and _all_ jars from `WEB-INF/lib` according to the order, if any, established by absolute or relative ordering clauses in web.xml.
|
||||||
|
|
||||||
|
If your webapp contains many jar files that you know do not contain any annotations, you can significantly speed up deployment by omitting them from scanning.
|
||||||
|
However, be careful if your webapp uses a `ServletContainerInitializer` with an `@HandlesTypes` annotation that you don't exclude jars that contain classes matching the annotation.
|
||||||
|
|
||||||
|
Use the `org.eclipse.jetty.server.webapp.WebInfIncludeJarPattern` context attribute to define a regular expression for jars and class directories to select for scanning.
|
||||||
|
|
||||||
|
Here's an example of a context xml file that sets a pattern that matches any jar on the webapp's classpath that starts with `spring-`:
|
||||||
|
|
||||||
|
[,xml,subs=verbatim]
|
||||||
|
----
|
||||||
|
<?xml version="1.0"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure class="org.eclipse.jetty.webapp.WebAppContext"> <1>
|
||||||
|
<Call name="setAttribute"> <2>
|
||||||
|
<Arg>org.eclipse.jetty.server.webapp.WebInfIncludeJarPattern</Arg> <3>
|
||||||
|
<Arg>.*/spring-[^/]*\.jar$</Arg> <4>
|
||||||
|
</Call>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
<1> Configures a link:{javadoc-url}/org/eclipse/jetty/webapp/WebAppContext.html[`WebAppContext`], which is the Jetty component that represents a standard Servlet web application.
|
||||||
|
<2> Specifies a context attribute.
|
||||||
|
<3> Specifies the name of the context attribute.
|
||||||
|
<4> Specifies the value of the context attribute.
|
||||||
|
|
||||||
|
=== Multi-threading
|
||||||
|
|
||||||
|
By default Jetty performs annotation scanning in a multi-threaded manner in order to complete it in the minimum amount of time.
|
||||||
|
|
||||||
|
If you don't want multi-threaded scanning, you can configure Jetty to revert to single-threaded scanning.
|
||||||
|
There are several options to configure this:
|
||||||
|
|
||||||
|
1. Set the context attribute `org.eclipse.jetty.annotations.multiThreaded` to `false`
|
||||||
|
2. Set the `Server` attribute `org.eclipse.jetty.annotations.multiThreaded` to `false`
|
||||||
|
3. Set the System property `org.eclipse.jetty.annotations.multiThreaded` to `false`
|
||||||
|
|
||||||
|
Method 1 will only affect the current webapp.
|
||||||
|
Method 2 will affect all webapps deployed to the same Server instance.
|
||||||
|
Method 3 will affect all webapps deployed in the same JVM.
|
||||||
|
|
||||||
|
By default, Jetty will wait a maximum of 60 seconds for all of the scanning threads to complete.
|
||||||
|
You can set this to a higher or lower number of seconds by doing one of the following:
|
||||||
|
|
||||||
|
1. Set the context attribute `org.eclipse.jetty.annotations.maxWait`
|
||||||
|
2. Set the `Server` attribute `org.eclipse.jetty.annotations.maxWait`
|
||||||
|
3. Set the System property `org.eclipse.jetty.annotations.maxWait`
|
||||||
|
|
||||||
|
Method 1 will only affect the current webapp.
|
||||||
|
Method 2 will affect all webapps deployed to the same Server instance.
|
||||||
|
Method 3 will affect all webapps deployed in the same JVM.
|
||||||
|
|
||||||
|
[[scis]]
|
||||||
|
== ServletContainerInitializers
|
||||||
|
|
||||||
|
The http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContainerInitializer.html[javax.servlet.ServletContainerInitializer] class can exist in: the container's classpath, the webapp's `WEB-INF/classes` directory, the webapp's `WEB-INF/lib` jars, or any external extraClasspath that you have configured on the webapp.
|
||||||
|
|
||||||
|
The Servlet Specification does not define any order in which a `ServletContainerInitializer` must be called when the webapp starts.
|
||||||
|
By default Jetty will call them in the following order:
|
||||||
|
|
||||||
|
1. ServletContainerInitializers from the container's classpath
|
||||||
|
2. ServletContainerInitializers from WEB-INF/classes
|
||||||
|
3. ServletContainerInitializers from WEB-INF/lib jars __in the order established in web.xml__, or in the order that the SCI is returned by the http://docs.oracle.com/javase/6/docs/api/java/util/ServiceLoader.html[javax.util.ServiceLoader] if there is _no_ ordering.
|
||||||
|
|
||||||
|
=== Exclusions
|
||||||
|
|
||||||
|
By default, as according to the Servlet Specification, all `ServletContainerInitializer` that are discovered are invoked.
|
||||||
|
|
||||||
|
Sometimes, depending on your requirements, you may need to prevent some being called at all.
|
||||||
|
|
||||||
|
In this case, you can define the `org.eclipse.jetty.containerInitializerExclusionPattern` context attribute.
|
||||||
|
|
||||||
|
This is a regular expression that defines http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html[patterns] of classnames that you want to exclude.
|
||||||
|
Here's an example of setting the context attribute in a context xml file:
|
||||||
|
|
||||||
|
[,xml,subs=verbatim]
|
||||||
|
----
|
||||||
|
<?xml version="1.0"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure class="org.eclipse.jetty.webapp.WebAppContext"> <1>
|
||||||
|
<Call name="setAttribute"> <2>
|
||||||
|
<Arg>org.eclipse.jetty.containerInitializerExclusionPattern</Arg> <3>
|
||||||
|
<Arg>com.acme.*|com.corp.SlowContainerInitializer</Arg> <4>
|
||||||
|
</Call>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
<1> Configures a link:{javadoc-url}/org/eclipse/jetty/webapp/WebAppContext.html[`WebAppContext`], which is the Jetty component that represents a standard Servlet web application.
|
||||||
|
<2> Specifies a context attribute.
|
||||||
|
<3> Specifies the name of the context attribute.
|
||||||
|
<4> Specifies the value of the context attribute.
|
||||||
|
|
||||||
|
In this example we exclude *all* `ServletContainerInitializer` instances in the `com.acme package`, and the specific class `com.corp.SlowContainerInitializer`.
|
||||||
|
|
||||||
|
It is possible to use exclusion and ordering together to control `ServletContainerInitializer` invocation - the exclusions will be applied before the ordering.
|
||||||
|
|
||||||
|
=== Ordering
|
||||||
|
|
||||||
|
If you need `ServletContainerInitializer` classes called in a specific order, you can use the context attribute `org.eclipse.jetty.containerInitializerOrder`.
|
||||||
|
Set it to a list of comma separated class names of `ServletContainerInitializers` in the order that you want them applied.
|
||||||
|
|
||||||
|
You may optionally use the wildcard character `+*+` *once* in the list.
|
||||||
|
It will match all `ServletContainerInitializer` classes not explicitly named in the list.
|
||||||
|
|
||||||
|
Here is an example context xml file that ensures the `com.example.PrioritySCI` will be called first, followed by the `com.acme.FooSCI`, then all other SCIs:
|
||||||
|
|
||||||
|
[,xml,subs=verbatim]
|
||||||
|
----
|
||||||
|
<?xml version="1.0"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure class="org.eclipse.jetty.webapp.WebAppContext"> <1>
|
||||||
|
<Call name="setAttribute"> <2>
|
||||||
|
<Arg>org.eclipse.jetty.containerInitializerOrder</Arg> <3>
|
||||||
|
<Arg>org.eclipse.jetty.websocket.javax.server.JavaxWebSocketServletContainerInitializer, com.acme.FooSCI, *</Arg> <4>
|
||||||
|
</Call>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
<1> Configures a link:{javadoc-url}/org/eclipse/jetty/webapp/WebAppContext.html[`WebAppContext`], which is the Jetty component that represents a standard Servlet web application.
|
||||||
|
<2> Specifies a context attribute.
|
||||||
|
<3> Specifies the name of the context attribute.
|
||||||
|
<4> Specifies the value of the context attribute.
|
|
@ -0,0 +1,160 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Architecture Overview
|
||||||
|
|
||||||
|
Jetty is an HTTP server and Servlet Container, and supports deployments of web applications.
|
||||||
|
|
||||||
|
The xref:server/index.adoc[Jetty server] listens on one or more network ports using one or more xref:protocols/index.adoc[protocol connectors].
|
||||||
|
|
||||||
|
Clients send HTTP requests for specific URIs, such as `+https://host/store/cart+`.
|
||||||
|
|
||||||
|
The HTTP requests arrive to the connectors through the network; the Jetty server processes the requests and, based on their URIs, forwards them to the appropriate xref:deploy/index.adoc[deployed web application].
|
||||||
|
|
||||||
|
[plantuml]
|
||||||
|
----
|
||||||
|
skinparam backgroundColor transparent
|
||||||
|
skinparam monochrome true
|
||||||
|
skinparam shadowing false
|
||||||
|
skinparam roundCorner 10
|
||||||
|
|
||||||
|
scale 1.25
|
||||||
|
|
||||||
|
cloud Internet as internet
|
||||||
|
rectangle "Jetty Server" as server
|
||||||
|
rectangle "HTTP/1.1 Connector" as http
|
||||||
|
rectangle "HTTP/2 Connector" as http2
|
||||||
|
rectangle "WebApp "Store"" as store
|
||||||
|
rectangle "WebApp "Catalog"" as catalog
|
||||||
|
|
||||||
|
internet -- http
|
||||||
|
internet -- http2
|
||||||
|
http -- server
|
||||||
|
http2 -- server
|
||||||
|
server -- store
|
||||||
|
server -- catalog
|
||||||
|
----
|
||||||
|
|
||||||
|
[[concepts]]
|
||||||
|
== Main Concepts
|
||||||
|
There are three main concepts on which the Jetty standalone server is based:
|
||||||
|
|
||||||
|
* The <<modules,Jetty _module_ system>>, where Jetty modules provides Jetty features.
|
||||||
|
* The <<jetty-base,`$JETTY_BASE` directory>>, that provides a place where you configure which Jetty modules you want to enable, configure the properties of each enabled module, and therefore configure the features you need for your web applications.
|
||||||
|
* The <<start,Jetty start mechanism>>, that starts a JVM that runs Jetty with the configuration you specified.
|
||||||
|
|
||||||
|
After installing Jetty, you will want to set up a <<jetty-base,`$JETTY_BASE` directory>> where you configure <<modules,Jetty modules>>.
|
||||||
|
|
||||||
|
[[modules]]
|
||||||
|
== Jetty Modules
|
||||||
|
|
||||||
|
The Jetty standalone server is made of Java components that are assembled together, configured and started to provide different features.
|
||||||
|
|
||||||
|
A Jetty _module_ provides one or more components that work together to provide typically one feature, although they may provide more than one feature.
|
||||||
|
|
||||||
|
A Jetty module is nothing more than Jetty components assembled together like you would do using Java APIs, just done in a declarative way using configuration files.
|
||||||
|
What you can do in Java code to assemble Jetty components can be done using Jetty modules.
|
||||||
|
|
||||||
|
A Jetty module may be dependent on other Jetty modules: for example, the `http` Jetty module depends on the `server` Jetty module which in turn depends on the `threadpool` and `logging` Jetty modules.
|
||||||
|
|
||||||
|
Every feature in a Jetty server is enabled by enabling the corresponding Jetty module(s).
|
||||||
|
|
||||||
|
For example, if you enable only the `http` Jetty module, then your Jetty standalone server will only be able to listen to a network port for clear-text HTTP requests.
|
||||||
|
It will not be able to process secure HTTP (i.e. `https`) requests, it will not be able to process WebSocket, or HTTP/2, or HTTP/3 or any other protocol because the correspondent modules have not been enabled.
|
||||||
|
|
||||||
|
You can even start a Jetty server _without_ listening on a network port -- for example because you have enabled a custom module you wrote that provides the features you need.
|
||||||
|
|
||||||
|
This allows the Jetty standalone server to be as small as necessary: modules that are not enabled are not loaded, don't waste memory, and you don't risk a client using a module that you did not know was even there.
|
||||||
|
|
||||||
|
For more detailed information about the Jetty module system, see xref:modules/index.adoc[this section].
|
||||||
|
|
||||||
|
[[jetty-base]]
|
||||||
|
== `$JETTY_HOME` and `$JETTY_BASE`
|
||||||
|
|
||||||
|
Instead of managing multiple Jetty distributions out of many locations, it is possible to maintain a separation between the binary installation of the standalone Jetty, known as `$JETTY_HOME`, and the customizations for your specific environment(s), known as `$JETTY_BASE`.
|
||||||
|
|
||||||
|
This separation between the binary installation directory and the specific configuration directory allows managing multiple, different, server configurations, and allows for quick, drop-in upgrades of Jetty.
|
||||||
|
|
||||||
|
There should always only be *one* `$JETTY_HOME` (per version of Jetty), but there can be many `$JETTY_BASE` directories that reference it.
|
||||||
|
|
||||||
|
This separation between `$JETTY_HOME` and `$JETTY_BASE` allows Jetty upgrades without affecting your web applications.
|
||||||
|
`$JETTY_HOME` contains the Jetty runtime and libraries and the default configuration, while a `$JETTY_BASE` contains your web applications and any override of the default configuration.
|
||||||
|
|
||||||
|
For example, with the `$JETTY_HOME` installation the default value for the network port for clear-text HTTP is `8080`.
|
||||||
|
However, you may want that port to be `6060`, because xref:protocols/index.adoc#proxy[Jetty is behind a load balancer] that is configured to forward to the backend on port `6060`.
|
||||||
|
In this case, you configure the clear-text HTTP port in `$JETTY_BASE`, not in `$JETTY_HOME`.
|
||||||
|
When you upgrade Jetty, you will upgrade only the files in `$JETTY_HOME`, and all the configuration in `$JETTY_BASE` will remain unchanged, keeping your clear-text HTTP port at `6060`.
|
||||||
|
|
||||||
|
Installing the Jetty runtime and libraries in `$JETTY_HOME` also allows you to leverage file system permissions: `$JETTY_HOME` may be owned by an administrator user (so that only administrators can upgrade it), while `$JETTY_BASE` directories may be owned by a less privileged user.
|
||||||
|
|
||||||
|
If you had changed the default configuration in `$JETTY_HOME`, when you upgrade Jetty, say from version `10.0.0` to version `10.0.1`, your changes would be lost.
|
||||||
|
Maintaining all the changes in `$JETTY_HOME`, and having to reconfigure these with each upgrade results in a massive commitment of time and effort.
|
||||||
|
|
||||||
|
To recap:
|
||||||
|
|
||||||
|
`$JETTY_HOME`::
|
||||||
|
This is the location for the Jetty binaries.
|
||||||
|
`$JETTY_BASE`::
|
||||||
|
This is the location for your configurations and customizations to the Jetty binaries.
|
||||||
|
|
||||||
|
[[start]]
|
||||||
|
== Start Mechanism
|
||||||
|
|
||||||
|
The Jetty start mechanism provides two features:
|
||||||
|
|
||||||
|
* The mean to configure your `$JETTY_BASE` by enabling the desired modules, and to display the configuration of your `$JETTY_BASE`.
|
||||||
|
* The mean to start Jetty itself, by starting a JVM that reads the Jetty configuration in `$JETTY_BASE`, which is then executed to assemble and start the Jetty components.
|
||||||
|
|
||||||
|
The Jetty start mechanism is invoked by executing `$JETTY_HOME/start.jar` from within your `$JETTY_BASE`, and you can think of it as the Jetty command line program, similar to many Unix/Windows command line programs.
|
||||||
|
|
||||||
|
For example, you can ask for help:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --help
|
||||||
|
----
|
||||||
|
|
||||||
|
Or you can list all available modules (or only those with a specific tag):
|
||||||
|
|
||||||
|
----
|
||||||
|
# List all the modules.
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --list-modules=*
|
||||||
|
|
||||||
|
# List all the modules tagged as "demo".
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --list-modules=demo
|
||||||
|
----
|
||||||
|
|
||||||
|
You can enable a module, for example the `http` module:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --add-module=http
|
||||||
|
----
|
||||||
|
|
||||||
|
Once you have one or more module enabled, you can display the current configuration, to verify that the configuration is correct:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --list-config
|
||||||
|
----
|
||||||
|
|
||||||
|
You can enable a Jetty demo module, which will deploy a demo web application:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --add-module=demo-simple
|
||||||
|
----
|
||||||
|
|
||||||
|
Finally, you can start Jetty:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar
|
||||||
|
----
|
||||||
|
|
||||||
|
Read more information at the xref:start/index.adoc[Jetty start mechanism section].
|
|
@ -0,0 +1,304 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Getting Started
|
||||||
|
|
||||||
|
If you are new to Eclipse Jetty, read on to download, install, start and deploy web applications to Jetty.
|
||||||
|
|
||||||
|
== Quick Setup
|
||||||
|
|
||||||
|
Jetty is distributed in an artifact that expands in a directory called `$JETTY_HOME`, which should not be modified.
|
||||||
|
|
||||||
|
Configuration for Jetty is typically done in a directory called `$JETTY_BASE`.
|
||||||
|
There may be more than one `$JETTY_BASE` directories with different configurations.
|
||||||
|
|
||||||
|
The following commands can be used to set up a `$JETTY_BASE` directory that supports deployment of `+*.war+` files and a clear-text HTTP connector:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ export JETTY_HOME=/path/to/jetty-home
|
||||||
|
$ mkdir /path/to/jetty-base
|
||||||
|
$ cd /path/to/jetty-base
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --add-module=server,http,deploy
|
||||||
|
----
|
||||||
|
|
||||||
|
The last command creates a `$JETTY_BASE/start.d/` directory and other directories that contain the configuration of the server, including the `$JETTY_BASE/webapps/` directory, in which standard `+*.war+` files can be deployed.
|
||||||
|
|
||||||
|
To deploy Jetty's demo web applications, run this command:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --add-module=demo
|
||||||
|
----
|
||||||
|
|
||||||
|
Now you can start the Jetty server with:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar
|
||||||
|
----
|
||||||
|
|
||||||
|
Point your browser at `+http://localhost:8080+` to see the demo web applications deployed in Jetty.
|
||||||
|
|
||||||
|
The Jetty server can be stopped with `ctrl+c` in the terminal window.
|
||||||
|
|
||||||
|
The following sections will guide you in details about <<download,downloading>>, <<install,installing>> and <<start,starting>> Jetty, and <<deploy,deploying>> your web applications to Jetty.
|
||||||
|
|
||||||
|
Read the xref:arch/index.adoc[Jetty architecture section] for more information about Jetty modules, `$JETTY_HOME`, `$JETTY_BASE` and how to customize and start Jetty.
|
||||||
|
|
||||||
|
[[download]]
|
||||||
|
== Downloading Jetty
|
||||||
|
|
||||||
|
The Eclipse Jetty distribution is available for download from https://eclipse.dev/jetty/download.php[]
|
||||||
|
|
||||||
|
The Eclipse Jetty distribution is available in both `zip` and `gzip` formats; download the one most appropriate for your system, typically `zip` for Windows and `gzip` for other operating systems.
|
||||||
|
|
||||||
|
[[install]]
|
||||||
|
== Installing Jetty
|
||||||
|
|
||||||
|
After the download, unpacking Eclipse Jetty will extract the files into a directory called `jetty-home-VERSION`, where `VERSION` is the version that you downloaded, for example `{version}`, so that the directory is called `jetty-home-{version}`.
|
||||||
|
|
||||||
|
Unpack Eclipse Jetty compressed file in a convenient location, for example under `/opt`.
|
||||||
|
|
||||||
|
CAUTION: For Windows users, you should unpack Jetty to a path that does not contain spaces.
|
||||||
|
|
||||||
|
The rest of the instructions in this documentation will refer to this location as `$JETTY_HOME`, or `${jetty.home}`.
|
||||||
|
|
||||||
|
IMPORTANT: It is important that *only* stable release versions are used in production environments.
|
||||||
|
Versions that have been deprecated or are released as Milestones (M), Alpha, Beta or Release Candidates (RC) are *not* suitable for production as they may contain security flaws or incomplete/non-functioning feature sets.
|
||||||
|
|
||||||
|
If you are new to Jetty, you should read the xref:arch/index.adoc[Jetty architecture section] to become familiar with the terms used in this documentation.
|
||||||
|
Otherwise, you can jump to the <<start,section on starting Jetty>>.
|
||||||
|
|
||||||
|
[[start]]
|
||||||
|
== Starting Jetty
|
||||||
|
|
||||||
|
Eclipse Jetty as a standalone server has no graphical user interface, so configuring and running the server is done from the command line.
|
||||||
|
|
||||||
|
Recall from the xref:arch/index.adoc[architecture section] that Jetty is based on xref:modules/index.adoc[modules], that provides features, and on xref:arch/index.adoc#jetty-base[`$JETTY_BASE`], the place where you configure which module (and therefore which feature) you want to enable, and where you configure module parameters.
|
||||||
|
|
||||||
|
Jetty is started by executing `$JETTY_HOME/start.jar` from within a `$JETTY_BASE` directory, so first we need to create a `$JETTY_BASE`:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ JETTY_BASE=/path/to/jetty.base
|
||||||
|
$ cd $JETTY_BASE
|
||||||
|
----
|
||||||
|
|
||||||
|
If you try to start Jetty from an empty `$JETTY_BASE` you get:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar
|
||||||
|
----
|
||||||
|
|
||||||
|
[jetty%nowrap]
|
||||||
|
....
|
||||||
|
[jetty]
|
||||||
|
....
|
||||||
|
|
||||||
|
Jetty exited complaining that there are no modules enabled, since the `$JETTY_BASE` you just created is empty and therefore there is no configuration to read to assemble the Jetty server.
|
||||||
|
|
||||||
|
However, it shows that `start.jar` takes parameters, whose details can be found in xref:start/index.adoc[this section].
|
||||||
|
|
||||||
|
You can explore what modules are available out of the box via:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --list-modules=*
|
||||||
|
----
|
||||||
|
|
||||||
|
Let's try to enable the `http` module (see also xref:protocols/index.adoc#http[this section] for additional information):
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --add-module=http
|
||||||
|
----
|
||||||
|
|
||||||
|
[jetty%nowrap]
|
||||||
|
....
|
||||||
|
[jetty]
|
||||||
|
args=--add-module=http
|
||||||
|
....
|
||||||
|
|
||||||
|
Now you can start Jetty:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar
|
||||||
|
----
|
||||||
|
|
||||||
|
[jetty%nowrap]
|
||||||
|
....
|
||||||
|
[jetty]
|
||||||
|
args=--module=http
|
||||||
|
highlight=(\{.*:8080})
|
||||||
|
....
|
||||||
|
|
||||||
|
Note how Jetty is listening on port `8080` for clear-text HTTP/1.1 connections.
|
||||||
|
|
||||||
|
After having enabled the `http` module, the `$JETTY_BASE` directory looks like this:
|
||||||
|
|
||||||
|
[source,subs=verbatim]
|
||||||
|
----
|
||||||
|
JETTY_BASE
|
||||||
|
├── resources
|
||||||
|
│ └── jetty-logging.properties <1>
|
||||||
|
└── start.d <2>
|
||||||
|
└── http.ini <3>
|
||||||
|
----
|
||||||
|
|
||||||
|
<1> The `resources/jetty-logging.properties` file has been created because the `http` modules depends on the `server` module, which in turn depends on the `logging` module; the `logging` module created this file that can be configured to control the server logging level.
|
||||||
|
<2> The `start.d/` directory contains the configuration files for the modules.
|
||||||
|
<3> The `start.d/http.ini` file is the `http` module configuration file, where you can specify values for the `http` module properties.
|
||||||
|
|
||||||
|
In the `http.ini` file you can find the following content (among other content):
|
||||||
|
|
||||||
|
.http.ini
|
||||||
|
[source,subs=verbatim]
|
||||||
|
----
|
||||||
|
--module=http <1>
|
||||||
|
# jetty.http.port=8080 <2>
|
||||||
|
...
|
||||||
|
----
|
||||||
|
|
||||||
|
<1> This line enables the `http` module and should not be modified.
|
||||||
|
<2> This line is commented out and specifies the default value for the module property `jetty.http.port`, which is the network port that listens for clear-text HTTP connections.
|
||||||
|
|
||||||
|
You can change the module property `jetty.http.port` value directly from the command line:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar jetty.http.port=9999
|
||||||
|
----
|
||||||
|
|
||||||
|
To make this change persistent, you can edit the `http.ini` file, uncomment the module property `jetty.http.port` and change its value to `9999`:
|
||||||
|
|
||||||
|
.http.ini
|
||||||
|
----
|
||||||
|
--module=http
|
||||||
|
jetty.http.port=9999
|
||||||
|
...
|
||||||
|
----
|
||||||
|
|
||||||
|
If you restart Jetty, the new value will be used:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar
|
||||||
|
----
|
||||||
|
|
||||||
|
[jetty%nowrap]
|
||||||
|
....
|
||||||
|
[jetty]
|
||||||
|
args=--module=http jetty.http.port=9999
|
||||||
|
highlight=(\{.*:9999})
|
||||||
|
....
|
||||||
|
|
||||||
|
Note how Jetty is now listening on port `9999` for clear-text HTTP/1.1 connections.
|
||||||
|
|
||||||
|
NOTE: If you want to enable support for different protocols such as secure HTTP/1.1 or HTTP/2 or HTTP/3, or configure Jetty behind a load balancer, read xref:protocols/index.adoc[this section].
|
||||||
|
|
||||||
|
The Jetty server is now up and running, but it has no web applications deployed, so it just replies with `404 Not Found` to every request.
|
||||||
|
It is time to <<deploy,deploy your web applications>> to Jetty.
|
||||||
|
|
||||||
|
For more detailed information about the Jetty start mechanism, you can read the xref:arch/index.adoc#start[Jetty start mechanism] section.
|
||||||
|
|
||||||
|
[[deploy]]
|
||||||
|
== Deploying Web Applications
|
||||||
|
|
||||||
|
For the purpose of deploying web applications to Jetty, there are two types of resources that can be deployed:
|
||||||
|
|
||||||
|
* Standard Web Application Archives, in the form of `+*.war+` files or web application directories, defined by the Servlet specification.
|
||||||
|
Their deployment is described in <<deploy-war,this section>>.
|
||||||
|
* Jetty context XML files, that allow you to customize the deployment of standard web applications, and also allow you use Jetty components, and possibly custom components written by you, to assemble your web applications.
|
||||||
|
Their deployment is described in xref:deploy/index.adoc[this section].
|
||||||
|
|
||||||
|
[[deploy-war]]
|
||||||
|
=== Deploying +*.war+ Files
|
||||||
|
|
||||||
|
A standard Servlet web application is packaged in either a `+*.war+` file or in a directory with the structure of a `+*.war+` file.
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
Recall that the structure of a `+*.war+` file is as follows:
|
||||||
|
|
||||||
|
[source,subs=verbatim]
|
||||||
|
----
|
||||||
|
mywebapp.war
|
||||||
|
├── index.html <1>
|
||||||
|
└── WEB-INF <2>
|
||||||
|
├── classes/ <3>
|
||||||
|
├── lib/ <4>
|
||||||
|
└── web.xml <5>
|
||||||
|
----
|
||||||
|
<1> Publicly accessible resources such as `+*.html+`, `+*.jsp+`, `+*.css+`, `+*.js+` files, etc. are placed in `+*.war+` or in sub-directories of the `+*.war+`.
|
||||||
|
<2> `WEB-INF` is a special directory used to store anything related to the web application that must not be publicly accessible, but may be accessed by other resources.
|
||||||
|
<3> `WEB-INF/classes` stores the web application compiled `+*.class+` files
|
||||||
|
<4> `WEB-INF/lib` stores the web application `+*.jar+` files
|
||||||
|
<5> `WEB-INF/web.xml` is the web application deployment descriptor defines the components and the configuration of your web application.
|
||||||
|
====
|
||||||
|
|
||||||
|
To deploy a standard web application, you need to enable the `deploy` module (see the `deploy` module complete definition xref:modules/standard.adoc#deploy[here]).
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --add-module=deploy
|
||||||
|
----
|
||||||
|
|
||||||
|
[jetty%nowrap]
|
||||||
|
....
|
||||||
|
[jetty]
|
||||||
|
setupArgs=--add-module=http
|
||||||
|
args=--add-module=deploy
|
||||||
|
....
|
||||||
|
|
||||||
|
The `deploy` module creates the `$JETTY_BASE/webapps` directory, the directory where `+*.war+` files or web application directories should be copied so that Jetty can deploy them.
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
The `deploy` module only provides the feature of deploying web applications.
|
||||||
|
|
||||||
|
Whether these web applications are served via clear-text HTTP/1.1, or secure HTTP/1.1, or secure HTTP/2, or HTTP/3 (or even all of these protocols) depends on whether the correspondent Jetty modules have been enabled.
|
||||||
|
Refer to the xref:protocols/index.adoc[section about protocols] for further information.
|
||||||
|
====
|
||||||
|
|
||||||
|
Now you need to copy a web application to the `$JETTY_BASE/webapps` directory, and you can use one of the demos shipped with Jetty:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --add-module=demo-simple
|
||||||
|
----
|
||||||
|
|
||||||
|
The `$JETTY_BASE` directory is now:
|
||||||
|
|
||||||
|
----
|
||||||
|
$JETTY_BASE
|
||||||
|
├── resources
|
||||||
|
│ └── jetty-logging.properties
|
||||||
|
├── start.d
|
||||||
|
│ ├── deploy.ini
|
||||||
|
│ └── http.ini
|
||||||
|
└── webapps
|
||||||
|
└── demo-simple.war
|
||||||
|
----
|
||||||
|
|
||||||
|
Now start Jetty:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar
|
||||||
|
----
|
||||||
|
|
||||||
|
[jetty%nowrap]
|
||||||
|
....
|
||||||
|
[jetty]
|
||||||
|
setupArgs=--add-modules=http,deploy,demo-simple
|
||||||
|
highlight=WebAppContext
|
||||||
|
....
|
||||||
|
|
||||||
|
Note the highlighted line that logs the deployment of `demo-simple.war`.
|
||||||
|
|
||||||
|
Now you can access the web application by pointing your browser to `+http://localhost:8080/demo-simple+`.
|
||||||
|
|
||||||
|
[[deploy-war-advanced]]
|
||||||
|
=== Advanced Deployment
|
||||||
|
|
||||||
|
If you want to customize the deployment of your web application, for example by specifying a `contextPath` different from the file/directory name, or by specifying JNDI entries, or by specifying virtual hosts, etc. read xref:deploy/index.adoc[this section].
|
|
@ -0,0 +1,418 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Web Application Deployment
|
||||||
|
|
||||||
|
Most of the times you want to be able to customize the deployment of your web applications, for example by changing the `contextPath`, or by adding JNDI entries, or by configuring virtual hosts, etc.
|
||||||
|
|
||||||
|
The customization is performed by the xref:modules/standard.adoc#deploy[`deploy` module] by processing <<jetty,Jetty context XML files>>.
|
||||||
|
|
||||||
|
The `deploy` module contains the `DeploymentManager` component that scans the `$JETTY_BASE/webapps` directory for changes, following the deployment rules described in <<rules,this section>>.
|
||||||
|
|
||||||
|
[[hot-static]]
|
||||||
|
== Hot vs Static Deployment
|
||||||
|
|
||||||
|
The `DeploymentManager` scans the `$JETTY_BASE/webapps` directory for changes every `N` seconds, where `N` is configured via the `jetty.deploy.scanInterval` property.
|
||||||
|
|
||||||
|
By default, the scan interval is `1` second, which means that _hot_ deployment is enabled: if a file is added/changed/removed from the `$JETTY_BASE/webapps` directory, the `DeploymentManager` will notice the change and respectively deploy/redeploy/undeploy the web application.
|
||||||
|
|
||||||
|
Setting the scan interval to `0` means that _static_ deployment is enabled, and the `DeploymentManager` will not scan the `$JETTY_BASE/webapps` directory for changes.
|
||||||
|
This means that to deploy/redeploy/undeploy a web application you will need to stop and restart Jetty.
|
||||||
|
|
||||||
|
The following command line disables _hot_ deployment by specifying the `jetty.deploy.scanInterval` property on the command line, and therefore only for this particular run:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar jetty.deploy.scanInterval=0
|
||||||
|
----
|
||||||
|
|
||||||
|
To make _static_ deployment persistent, you need to edit the `deploy` module configuration file, `$JETTY_BASE/start.d/deploy.ini`, uncomment the module property `jetty.deploy.scanInterval` and change its value to `0`:
|
||||||
|
|
||||||
|
.deploy.ini
|
||||||
|
[source,subs=quotes]
|
||||||
|
----
|
||||||
|
--module=deploy
|
||||||
|
#jetty.deploy.scanInterval=0#
|
||||||
|
...
|
||||||
|
----
|
||||||
|
|
||||||
|
[[rules]]
|
||||||
|
== Deployment Rules
|
||||||
|
|
||||||
|
_Adding_ a `+*.war+` file, a `+*.war+` directory, a Jetty context XML file or a normal directory to `$JETTY_BASE/webapps` causes the `DeploymentManager` to deploy the new web application.
|
||||||
|
|
||||||
|
_Updating_ a `+*.war+` file or a Jetty context XML file causes the `DeploymentManager` to redeploy the web application, which means that the Jetty context component representing the web application is stopped, then reconfigured, and then restarted.
|
||||||
|
|
||||||
|
_Removing_ a `+*.war+` file, a `+*.war+` directory, a Jetty context XML file or a normal directory from `$JETTY_BASE/webapps` causes the `DeploymentManager` to undeploy the web application, which means that the Jetty context component representing the web application is stopped and removed from the Jetty server.
|
||||||
|
|
||||||
|
When a file or directory is added to `$JETTY_BASE/webapps`, the `DeploymentManager` derives the web application `contextPath` from the file or directory name, with the following rules:
|
||||||
|
|
||||||
|
* If the directory name is, for example, `mywebapp/`, it is deployed as a standard web application if it contains a `WEB-INF/` subdirectory, otherwise it is deployed as a web application of static content.
|
||||||
|
The `contextPath` would be `/mywebapp` (that is, the web application is reachable at `+http://localhost:8080/mywebapp/+`).
|
||||||
|
* If the directory name is `ROOT`, case insensitive, the `contextPath` is `/` (that is, the web application is reachable at `+http://localhost:8080/+`).
|
||||||
|
* If the directory name ends with `.d`, for example `config.d/`, it is ignored, although it may be referenced to configure other web applications (for example to store common files).
|
||||||
|
* If the `+*.war+` file name is, for example, `mywebapp.war`, it is deployed as a standard web application with the context path `/mywebapp` (that is, the web application is reachable at `+http://localhost:8080/mywebapp/+`).
|
||||||
|
* If the file name is `ROOT.war`, case insensitive, the `contextPath` is `/` (that is, the web application is reachable at `+http://localhost:8080/+`).
|
||||||
|
* If both the `mywebapp.war` file and the `mywebapp/` directory exist, only the file is deployed.
|
||||||
|
This allows the directory with the same name to be the `+*.war+` file unpack location and avoid that the web application is deployed twice.
|
||||||
|
* A <<jetty,Jetty context XML file>> named `mywebapp.xml` is deployed as a web application by processing the directives contained in the XML file itself, which must set the `contextPath`.
|
||||||
|
* If both `mywebapp.xml` and `mywebapp.war` exist, only the XML file is deployed.
|
||||||
|
This allows the XML file to reference the `+*.war+` file and avoid that the web application is deployed twice.
|
||||||
|
|
||||||
|
[[jetty]]
|
||||||
|
== Deploying Jetty Context XML Files
|
||||||
|
|
||||||
|
A Jetty context XML file is a xref:xml/index.adoc[Jetty XML file] that allows you to customize the deployment of web applications.
|
||||||
|
|
||||||
|
NOTE: Recall that the `DeploymentManager` component of the Jetty `deploy` module <<rules,gives priority>> to Jetty context XML files over `+*.war+` files or directories.
|
||||||
|
|
||||||
|
To deploy a web application using a Jetty context XML file, simply place the file in the `$JETTY_BASE/webapps` directory.
|
||||||
|
|
||||||
|
A simple Jetty context XML file, for example named `wiki.xml` is the following:
|
||||||
|
|
||||||
|
.wiki.xml
|
||||||
|
[,xml,subs=verbatim]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure class="org.eclipse.jetty.webapp.WebAppContext"> <1>
|
||||||
|
<Set name="contextPath">/wiki</Set> <2>
|
||||||
|
<Set name="war">/opt/myapps/myapp.war</Set> <3>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
<1> Configures a link:{javadoc-url}/org/eclipse/jetty/webapp/WebAppContext.html[`WebAppContext`], which is the Jetty component that represents a standard Servlet web application.
|
||||||
|
<2> Specifies the web application `contextPath`, which may be different from the `+*.war+` file name.
|
||||||
|
<3> Specifies the file system path of the `+*.war+` file.
|
||||||
|
|
||||||
|
The `$JETTY_BASE` directory would look like this:
|
||||||
|
|
||||||
|
----
|
||||||
|
$JETTY_BASE
|
||||||
|
├── resources
|
||||||
|
│ └── jetty-logging.properties
|
||||||
|
├── start.d
|
||||||
|
│ ├── deploy.ini
|
||||||
|
│ └── http.ini
|
||||||
|
└── webapps
|
||||||
|
└── wiki.xml
|
||||||
|
----
|
||||||
|
|
||||||
|
TIP: The `+*.war+` file may be placed anywhere in the file system and does not need to be placed in the `$JETTY_BASE/webapps` directory.
|
||||||
|
|
||||||
|
IMPORTANT: If you place both the Jetty context XML file _and_ the `+*.war+` file in the `$JETTY_BASE/webapps` directory, remember that they must have the same file name, for example `wiki.xml` and `wiki.war`, so that the `DeploymentManager` deploys the web application only once using the Jetty context XML file (and not the `+*.war+` file).
|
||||||
|
|
||||||
|
You can use the features of xref:xml/index.adoc[Jetty XML files] to avoid to hard-code file system paths or other configurations in your Jetty context XML files, for example by using system properties:
|
||||||
|
|
||||||
|
.wiki.xml
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
|
||||||
|
<Set name="contextPath">/wiki</Set>
|
||||||
|
<Set name="war"><SystemProperty name="myapps.dir"/>/myapp.war</Set>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
Note how the `+*.war+` file path is now obtained by resolving the system property `myapps.dir` that you can specify on the command line when you start Jetty:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar -Dmyapps.dir=/opt/myapps
|
||||||
|
----
|
||||||
|
|
||||||
|
[[jndi]]
|
||||||
|
== Configuring JNDI Entries
|
||||||
|
|
||||||
|
A web application may _reference_ a JNDI entry, such as a JDBC `DataSource` from the web application `web.xml` file.
|
||||||
|
The JNDI entry must be _defined_ in a xref:jndi/index.adoc#xml[Jetty XML file], for example a context XML like so:
|
||||||
|
|
||||||
|
.mywebapp.xml
|
||||||
|
[,xml,subs=normal]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure id="wac" class="org.eclipse.jetty.webapp.WebAppContext">
|
||||||
|
<Set name="contextPath">/mywebapp</Set>
|
||||||
|
<Set name="war">/opt/webapps/mywebapp.war</Set>
|
||||||
|
# <New class="org.eclipse.jetty.plus.jndi.Resource">
|
||||||
|
<Arg><Ref refid="wac"/></Arg>
|
||||||
|
<Arg>jdbc/myds</Arg>
|
||||||
|
<Arg>
|
||||||
|
<New class="com.mysql.cj.jdbc.MysqlConnectionPoolDataSource">
|
||||||
|
<Set name="url">jdbc:mysql://localhost:3306/databasename</Set>
|
||||||
|
<Set name="user">user</Set>
|
||||||
|
<Set name="password">password</Set>
|
||||||
|
</New>
|
||||||
|
</Arg>
|
||||||
|
</New>#
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
For more information and examples on how to use JNDI in Jetty, refer to the xref:jndi/index.adoc[JNDI] feature section.
|
||||||
|
|
||||||
|
[IMPORTANT]
|
||||||
|
====
|
||||||
|
Class `com.mysql.cj.jdbc.MysqlConnectionPoolDataSource` is present in the MySQL JDBC driver file, `mysql-connector-java-<version>.jar`, which must be available on the server's classpath .
|
||||||
|
|
||||||
|
If the class is instead present _within_ the web application, then the JNDI entry must be declared in a `WEB-INF/jetty-env.xml` file - see the xref:jndi/index.adoc[JNDI] feature section for more information and examples.
|
||||||
|
|
||||||
|
====
|
||||||
|
|
||||||
|
[[virtual-hosts]]
|
||||||
|
== Configuring Virtual Hosts
|
||||||
|
|
||||||
|
A _virtual host_ is an internet domain name, registered in the Domain Name Server (DNS), for an IP address such that multiple virtual hosts will resolve to the same IP address of a single server instance.
|
||||||
|
|
||||||
|
If you have multiple web applications deployed on the same Jetty server, by using virtual hosts you will be able to target a specific web application.
|
||||||
|
|
||||||
|
For example, you may have a web application for your business and a web application for your hobbies , both deployed in the same Jetty server.
|
||||||
|
By using virtual hosts, you will be able to have the first web application available at `+http://domain.biz/+`, and the second web application available at `+http://hobby.net/+`.
|
||||||
|
|
||||||
|
Another typical case is when you want to use different subdomains for different web application, for example a project website is at `+http://project.org/+` and the project documentation is at `+http://docs.project.org+`.
|
||||||
|
|
||||||
|
Virtual hosts can be used with any context that is a subclass of link:{javadoc-url}/org/eclipse/jetty/server/handler/ContextHandler.html[ContextHandler].
|
||||||
|
|
||||||
|
[[virtual-hosts-names]]
|
||||||
|
=== Virtual Host Names
|
||||||
|
|
||||||
|
Jetty supports the following variants to be specified as virtual host names:
|
||||||
|
|
||||||
|
`www.hostname.com`::
|
||||||
|
A fully qualified domain name. It is important to list all variants as a site may receive traffic for both `www.hostname.com` and `hostname.com`.
|
||||||
|
|
||||||
|
`*.hostname.com`::
|
||||||
|
A wildcard domain name which will match only one level of arbitrary subdomains.
|
||||||
|
*.foo.com will match www.foo.com and m.foo.com, but not www.other.foo.com.
|
||||||
|
|
||||||
|
`10.0.0.2`::
|
||||||
|
An IP address may be set as a virtual host to indicate that a web application should handle requests received on the network interface with that IP address for protocols that do not indicate a host name such as HTTP/0.9 or HTTP/1.0.
|
||||||
|
|
||||||
|
`@ConnectorName`::
|
||||||
|
A Jetty server `Connector` name to indicate that a web application should handle requests received on the server `Connector` with that name, and therefore received on a specific socket address (either an IP port for `ServerConnector`, or a Unix-Domain path for `UnixDomainServerConnector`).
|
||||||
|
A server `Connector` name can be set via link:{javadoc-url}/org/eclipse/jetty/server/AbstractConnector.html#setName(java.lang.String)[].
|
||||||
|
|
||||||
|
`www.√integral.com`::
|
||||||
|
Non-ASCII and https://en.wikipedia.org/wiki/Internationalized_domain_name[IDN] domain names can be set as virtual hosts using https://en.wikipedia.org/wiki/Punycode[Puny Code] equivalents that may be obtained from a https://www.punycoder.com/[Punycode/IDN converters].
|
||||||
|
For example if the non-ASCII domain name `www.√integral.com` is given to a browser, then the browser will make a request that uses the domain name `www.xn--integral-7g7d.com`, which is the name that should be added as the virtual host name.
|
||||||
|
|
||||||
|
[[virtual-hosts-config]]
|
||||||
|
=== Virtual Hosts Configuration
|
||||||
|
|
||||||
|
If you have a web application `mywebapp.war` you can configure its virtual hosts in this way:
|
||||||
|
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
|
||||||
|
<Set name="contextPath">/mywebapp</Set>
|
||||||
|
<Set name="war">/opt/webapps/mywebapp.war</Set>
|
||||||
|
<Set name="virtualHosts">
|
||||||
|
<Array type="java.lang.String">
|
||||||
|
<Item>mywebapp.com</Item>
|
||||||
|
<Item>www.mywebapp.com</Item>
|
||||||
|
<Item>mywebapp.net</Item>
|
||||||
|
<Item>www.mywebapp.net</Item>
|
||||||
|
</Array>
|
||||||
|
</Set>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
Your web application will be available at:
|
||||||
|
|
||||||
|
* `+http://mywebapp.com/mywebapp+`
|
||||||
|
* `+http://www.mywebapp.com/mywebapp+`
|
||||||
|
* `+http://mywebapp.net/mywebapp+`
|
||||||
|
* `+http://www.mywebapp.net/mywebapp+`
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
You configured the `contextPath` of your web application to `/mywebapp`.
|
||||||
|
|
||||||
|
As such, a request to `+http://mywebapp.com/other+` will not match your web application because the `contextPath` does not match.
|
||||||
|
|
||||||
|
Likewise, a request to `+http://other.com/mywebapp+` will not match your web application because the virtual host does not match.
|
||||||
|
====
|
||||||
|
|
||||||
|
[[virtual-hosts-same-context]]
|
||||||
|
=== Same Context Path, Different Virtual Hosts
|
||||||
|
|
||||||
|
If you want to deploy different web applications to the same context path, typically the root context path `/`, you must use virtual hosts to differentiate among web applications.
|
||||||
|
|
||||||
|
You have `domain.war` that you want to deploy at `+http://domain.biz/+` and `hobby.war` that you want to deploy at `+http://hobby.net+`.
|
||||||
|
|
||||||
|
To achieve this, you simply use the same context path of `/` for each of your webapps, while specifying different virtual hosts for each of your webapps:
|
||||||
|
|
||||||
|
.domain.xml
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
|
||||||
|
<Set name="contextPath">/</Set>
|
||||||
|
<Set name="war">/opt/webapps/domain.war</Set>
|
||||||
|
<Set name="virtualHosts">
|
||||||
|
<Array type="java.lang.String">
|
||||||
|
<Item>domain.biz</Item>
|
||||||
|
</Array>
|
||||||
|
</Set>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
.hobby.xml
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
|
||||||
|
<Set name="contextPath">/</Set>
|
||||||
|
<Set name="war">/opt/webapps/hobby.war</Set>
|
||||||
|
<Set name="virtualHosts">
|
||||||
|
<Array type="java.lang.String">
|
||||||
|
<Item>hobby.net</Item>
|
||||||
|
</Array>
|
||||||
|
</Set>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
[[virtual-hosts-port]]
|
||||||
|
=== Different Port, Different Web Application
|
||||||
|
|
||||||
|
Sometimes it is required to serve different web applications from different socket addresses (either different IP ports, or different Unix-Domain paths), and therefore from different server ``Connector``s.
|
||||||
|
|
||||||
|
For example, you want requests to `+http://localhost:8080/+` to be served by one web application, but requests to `+http://localhost:9090/+` to be served by another web application.
|
||||||
|
|
||||||
|
This configuration may be useful when Jetty sits behind a load balancer.
|
||||||
|
|
||||||
|
In this case, you want to xref:protocols/index.adoc[configure multiple connectors], each with a different name, and then reference the connector name in the web application virtual host configuration:
|
||||||
|
|
||||||
|
.domain.xml
|
||||||
|
[,xml,highlight=10]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
|
||||||
|
<Set name="contextPath">/</Set>
|
||||||
|
<Set name="war">/opt/webapps/domain.war</Set>
|
||||||
|
<Set name="virtualHosts">
|
||||||
|
<Array type="java.lang.String">
|
||||||
|
<Item>@port8080</Item>
|
||||||
|
</Array>
|
||||||
|
</Set>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
.hobby.xml
|
||||||
|
[,xml,highlight=10]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
|
||||||
|
<Set name="contextPath">/</Set>
|
||||||
|
<Set name="war">/opt/webapps/hobby.war</Set>
|
||||||
|
<Set name="virtualHosts">
|
||||||
|
<Array type="java.lang.String">
|
||||||
|
<Item>@port9090</Item>
|
||||||
|
</Array>
|
||||||
|
</Set>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
Web application `domain.war` has a virtual host of `@port8080`, where `port8080` is the name of a Jetty connector.
|
||||||
|
|
||||||
|
Likewise, web application `hobby.war` has a virtual host of `@port9090`, where `port9090` is the name of another Jetty connector.
|
||||||
|
|
||||||
|
See xref:protocols/index.adoc[this section] for further information about how to configure connectors.
|
||||||
|
====
|
||||||
|
|
||||||
|
[[extract-war]]
|
||||||
|
== Configuring `+*.war+` File Extraction
|
||||||
|
|
||||||
|
By default, `+*.war+` files are uncompressed and its content extracted in a temporary directory.
|
||||||
|
// TODO: reference the `work` module and how it works, perhaps in a section about the `deploy` module?
|
||||||
|
The web application resources are served by Jetty from the files extracted in the temporary directory, not from the files within the `+*.war+` file, for performance reasons.
|
||||||
|
|
||||||
|
If you do not want Jetty to extract the `+*.war+` files, you can disable this feature, for example:
|
||||||
|
|
||||||
|
.mywebapp.xml
|
||||||
|
[,xml,highlight=8]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
|
||||||
|
<Set name="contextPath">/mywebapp</Set>
|
||||||
|
<Set name="war">/opt/webapps/mywebapp.war</Set>
|
||||||
|
<Set name="extractWAR">false</Set>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
[[jetty-override-web-xml]]
|
||||||
|
== Overriding `web.xml`
|
||||||
|
|
||||||
|
You can configure an additional `web.xml` that complements the `web.xml` file that is present in the web application `+*.war+` file.
|
||||||
|
This additional `web.xml` is processed _after_ the `+*.war+` file `web.xml`.
|
||||||
|
This allows you to add host specific configuration or server specific configuration without having to extract the web application `web.xml`, modify it, and repackage it in the `+*.war+` file.
|
||||||
|
|
||||||
|
.mywebapp.xml
|
||||||
|
[,xml,highlight=8]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
|
||||||
|
<Set name="contextPath">/mywebapp</Set>
|
||||||
|
<Set name="war">/opt/webapps/mywebapp.war</Set>
|
||||||
|
<Set name="overrideDescriptor">/opt/webapps/mywebapp-web.xml</Set>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
The format of the additional `web.xml` is exactly the same as a standard `web.xml` file, for example:
|
||||||
|
|
||||||
|
.mywebapp-web.xml
|
||||||
|
[,xml,linenums,highlight=10-11]
|
||||||
|
----
|
||||||
|
<?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_4_0.xsd"
|
||||||
|
version="4.0">
|
||||||
|
<servlet>
|
||||||
|
<servlet-name>my-servlet</servlet-name>
|
||||||
|
<init-param>
|
||||||
|
<param-name>host</param-name>
|
||||||
|
<param-value>192.168.0.13</param-value>
|
||||||
|
</init-param>
|
||||||
|
</servlet>
|
||||||
|
</web-app>
|
||||||
|
----
|
||||||
|
|
||||||
|
In the example above, you configured the `my-servlet` Servlet (defined in the web application `web.xml`), adding a host specific `init-param` with the IP address of the host.
|
||||||
|
|
||||||
|
|
||||||
|
// TODO: move this section to its own file
|
||||||
|
// TODO: configuring from the Jetty context XML file happens before web.xml
|
||||||
|
// What about jetty-web.xml? Can this be specified externally, e.g. WebAppContext.setJettyWebXml() ?
|
||||||
|
[[init-params]]
|
||||||
|
== Configuring ``init-param``s
|
||||||
|
|
||||||
|
TODO
|
|
@ -0,0 +1,36 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Eclipse Jetty Features
|
||||||
|
|
||||||
|
If you know Eclipse Jetty already, jump to a feature:
|
||||||
|
|
||||||
|
Protocols::
|
||||||
|
* xref:protocols/index.adoc#http[HTTP/1.1 Support]
|
||||||
|
* xref:protocols/index.adoc#http2[HTTP/2 Support]
|
||||||
|
* xref:protocols/index.adoc#http3[HTTP/3 Support]
|
||||||
|
* xref:protocols/index.adoc#websocket[WebSocket Support]
|
||||||
|
|
||||||
|
Technologies::
|
||||||
|
* xref:annotations/index.adoc[Servlet Annotations]
|
||||||
|
* xref:jaas/index.adoc[JAAS]
|
||||||
|
* xref:jndi/index.adoc[JNDI]
|
||||||
|
* xref:jsp/index.adoc[JSP]
|
||||||
|
* xref:jmx/index.adoc[JMX Monitoring & Management]
|
||||||
|
|
||||||
|
Clustering::
|
||||||
|
* xref:session/index.adoc[HTTP Session Caching and Clustering]
|
||||||
|
|
||||||
|
Performance::
|
||||||
|
* xref:server/index.adoc#threadpool-virtual[Virtual Threads]
|
||||||
|
* xref:quickstart/index.adoc[Faster Web Application Deployment]
|
|
@ -0,0 +1,24 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Eclipse Jetty How-Tos
|
||||||
|
|
||||||
|
* xref:protocols/index.adoc#http[Configure Clear-Text HTTP/1.1]
|
||||||
|
* xref:protocols/index.adoc#https[Configure Secure HTTP/1.1 (https)]
|
||||||
|
* xref:protocols/index.adoc#http2c[Configure Clear-Text HTTP/2]
|
||||||
|
* xref:protocols/index.adoc#http2s[Configure Secure HTTP/2]
|
||||||
|
* xref:protocols/index.adoc#http3[Configure HTTP/3]
|
||||||
|
* xref:protocols/index.adoc#proxy[Configure Jetty Behind a Load Balancer or Reverse Proxy]
|
||||||
|
* xref:server/index.adoc#logging[Configure Jetty Logging]
|
||||||
|
* xref:server/index.adoc#threadpool[Configure Jetty Thread Pool and Virtual Threads]
|
||||||
|
* xref:troubleshooting/index.adoc[Troubleshooting]
|
|
@ -0,0 +1,17 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
[reftext=Operations Guide]
|
||||||
|
= Jetty {page-version} Operations Guide
|
||||||
|
|
||||||
|
The Eclipse Jetty Operations Guide targets sysops, devops, and developers who want to install Eclipse Jetty as a standalone server to deploy web applications.
|
|
@ -0,0 +1,333 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= JAAS
|
||||||
|
|
||||||
|
JAAS implements a Java version of the standard Pluggable Authentication Module (PAM) framework.
|
||||||
|
|
||||||
|
JAAS can be used for two purposes:
|
||||||
|
|
||||||
|
* for authentication of users, to reliably and securely determine who is currently executing Java code, regardless of whether the code is running as an application, an applet, a bean, or a servlet
|
||||||
|
* for authorization of users to ensure they have the access control rights (permissions) required to do the actions performed
|
||||||
|
|
||||||
|
JAAS authentication is performed in a pluggable fashion.
|
||||||
|
This permits applications to remain independent from underlying authentication technologies.
|
||||||
|
New or updated authentication technologies can be plugged under an application without requiring modifications to the application itself.
|
||||||
|
|
||||||
|
See Java Authentication and Authorization Service (JAAS) http://java.sun.com/javase/7/docs/technotes/guides/security/jaas/JAASRefGuide.html[Reference Guide] for more information about JAAS.
|
||||||
|
|
||||||
|
The Jetty JAAS support aims to dictate as little as possible whilst providing a sufficiently flexible infrastructure to allow users to drop either one of the <<loginmodules,JAAS Login Modules that ships with Jetty>>, or their
|
||||||
|
own custom https://docs.oracle.com/javase/7/docs/api/javax/security/auth/spi/LoginModule.html[LoginModule]s.
|
||||||
|
|
||||||
|
[[configuration]]
|
||||||
|
== Configuration
|
||||||
|
|
||||||
|
[[module]]
|
||||||
|
=== The `jaas` module
|
||||||
|
|
||||||
|
Enable the `jaas` module:
|
||||||
|
|
||||||
|
----
|
||||||
|
include::{jetty-home}/modules/jaas.mod[]
|
||||||
|
----
|
||||||
|
|
||||||
|
The configurable items in the resulting `$jetty.base/start.d/jaas.ini` file are:
|
||||||
|
|
||||||
|
jetty.jaas.login.conf::
|
||||||
|
This is the location of the file that will be referenced by the system property `java.security.auth.login.config`: Jetty sets this system property for you based on the value of this property.
|
||||||
|
The value of this property is assumed to be _relative to ``$JETTY_BASE``_.
|
||||||
|
The default value is `etc/login.conf`, which resolves to `$JETTY_BASE/etc/login.conf`.
|
||||||
|
If you don't want to put your login module configuration file here, you can change this property to point to where it is.
|
||||||
|
|
||||||
|
See more about the contents of this file in the <<loginconf,Configuring JAAS>> section.
|
||||||
|
|
||||||
|
[[webapp]]
|
||||||
|
=== Configure the webapp for JAAS
|
||||||
|
|
||||||
|
The `<realm-name>` in `web.xml` will be used to identify the `org.eclipse.jetty.jaas.JAASLoginService` declaration that integrates JAAS with Jetty.
|
||||||
|
|
||||||
|
For example, this `web.xml` contains a realm called `Test JAAS Realm`:
|
||||||
|
|
||||||
|
[,xml,subs=verbatim]
|
||||||
|
----
|
||||||
|
<login-config>
|
||||||
|
<auth-method>FORM</auth-method>
|
||||||
|
<realm-name>Test JAAS Realm</realm-name> <!--1-->
|
||||||
|
<form-login-config>
|
||||||
|
<form-login-page>/login/login</form-login-page>
|
||||||
|
<form-error-page>/login/error</form-error-page>
|
||||||
|
</form-login-config>
|
||||||
|
</login-config>
|
||||||
|
----
|
||||||
|
<1> The name of the realm, which must be _identical_ to the name of an `org.eclipse.jetty.jaas.JAASLoginService` declaration.
|
||||||
|
|
||||||
|
We now need to declare an `org.eclipse.jetty.jaas.JAASLoginService` that references the realm name of `Test JAAS Realm`.
|
||||||
|
Here's an example of a suitable XML snippet:
|
||||||
|
|
||||||
|
[,xml,subs=verbatim]
|
||||||
|
----
|
||||||
|
<New class="org.eclipse.jetty.jaas.JAASLoginService">
|
||||||
|
<Set name="Name">Test JAAS Realm</Set> <!--1-->
|
||||||
|
<Set name="LoginModuleName">xyz</Set> <!--2-->
|
||||||
|
</New>
|
||||||
|
----
|
||||||
|
<1> The name is the _same_ as that declared in the `<realm-name>` in `web.xml`.
|
||||||
|
<2> The name that identifies a set of `javax.security.auth.spi.LoginModule` configurations that comprise the <<loginconf,JAAS config file>> identified in the `jetty.jaas.login.conf` property of the <<module,`jaas` module>>.
|
||||||
|
|
||||||
|
The `org.eclipse.jetty.jaas.JAASLoginService` can be declared in a couple of different places, pick whichever suits your purposes best:
|
||||||
|
|
||||||
|
* If you have more than one webapp that you would like to use the same security infrastructure, then you can declare your `org.eclipse.jetty.jaas.JAASLoginService` as a bean that is added to the `org.eclipse.jetty.server.Server`.
|
||||||
|
The file in which you declare this needs to be on Jetty's execution path.
|
||||||
|
The recommended procedure is to create a file in your `$jetty.base/etc` directory and then ensure it is on the classpath either by adding it to the Jetty xref:start/index.adoc[start command line], or more conveniently to a xref:modules/custom.adoc[custom module].
|
||||||
|
+
|
||||||
|
Here's an example of this type of XML file:
|
||||||
|
+
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<?xml version="1.0"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
<Configure id="Server" class="org.eclipse.jetty.server.Server">
|
||||||
|
<Call name="addBean">
|
||||||
|
<Arg>
|
||||||
|
<New class="org.eclipse.jetty.jaas.JAASLoginService">
|
||||||
|
<Set name="name">Test JAAS Realm</Set>
|
||||||
|
<Set name="LoginModuleName">xyz</Set>
|
||||||
|
</New>
|
||||||
|
</Arg>
|
||||||
|
</Call>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
* Alternatively, if you want to use JAAS with a specific webapp only, you declare your `org.eclipse.jetty.jaas.JAASLoginService` in a context XLM file specific to that webapp:
|
||||||
|
+
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<?xml version="1.0"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
|
||||||
|
<Set name="securityHandler">
|
||||||
|
<New class="org.eclipse.jetty.security.ConstraintSecurityHandler">
|
||||||
|
<Set name="loginService">
|
||||||
|
<New class="org.eclipse.jetty.jaas.JAASLoginService">
|
||||||
|
<Set name="name">Test JAAS Realm</Set>
|
||||||
|
<Set name="loginModuleName">xyz</Set>
|
||||||
|
</New>
|
||||||
|
</Set>
|
||||||
|
</New>
|
||||||
|
</Set>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
[[loginconf]]
|
||||||
|
=== Configure JAAS
|
||||||
|
|
||||||
|
We now need to setup the contents of the file we specified as the `jetty.jaas.login.conf` property when we <<module,configured the `jaas` module>>.
|
||||||
|
Refer to the https://docs.oracle.com/javase/7/docs/api/javax/security/auth/login/Configuration.html[syntax rules] of this file for a full description.
|
||||||
|
|
||||||
|
Remembering the example we set up <<webapp,previously>>, the contents of the `$jetty.base/etc/login.conf` file could look as follows:
|
||||||
|
|
||||||
|
[source,subs=verbatim]
|
||||||
|
----
|
||||||
|
xyz { <1>
|
||||||
|
com.acme.SomeLoginModule required debug=true; <2>
|
||||||
|
com.other.OtherLoginModule optional; <3>
|
||||||
|
};
|
||||||
|
----
|
||||||
|
<1> The name of the configuration _exactly_ as specified in your `org.eclipse.jetty.jaas.JAASLoginService` declaration.
|
||||||
|
<2> The first `LoginModule` declaration, containing the classname of the `LoginModule` and its configuration properties.
|
||||||
|
<3> A second `LoginModule` declaration.
|
||||||
|
You can provide as many `LoginModule` alternatives as you like, with a minimum of one.
|
||||||
|
Refer to the https://docs.oracle.com/javase/7/docs/api/javax/security/auth/login/Configuration.html[JAAS documentation] for more information on the standard configuration properties, and how JAAS interprets this file.
|
||||||
|
|
||||||
|
[[loginmodules]]
|
||||||
|
== Provided LoginModules
|
||||||
|
|
||||||
|
* link:{javadoc-url}/org/eclipse/jetty/jaas/spi/JDBCLoginModule.html[`org.eclipse.jetty.jaas.spi.JDBCLoginModule`]
|
||||||
|
* link:{javadoc-url}/org/eclipse/jetty/jaas/spi/PropertyFileLoginModule.html[`org.eclipse.jetty.jaas.spi.PropertyFileLoginModule`]
|
||||||
|
* link:{javadoc-url}/org/eclipse/jetty/jaas/spi/DataSourceLoginModule.html[`org.eclipse.jetty.jaas.spi.DataSourceLoginModule`]
|
||||||
|
* link:{javadoc-url}/org/eclipse/jetty/jaas/spi/LdapLoginModule.html[`org.eclipse.jetty.jaas.ldap.LdapLoginModule`]
|
||||||
|
|
||||||
|
[[og-password]]
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
Passwords can be stored in clear text, obfuscated or checksummed.
|
||||||
|
The class link:{javadoc-url}/org/eclipse/jetty/util/security/Password.html[`org.eclipse.jetty.util.security.Password`] should be used to generate all varieties of passwords,the output from which can be put in to property files or entered into database tables.
|
||||||
|
====
|
||||||
|
|
||||||
|
=== JDBCLoginModule
|
||||||
|
|
||||||
|
The `org.eclipse.jetty.jaas.spi.JDBCLoginModule` stores user passwords and roles in a database accessed via JDBC calls.
|
||||||
|
You can configure the JDBC connection information, as well as the names of the table and columns storing the username and credential, and the names of the table and columns storing the roles.
|
||||||
|
|
||||||
|
Here is an example <<loginconf,login module configuration file>> entry for it using an HSQLDB driver:
|
||||||
|
|
||||||
|
[source,subs=verbatim]
|
||||||
|
----
|
||||||
|
jdbc { <1>
|
||||||
|
org.eclipse.jetty.jaas.spi.JDBCLoginModule required <2><3>
|
||||||
|
dbUrl="jdbc:hsqldb:." <4>
|
||||||
|
dbUserName="sa" <5>
|
||||||
|
dbDriver="org.hsqldb.jdbcDriver" <6>
|
||||||
|
userTable="myusers" <7>
|
||||||
|
userField="myuser" <8>
|
||||||
|
credentialField="mypassword" <9>
|
||||||
|
userRoleTable="myuserroles" <10>
|
||||||
|
userRoleUserField="myuser" <11>
|
||||||
|
userRoleRoleField="myrole"; <12>
|
||||||
|
};
|
||||||
|
----
|
||||||
|
<1> The name of the configuration.
|
||||||
|
<2> The name of the `LoginModule` class.
|
||||||
|
<3> A standard JAAS flag making successful authentication via this `LoginModule` mandatory.
|
||||||
|
<4> The JDBC url used to connect to the database.
|
||||||
|
<5> The name of the JDBC user to use for the connection.
|
||||||
|
<6> The name of the JDBC Driver class.
|
||||||
|
<7> The name of the table holding the user authenication information.
|
||||||
|
<8> The name of the column holding the user name.
|
||||||
|
<9> The name of the column holding the user credential.
|
||||||
|
<10> The name of the table holding the user authorization information.
|
||||||
|
<11> The name of the column holding the user name.
|
||||||
|
<12> The name of the column holding the user role.
|
||||||
|
|
||||||
|
The properties *7*-*12* are used to format the following queries:
|
||||||
|
|
||||||
|
[,sql]
|
||||||
|
----
|
||||||
|
select <credentialField> from <userTable> where <userField>=?
|
||||||
|
select <userRoleRoleField> from <userRoleTable> where <userRoleUserField>=?
|
||||||
|
----
|
||||||
|
|
||||||
|
Credential and role information is lazily read from the database when a previously unauthenticated user requests authentication.
|
||||||
|
Note that this information is _only_ cached for the length of the authenticated session.
|
||||||
|
When the user logs out or the session expires, the information is flushed from memory.
|
||||||
|
|
||||||
|
Note that passwords can be stored in the database in plain text or encoded formats -- see the note on "Passwords/Credentials" above.
|
||||||
|
|
||||||
|
=== DataSourceLoginModule
|
||||||
|
|
||||||
|
Similar to the `org.eclipse.jetty.jaas.spi.JDBCLoginModule`, but using a `javax.sql.DataSource` to connect to the database instead of a JDBC driver.
|
||||||
|
The `javax.sql.DataSource` is obtained at runtime by performing a JNDI lookup on `java:comp/env/$\{dnJNDIName}`.
|
||||||
|
|
||||||
|
A sample login module configuration for this `LoginModule`:
|
||||||
|
|
||||||
|
[source,subs=verbatim]
|
||||||
|
----
|
||||||
|
ds { <1>
|
||||||
|
org.eclipse.jetty.jaas.spi.DataSourceLoginModule required <2><3>
|
||||||
|
dbJNDIName="ds" <4>
|
||||||
|
userTable="myusers" <5>
|
||||||
|
userField="myuser" <6>
|
||||||
|
credentialField="mypassword" <7>
|
||||||
|
userRoleTable="myuserroles" <8>
|
||||||
|
userRoleUserField="myuser" <9>
|
||||||
|
userRoleRoleField="myrole"; <10>
|
||||||
|
};
|
||||||
|
----
|
||||||
|
<1> The name of the configuration.
|
||||||
|
<2> The name of the `LoginModule` class.
|
||||||
|
<3> A standard JAAS flag making successful authentication via this `LoginModule` mandatory.
|
||||||
|
<4> The JNDI name, relative to `java:comp/env/` to lookup to obtain the `javax.sql.DataSource`.
|
||||||
|
<5> The name of the table holding the user authenication information.
|
||||||
|
<6> The name of the column holding the user name.
|
||||||
|
<7> The name of the column holding the user credential.
|
||||||
|
<8> The name of the table holding the user authorization information.
|
||||||
|
<9> The name of the column holding the user name.
|
||||||
|
<10> The name of the column holding the user role.
|
||||||
|
|
||||||
|
=== PropertyFileLoginModule
|
||||||
|
|
||||||
|
With this login module implementation, the authentication and role information is read from a property file.
|
||||||
|
|
||||||
|
[source,subs=verbatim]
|
||||||
|
----
|
||||||
|
props { <1>
|
||||||
|
org.eclipse.jetty.jaas.spi.PropertyFileLoginModule required <2><3>
|
||||||
|
file="/somewhere/somefile.props"; <4>
|
||||||
|
};
|
||||||
|
----
|
||||||
|
<1> The name of the configuration.
|
||||||
|
<2> The name of the `LoginModule` class.
|
||||||
|
<3> A standard JAAS flag making successful authentication via this `LoginModule` mandatory.
|
||||||
|
<4> The location of a properties file containing the authentication and authorization information.
|
||||||
|
|
||||||
|
The property file must be of the format:
|
||||||
|
|
||||||
|
[,text,subs=verbatim]
|
||||||
|
----
|
||||||
|
<username>: <password> [,<rolename> ...]
|
||||||
|
----
|
||||||
|
|
||||||
|
Here's an example:
|
||||||
|
|
||||||
|
----
|
||||||
|
fred: OBF:1xmk1w261u9r1w1c1xmq,user,admin
|
||||||
|
harry: changeme,user,developer
|
||||||
|
tom: MD5:164c88b302622e17050af52c89945d44,user
|
||||||
|
dick: CRYPT:adpexzg3FUZAk,admin
|
||||||
|
----
|
||||||
|
|
||||||
|
The contents of the file are fully read in and cached in memory the first time a user requests authentication.
|
||||||
|
|
||||||
|
=== LdapLoginModule
|
||||||
|
|
||||||
|
The `org.eclipse.jetty.jaas.spi.LdapLoginModule` uses LDAP to access authentication and authorization information stored in a directory.
|
||||||
|
The LDAP connection information and structure of the authentication/authorization data can be configured.
|
||||||
|
|
||||||
|
Here's an example:
|
||||||
|
|
||||||
|
[source,subs=verbatim]
|
||||||
|
----
|
||||||
|
example { <1>
|
||||||
|
org.eclipse.jetty.jaas.spi.LdapLoginModule required <2><3>
|
||||||
|
contextFactory="com.sun.jndi.ldap.LdapCtxFactory" <4>
|
||||||
|
hostname="ldap.example.com" <5>
|
||||||
|
port="389" <6>
|
||||||
|
bindDn="cn=Directory Manager" <7>
|
||||||
|
bindPassword="directory" <8>
|
||||||
|
authenticationMethod="simple" <9>
|
||||||
|
useLdaps="true" <10>
|
||||||
|
userBaseDn="ou=people,dc=alcatel" <11>
|
||||||
|
userRdnAttribute="uid" <12>
|
||||||
|
userIdAttribute="cn" <13>
|
||||||
|
userPasswordAttribute="userPassword" <14>
|
||||||
|
userObjectClass="inetOrgPerson" <15>
|
||||||
|
roleBaseDn="ou=groups,dc=example,dc=com" <16>
|
||||||
|
roleNameAttribute="cn" <17>
|
||||||
|
roleMemberAttribute="uniqueMember" <18>
|
||||||
|
roleObjectClass="groupOfUniqueNames"; <19>
|
||||||
|
forceBindingLogin="false" <20>
|
||||||
|
debug="false" <21>
|
||||||
|
};
|
||||||
|
----
|
||||||
|
<1> The name of the configuration.
|
||||||
|
<2> The name of the `LoginModule` class.
|
||||||
|
<3> A standard JAAS flag making successful authentication via this `LoginModule` mandatory.
|
||||||
|
<4> The name of the context factory to use for the LDAP connection.
|
||||||
|
<5> The hostname for the LDAP connection. Optional.
|
||||||
|
<6> The port for the LDAP connection. Optional.
|
||||||
|
<7> The caller security Principal. Optional.
|
||||||
|
<8> The caller security credential. Optional.
|
||||||
|
<9> The security level for the LDAP connection environment. Optional.
|
||||||
|
<10> If true, use `ldaps` instead of `ldap` for the connection url.
|
||||||
|
<11> The distinguished name of the directory to search for user information.
|
||||||
|
<12> The name of the attribute for the user roles.
|
||||||
|
<13> The name of the attribute for the user id.
|
||||||
|
<14> The name of the attribute for the user password.
|
||||||
|
<15> The `ObjectClass` for users.
|
||||||
|
<16> The distinguished name of the directory to search for role information.
|
||||||
|
<17> The name of the attribute for roles.
|
||||||
|
<18> The name of the attribute storing the user for the roles `ObjectClass`.
|
||||||
|
<19> The name of the `ObjectClass` for roles.
|
||||||
|
<20> If true, the authentication proceeds on the basis of a successful LDAP binding using the username and credential provided by the user.
|
||||||
|
If false, then authentication proceeds based on username and password information retrieved from LDAP.
|
||||||
|
<21> If true, failed login attempts are logged on the server.
|
|
@ -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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= JASPI
|
||||||
|
|
||||||
|
Enabling this module allows Jetty to utilize authentication modules that implement the JSR 196 (JASPI) specification. JASPI provides an SPI (Service Provider Interface) for pluggable, portable, and standardized authentication modules. Compatible modules are portable between servers that support the JASPI specification. This module provides a bridge from Java Authentication to the Jetty Security framework.
|
||||||
|
|
||||||
|
Only modules conforming to the "Servlet Container Profile" with the ServerAuthModule interface within the https://www.jcp.org/en/jsr/detail?id=196[JASPI Spec] are supported. These modules must be configured before start-up. Operations for runtime registering or de-registering authentication modules are not supported.
|
||||||
|
|
||||||
|
[[configuration]]
|
||||||
|
== Configuration
|
||||||
|
|
||||||
|
[[module]]
|
||||||
|
=== The `jaspi` module
|
||||||
|
|
||||||
|
Enable the `jaspi` module:
|
||||||
|
|
||||||
|
----
|
||||||
|
include::{jetty-home}/modules/jaspi.mod[]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[xml]]
|
||||||
|
=== Configure JASPI
|
||||||
|
|
||||||
|
To enable the `jaspi` module you can use the following command (issued from within the `$JETTY_BASE` directory):
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --add-modules=jaspi
|
||||||
|
----
|
||||||
|
|
||||||
|
You can then register a `AuthConfigProvider` onto the static `AuthConfigFactory` obtained with `AuthConfigFactory.getFactory()`. This registration can be done in the XML configuration file which will be copied to `$JETTY_BASE/etc/jaspi/jaspi-authmoduleconfig.xml` when the module is enabled.
|
||||||
|
|
||||||
|
==== JASPI Demo
|
||||||
|
The `jaspi-demo` module illustrates setting up HTTP Basic Authentication using a Java Authentication module that comes packaged with jetty: `org.eclipse.jetty.security.jaspi.modules.BasicAuthenticationAuthModule`, and applies it for a context named `/test`.
|
||||||
|
|
||||||
|
[, xml]
|
||||||
|
----
|
||||||
|
include::{jetty-home}/etc/jaspi/jaspi-demo.xml[]
|
||||||
|
----
|
||||||
|
|
||||||
|
This example uses the `AuthConfigProvider` implementation provided by Jetty to register a `ServerAuthModule` directly. Other custom or 3rd party modules that are compatible with the `ServerAuthModule` interface in JASPI can be registered in the same way.
|
||||||
|
|
||||||
|
=== Integration with Jetty Authentication Mechanisms
|
||||||
|
|
||||||
|
To integrate with Jetty authentication mechanisms you must add a `LoginService` to your context. The `LoginService` provides a way for you to obtain a `UserIdentity` from a username and credentials. JASPI can interact with this Jetty `LoginService` by using the `PasswordValidationCallback`.
|
||||||
|
|
||||||
|
The `CallerPrincipalCallback` and `GroupPrincipalCallback` do not require use of a Jetty `LoginService`. The principal from the `CallerPrincipalCallback` will be used directly with the `IdentityService` to produce a `UserIdentity`.
|
||||||
|
|
||||||
|
=== Replacing the Jetty DefaultAuthConfigFactory
|
||||||
|
|
||||||
|
Jetty provides an implementation of the `AuthConfigFactory` interface which is used to register `AuthConfigProviders`. This can be replaced by a custom implementation by adding a custom module which provides `auth-config-factory`.
|
||||||
|
This custom module must reference an XML file which sets a new instance of the `AuthConfigFactory` with the static method `AuthConfigFactory.setFactory()`.
|
||||||
|
For an example of this see the `jaspi-default-auth-config-factory` module, which provides the default implementation used by Jetty.
|
||||||
|
|
||||||
|
----
|
||||||
|
include::{jetty-home}/modules/jaspi-default-auth-config-factory.mod[]
|
||||||
|
----
|
|
@ -0,0 +1,281 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= JMX Monitoring & Management
|
||||||
|
|
||||||
|
Monitoring and management of a Jetty server is important because it allows you to monitor the status of the server (_"Is the server processing requests?"_) and to manage -- i.e. read and possibly change -- its configuration.
|
||||||
|
|
||||||
|
The ability to read and change the Jetty configuration is very important for troubleshooting Jetty -- please refer to the xref:troubleshooting/index.adoc[troubleshooting section] for more information.
|
||||||
|
|
||||||
|
Jetty relies on the Java Management Extensions (JMX) APIs included in OpenJDK to provide monitoring and management.
|
||||||
|
|
||||||
|
The JMX APIs support a JVM-local `MBeanServer`, accessible only from within the JVM itself (or by tools that can _attach_ to a running JVM), and a way to expose the `MBeanServer` to remote clients via Java's RMI (Remote Method Invocation).
|
||||||
|
|
||||||
|
[[local]]
|
||||||
|
== Enabling Local JMX Support
|
||||||
|
|
||||||
|
As with many other Jetty features, local JMX support is enabled with the `jmx` Jetty module:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --add-module=jmx
|
||||||
|
----
|
||||||
|
|
||||||
|
With the `jmx` Jetty module enabled, Jetty components will be exported as JMX _MBeans_ to the JVM platform `MBeanServer`, so that they can be accessed by JMX compliant tools.
|
||||||
|
|
||||||
|
Each Jetty component will export to its correspondent MBean relevant configuration parameters, so that a JMX tool can read and possibly change the component configuration through the MBean.
|
||||||
|
|
||||||
|
Note that the Jetty MBeans are registered into the platform `MBeanServer`, but are not available to remote clients: they are _local_ to the JVM.
|
||||||
|
|
||||||
|
This configuration is useful when you develop and test your Jetty server locally.
|
||||||
|
|
||||||
|
JMX compliant tools such as https://adoptium.net/jmc.html[Java Mission Control (JMC)] can be started locally on your machine and can attach to other JVMs running on your machine, showing you the registered MBeans among which you will find the Jetty MBeans.
|
||||||
|
|
||||||
|
NOTE: Enabling only the local JMX support is the most secure option for monitoring and management, but only users that have local access to the JVM will be able to browse the MBeans.
|
||||||
|
If you need to access the MBeans from a remote machine, read <<remote,this section>>.
|
||||||
|
|
||||||
|
[[remote]]
|
||||||
|
== Enabling Remote JMX Support
|
||||||
|
|
||||||
|
There are two ways to configure a Jetty server so that it is possible to access the JVM platform MBeans from remote clients:
|
||||||
|
|
||||||
|
* Use the `com.sun.management.jmxremote` and related system properties when starting Jetty.
|
||||||
|
Unfortunately, this solution does not work well with firewalls, and will not be discussed further.
|
||||||
|
* Use the `jmx-remote` Jetty module.
|
||||||
|
|
||||||
|
Both ways use Java's Remote Method Invocation (RMI) to communicate between the client and the server.
|
||||||
|
|
||||||
|
[IMPORTANT]
|
||||||
|
.Refresher: How RMI Works
|
||||||
|
====
|
||||||
|
A server application that wants to make an object available to remote clients must _export_ the object.
|
||||||
|
|
||||||
|
Exporting an object creates an RMI _stub_ that contains the host/port of the RMI _server_ that accepts incoming invocations from clients and forwards them to the object.
|
||||||
|
During the creation of the RMI stub, the host stored in the RMI stub is retrieved from the local name resolution system (for example, in Linux, from `/etc/hosts`).
|
||||||
|
|
||||||
|
The RMI stub is then sent, along with a name that uniquely identifies the object, to the RMI _registry_.
|
||||||
|
The RMI registry is a service that maps names to RMI stubs; it may be external to both clients and server, although often it is part of the server JVM.
|
||||||
|
|
||||||
|
When a client application wants to connect to the server object using RMI, it first connects to the RMI registry to download the RMI stub for the RMI server; recall that the RMI stub contains the host/port to connect to the RMI server.
|
||||||
|
Then, the client uses the RMI stub to connect to the RMI server, typically to a host/port that may be different from the RMI registry host/port (in particular, by default the RMI server port will be different from the RMI registry port).
|
||||||
|
====
|
||||||
|
|
||||||
|
Remote access to the platform MBeans, and therefore the Jetty MBeans, is enabled by the `jmx-remote` Jetty module:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --add-module=jmx-remote
|
||||||
|
----
|
||||||
|
|
||||||
|
This command creates the `jmx-remote.ini` file:
|
||||||
|
|
||||||
|
[source,subs=quotes]
|
||||||
|
----
|
||||||
|
JETTY_BASE
|
||||||
|
└── start.d
|
||||||
|
└── #jmx-remote.ini#
|
||||||
|
----
|
||||||
|
|
||||||
|
Enabling the `jmx-remote` module transitively enables the <<local,`jmx` module>> as well.
|
||||||
|
|
||||||
|
The configuration for the RMI registry and the RMI server is specified by a `JMXServiceURL`.
|
||||||
|
The string format of an RMI `JMXServiceURL` is the following:
|
||||||
|
|
||||||
|
----
|
||||||
|
service:jmx:rmi://<rmi_server_host>:<rmi_server_port>/jndi/rmi://<rmi_registry_host>:<rmi_registry_port>/jmxrmi
|
||||||
|
----
|
||||||
|
|
||||||
|
Below you can find examples of ``JMXServiceURL``s:
|
||||||
|
|
||||||
|
[source,subs=quotes]
|
||||||
|
----
|
||||||
|
*service:jmx:rmi:///jndi/rmi:///jmxrmi*
|
||||||
|
where:
|
||||||
|
rmi_server_host = local host address
|
||||||
|
rmi_server_port = randomly chosen
|
||||||
|
rmi_registry_host = local host address
|
||||||
|
rmi_registry_port = 1099
|
||||||
|
|
||||||
|
*service:jmx:rmi://0.0.0.0:1099/jndi/rmi://0.0.0.0:1099/jmxrmi*
|
||||||
|
where:
|
||||||
|
rmi_server_host = any address
|
||||||
|
rmi_server_port = 1099
|
||||||
|
rmi_registry_host = any address
|
||||||
|
rmi_registry_port = 1099
|
||||||
|
|
||||||
|
*service:jmx:rmi://localhost:1100/jndi/rmi://localhost:1099/jmxrmi*
|
||||||
|
where:
|
||||||
|
rmi_server_host = loopback address
|
||||||
|
rmi_server_port = 1100
|
||||||
|
rmi_registry_host = loopback address
|
||||||
|
rmi_registry_port = 1099
|
||||||
|
----
|
||||||
|
|
||||||
|
The default `JMXServiceURL` configured by the `jmx-remote` module is the following:
|
||||||
|
|
||||||
|
----
|
||||||
|
service:jmx:rmi://localhost:1099/jndi/rmi://localhost:1099/jmxrmi
|
||||||
|
----
|
||||||
|
|
||||||
|
With the default configuration, only clients that are local to the server machine can connect to the RMI registry and RMI server - this is done for security reasons.
|
||||||
|
However, even with this local-only configuration, it would still be possible to access the MBeans from remote using an SSH tunnel, as explained in <<remote-ssh-tunnel,this section>>.
|
||||||
|
|
||||||
|
By specifying an appropriate `JMXServiceURL`, you can fine tune the network address the RMI registry and the RMI server bind to, and the ports that the RMI registry and the RMI server listen to.
|
||||||
|
The RMI server and RMI registry hosts and ports can be the same (as in the default configuration) because RMI is able to multiplex traffic arriving to one port to multiple RMI objects.
|
||||||
|
|
||||||
|
If you need to allow JMX remote access through a firewall, you must open both the RMI registry and the RMI server ports.
|
||||||
|
The default configuration simplifies the firewall configuration because you only need to open port `1099`.
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
When Jetty is started with the `jmx-remote` module enabled, the RMI stub of the Jetty component that provides access to the MBeans is exported to the RMI registry.
|
||||||
|
|
||||||
|
The RMI stub contains the host/port to connect to the RMI server, but the host is typically the machine host name, not the host specified in the `JMXServiceURL` (the latter is only used to specify the network address the RMI server binds to).
|
||||||
|
|
||||||
|
To control the host stored in the RMI stub you need to set the system property `java.rmi.server.hostname` with the desired value in the module configuration file, `jmx-remote.ini`.
|
||||||
|
====
|
||||||
|
|
||||||
|
IMPORTANT: If your client cannot connect to the server, the most common cause is a mismatch between the RMI server host of the `JMXServiceURL` and the RMI server host of the RMI stub.
|
||||||
|
|
||||||
|
You can customize the RMI server host/port, the RMI registry host/port and the system property `java.rmi.server.hostname` by editing the `jmx-remote.ini` configuration file.
|
||||||
|
Further information about the `jmx-remote` module configuration can be found xref:modules/standard.adoc#jmx-remote[here].
|
||||||
|
|
||||||
|
[[remote-ssh-tunnel]]
|
||||||
|
=== Remote JMX Access with Port Forwarding via SSH Tunnel
|
||||||
|
|
||||||
|
You can access JMX MBeans on a remote machine when the RMI ports are not open, for example because of firewall policies, but you have SSH access to the machine, using local port forwarding via an SSH tunnel.
|
||||||
|
|
||||||
|
In this case you want to configure the `JMXServiceURL` that binds the RMI server and the RMI registry to the loopback interface only and to the same port:
|
||||||
|
|
||||||
|
----
|
||||||
|
service:jmx:rmi://localhost:1099/jndi/rmi://localhost:1099/jmxrmi
|
||||||
|
----
|
||||||
|
|
||||||
|
You must set the system property `-Djava.rmi.server.hostname=localhost` so that the RMI stub contains `localhost` as the host name to connect to.
|
||||||
|
This is, incidentally, the default configuration of the `jmx-remote` module.
|
||||||
|
|
||||||
|
Then you set up the local port forwarding with the SSH tunnel:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ ssh -L 1099:localhost:1099 <user>@<machine_host>
|
||||||
|
----
|
||||||
|
|
||||||
|
Thanks to the local port forwarding of the SSH tunnel, when the client connects to `localhost:1099` on your local computer, the traffic will be forwarded to `machine_host` and when there, the SSH daemon will forward the traffic to `localhost:1099` on `machine_host`, which is exactly where the RMI server and the RMI registry listens to.
|
||||||
|
|
||||||
|
The client first contacts the RMI registry, so it connects to `localhost:1099` on your local computer; the traffic is forwarded to `machine_host` through the SSH tunnel, connects to the RMI registry and the RMI stub is downloaded to the client.
|
||||||
|
|
||||||
|
Then the client uses the RMI stub to connect to the RMI server. The RMI stub contains `localhost` as the RMI server host because that is what you have configured with the system property `java.rmi.server.hostname`.
|
||||||
|
|
||||||
|
The client will connect again to `localhost:1099` on your local computer, this time to contact the RMI server; the traffic is forwarded to `machine_host` through the SSH tunnel, arrives to `machine_host` and connects to the RMI server.
|
||||||
|
|
||||||
|
[[remote-auth]]
|
||||||
|
=== Remote JMX Access Authentication & Authorization
|
||||||
|
|
||||||
|
The standard `javax.management.remote.JMXConnectorServer` class, used by the `jmx-remote` module to provide remote JMX access to Jetty MBeans, provides several options to authenticate and authorize users.
|
||||||
|
For a complete guide to controlling authentication and authorization in JMX, see https://docs.oracle.com/en/java/javase/11/management/monitoring-and-management-using-jmx-technology.html[the official JMX documentation].
|
||||||
|
|
||||||
|
The simplest way to control JMX authentication and authorization is to specify two files: one contains username and password pairs, and the other contains username and permission pairs.
|
||||||
|
|
||||||
|
This is achieved by enabling the `jmx-remote-auth` Jetty module:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --add-module=jmx-remote-auth
|
||||||
|
----
|
||||||
|
|
||||||
|
Enabling the `jmx-remote-auth` Jetty module creates the following files:
|
||||||
|
|
||||||
|
----
|
||||||
|
$JETTY_BASE
|
||||||
|
├── etc
|
||||||
|
│ ├── jmxremote.access
|
||||||
|
│ ├── jmxremote.password
|
||||||
|
│ └── jmx-remote-auth.xml
|
||||||
|
└── start.d
|
||||||
|
├── jmx-remote-auth.ini
|
||||||
|
└── jmx-remote.ini
|
||||||
|
----
|
||||||
|
|
||||||
|
Then you edit the `$JETTY_BASE/etc/jmxremote.password` file, adding the username/password pairs that you need:
|
||||||
|
|
||||||
|
.$JETTY_BASE/etc/jmxremote.password
|
||||||
|
----
|
||||||
|
# The file format is: <username> <password>
|
||||||
|
alice wonderland
|
||||||
|
bob marley
|
||||||
|
----
|
||||||
|
|
||||||
|
You must also edit the `$JETTY_BASE/etc/jmxremote.access` file to give permissions to your users:
|
||||||
|
|
||||||
|
.$JETTY_BASE/etc/jmxremote.access
|
||||||
|
----
|
||||||
|
# The file format is: <username> <readonly|readwrite>
|
||||||
|
alice readwrite
|
||||||
|
bob readonly
|
||||||
|
----
|
||||||
|
|
||||||
|
The above files define user `alice` with password `wonderland` to have `readwrite` access, and user `bob` with password `marley` to have `readonly` access.
|
||||||
|
|
||||||
|
[[remote-secure]]
|
||||||
|
=== Securing Remote JMX Access with TLS
|
||||||
|
|
||||||
|
The JMX communication via RMI happens by default in clear-text, but it is possible to secure the JMX communication via RMI with TLS.
|
||||||
|
|
||||||
|
If you want to reuse the configuration that you are using for the xref:protocols/index.adoc#https[`https` module], you can just enable the `jmx-remote-ssl.xml` Jetty module:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --add-module=jmx-remote-ssl
|
||||||
|
----
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
The `jmx-remote-ssl` Jetty module depends on the `ssl` Jetty module that in turn requires a KeyStore (read xref:protocols/index.adoc#ssl[this section] for more information).
|
||||||
|
====
|
||||||
|
|
||||||
|
The KeyStore must contain a valid certificate signed by a Certification Authority.
|
||||||
|
Having certificates signed by a Certification Authority simplifies by a lot the configuration needed to get the RMI communication over TLS working properly.
|
||||||
|
|
||||||
|
The RMI mechanic is the usual one: the RMI client (typically a monitoring console) will connect first to the RMI registry (using TLS), download the RMI stub that contains the address and port of the RMI server to connect to, then connect to the RMI server (using TLS).
|
||||||
|
|
||||||
|
This also mean that if the RMI registry and the RMI server are on different hosts, the RMI client must have available the cryptographic material to validate the certificates from both hosts.
|
||||||
|
This is where having certificates signed by a Certification Authority simplifies the configuration: if they are signed by a well known Certification Authority, the client does not need any extra configuration -- everything will be handled by the Java runtime.
|
||||||
|
|
||||||
|
If the certificates are not signed by a Certification Authority (for example the certificate is self-signed), then you need to specify the TLS system properties that allow RMI (especially when acting as an RMI client) to retrieve the cryptographic material necessary to establish the TLS connection.
|
||||||
|
|
||||||
|
[IMPORTANT]
|
||||||
|
====
|
||||||
|
When the RMI server exports the `JMXConnectorServer` it acts as an RMI _client_ towards the RMI registry, and as such you must specify the TLS system properties as detailed below.
|
||||||
|
====
|
||||||
|
|
||||||
|
You must edit the `$JETTY_BASE/start.d/jmx-remote-ssl.ini` file and add the TrustStore path and password:
|
||||||
|
|
||||||
|
.$JETTY_BASE/start.d/jmx-remote-ssl.ini
|
||||||
|
----
|
||||||
|
--module=jmx-remote-ssl
|
||||||
|
|
||||||
|
# System properties necessary for non-trusted certificates.
|
||||||
|
-Djavax.net.ssl.trustStore=/path/to/trustStore.p12
|
||||||
|
-Djavax.net.ssl.trustStorePassword=password
|
||||||
|
----
|
||||||
|
|
||||||
|
[IMPORTANT]
|
||||||
|
====
|
||||||
|
The TrustStore must contain the certificate you want to trust.
|
||||||
|
|
||||||
|
If you are using self-signed certificates, the KeyStore already contains the self-signed certificate and therefore the KeyStore can be used as a TrustStore, and the system properties above can refer to the KeyStore path and password.
|
||||||
|
====
|
||||||
|
|
||||||
|
JMX compliant tools that offer a graphical user interface also must be started specifying the TrustStore path and password.
|
||||||
|
|
||||||
|
For example, to launch https://adoptium.net/jmc.html[Java Mission Control (JMC)]:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ jmc -vmargs -Djavax.net.ssl.trustStore=/path/to/trustStore.p12 -Djavax.net.ssl.trustStorePassword=password
|
||||||
|
----
|
|
@ -0,0 +1,391 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= JNDI
|
||||||
|
|
||||||
|
Enable the `plus` module in order to be able to use JNDI resources in your webapp.
|
||||||
|
If you already have the `annotations` module enabled, then it will already be enabled.
|
||||||
|
|
||||||
|
If you have extra jars associated with your JNDI resources, eg database drivers etc, that are not located inside your webapp then you should place those jars into your `$jetty.base/lib/ext` directory.
|
||||||
|
If your base doesn't already contain this directory, then enable the `ext` module, and Jetty will create the directory for you and ensure its contents are on the server's classpath.
|
||||||
|
|
||||||
|
You can now declare JNDI resources and reference them within your webapps.
|
||||||
|
|
||||||
|
== Declaring resources
|
||||||
|
|
||||||
|
You must declare the objects you want bound into the environment so that you can then hook them into your webapp via `env-entry`, `resource-ref` and `resource-env-refs` in `web.xml`, `web-fragment.xml` or `override-web.xml`.
|
||||||
|
|
||||||
|
You make these declarations in Jetty XML files that are either _external_ or _internal_ to your webapp.
|
||||||
|
A server or context XML file is external to your webapp.
|
||||||
|
The special `WEB-INF/jetty-env.xml` file is internal to your webapp.
|
||||||
|
See the section on <<xml,Jetty XML files>> for more information on how to choose in which XML file to place your declarations.
|
||||||
|
|
||||||
|
For now, let's look at _what_ you declare in the XML file, regardless of its location.
|
||||||
|
|
||||||
|
Declaring a JDNI resource to be referenced later in your webapp is accomplished by declaring new instances of the following types:
|
||||||
|
|
||||||
|
<<env,`org.eclipse.jetty.plus.jndi.EnvEntry`>>::
|
||||||
|
Used for `env-entry` type of entries
|
||||||
|
<<resource,`org.eclipse.jetty.plus.jndi.Resource`>>::
|
||||||
|
Used for most other type of resources
|
||||||
|
<<tx,`org.eclipse.jetty.plus.jndi.Transaction`>>::
|
||||||
|
For a JTA manager
|
||||||
|
<<link,`org.eclipse.jetty.plus.jndi.Link`>>::
|
||||||
|
For the link between a `web.xml` resource name and a naming entry
|
||||||
|
|
||||||
|
Declarations of each of these types follow a similar pattern:
|
||||||
|
|
||||||
|
[,xml,subs=verbatim]
|
||||||
|
----
|
||||||
|
<New class="org.eclipse.jetty.plus.jndi.xxxx"> <!--1-->
|
||||||
|
<Arg><!-- scope --></Arg> <!--2-->
|
||||||
|
<Arg><!-- name --></Arg> <!--3-->
|
||||||
|
<Arg><!-- value --></Arg> <!--4-->
|
||||||
|
</New>
|
||||||
|
----
|
||||||
|
<1> Defines a resource to Jetty.
|
||||||
|
<2> Specifies the <<scope,scope>> of the resource.
|
||||||
|
<3> Specifies the name of the resource which will be looked up by the webapp relative to the `java:comp/env` namespace.
|
||||||
|
<4> Specifies the value of the resource.
|
||||||
|
|
||||||
|
|
||||||
|
[[env]]
|
||||||
|
=== org.eclipse.jetty.plus.jndi.EnvEntry
|
||||||
|
|
||||||
|
Sometimes it is useful to pass configuration information to a webapp at runtime that you either cannot or cannot conveniently code into a `web.xml` `<env-entry>`.
|
||||||
|
In such cases, you can use the `org.eclipse.jetty.plus.jndi.EnvEntry` class, and optionally even override an entry of the same name in `web.xml`.
|
||||||
|
|
||||||
|
Here's an example that defines the equivalent of an `env-entry` called `mySpecialValue` with value `4000` that overrides an `<env-entry>` declaration of the same name in web.xml:
|
||||||
|
|
||||||
|
[,xml,subs=verbatim]
|
||||||
|
----
|
||||||
|
<New class="org.eclipse.jetty.plus.jndi.EnvEntry"> <!--1-->
|
||||||
|
<Arg></Arg> <!--2-->
|
||||||
|
<Arg>mySpecialValue</Arg> <!--3-->
|
||||||
|
<Arg type="java.lang.Integer">4000</Arg> <!--4-->
|
||||||
|
<Arg type="boolean">true</Arg> <!--5-->
|
||||||
|
</New>
|
||||||
|
----
|
||||||
|
<1> Define an `EnvEntry` that corresponds to an `<env-entry>`.
|
||||||
|
<2> <<scope,Scoped>> at the JVM level.
|
||||||
|
<3> The name of the entry, corresponding to a lookup by the webapp of `java:comp/env/mySpecialValue`.
|
||||||
|
<4> The value of the entry, in this case the integer value `4000`.
|
||||||
|
<5> `true` means to override the value of an `<env-entry>` of the same name in `web.xml`.
|
||||||
|
|
||||||
|
Note that if you don't want to override the `web.xml` value, simply omit the last argument, or set it to `false`.
|
||||||
|
|
||||||
|
The Servlet Specification allows binding only the following object types to an `env-entry`:
|
||||||
|
|
||||||
|
* java.lang.String
|
||||||
|
* java.lang.Integer
|
||||||
|
* java.lang.Float
|
||||||
|
* java.lang.Double
|
||||||
|
* java.lang.Long
|
||||||
|
* java.lang.Short
|
||||||
|
* java.lang.Character
|
||||||
|
* java.lang.Byte
|
||||||
|
* java.lang.Boolean
|
||||||
|
|
||||||
|
Jetty is a little more flexible and allows you to also bind:
|
||||||
|
|
||||||
|
* custom POJOs
|
||||||
|
* http://docs.oracle.com/javase/1.5.0/docs/api/javax/naming/Reference.html[`javax.naming.References`]
|
||||||
|
* http://docs.oracle.com/javase/1.5.0/docs/api/javax/naming/Referenceable.html[`javax.naming.Referenceables`]
|
||||||
|
|
||||||
|
Be aware that if you take advantage of this feature, your web application is __not portable__.
|
||||||
|
|
||||||
|
[[resource]]
|
||||||
|
=== org.eclipse.jetty.plus.jndi.Resource
|
||||||
|
|
||||||
|
You can configure any type of resource that you want to refer to in `web.xml` via a `resource-ref` or `resource-env-ref` by using the `org.eclipse.jetty.plus.jndi.Resource` type of naming entry.
|
||||||
|
|
||||||
|
You provide the scope, the name of the object (relative to `java:comp/env`) and a POJO, `javax.naming.Reference` or `javax.naming.Referenceable` instance.
|
||||||
|
|
||||||
|
Let's examine how to configure some of the most common types of resources.
|
||||||
|
|
||||||
|
==== DataSources
|
||||||
|
|
||||||
|
In this example, we'll configure a http://db.apache.org/derby[Derby] DataSource named `jdbc/myds`:
|
||||||
|
|
||||||
|
[,xml,subs=verbatim]
|
||||||
|
----
|
||||||
|
<Configure id='wac' class="org.eclipse.jetty.webapp.WebAppContext">
|
||||||
|
<New class="org.eclipse.jetty.plus.jndi.Resource">
|
||||||
|
<Arg><Ref refid="wac"/></Arg>
|
||||||
|
<Arg>jdbc/myds</Arg>
|
||||||
|
<Arg>
|
||||||
|
<New class="org.apache.derby.jdbc.EmbeddedDataSource">
|
||||||
|
<Set name="DatabaseName">test</Set>
|
||||||
|
<Set name="createDatabase">create</Set>
|
||||||
|
</New>
|
||||||
|
</Arg>
|
||||||
|
</New>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
This would linked into the webapps JNDI namespace via an entry in a `web.xml` like so:
|
||||||
|
|
||||||
|
[,xml,subs=verbatim]
|
||||||
|
----
|
||||||
|
<resource-ref>
|
||||||
|
<res-ref-name>jdbc/myds</res-ref-name>
|
||||||
|
<res-type>javax.sql.DataSource</res-type>
|
||||||
|
<res-auth>Container</res-auth>
|
||||||
|
</resource-ref>
|
||||||
|
----
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
When configuring Resources, ensure that the type of object you configure matches the type of object you expect to look up in `java:comp/env`.
|
||||||
|
For database connection factories, this means that the object you register as a Resource _must_ implement the `javax.sql.DataSource` interface.
|
||||||
|
|
||||||
|
Also note that the http://jcp.org/aboutJava/communityprocess/pr/jsr244/index.html[J2EE Specification] recommends storing DataSources relative to `jdbc/` and thus looked up by the application as `java:comp/env/jdbc/xxx`.
|
||||||
|
Eg The Datasource bound in Jetty as `jdbc/users` would be looked up by the application as `java:comp/env/jdbc/users`
|
||||||
|
|
||||||
|
====
|
||||||
|
|
||||||
|
//TODO For more examples of datasource configurations, see xref:jndi-datasource-examples[].
|
||||||
|
|
||||||
|
|
||||||
|
==== JMS Queues, Topics and ConnectionFactories
|
||||||
|
|
||||||
|
Jetty can bind any implementation of the JMS destinations and connection factories.
|
||||||
|
|
||||||
|
Here is an example of binding an http://activemq.apache.org[ActiveMQ] in-JVM connection factory:
|
||||||
|
|
||||||
|
[,xml,subs=verbatim]
|
||||||
|
----
|
||||||
|
<Configure id='wac' class="org.eclipse.jetty.webapp.WebAppContext">
|
||||||
|
<New class="org.eclipse.jetty.plus.jndi.Resource">
|
||||||
|
<Arg><Ref refid='wac'/></Arg>
|
||||||
|
<Arg>jms/connectionFactory</Arg>
|
||||||
|
<Arg>
|
||||||
|
<New class="org.apache.activemq.ActiveMQConnectionFactory">
|
||||||
|
<Arg>vm://localhost?broker.persistent=false</Arg>
|
||||||
|
</New>
|
||||||
|
</Arg>
|
||||||
|
</New>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
The corresponding entry in `web.xml` to bind the ConnectionFactory into the webapp's JNDI namespace would be:
|
||||||
|
|
||||||
|
[,xml,subs=verbatim]
|
||||||
|
----
|
||||||
|
<resource-ref>
|
||||||
|
<res-ref-name>jms/connectionFactory</res-ref-name>
|
||||||
|
<res-type>javax.jms.ConnectionFactory</res-type>
|
||||||
|
<res-auth>Container</res-auth>
|
||||||
|
</resource-ref>
|
||||||
|
----
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
The http://jcp.org/aboutJava/communityprocess/pr/jsr244/index.html[J2EE Specification] recommends storing JMS connection factories under `jms/`.
|
||||||
|
Eg The ConnectionFactory bound in Jetty as `jms/inqueue` would be looked up by the application as `java:comp/env/jms/inqueue`.
|
||||||
|
====
|
||||||
|
|
||||||
|
==== Mail
|
||||||
|
|
||||||
|
To configure access to `javax.mail.Session` from within a webapp, declare an `org.eclipse.jetty.plus.jndi.Resource` with an `org.eclipse.jetty.jndi.factories.MailSessionReference` that will hold the mail configuration and create the instance of the `Session` when it is referenced:
|
||||||
|
|
||||||
|
[,xml,subs=verbatim]
|
||||||
|
----
|
||||||
|
<Configure id='wac' class="org.eclipse.jetty.webapp.WebAppContext">
|
||||||
|
<New class="org.eclipse.jetty.plus.jndi.Resource">
|
||||||
|
<Arg><Ref refid="wac"/></Arg>
|
||||||
|
<Arg>mail/Session</Arg>
|
||||||
|
<Arg>
|
||||||
|
<New class="org.eclipse.jetty.jndi.factories.MailSessionReference"> <!--1-->
|
||||||
|
<Set name="user">fred</Set> <!--2-->
|
||||||
|
<Set name="password">OBF:1xmk1w261z0f1w1c1xmq</Set> <!--3-->
|
||||||
|
<Set name="properties"> <!--4-->
|
||||||
|
<New class="java.util.Properties">
|
||||||
|
<Put name="mail.smtp.host">XXX</Put>
|
||||||
|
<Put name="mail.from">me@me</Put>
|
||||||
|
<Put name="mail.debug">true</Put>
|
||||||
|
</New>
|
||||||
|
</Set>
|
||||||
|
</New>
|
||||||
|
</Arg>
|
||||||
|
</New>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
<1> Use the `org.eclipse.jetty.jndi.factories.MailSessionReference` class to hold the configuration.
|
||||||
|
<2> Set the username for the mail instance.
|
||||||
|
<3> Set the password for the mail instance - use Jetty's secure password obfuscation to obscure the password.
|
||||||
|
<4> Set all other applicable properties.
|
||||||
|
|
||||||
|
The webapp performs a lookup for `java:comp/env/mail/Session` at runtime and obtains a `javax.mail.Session` that has the correct configuration to permit it to send email via SMTP.
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
Jetty does not provide the `javax.mail` and `javax.activation` jars.
|
||||||
|
|
||||||
|
Note also that the http://jcp.org/aboutJava/communityprocess/pr/jsr244/index.html[J2EE Specification] recommends storing JavaMail connection factories under `mail/`.
|
||||||
|
Eg The `MailSessionReference` bound to jetty as `mail/smtp` would be looked up by the application as `java:comp/env/mail/smtp`.
|
||||||
|
====
|
||||||
|
|
||||||
|
[[tx]]
|
||||||
|
=== org.eclipse.jetty.plus.jndi.Transaction
|
||||||
|
|
||||||
|
To perform distributed transactions with your resources, a _transaction manager_ that supports the JTA interfaces is required.
|
||||||
|
The transaction manager is looked up by the application as `java:comp/UserTransaction`.
|
||||||
|
|
||||||
|
Jetty does not ship with a JTA manager, but _does_ provide the infrastructure to plug in the JTA manager of your choice.
|
||||||
|
|
||||||
|
Use the link:{javadoc-url}/org/eclipse/jetty/plus/jndi/Transaction.html[org.eclipse.jetty.plus.jndi.Transaction] object in a <<xml,Jetty XML file>> to configure the JTA manager.
|
||||||
|
|
||||||
|
The following example configures the http://www.atomikos.com/[Atomikos] transaction manager:
|
||||||
|
|
||||||
|
[,xml,subs=verbatim]
|
||||||
|
----
|
||||||
|
<New id="tx" class="org.eclipse.jetty.plus.jndi.Transaction">
|
||||||
|
<Arg>
|
||||||
|
<New class="com.atomikos.icatch.jta.J2eeUserTransaction"/>
|
||||||
|
</Arg>
|
||||||
|
</New>
|
||||||
|
----
|
||||||
|
|
||||||
|
Jetty will automatically bind this JTA manager to the webapp's JNDI namespace at `java:comp/UserTransaction`.
|
||||||
|
|
||||||
|
[[link]]
|
||||||
|
=== org.eclipse.jetty.plus.jndi.Link
|
||||||
|
|
||||||
|
Usually, the name you provide for the `org.eclipse.jetty.plus.jndi.Resource` is the same name you reference in `web.xml`.
|
||||||
|
This ensures that the two are linked together and thus accessible to your webapp.
|
||||||
|
|
||||||
|
However, if the names cannot be the same, then it is possible to effectively alias one to another using an `org.eclipse.jetty.plus.jndi.Link`.
|
||||||
|
|
||||||
|
Let's look at an example.
|
||||||
|
|
||||||
|
Supposing you have a declaration for a Datasource named `jdbc/workforce` in a Jetty context XML file, but your web.xml wants to link to a `<resource-ref>` named `jdbc/employees`, and you cannot edit the web.xml.
|
||||||
|
You can create a `WEB-INF/jetty-env.xml` file with an `org.eclipse.jetty.plus.jndi.Link` that ties together the names `jdbc/workforce` and `jdbc/employees`:
|
||||||
|
|
||||||
|
The context XML file declares `jdbc/workforce`:
|
||||||
|
|
||||||
|
[,xml,subs=verbatim]
|
||||||
|
----
|
||||||
|
<Configure id='wac' class="org.eclipse.jetty.webapp.WebAppContext">
|
||||||
|
<New class="org.eclipse.jetty.plus.jndi.Resource">
|
||||||
|
<Arg><Ref refid="wac"/></Arg>
|
||||||
|
<Arg>jdbc/workforce</Arg>
|
||||||
|
<Arg>
|
||||||
|
<New class="org.apache.derby.jdbc.EmbeddedDataSource">
|
||||||
|
<Set name="DatabaseName">test</Set>
|
||||||
|
<Set name="createDatabase">create</Set>
|
||||||
|
</New>
|
||||||
|
</Arg>
|
||||||
|
</New>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
The `web.xml` refers to it as `jdbc/employees`:
|
||||||
|
|
||||||
|
[,xml,subs=verbatim]
|
||||||
|
----
|
||||||
|
<resource-ref>
|
||||||
|
<res-ref-name>jdbc/employees</res-ref-name>
|
||||||
|
<res-type>javax.sql.DataSource</res-type>
|
||||||
|
<res-auth>Container</res-auth>
|
||||||
|
</resource-ref>
|
||||||
|
----
|
||||||
|
|
||||||
|
Create a `WEB-INF/jetty-env.xml` file with a `org.eclipse.jetty.plus.jndi.Link` to link these names together:
|
||||||
|
|
||||||
|
[,xml,subs=verbatim]
|
||||||
|
----
|
||||||
|
<New class="org.eclipse.jetty.plus.jndi.Link">
|
||||||
|
<Arg><Ref refid='wac'/></Arg>
|
||||||
|
<Arg>jdbc/employees</Arg> <!--1-->
|
||||||
|
<Arg>jdbc/workforce</Arg> <!--2-->
|
||||||
|
</New>
|
||||||
|
----
|
||||||
|
<1> The name as referenced in the `web.xml` file.
|
||||||
|
<2> The name as referenced in the context XML file.
|
||||||
|
|
||||||
|
[[xml]]
|
||||||
|
=== Jetty XML files
|
||||||
|
|
||||||
|
You can define naming resources in three places:
|
||||||
|
|
||||||
|
Server XML file::
|
||||||
|
Naming resources defined in a server XML file are <<scope,scoped>> at either the JVM level or the `org.eclipse.jetty.server.Server` level.
|
||||||
|
The classes for the resource _must_ be visible at the Jetty *container* level.
|
||||||
|
If instead the classes for the resource only exist inside your webapp, you must declare it in a `WEB-INF/jetty-env.xml` file.
|
||||||
|
Context XML file::
|
||||||
|
Entries in a context XML file should be <<scope,scoped>> at the level of the webapp to which they apply (although it is possible to use a less strict scoping level of Server or JVM, but not recommended).
|
||||||
|
As with resources declared in a server XML file, classes associated with the resource _must_ be visible on the *container's* classpath.
|
||||||
|
WEB-INF/jetty-env.xml::
|
||||||
|
Naming resources in a `WEB-INF/jetty-env.xml` file are <<scope,scoped>> to the webapp in which the file resides.
|
||||||
|
While you can enter JVM or Server scopes if you choose, we do not recommend doing so.
|
||||||
|
The resources defined here may use classes from inside your webapp.
|
||||||
|
|
||||||
|
[[scope]]
|
||||||
|
=== Resource scoping
|
||||||
|
|
||||||
|
Naming resources within Jetty belong to one of three different scopes, in increasing order of restrictiveness:
|
||||||
|
|
||||||
|
*JVM scope:*
|
||||||
|
The name is unique across the JVM instance, and is visible to all application code.
|
||||||
|
This scope is represented by a `null` first parameter to the resource declaration.
|
||||||
|
For example:
|
||||||
|
[,xml,subs=verbatim]
|
||||||
|
----
|
||||||
|
<New id="cf" class="org.eclipse.jetty.plus.jndi.Resource">
|
||||||
|
<Arg></Arg> <!--1-->
|
||||||
|
<Arg>jms/connectionFactory</Arg>
|
||||||
|
<Arg>
|
||||||
|
<New class="org.apache.activemq.ActiveMQConnectionFactory">
|
||||||
|
<Arg>vm://localhost?broker.persistent=false</Arg>
|
||||||
|
</New>
|
||||||
|
</Arg>
|
||||||
|
</New>
|
||||||
|
----
|
||||||
|
<1> Empty first arg equates to JVM scope for the object bound to name `jms/connectionFactory`.
|
||||||
|
|
||||||
|
*Server scope:*
|
||||||
|
The name is unique to a Server instance, and is only visible to applications associated with that instance.
|
||||||
|
This scope is represented by referencing the Server instance as the first parameter to the resource declaration.
|
||||||
|
For example:
|
||||||
|
[,xml,subs=verbatim]
|
||||||
|
----
|
||||||
|
<New id="cf" class="org.eclipse.jetty.plus.jndi.Resource">
|
||||||
|
<Arg><Ref refid="Server"/></Arg> <!--1-->
|
||||||
|
<Arg>jms/connectionFactory</Arg>
|
||||||
|
<Arg>
|
||||||
|
<New class="org.apache.activemq.ActiveMQConnectionFactory">
|
||||||
|
<Arg>vm://localhost?broker.persistent=false</Arg>
|
||||||
|
</New>
|
||||||
|
</Arg>
|
||||||
|
</New>
|
||||||
|
----
|
||||||
|
<1> We refer to the id `Server` which identifies the default `org.eclipse.jetty.server.Server` instance.
|
||||||
|
|
||||||
|
*Webapp scope:*
|
||||||
|
The name is unique to the `org.eclipse.jetty.webapp.WebAppContext` instance, and is only visible to that application.
|
||||||
|
This scope is represented by referencing the instance as the first parameter to the resource declaration.
|
||||||
|
For example:
|
||||||
|
[,xml,subs=verbatim]
|
||||||
|
----
|
||||||
|
<New class="org.eclipse.jetty.plus.jndi.Resource">
|
||||||
|
<Arg><Ref refid='wac'/></Arg> <!--1-->
|
||||||
|
<Arg>jms/connectionFactory</Arg>
|
||||||
|
<Arg>
|
||||||
|
<New class="org.apache.activemq.ActiveMQConnectionFactory">
|
||||||
|
<Arg>vm://localhost?broker.persistent=false</Arg>
|
||||||
|
</New>
|
||||||
|
</Arg>
|
||||||
|
</New>
|
||||||
|
----
|
||||||
|
<1> We refer to an instance of an `org.eclipse.jetty.webapp.WebAppContext` which has been previously defined.
|
|
@ -0,0 +1,41 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= JavaServer Faces TagLibs
|
||||||
|
|
||||||
|
If you want to use JSF with your webapp, you should copy the relevant jars from your implementation of choice into your `$JETTY_BASE` directory, ideally into `$JETTY_BASE/lib/ext`.
|
||||||
|
If that directory does not exist, enable the `ext` module, which will create the directory and ensure all jars within it are put onto the container classpath.
|
||||||
|
|
||||||
|
|
||||||
|
Then you will need to tell Jetty which of those jars contains the `+*.tld+` files.
|
||||||
|
To accomplish that, you need to specify either the name of the file or a pattern that matches the name/s of the file/s as the `org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern` context attribute.
|
||||||
|
You will need to preserve the existing value of the attribute, and add in your extra pattern.
|
||||||
|
|
||||||
|
Here's an example of using a context xml file to add in a pattern to match files starting with `jsf-`, which contain the `+*.tld+` files:
|
||||||
|
|
||||||
|
[,xml,subs=verbatim]
|
||||||
|
----
|
||||||
|
<?xml version="1.0"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure class="org.eclipse.jetty.webapp.WebAppContext"> <!--1-->
|
||||||
|
<Call name="setAttribute"> <!--2-->
|
||||||
|
<Arg>org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern</Arg> <!--3-->
|
||||||
|
<Arg>.*/jetty-servlet-api-[^/]*\.jar$|.*/javax.servlet.jsp.jstl-.*\.jar$|.*/org.apache.taglibs.taglibs-standard-impl-.*\.jar$|.*/jsf-[^/]*\.jar$</Arg> <!--4-->
|
||||||
|
</Call>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
<1> Configures a link:{javadoc-url}/org/eclipse/jetty/webapp/WebAppContext.html[`WebAppContext`], which is the Jetty component that represents a standard Servlet web application.
|
||||||
|
<2> Specifies a context attribute.
|
||||||
|
<3> Specifies the name of the context attribute.
|
||||||
|
<4> Adds the additional pattern `+.*/jsf-[^/]*\.jar$+` to those already existing.
|
|
@ -0,0 +1,170 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Java Server Pages
|
||||||
|
|
||||||
|
Jetty supports JSP via the `jsp` module, which is based on Apache Jasper:
|
||||||
|
|
||||||
|
----
|
||||||
|
include::{jetty-home}/modules/jsp.mod[]
|
||||||
|
----
|
||||||
|
|
||||||
|
Logging has been bridged to Jetty logging, so you can enable logging for the `org.apache.jasper` package, subpackages and classes as usual.
|
||||||
|
|
||||||
|
== Configuration of the JSP Servlet
|
||||||
|
|
||||||
|
The `org.eclipse.jetty.jsp.JettyJspServlet` is the servlet responsible for serving JSPs.
|
||||||
|
|
||||||
|
It is configured as the default jsp servlet in the `$JETTY_HOME/etc/webdefault.xml` file.
|
||||||
|
Notice that Jetty identifies the jsp servlet by the presence of the `id="jsp"` attribute in the `<servlet>` declaration.
|
||||||
|
|
||||||
|
That file maps the `org.eclipse.jetty.jsp.JettyJspServlet` to the following partial urls:
|
||||||
|
|
||||||
|
* `+*.jsp+`
|
||||||
|
* `+*.jspf+`
|
||||||
|
* `+*.jspx+`
|
||||||
|
* `+*.xsp+`
|
||||||
|
* `+*.JSP+`
|
||||||
|
* `+*.JSPF+`
|
||||||
|
* `+*.JSPX+`
|
||||||
|
* `+*.XSP+`
|
||||||
|
|
||||||
|
You can change to a different servlet, change or add ``<init-param>``s or add extra ``<servlet-mapping>``s in your `web.xml` file.
|
||||||
|
|
||||||
|
Here's an example of adding an `<init-param>` to augment the definitions from the standard `webdefault.xml` file:
|
||||||
|
|
||||||
|
[,xml,subs=verbatim]
|
||||||
|
----
|
||||||
|
<servlet id="jsp"> <!--1-->
|
||||||
|
<servlet-name>jsp</servlet-name> <!--2-->
|
||||||
|
<init-param>
|
||||||
|
<param-name>keepgenerated</param-name> <!--3-->
|
||||||
|
<param-value>true</param-value> <!--4-->
|
||||||
|
</init-param>
|
||||||
|
</servlet>
|
||||||
|
----
|
||||||
|
<1> This identifies this servlet as the jsp servlet to Jetty.
|
||||||
|
<2> This identifies this declaration as augmenting the already-defined servlet called `jsp`.
|
||||||
|
<3> This init param controls whether the jsp servlet retains the `+*.java+` files generated during jsp compilation.
|
||||||
|
<4> This sets the value of the init param
|
||||||
|
|
||||||
|
Another element you might consider adding to the default setup is `async-supported`:
|
||||||
|
|
||||||
|
[,xml,subs=verbatim]
|
||||||
|
----
|
||||||
|
<servlet id="jsp"> <!--1-->
|
||||||
|
<servlet-name>jsp</servlet-name> <!--2-->
|
||||||
|
<async-supported>true</async-supported> <!--3-->
|
||||||
|
</servlet>
|
||||||
|
----
|
||||||
|
<1> This identifies this servlet as the jsp servlet to Jetty.
|
||||||
|
<2> This identifies this declaration as augmenting the already-defined servlet called `jsp`.
|
||||||
|
<3> By default, the jsp servlet does not support async.
|
||||||
|
|
||||||
|
There are many configuration parameters for the Apache Jasper JSP Servlet, here are some of them:
|
||||||
|
|
||||||
|
.JSP Servlet Parameters
|
||||||
|
[cols=",,,",options="header"]
|
||||||
|
|===
|
||||||
|
|init param |Description |Default |`webdefault.xml`
|
||||||
|
|
||||||
|
|checkInterval |If non-zero and `development` is `false`, background jsp recompilation is enabled. This value is the interval in seconds between background recompile checks.
|
||||||
|
|0 |–
|
||||||
|
|classpath |The classpath is dynamically generated if the context has a URL classloader. The `org.apache.catalina.jsp_classpath`
|
||||||
|
context attribute is used to add to the classpath, but if this is not set, this `classpath` configuration item is added to the classpath instead.` |- |–
|
||||||
|
|
||||||
|
|classdebuginfo |Include debugging info in class file. |true |–
|
||||||
|
|
||||||
|
|compilerClassName |If not set, defaults to the Eclipse jdt compiler. |- |–
|
||||||
|
|
||||||
|
|compiler |Used if the Eclipse jdt compiler cannot be found on the
|
||||||
|
classpath. It is the classname of a compiler that Ant should invoke. |–
|
||||||
|
|–
|
||||||
|
|
||||||
|
|compilerTargetVM |Target vm to compile for. |1.8 |1.8
|
||||||
|
|
||||||
|
|compilerSourceVM |Sets source compliance level for the jdt compiler.
|
||||||
|
|1.8 |1.8
|
||||||
|
|
||||||
|
|development |If `true` recompilation checks occur at the frequency governed by `modificationTestInterval`. |true |–
|
||||||
|
|
||||||
|
|displaySourceFragment |Should a source fragment be included in
|
||||||
|
exception messages |true |–
|
||||||
|
|
||||||
|
|dumpSmap |Dump SMAP JSR45 info to a file. |false |–
|
||||||
|
|
||||||
|
|enablePooling |Determines whether tag handler pooling is enabled. |true |–
|
||||||
|
|
||||||
|
|engineOptionsClass |Allows specifying the Options class used to
|
||||||
|
configure Jasper. If not present, the default EmbeddedServletOptions
|
||||||
|
will be used. |- |–
|
||||||
|
|
||||||
|
|errorOnUseBeanInvalidClassAttribute |Should Jasper issue an error when
|
||||||
|
the value of the class attribute in an useBean action is not a valid
|
||||||
|
bean class |true |–
|
||||||
|
|
||||||
|
|fork |Only relevant if you use Ant to compile JSPs: by default Jetty will use the Eclipse jdt compiler.|true |-
|
||||||
|
|
||||||
|
|genStrAsCharArray |Option for generating Strings as char arrays. |false |–
|
||||||
|
|
||||||
|
|ieClassId |The class-id value to be sent to Internet Explorer when
|
||||||
|
using <jsp:plugin> tags. |clsid:8AD9C840-044E-11D1-B3E9-00805F499D93 |–
|
||||||
|
|
||||||
|
|javaEncoding |Pass through the encoding to use for the compilation.
|
||||||
|
|UTF8 |–
|
||||||
|
|
||||||
|
|jspIdleTimeout |The amount of time in seconds a JSP can be idle before
|
||||||
|
it is unloaded. A value of zero or less indicates never unload. |-1 |–
|
||||||
|
|
||||||
|
|keepgenerated |Do you want to keep the generated Java files around?
|
||||||
|
|true |–
|
||||||
|
|
||||||
|
|mappedFile |Support for mapped Files. Generates a servlet that has a
|
||||||
|
print statement per line of the JSP file |true |–
|
||||||
|
|
||||||
|
|maxLoadedJsps |The maximum number of JSPs that will be loaded for a web
|
||||||
|
application. If more than this number of JSPs are loaded, the least
|
||||||
|
recently used JSPs will be unloaded so that the number of JSPs loaded at
|
||||||
|
any one time does not exceed this limit. A value of zero or less
|
||||||
|
indicates no limit. |-1 |–
|
||||||
|
|
||||||
|
|modificationTestInterval |If `development=true`, interval between
|
||||||
|
recompilation checks, triggered by a request. |4 |–
|
||||||
|
|
||||||
|
|quoteAttributeEL | When EL is used in an attribute value on a JSP page, should the rules for quoting of attributes described in JSP.1.6 be applied to the expression
|
||||||
|
|true |-
|
||||||
|
|
||||||
|
|recompileOnFail |If a JSP compilation fails should the
|
||||||
|
modificationTestInterval be ignored and the next access trigger a
|
||||||
|
re-compilation attempt? Used in development mode only and is disabled by
|
||||||
|
default as compilation may be expensive and could lead to excessive
|
||||||
|
resource usage. |false |–
|
||||||
|
|
||||||
|
|scratchDir |Directory where servlets are generated. The default is the value of the context attribute `javax.servlet.context.tempdir`, or the system property `java.io.tmpdir` if the context attribute is not set. |– |–
|
||||||
|
|
||||||
|
|strictQuoteEscaping |Should the quote escaping required by section JSP.1.6 of the JSP specification be applied to scriplet expression.
|
||||||
|
|true|-
|
||||||
|
|
||||||
|
|suppressSmap |Generation of SMAP info for JSR45 debugging. |false |–
|
||||||
|
|
||||||
|
|trimSpaces |Should template text that consists entirely of whitespace be removed from the output (true), replaced with a single space (single) or left unchanged (false)? Note that if a JSP page or tag file specifies a trimDirectiveWhitespaces value of true, that will take precedence over this configuration setting for that page/tag.
|
||||||
|
trimmed? |false |–
|
||||||
|
|
||||||
|
|xpoweredBy |Generate an X-Powered-By response header. |false |false
|
||||||
|
|
||||||
|
|===
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
If the value you set doesn't take effect, try using all lower case instead of camel case, or capitalizing only some of the words in the name, as Jasper is inconsistent in its parameter naming strategy.
|
||||||
|
====
|
|
@ -0,0 +1,22 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= JavaServer Pages Standard Tag Libraries
|
||||||
|
|
||||||
|
The JavaServer Pages Standard Tag Library (JSTL) is part of the Jetty distribution, and is available via the `jstl` module:
|
||||||
|
|
||||||
|
----
|
||||||
|
include::{jetty-home}/modules/jstl.mod[]
|
||||||
|
----
|
||||||
|
|
||||||
|
When enabled, Jetty will make the JSTL tags available for your webapps.
|
|
@ -0,0 +1,274 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Configuring SSL/TLS KeyStores
|
||||||
|
|
||||||
|
A KeyStore is a file on the file system that contains a private key and a public certificate, along with the certificate chain of the certificate authorities that issued the certificate.
|
||||||
|
The private key, the public certificate and the certificate chain, but more generally the items present in a KeyStore, are typically referred to as "cryptographic material".
|
||||||
|
|
||||||
|
Keystores may encode the cryptographic material with different encodings, the most common being https://en.wikipedia.org/wiki/PKCS_12[PKCS12], and are typically protected by a password.
|
||||||
|
|
||||||
|
Refer to the xref:protocols/index.adoc#ssl[secure protocols section] for more information about how to configure a secure connector using a KeyStore.
|
||||||
|
|
||||||
|
[[create]]
|
||||||
|
== Creating a KeyStore
|
||||||
|
|
||||||
|
KeyStores are created with the JDK tool `$JAVA_HOME/bin/keytool`.
|
||||||
|
|
||||||
|
The following command creates a KeyStore file containing a private key and a self-signed certificate:
|
||||||
|
|
||||||
|
[source,subs=verbatim]
|
||||||
|
----
|
||||||
|
keytool
|
||||||
|
-genkeypair <1>
|
||||||
|
-alias mykey <2>
|
||||||
|
-validity 90 <3>
|
||||||
|
-keyalg RSA <4>
|
||||||
|
-keysize 2048 <5>
|
||||||
|
-keystore /path/to/keystore.p12 <6>
|
||||||
|
-storetype pkcs12 <7>
|
||||||
|
-dname "CN=domain.com, OU=Unit, O=Company, L=City, S=State, C=Country" <8>
|
||||||
|
-ext san=dns:www.domain.com,dns:domain.org <9>
|
||||||
|
-v <10>
|
||||||
|
----
|
||||||
|
<1> the command to generate a key and certificate pair
|
||||||
|
<2> the alias name of the key and certificate pair
|
||||||
|
<3> specifies the number of days after which the certificate expires
|
||||||
|
<4> the algorithm _must_ be RSA (the DSA algorithm does not work for web sites)
|
||||||
|
<5> indicates the strength of the key
|
||||||
|
<6> the KeyStore file
|
||||||
|
<7> the KeyStore type, stick with the standard PKCS12
|
||||||
|
<8> the distinguished name (more below) -- customize it with your values for CN, OU, O, L, S and C
|
||||||
|
<9> the extension with the subject alternative names (more below)
|
||||||
|
<10> verbose output
|
||||||
|
|
||||||
|
The command prompts for the KeyStore password that you must choose to protect the access to the KeyStore.
|
||||||
|
|
||||||
|
[IMPORTANT]
|
||||||
|
====
|
||||||
|
The important parts of the command above are the _Common Name_ (CN) part of the distinguished name, and the subject alternative names (SAN).
|
||||||
|
|
||||||
|
The CN value must be the main domain you own and that you want to use for your web applications.
|
||||||
|
For example, if you have bought domains `domain.com` and `domain.org`, you want to specify `CN=domain.com` as your main domain.
|
||||||
|
|
||||||
|
Furthermore, to specify additional domains or subdomains within the same certificate, you must specify the SAN extension.
|
||||||
|
In the example above, `san=dns:www.domain.com,dns:domain.org` specifies `www.domain.com` and `domain.org` as alternative names for your web applications (that you can configure using xref:deploy/index.adoc#virtual-hosts[virtual hosts]).
|
||||||
|
|
||||||
|
In rare cases, you may want to specify IP addresses, rather than domains, in the SAN extension.
|
||||||
|
The syntax in such case is `san=ip:127.0.0.1,ip:[::1]`, which specifies as subject alternative names IPv4 `127.0.0.1` and IPv6 `[::1]`.
|
||||||
|
====
|
||||||
|
|
||||||
|
[[create-many]]
|
||||||
|
=== KeyStores with Multiple Entries
|
||||||
|
|
||||||
|
A single KeyStore may contain multiple key/certificate pairs.
|
||||||
|
This is useful when you need to support multiple domains on the same Jetty server (typically accomplished using xref:deploy/index.adoc#virtual-hosts[virtual hosts]).
|
||||||
|
|
||||||
|
You can create multiple key/certificate pairs as detailed in the <<create,previous section>>, provided that you assign each one to a different alias.
|
||||||
|
|
||||||
|
Compliant TLS clients will send the xref:protocols/index.adoc#ssl-sni[TLS SNI extension] when creating new connections, and Jetty will automatically choose the right certificate by matching the SNI name sent by the client with the CN or SAN of certificates present in the KeyStore.
|
||||||
|
|
||||||
|
[[csr]]
|
||||||
|
== Creating a Certificate Signing Request
|
||||||
|
|
||||||
|
Self-signed certificates are not trusted by browsers and generic clients: you need to establish a trust chain by having your self-signed certificate signed by a certificate authority (CA).
|
||||||
|
|
||||||
|
Browsers and generic clients (e.g. Java clients) have an internal list of trusted certificate authorities root certificates; they use these trusted certificates to verify the certificate they received from the server when they connect to your web applications.
|
||||||
|
|
||||||
|
To have your self-signed certificate signed by a certificate authority you first need to produce a _certificate signing request_ (CSR):
|
||||||
|
|
||||||
|
[source,subs=verbatim]
|
||||||
|
----
|
||||||
|
keytool
|
||||||
|
-certreq <1>
|
||||||
|
-file domain.com.csr <2>
|
||||||
|
-keystore keystore.p12 <3>
|
||||||
|
----
|
||||||
|
<1> the command to generate a certificate signing request
|
||||||
|
<2> the file to save the CSR
|
||||||
|
<3> the keystore that contains the self-signed certificate
|
||||||
|
|
||||||
|
Then, you have to send the CSR file to the certificate authority of your choice, and wait for their reply (they will probably require a proof that you really own the domains indicated in your certificate).
|
||||||
|
|
||||||
|
Eventually, the certificate authority will reply to you with one or more files containing the CA certificate chain, and your certificate signed by their certificate chain.
|
||||||
|
|
||||||
|
[[csr-import]]
|
||||||
|
== Importing the Signed Certificate
|
||||||
|
|
||||||
|
The file you receive from the CA is typically in PEM format, and you *must* import it back into the same KeyStore file you used to generate the CSR.
|
||||||
|
You must import *both* the certificate chain and your signed certificate.
|
||||||
|
|
||||||
|
First, import the certificate chain:
|
||||||
|
|
||||||
|
[source,subs=verbatim]
|
||||||
|
----
|
||||||
|
keytool
|
||||||
|
-importcert <1>
|
||||||
|
-alias ca <2>
|
||||||
|
-file chain_from_ca.pem <3>
|
||||||
|
-keystore keystore.p12 <4>
|
||||||
|
-trustcacerts <5>
|
||||||
|
-v <6>
|
||||||
|
----
|
||||||
|
<1> the command to import certificates
|
||||||
|
<2> use the `ca` alias to differentiate from the alias of the server certificate
|
||||||
|
<3> the file containing the certificate chain received from the CA
|
||||||
|
<4> your KeyStore file
|
||||||
|
<5> specify that you trust CA certificates
|
||||||
|
<6> verbose output
|
||||||
|
|
||||||
|
Then, import the signed certificate:
|
||||||
|
|
||||||
|
----
|
||||||
|
keytool
|
||||||
|
-importcert
|
||||||
|
-file signed_certificate.pem
|
||||||
|
-keystore keystore.p12
|
||||||
|
-trustcacerts
|
||||||
|
-v
|
||||||
|
----
|
||||||
|
|
||||||
|
Now you have a trusted certificate in your KeyStore that you can use for the domains of your web applications.
|
||||||
|
|
||||||
|
// TODO: add a section about renewal?
|
||||||
|
|
||||||
|
Refer to the section about configuring xref:protocols/index.adoc#ssl[secure protocols] to configure the secure connector with your newly created KeyStore.
|
||||||
|
|
||||||
|
[[client-authn]]
|
||||||
|
== Creating a KeyStore for Client Certificate Authentication
|
||||||
|
|
||||||
|
For the majority of secure web sites, it is the client (typically the browser) that validates the certificate sent by the server (by verifying the certificate chain).
|
||||||
|
This is the _server domain certificate_.
|
||||||
|
|
||||||
|
However, the TLS protocol supports a _mutual authentication_ mode where also the client must send a certificate to the server, that the server validates.
|
||||||
|
|
||||||
|
You typically want to sign the client certificate(s) with a server certificate that you control, and you must distribute the client certificate(s) to all the clients that need it, and redistribute the client certificates when they expire.
|
||||||
|
The _server authentication certificate_ may be different from the _server domain certificate_, but it's typically stored in the same KeyStore for simplicity (although under a different alias).
|
||||||
|
|
||||||
|
First, you want to create the private key and server authentication certificate that you will use to sign client certificates:
|
||||||
|
|
||||||
|
[source,subs=verbatim]
|
||||||
|
----
|
||||||
|
keytool
|
||||||
|
-genkeypair
|
||||||
|
-alias server_authn <1>
|
||||||
|
-validity 90
|
||||||
|
-keyalg RSA
|
||||||
|
-keysize 2048
|
||||||
|
-keystore keystore.p12 <2>
|
||||||
|
-storetype pkcs12
|
||||||
|
-dname "CN=server_authn, OU=Unit, O=Company, L=City, S=State, C=Country" <3>
|
||||||
|
-ext bc=ca:true <4>
|
||||||
|
-v
|
||||||
|
----
|
||||||
|
<1> use the `server_authn` alias to differentiate from the alias of the server certificate
|
||||||
|
<2> the KeyStore file
|
||||||
|
<3> the CN is not that important, since this certificate will not be validated by clients
|
||||||
|
<4> the extension with the basic constraints (more below)
|
||||||
|
|
||||||
|
IMPORTANT: The important difference with the <<create,creation of a server certificate>> is the _basic constraints_ extension (`bc`) that indicates that this certificates acts as a certificate authority (`ca:true`).
|
||||||
|
|
||||||
|
Now you want to export both the private key and server authentication certificate.
|
||||||
|
Unfortunately, the `keytool` program cannot export private keys, so you need to use a different command line program like `openssl`, or a graphical program like https://keystore-explorer.org/[KeyStore Explorer].
|
||||||
|
|
||||||
|
Let's use `openssl` to export the server authentication private key:
|
||||||
|
|
||||||
|
----
|
||||||
|
openssl
|
||||||
|
pkcs12
|
||||||
|
-in keystore.p12
|
||||||
|
-nodes
|
||||||
|
-nocerts
|
||||||
|
-out server_authn.key
|
||||||
|
----
|
||||||
|
|
||||||
|
Now let's export the server authentication certificate:
|
||||||
|
|
||||||
|
----
|
||||||
|
keytool
|
||||||
|
-exportcert
|
||||||
|
-keystore keystore.p12
|
||||||
|
-rfc
|
||||||
|
-file server_authn.crt
|
||||||
|
-v
|
||||||
|
----
|
||||||
|
|
||||||
|
At this point, you want to create a client KeyStore, so that you can sign the client certificate with the server authentication cryptographic material:
|
||||||
|
|
||||||
|
[source,subs=verbatim]
|
||||||
|
----
|
||||||
|
keytool
|
||||||
|
-genkeypair
|
||||||
|
-validity 90
|
||||||
|
-keyalg RSA
|
||||||
|
-keysize 2048
|
||||||
|
-keystore client_keystore.p12 <1>
|
||||||
|
-storetype pkcs12
|
||||||
|
-dname "CN=client, OU=Unit, O=Company, L=City, S=State, C=Country" <2>
|
||||||
|
-v
|
||||||
|
----
|
||||||
|
<1> the client KeyStore file
|
||||||
|
<2> the CN is not that important, since it will not be validated by the server
|
||||||
|
|
||||||
|
Now produce a certificate signing request (CSR):
|
||||||
|
|
||||||
|
----
|
||||||
|
keytool
|
||||||
|
-certreq
|
||||||
|
-file client.csr
|
||||||
|
-keystore client_keystore.p12
|
||||||
|
----
|
||||||
|
|
||||||
|
Now you need to sign the CSR, but again the `keytool` program does not have this functionality, and you must resort again to use `openssl`:
|
||||||
|
|
||||||
|
----
|
||||||
|
openssl
|
||||||
|
x509
|
||||||
|
-req
|
||||||
|
-days 90
|
||||||
|
-in client.csr
|
||||||
|
-CA server_authn.crt
|
||||||
|
-CAkey server_authn.key
|
||||||
|
-CAcreateserial
|
||||||
|
-sha256
|
||||||
|
-out signed.crt
|
||||||
|
----
|
||||||
|
|
||||||
|
Now you need to import the server authentication certificate and the signed client certificate into the client KeyStore.
|
||||||
|
|
||||||
|
First, the server authentication certificate:
|
||||||
|
|
||||||
|
----
|
||||||
|
keytool
|
||||||
|
-importcert
|
||||||
|
-alias ca
|
||||||
|
-file server_authn.crt
|
||||||
|
-keystore client_keystore.p12
|
||||||
|
-v
|
||||||
|
----
|
||||||
|
|
||||||
|
Then, the signed client certificate:
|
||||||
|
|
||||||
|
----
|
||||||
|
keytool
|
||||||
|
-importcert
|
||||||
|
-file signed.crt
|
||||||
|
-keystore client_keystore.p12
|
||||||
|
-v
|
||||||
|
----
|
||||||
|
|
||||||
|
Now you can distribute `client_keystore.p12` to your client(s).
|
||||||
|
|
||||||
|
// TODO: add a section about renewal?
|
||||||
|
|
||||||
|
Refer to the section about configuring xref:protocols/index.adoc#ssl[secure protocols] to configure the secure connector to require client authentication.
|
|
@ -0,0 +1,235 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Custom Jetty Modules
|
||||||
|
|
||||||
|
In addition to the modules that come packaged with Jetty, you can create your own custom modules.
|
||||||
|
|
||||||
|
NOTE: Make sure you have read the xref:modules/index.adoc[Jetty modules section] if you are not familiar with the concepts used in this section.
|
||||||
|
|
||||||
|
Custom modules can be used for a number of reasons -- they can extend Jetty features, or add new features, or make additional libraries available to the server, etc.
|
||||||
|
|
||||||
|
[[modify]]
|
||||||
|
== Modifying an Existing Module
|
||||||
|
|
||||||
|
The standard Jetty modules typically come with a number of configurable properties that can be easily customized without the need of writing a custom module.
|
||||||
|
|
||||||
|
However, there may be cases where the customization is more complex than a simple property, and a custom module is necessary.
|
||||||
|
|
||||||
|
For example, let's assume that you want to modify the order of the TLS cipher suites offered by the server when a client connects, using the https://www.openssl.org/docs/man1.1.0/man1/ciphers.html[OpenSSL cipher list format].
|
||||||
|
|
||||||
|
The Jetty class that handles the TLS configuration is `SslContextFactory`, and it already has a method `setCipherComparator(Comparator<String>)`; however, you need to pass your custom implementation, which cannot be represented with a simple module property.
|
||||||
|
|
||||||
|
The `SslContextFactory` component is already allocated by the standard Jetty module `ssl`, so what you need to do is the following:
|
||||||
|
|
||||||
|
* Write the custom cipher `Comparator` and package it into a `+*.jar+` file (exercise left to reader).
|
||||||
|
* Write a custom Jetty XML file that calls the `SslContextFactory.setCipherComparator(Comparator<String>)` method.
|
||||||
|
* Write a custom Jetty module file that depends on the standard `ssl` module.
|
||||||
|
|
||||||
|
Start with the custom Jetty XML file, `$JETTY_BASE/etc/custom-ssl.xml`:
|
||||||
|
|
||||||
|
.custom-ssl.xml
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
<Configure>
|
||||||
|
<Ref refid="sslContextFactory"> <!--1-->
|
||||||
|
<Set name="CipherComparator"> <!--2-->
|
||||||
|
<New class="com.acme.ssl.CustomCipherComparator"> <!--3-->
|
||||||
|
<Arg>
|
||||||
|
<Property name="com.acme.ssl.cipherList"> <!--4-->
|
||||||
|
<Default>ECDH+AESGCM:ECDH+AES256:!aNULL:!MD5:!DSS:!ADH</Default>
|
||||||
|
</Property>
|
||||||
|
</Arg>
|
||||||
|
</New>
|
||||||
|
</Set>
|
||||||
|
</Ref>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
<1> Reference the existing `SslContextFactory` object created by the standard `ssl` module using its `id`.
|
||||||
|
<2> Call the `setCipherComparator()` method.
|
||||||
|
<3> Instantiate your custom cipher comparator.
|
||||||
|
<4> Pass to the constructor the ordering string in OpenSSL format, reading it from the module property `com.acme.ssl.cipherList`.
|
||||||
|
|
||||||
|
CAUTION: The cipher list used above may not be secure -- it's just an example.
|
||||||
|
|
||||||
|
Then write your custom module in the `$JETTY_BASE/modules/custom-ssl.mod` file:
|
||||||
|
|
||||||
|
.custom-ssl.mod
|
||||||
|
[source,subs=verbatim]
|
||||||
|
----
|
||||||
|
[description]
|
||||||
|
Customizes the standard ssl module.
|
||||||
|
|
||||||
|
[tags] <1>
|
||||||
|
acme
|
||||||
|
|
||||||
|
[depends] <2>
|
||||||
|
ssl
|
||||||
|
|
||||||
|
[lib] <3>
|
||||||
|
lib/custom-cipher-comparator.jar
|
||||||
|
|
||||||
|
[xml] <4>
|
||||||
|
etc/custom-ssl.xml
|
||||||
|
|
||||||
|
[ini-template] <5>
|
||||||
|
## The cipher list in OpenSSL format.
|
||||||
|
# com.acme.ssl.cipherList=ECDH+AESGCM:ECDH+AES256:!aNULL:!MD5:!DSS:!ADH
|
||||||
|
|
||||||
|
----
|
||||||
|
<1> A tag that characterizes this custom module (see xref:modules/index.adoc#directive-tags[here]).
|
||||||
|
<2> This custom module depends on the standard `ssl` module.
|
||||||
|
<3> The custom cipher comparator class is compiled and packaged into this `+*.jar+` file.
|
||||||
|
<4> The custom Jetty XML file from above.
|
||||||
|
<5> The text that will be copied in the `custom-ssl.ini` file when this custom module will be enabled.
|
||||||
|
|
||||||
|
Now you can xref:start/index.adoc#configure-enable[enable] the custom module with the following command issued from the `$JETTY_BASE` directory:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --add-modules=https,custom-ssl
|
||||||
|
----
|
||||||
|
|
||||||
|
The command above will produce the following `$JETTY_BASE` directory structure:
|
||||||
|
|
||||||
|
[source,subs=normal]
|
||||||
|
----
|
||||||
|
$JETTY_BASE
|
||||||
|
├── etc
|
||||||
|
│ └── custom-ssl.xml
|
||||||
|
├── modules
|
||||||
|
│ └── custom-ssl.mod
|
||||||
|
├── resources
|
||||||
|
│ └── jetty-logging.properties
|
||||||
|
└── start.d
|
||||||
|
├── https.ini
|
||||||
|
└── ##custom-ssl.ini##
|
||||||
|
----
|
||||||
|
|
||||||
|
In the custom XML file you have used a custom module property to parametrize your custom cipher comparator.
|
||||||
|
This custom module property was then referenced in the `[ini-template]` section of the custom module file, so that when the custom module is enabled, a correspondent `custom-ssl.ini` file is created.
|
||||||
|
|
||||||
|
In this way, updating the cipher list won't require you to update the XML file, but just the `custom-ssl.ini` file.
|
||||||
|
|
||||||
|
[[create]]
|
||||||
|
== Creating a New Module
|
||||||
|
|
||||||
|
In the cases where you need to enhance Jetty with a custom functionality, you can write a new Jetty module that provides it.
|
||||||
|
|
||||||
|
For example, let's assume that you need to add a custom auditing component that integrates with the auditing tools used by your company.
|
||||||
|
This custom auditing component should measure the HTTP request processing times and record them (how they are recorded is irrelevant here -- could be in a local log file or sent via network to an external service).
|
||||||
|
|
||||||
|
The Jetty libraries already provide a way to measure HTTP request processing times via xref:programming-guide:server/http.adoc#channel-events[`HttpChannel` events]: you write a custom component that implements the `HttpChannel.Listener` interface and add it as a bean to the server `Connector` that receives the HTTP requests.
|
||||||
|
|
||||||
|
The steps to create a Jetty module are similar to those necessary to <<modify,modify an existing module>>:
|
||||||
|
|
||||||
|
* Write the auditing component and package it into a `+*.jar+` file.
|
||||||
|
* Write a custom Jetty XML file that wires the auditing component to the `ServerConnector`.
|
||||||
|
* Write a custom Jetty module file that puts everything together.
|
||||||
|
|
||||||
|
Let's start with the auditing component, sketched below:
|
||||||
|
|
||||||
|
[,java]
|
||||||
|
----
|
||||||
|
package com.acme.audit;
|
||||||
|
|
||||||
|
public class AuditingHttpChannelListener implements HttpChannel.Listener {
|
||||||
|
// Auditing is implemented here.
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|
||||||
|
Let's assume that this class is compiled and packaged into `acme-audit.jar`, and that it has a dependency on `acme-util.jar`.
|
||||||
|
Both `+*.jar+` files will be put in the `$JETTY_BASE/lib/` directory.
|
||||||
|
|
||||||
|
Next, let's write the Jetty XML file that wires the auditing component to the `ServerConnector`, `$JETTY_BASE/etc/acme-audit.xml`:
|
||||||
|
|
||||||
|
.acme-audit.xml
|
||||||
|
[,xml,subs=verbatim,options=nowrap]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
<Configure>
|
||||||
|
<Ref refid="httpConnector"> <!--1-->
|
||||||
|
<Call name="addBean"> <!--2-->
|
||||||
|
<Arg>
|
||||||
|
<New class="com.acme.audit.AuditingHttpChannelListener"> <!--3-->
|
||||||
|
<Set name="someProperty">
|
||||||
|
<Property name="com.acme.audit.some.property" default="42" /> <!--4-->
|
||||||
|
</Set>
|
||||||
|
</New>
|
||||||
|
</Arg>
|
||||||
|
</Call>
|
||||||
|
</Ref>
|
||||||
|
</Configure>
|
||||||
|
|
||||||
|
----
|
||||||
|
<1> Reference the existing clear-text HTTP `ServerConnector` object created by the standard `http` module.
|
||||||
|
<2> Call `addBean()` on the `ServerConnector` to wire the auditing component.
|
||||||
|
<3> Instantiate the auditing component.
|
||||||
|
<4> Configure the auditing component with a property.
|
||||||
|
|
||||||
|
The last step is to create the custom Jetty module file for the auditing component, `$JETTY_BASE/modules/acme-audit.mod`:
|
||||||
|
|
||||||
|
.acme-audit.mod
|
||||||
|
----
|
||||||
|
[description]
|
||||||
|
Adds auditing to the clear-text HTTP connector
|
||||||
|
|
||||||
|
[tags] <1>
|
||||||
|
acme
|
||||||
|
audit
|
||||||
|
|
||||||
|
[depends] <2>
|
||||||
|
http
|
||||||
|
|
||||||
|
[libs] <3>
|
||||||
|
lib/acme-audit.jar
|
||||||
|
lib/acme-util.jar
|
||||||
|
|
||||||
|
[xml] <4>
|
||||||
|
etc/acme-audit.xml
|
||||||
|
|
||||||
|
[ini-template] <5>
|
||||||
|
## An auditing property.
|
||||||
|
# com.acme.audit.some.property=42
|
||||||
|
----
|
||||||
|
<1> The tags that characterize this custom module (see xref:modules/index.adoc#directive-tags[here]).
|
||||||
|
<2> This custom module depends on the standard `http` module.
|
||||||
|
<3> The `+*.jar+` files that contains the custom auditing component, and its dependencies.
|
||||||
|
<4> The custom Jetty XML file from above.
|
||||||
|
<5> The text that will be copied in the `acme-audit.ini` file when this custom module will be enabled.
|
||||||
|
|
||||||
|
Now you can xref:start/index.adoc#configure-enable[enable] the custom auditing module with the following command issued from the `$JETTY_BASE` directory:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --add-modules=http,acme-audit
|
||||||
|
----
|
||||||
|
|
||||||
|
The command above will produce the following `$JETTY_BASE` directory structure:
|
||||||
|
|
||||||
|
[source,subs=normal]
|
||||||
|
----
|
||||||
|
$JETTY_BASE
|
||||||
|
├── etc
|
||||||
|
│ └── acme-audit.xml
|
||||||
|
├── modules
|
||||||
|
│ └── acme-audit.mod
|
||||||
|
├── resources
|
||||||
|
│ └── jetty-logging.properties
|
||||||
|
└── start.d
|
||||||
|
├── http.ini
|
||||||
|
└── ##acme-audit.ini##
|
||||||
|
----
|
||||||
|
|
||||||
|
Enabling the custom auditing component will create the `$JETTY_BASE/start.d/acme-audit.ini` module configuration file that you can edit to configure auditing properties.
|
|
@ -0,0 +1,493 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Jetty Modules
|
||||||
|
|
||||||
|
A Jetty _module_ provides one or more Java components that work together to implement one or more features.
|
||||||
|
Such features could be listening for clear-text HTTP/1.1 requests, exposing Jetty components to JMX, provide hot-deployment of web applications, etc.
|
||||||
|
|
||||||
|
Every Jetty feature is provided by a Jetty module.
|
||||||
|
|
||||||
|
A Jetty module is defined in a `<name>.mod` file, where `<name>` is the module name (see also the <<names,section about module names>>).
|
||||||
|
|
||||||
|
Jetty module files are read from the typical xref:start/index.adoc#configure[configuration source directories], under the `modules/` subdirectory; from higher priority to lower priority:
|
||||||
|
|
||||||
|
* The `$JETTY_BASE/modules/` directory.
|
||||||
|
* If a directory is specified with the `--include-jetty-dir` option, its `modules/` subdirectory.
|
||||||
|
* The `$JETTY_HOME/modules/` directory.
|
||||||
|
|
||||||
|
The standard Jetty modules that Jetty provides out-of-the-box are under `$JETTY_HOME/modules/`.
|
||||||
|
|
||||||
|
xref:modules/custom.adoc[Custom Jetty modules] should be put under `$JETTY_BASE/modules/`.
|
||||||
|
|
||||||
|
[[names]]
|
||||||
|
== Module Names
|
||||||
|
|
||||||
|
A Jetty module has a unique name.
|
||||||
|
The module name is by default derived from the file name, so module file `acme.mod` identifies a module named `acme`.
|
||||||
|
|
||||||
|
However, a module file may specify a <<directive-provides,+[provides>>+] directive for a _virtual_ module, so that many modules may provide a different implementation for the same feature.
|
||||||
|
|
||||||
|
For example, among the standard modules provided by Jetty, the `server` module depends on the `logging` module, but there is no correspondent `logging.mod` file.
|
||||||
|
|
||||||
|
However, the `logging-jetty.mod` file has, among others, this section:
|
||||||
|
|
||||||
|
.logging-jetty.mod
|
||||||
|
----
|
||||||
|
[provides]
|
||||||
|
logging|default
|
||||||
|
----
|
||||||
|
|
||||||
|
This section means that the `logging-jetty.mod` file provides the virtual module `logging`, and it is the default provider.
|
||||||
|
|
||||||
|
The `logging-log4j2.mod` file has a similar section:
|
||||||
|
|
||||||
|
.logging-log4j2.mod
|
||||||
|
----
|
||||||
|
[provides]
|
||||||
|
logging
|
||||||
|
----
|
||||||
|
|
||||||
|
If there are no enabled modules that provide the `logging` virtual module, either explicitly or transitively, then the default provider is used, in this case `logging-jetty.mod`.
|
||||||
|
|
||||||
|
Otherwise, a module that provides the `logging` virtual module is explicitly or transitively enabled, and the default provider is not used.
|
||||||
|
|
||||||
|
[[components]]
|
||||||
|
== Module Components
|
||||||
|
|
||||||
|
A Jetty module may provide one or more Java components that implement a feature.
|
||||||
|
These Java components are nothing more than regular Java classes that are instantiated and configured via xref:xml/index.adoc[Jetty XML] files.
|
||||||
|
|
||||||
|
The Jetty XML file of a Jetty module may instantiate and assemble together its own components, or reference existing components from other Jetty modules to enhance or reconfigure them.
|
||||||
|
|
||||||
|
The Jetty module's XML files are read from the typical xref:start/index.adoc#configure[configuration source directories], under the `etc/` subdirectory; from higher priority to lower priority:
|
||||||
|
|
||||||
|
* The `$JETTY_BASE/etc/` directory.
|
||||||
|
* If a directory is specified with the `--include-jetty-dir` option, its `etc/` subdirectory.
|
||||||
|
* The `$JETTY_HOME/etc/` directory.
|
||||||
|
|
||||||
|
The standard Jetty modules XML files that Jetty provides out-of-the-box are under `$JETTY_HOME/etc/`.
|
||||||
|
|
||||||
|
For example, a Jetty XML file that allocates Jetty's `QueuedThreadPool` could be as simple as:
|
||||||
|
|
||||||
|
[,xml]
|
||||||
|
.jetty-threadpool.xml
|
||||||
|
----
|
||||||
|
<?xml version="1.0"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
<Configure>
|
||||||
|
<New id="threadPool" class="org.eclipse.jetty.util.thread.QueuedThreadPool">
|
||||||
|
<Set name="maxThreads" type="int">
|
||||||
|
<Property name="jetty.threadPool.maxThreads" default="256"/>
|
||||||
|
</Set>
|
||||||
|
</New>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
Note how the Jetty XML file above is allocating (with the `<New>` element) a `QueuedThreadPool` instance, giving it the unique `id` of `threadPool` (so that other modules can reference it, if they need to).
|
||||||
|
It is then calling the setter method `QueuedThreadPool.setMaxThreads(int)` with the value defined by the <<properties,module property>> `jetty.threadPool.maxThreads`; if the property value is not defined, it will have the default value of `256`.
|
||||||
|
|
||||||
|
This is nothing more than Java code in XML format with configurable properties support that can be leveraged by the xref:start/index.adoc[Jetty start mechanism].
|
||||||
|
|
||||||
|
The Jetty module's XML files make easy to instantiate and assemble Java components (just write the equivalent Java code in XML format), and make easy to configure them by declaring module properties that can be easily customized elsewhere (for example, in `+*.ini+` files as described in xref:start/index.adoc#configure-enable[this section], or on the command line as described in xref:start/index.adoc#start[this section]).
|
||||||
|
|
||||||
|
[IMPORTANT]
|
||||||
|
====
|
||||||
|
Remember that the standard Jetty XML files in `$JETTY_HOME/etc/` should not be modified.
|
||||||
|
|
||||||
|
Even if you need to modify a standard Jetty component, write a new Jetty XML file, save it under `$JETTY_BASE/etc/`, and create a xref:modules/custom.adoc[custom Jetty module] so that it gets processed when Jetty starts.
|
||||||
|
====
|
||||||
|
|
||||||
|
[[properties]]
|
||||||
|
== Module Properties
|
||||||
|
|
||||||
|
A Jetty module property is declared in the <<components,module XML file(s)>> via the `<Property>` element.
|
||||||
|
Modules properties are used to parametrize Jetty components so that you can customize their values when Jetty starts, rather than hard-coding it in the XML files.
|
||||||
|
|
||||||
|
NOTE: You can declare your own properties, but the `+jetty.*+` namespace is reserved.
|
||||||
|
|
||||||
|
A module property can be given a value in a Jetty module `[ini]` section (see <<directive-ini,here>>), in a `+*.ini+` file as described in xref:start/index.adoc#configure-enable[this section], or on the command line as described in xref:start/index.adoc#start[this section].
|
||||||
|
|
||||||
|
The syntax to specify a property value is the following:
|
||||||
|
|
||||||
|
<name>=<value>::
|
||||||
|
Sets the property value unconditionally.
|
||||||
|
<name>+=<value>::
|
||||||
|
Appends the value to the existing value.
|
||||||
|
This is useful to append a value to properties that accept a comma separated list of values, for example:
|
||||||
|
+
|
||||||
|
----
|
||||||
|
jetty.webapp.addServerClasses+=,com.acme
|
||||||
|
----
|
||||||
|
+
|
||||||
|
// TODO: check what happens if the property is empty and +=,value is done: is the comma stripped? If so add a sentence about this.
|
||||||
|
<name>?=<value>::
|
||||||
|
Sets the property value only if it is not already set.
|
||||||
|
This is useful to define default values, for example for "version" properties, where the "version" property can be explicitly configured to a newer version, but if it is not explicitly configured it will have a default version (see also xref:start/index.adoc#configure-custom-module[here]).
|
||||||
|
For example:
|
||||||
|
+
|
||||||
|
----
|
||||||
|
conscrypt.version?=2.5.1
|
||||||
|
jetty.sslContext.provider?=Conscrypt
|
||||||
|
----
|
||||||
|
|
||||||
|
[[directives]]
|
||||||
|
== Module Directives
|
||||||
|
|
||||||
|
Lines that start with `#` are comments.
|
||||||
|
|
||||||
|
[[directive-description]]
|
||||||
|
=== [description]
|
||||||
|
|
||||||
|
A text that describes the module.
|
||||||
|
|
||||||
|
This text will be shown by the xref:start/index.adoc#configure[Jetty start mechanism] when using the `--list-modules` command.
|
||||||
|
|
||||||
|
[[directive-tags]]
|
||||||
|
=== [tags]
|
||||||
|
|
||||||
|
A list of words that characterize the module.
|
||||||
|
|
||||||
|
Modules that have the same tags will be shown by the Jetty start mechanism when using the `--list-modules=<tag>` command.
|
||||||
|
|
||||||
|
.example.mod
|
||||||
|
----
|
||||||
|
[tags]
|
||||||
|
demo
|
||||||
|
webapp
|
||||||
|
jsp
|
||||||
|
----
|
||||||
|
|
||||||
|
[[directive-provides]]
|
||||||
|
=== [provides]
|
||||||
|
|
||||||
|
A module name with an optional `default` specifier.
|
||||||
|
|
||||||
|
As explained in the <<names,module name section>>, there can be many module files each providing a different implementation for the same feature.
|
||||||
|
|
||||||
|
The format is:
|
||||||
|
|
||||||
|
----
|
||||||
|
[provides]
|
||||||
|
<module_name>[|default]
|
||||||
|
----
|
||||||
|
|
||||||
|
where the `|default` part is optional and specifies that the module is the default provider.
|
||||||
|
|
||||||
|
[[directive-depends]]
|
||||||
|
=== [depends]
|
||||||
|
|
||||||
|
A list of module names that this module depends on.
|
||||||
|
|
||||||
|
For example, the standard module `http` depends on module `server`.
|
||||||
|
Enabling the `http` module also enables, transitively, the `server` module, since the `http` module cannot work without the `server` module; when the `server` module is transitively enabled, the modules it depends on will be transitively enabled, and so on recursively.
|
||||||
|
|
||||||
|
The `[depends]` directive establishes a https://en.wikipedia.org/wiki/Partially_ordered_set[_partial order_] relationship among modules so that enabled modules can be sorted and organized in a graph.
|
||||||
|
Circular dependencies are not allowed.
|
||||||
|
|
||||||
|
The order of the enabled modules is used to determine the processing of the configuration, for example the order of processing of the <<directive-files,+[files>>+] section, the order of processing of XML files defined in the <<directive-xml,+[xml>>+] section, etc.
|
||||||
|
|
||||||
|
[[directive-after]]
|
||||||
|
=== [after]
|
||||||
|
|
||||||
|
This directive indicates that this module is ordered after the listed module names, if they are enabled.
|
||||||
|
|
||||||
|
For example, module `https` is `[after]` module `http2`.
|
||||||
|
Enabling the `https` module _does not_ enable the `http2` module.
|
||||||
|
|
||||||
|
However, if the `http2` module is enabled (explicitly or transitively), then the `https` module is <<directive-depends,sorted>> _after_ the `http2` module.
|
||||||
|
In this way, you are guaranteed that the `https` module is processed after the `http2` module.
|
||||||
|
|
||||||
|
[[directive-before]]
|
||||||
|
=== [before]
|
||||||
|
|
||||||
|
This directive indicates that this module is ordered before the listed module names, if they are enabled.
|
||||||
|
|
||||||
|
One use of this directive is to create a prerequisite module without the need to modify the `depends` directive of an existing module.
|
||||||
|
For example, to create a custom `org.eclipse.jetty.server.Server` subclass instance to be used by the standard `server` module, without modifying the existing `server.mod` file nor the `jetty.xml` file that it uses. This can be achieved by creating the `custom-server` xref:modules/custom.adoc[Jetty custom module]:
|
||||||
|
|
||||||
|
.custom-server.mod
|
||||||
|
----
|
||||||
|
[description]
|
||||||
|
This module creates a custom Server subclass instance.
|
||||||
|
|
||||||
|
[before]
|
||||||
|
server
|
||||||
|
|
||||||
|
[xml]
|
||||||
|
etc/custom-server.xml
|
||||||
|
----
|
||||||
|
|
||||||
|
The `custom-server.xml` file is the following:
|
||||||
|
|
||||||
|
.custom-server.xml
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<?xml version="1.0"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
<Configure id="Server" class="com.acme.server.CustomJettyServer">
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
The presence of the `[before]` directive in `custom-server.mod` causes the processing of the `custom-server.xml` file to happen before the processing of the standard `jetty.xml` file referenced by the standard `server.mod` Jetty module.
|
||||||
|
|
||||||
|
Thus, the instance assigned to the `Server` identifier is your custom `com.acme.server.CustomJettyServer` instance from the `custom-server.xml` file; this instance is then used while processing the `jetty.xml` file.
|
||||||
|
|
||||||
|
[[directive-files]]
|
||||||
|
=== [files]
|
||||||
|
|
||||||
|
A list of paths (directories and/or files) that are necessary for the module, created or resolved when the module is enabled.
|
||||||
|
|
||||||
|
Each path may be of the following types:
|
||||||
|
|
||||||
|
Path Name::
|
||||||
|
A path name representing a file, or a directory if the path name ends with `/`, such as `webapps/`.
|
||||||
|
The file or directory will be created relative to `$JETTY_BASE`, if not already present.
|
||||||
|
+
|
||||||
|
For example:
|
||||||
|
+
|
||||||
|
----
|
||||||
|
[files]
|
||||||
|
logs/
|
||||||
|
----
|
||||||
|
|
||||||
|
Maven Artifact::
|
||||||
|
An URI representing a Maven artifact to be downloaded from Maven Central, if not already present.
|
||||||
|
Property expansion is supported.
|
||||||
|
+
|
||||||
|
The format is:
|
||||||
|
+
|
||||||
|
----
|
||||||
|
[files]
|
||||||
|
maven://<groupId>/<artifactId>/<version>[/<type>]|<pathName>
|
||||||
|
----
|
||||||
|
+
|
||||||
|
where `<type>` is optional, and `<pathName>` after the `|` is the path under `$JETTY_BASE` where the downloaded file should be saved.
|
||||||
|
+
|
||||||
|
For example:
|
||||||
|
+
|
||||||
|
[,options=nowrap]
|
||||||
|
----
|
||||||
|
[files]
|
||||||
|
maven://org.postgresql/postgresql/${postgresql-version}|lib/postgresql-${postgresql-version}.jar
|
||||||
|
----
|
||||||
|
|
||||||
|
BaseHome::
|
||||||
|
An URI representing a `$JETTY_HOME` resource to be copied in `$JETTY_BASE`, if not already present.
|
||||||
|
URIs of this type are typically only used by standard Jetty modules; custom modules should not need to use it.
|
||||||
|
+
|
||||||
|
The format is:
|
||||||
|
+
|
||||||
|
----
|
||||||
|
[files]
|
||||||
|
basehome:<jettyHomePathName>|<pathName>
|
||||||
|
----
|
||||||
|
+
|
||||||
|
For example:
|
||||||
|
+
|
||||||
|
----
|
||||||
|
[files]
|
||||||
|
basehome:modules/demo.d/demo-moved-context.xml|webapps/demo-moved-context.xml
|
||||||
|
----
|
||||||
|
|
||||||
|
HTTP URL::
|
||||||
|
|
||||||
|
An `http://` or `https://` URL to be downloaded, if not already present.
|
||||||
|
+
|
||||||
|
The format is:
|
||||||
|
+
|
||||||
|
----
|
||||||
|
[files]
|
||||||
|
<httpURL>|<pathName>
|
||||||
|
----
|
||||||
|
+
|
||||||
|
For example:
|
||||||
|
+
|
||||||
|
----
|
||||||
|
[files]
|
||||||
|
https://acme.com/favicon.ico|webapps/acme/favicon.ico
|
||||||
|
----
|
||||||
|
|
||||||
|
[[directive-libs]]
|
||||||
|
=== [libs]
|
||||||
|
|
||||||
|
A list of paths, relative to the xref:start/index.adoc#configure[configuration source directories], of `+*.jar+` library files and/or directories that are added to the server class-path (or module-path when xref:start/start-jpms.adoc[running in JPMS mode]).
|
||||||
|
|
||||||
|
The `[libs]` section if often used in conjunction with the `[files]` section.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
----
|
||||||
|
[files]
|
||||||
|
maven://org.postgresql/postgresql/${postgresql-version}|lib/postgresql-${postgresql-version}.jar
|
||||||
|
|
||||||
|
[libs]
|
||||||
|
lib/postgresql-${postgresql-version}.jar
|
||||||
|
----
|
||||||
|
|
||||||
|
The `postgresql-<version>.jar` artifact is downloaded from Maven Central, if not already present, into the `$JETTY_BASE/lib/` directory when the module is enabled.
|
||||||
|
|
||||||
|
When Jetty starts, the `$JETTY_BASE/lib/postgresql-<version>.jar` will be in the server class-path (or module-path).
|
||||||
|
|
||||||
|
[[directive-xml]]
|
||||||
|
=== [xml]
|
||||||
|
|
||||||
|
A list of paths, relative to the xref:start/index.adoc#configure[configuration source directories], of Jetty `+*.xml+` files that are passed as program arguments to be processed when Jetty starts (see the xref:start/index.adoc#start-xml[section about assembling Jetty components]).
|
||||||
|
|
||||||
|
Jetty XML files are read from the typical xref:start/index.adoc#configure[configuration source directories], under the `etc/` subdirectory.
|
||||||
|
Standard Jetty XML files are under `$JETTY_HOME/etc/`, while custom Jetty XML files are typically under `$JETTY_BASE/etc/`.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
----
|
||||||
|
[xml]
|
||||||
|
etc/custom/components.xml
|
||||||
|
----
|
||||||
|
|
||||||
|
[[directive-ini]]
|
||||||
|
=== [ini]
|
||||||
|
|
||||||
|
A list of program arguments to pass to the command line when Jetty is started.
|
||||||
|
|
||||||
|
The program arguments may include any command line option (see xref:start/index.adoc#reference[here] for the list of command line options), <<properties,module properties>> and/or <<components,module XML files>>.
|
||||||
|
|
||||||
|
A property defined in the `[ini]` section is available in the `+*.mod+` module file for property expansion, for example:
|
||||||
|
|
||||||
|
----
|
||||||
|
[ini]
|
||||||
|
postgresql-version?=42.2.18
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
lib/postgresql-${postgresql-version}.jar
|
||||||
|
----
|
||||||
|
|
||||||
|
In the example above, the `[lib]` section contains `$\{postgresql-version}`, a reference to property `postgresql-version` whose value is defined in the `[ini]` section.
|
||||||
|
The expression `${<property>}` _expands_ the property replacing the expression with the property value.
|
||||||
|
|
||||||
|
See also the xref:start/start-jpms.adoc[JPMS section] for additional examples about the `[ini]` section.
|
||||||
|
|
||||||
|
[[directive-ini-template]]
|
||||||
|
=== [ini-template]
|
||||||
|
|
||||||
|
A list of properties to be copied in the `+*.ini+` file generated when xref:start/index.adoc#configure-enable[the module is enabled].
|
||||||
|
|
||||||
|
The list of properties is derived from the <<components,module XML file(s)>> that declare them.
|
||||||
|
|
||||||
|
The properties are typically assigned their default value and commented out, so that it is evident which properties have been uncommented and customized with a non-default value.
|
||||||
|
|
||||||
|
[[directive-exec]]
|
||||||
|
=== [exec]
|
||||||
|
|
||||||
|
A list of JVM command line options and/or system properties passed to a forked JVM.
|
||||||
|
|
||||||
|
When the `[exec]` section is present, the JVM running the Jetty start mechanism will fork another JVM, passing the JVM command line options and system properties listed in the `[exec]` sections of the enabled modules.
|
||||||
|
|
||||||
|
This is necessary because JVM options such as `-Xmx` (that specifies the max JVM heap size) cannot be changed in a running JVM.
|
||||||
|
For an example, see xref:start/index.adoc#configure-custom-module-exec[this section].
|
||||||
|
|
||||||
|
You can avoid that the Jetty start mechanism forks the second JVM, as explained in xref:start/index.adoc#configure-dry-run[this section].
|
||||||
|
|
||||||
|
[[directive-jpms]]
|
||||||
|
=== [jpms]
|
||||||
|
|
||||||
|
A list of JVM command line options related to the Java Module System.
|
||||||
|
|
||||||
|
This section is processed only when Jetty is xref:start/start-jpms.adoc[started in JPMS mode].
|
||||||
|
|
||||||
|
The directives are:
|
||||||
|
|
||||||
|
add-modules::
|
||||||
|
Equivalent to the JVM option `--add-modules`.
|
||||||
|
The format is:
|
||||||
|
+
|
||||||
|
----
|
||||||
|
[jpms]
|
||||||
|
add-modules: <module>(,<module>)*
|
||||||
|
----
|
||||||
|
+
|
||||||
|
where `module` is a JPMS module name.
|
||||||
|
|
||||||
|
patch-module::
|
||||||
|
Equivalent to the JVM option `--patch-module`.
|
||||||
|
The format is:
|
||||||
|
+
|
||||||
|
----
|
||||||
|
[jpms]
|
||||||
|
patch-module: <module>=<file>(:<file>)*
|
||||||
|
----
|
||||||
|
where `module` is a JPMS module name.
|
||||||
|
|
||||||
|
add-opens::
|
||||||
|
Equivalent to the JVM option `--add-opens`.
|
||||||
|
The format is:
|
||||||
|
+
|
||||||
|
----
|
||||||
|
[jpms]
|
||||||
|
add-opens: <module>/<package>=<target-module>(,<target-module>)*
|
||||||
|
----
|
||||||
|
where `module` and `target-module` are a JPMS module names.
|
||||||
|
|
||||||
|
add-exports::
|
||||||
|
Equivalent to the JVM option `--add-exports`.
|
||||||
|
The format is:
|
||||||
|
+
|
||||||
|
----
|
||||||
|
[jpms]
|
||||||
|
add-exports: <module>/<package>=<target-module>(,<target-module>)*
|
||||||
|
----
|
||||||
|
where `module` and `target-module` are a JPMS module names.
|
||||||
|
|
||||||
|
add-reads::
|
||||||
|
Equivalent to the JVM option `--add-exports`.
|
||||||
|
The format is:
|
||||||
|
+
|
||||||
|
----
|
||||||
|
[jpms]
|
||||||
|
add-reads: <module>=<target-module>(,<target-module>)*
|
||||||
|
----
|
||||||
|
where `module` and `target-module` are a JPMS module names.
|
||||||
|
|
||||||
|
[[directive-license]]
|
||||||
|
=== [license]
|
||||||
|
|
||||||
|
The license under which the module is released.
|
||||||
|
|
||||||
|
A Jetty module may be released under a license that is different from Jetty's, or use libraries that require end-users to accept their licenses in order to be used.
|
||||||
|
|
||||||
|
You can put the license text in the `[license]` section, and when the Jetty module is enabled the license text will be printed on the terminal, and the user prompted to accept the license.
|
||||||
|
If the user does not accept the license, the module will not be enabled.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
----
|
||||||
|
[license]
|
||||||
|
Acme Project is an open source project hosted on GitHub
|
||||||
|
and released under the Apache 2.0 license.
|
||||||
|
https://www.apache.org/licenses/LICENSE-2.0.txt
|
||||||
|
----
|
||||||
|
|
||||||
|
[[directive-version]]
|
||||||
|
=== [version]
|
||||||
|
|
||||||
|
The minimum Jetty version for which this module is valid.
|
||||||
|
|
||||||
|
For example, a module may only be valid for Jetty 10 and later, but not for earlier Jetty versions (because it references components that have been introduced in Jetty 10).
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
----
|
||||||
|
[version]
|
||||||
|
10.0
|
||||||
|
----
|
||||||
|
|
||||||
|
A Jetty module with such a section will only work for Jetty 10.0.x or later.
|
|
@ -0,0 +1,564 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Standard Modules
|
||||||
|
|
||||||
|
[[alpn]]
|
||||||
|
== Module `alpn`
|
||||||
|
|
||||||
|
The `alpn` module enables support for the ALPN negotiation mechanism of the TLS protocol.
|
||||||
|
|
||||||
|
You can configure the list of application protocols negotiated by the ALPN mechanism, as well as the default protocol to use if the ALPN negotiation fails (for example, the client does not support ALPN).
|
||||||
|
|
||||||
|
The module properties are:
|
||||||
|
|
||||||
|
----
|
||||||
|
include::{jetty-home}/modules/alpn.mod[tags=documentation]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[bytebufferpool]]
|
||||||
|
== Module `bytebufferpool`
|
||||||
|
|
||||||
|
The `bytebufferpool` module allows you to configure the server-wide `ByteBuffer` pool.
|
||||||
|
Pooling ``ByteBuffer``s results in less memory usage and less pressure on the Garbage Collector.
|
||||||
|
|
||||||
|
``ByteBuffer``s are pooled in _buckets_; each bucket as a capacity that is a multiple of a capacity factor that you can configure.
|
||||||
|
For example, if a request for a `ByteBuffer` of capacity 2000 is requested, and the capacity factor is 1024, then the pool will allocate a buffer from the second bucket, of capacity 2048 (1024 * 2).
|
||||||
|
|
||||||
|
Applications that need to sustain many concurrent requests -- or load spikes -- may require many buffers during peak load. These buffers will remain pooled once the system transitions to a lighter load (or becomes idle), and it may be undesirable to retain a lot of memory for an idle system.
|
||||||
|
|
||||||
|
It is possible to configure the max heap memory and the max direct memory that the pool retains.
|
||||||
|
Excess buffers will not be pooled and will be eventually garbage collected.
|
||||||
|
|
||||||
|
The module file is `$JETTY_HOME/modules/bytebufferpool.mod`:
|
||||||
|
|
||||||
|
----
|
||||||
|
include::{jetty-home}/modules/bytebufferpool.mod[]
|
||||||
|
----
|
||||||
|
|
||||||
|
Among the configurable properties, the most relevant are:
|
||||||
|
|
||||||
|
`jetty.byteBufferPool.maxHeapMemory`::
|
||||||
|
This property allows you to cap the max heap memory retained by the pool.
|
||||||
|
|
||||||
|
`jetty.byteBufferPool.maxDirectMemory`::
|
||||||
|
This property allows you to cap the max direct memory retained by the pool.
|
||||||
|
|
||||||
|
[[console-capture]]
|
||||||
|
== Module `console-capture`
|
||||||
|
|
||||||
|
The `console-capture` module captures `System.out` and `System.err` output and appends it to a rolling file.
|
||||||
|
|
||||||
|
The file is rolled every day at the midnight of the configured timezone.
|
||||||
|
Old, rolled files are kept for the number of days specified by the `jetty.console-capture.retainDays` property.
|
||||||
|
|
||||||
|
The module properties are:
|
||||||
|
|
||||||
|
----
|
||||||
|
include::{jetty-home}/modules/console-capture.mod[tags=documentation]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[deploy]]
|
||||||
|
== Module `deploy`
|
||||||
|
|
||||||
|
The `deploy` module provides the deployment feature through a `DeploymentManager` component that watches a directory for changes (see xref:deploy/index.adoc[how to deploy web applications] for more information).
|
||||||
|
|
||||||
|
Files or directories added in this monitored directory cause the `DeploymentManager` to deploy them as web applications; updating files already existing in this monitored directory cause the `DeploymentManager` to re-deploy the correspondent web application; removing files in this monitored directory cause the `DeploymentManager` to undeploy the correspondent web application (see also xref:deploy/index.adoc#rules[here] for more information).
|
||||||
|
|
||||||
|
The module file is `$JETTY_HOME/modules/deploy.mod`:
|
||||||
|
|
||||||
|
----
|
||||||
|
include::{jetty-home}/modules/deploy.mod[]
|
||||||
|
----
|
||||||
|
|
||||||
|
Among the configurable properties, the most relevant are:
|
||||||
|
|
||||||
|
`jetty.deploy.monitoredDir`::
|
||||||
|
The name of the monitored directory.
|
||||||
|
`jetty.deploy.scanInterval`::
|
||||||
|
The scan period in seconds, that is how frequently the `DeploymentManager` wakes up to scan the monitored directory for changes.
|
||||||
|
Setting `jetty.deploy.scanInterval=0` disabled _hot_ deployment so that only static deployment will be possible (see also xref:deploy/index.adoc#hot-static[here] for more information).
|
||||||
|
|
||||||
|
[[http]]
|
||||||
|
== Module `http`
|
||||||
|
|
||||||
|
The `http` module provides the clear-text connector and support for the clear-text HTTP/1.1 protocol, and depends on the <<server,`server` module>>.
|
||||||
|
|
||||||
|
The module properties to configure the clear-text connector are:
|
||||||
|
|
||||||
|
----
|
||||||
|
include::{jetty-home}/modules/http.mod[tags=documentation]
|
||||||
|
----
|
||||||
|
|
||||||
|
Among the configurable properties, the most relevant are:
|
||||||
|
|
||||||
|
`jetty.http.port`::
|
||||||
|
The network port that Jetty listens to for clear-text HTTP/1.1 connections -- default `8080`.
|
||||||
|
`jetty.http.idleTimeout`::
|
||||||
|
The amount of time a connection can be idle (i.e. no bytes received and no bytes sent) until the server decides to close it to save resources -- default `30` seconds.
|
||||||
|
`jetty.http.acceptors`::
|
||||||
|
The number of threads that compete to accept connections -- default 1. Use -1 to let the accept heuristic decides the value; the current heuristic calculates a value based on the number of cores).
|
||||||
|
Refer to <<http-acceptors,this section>> for more information about acceptor threads.
|
||||||
|
`jetty.http.selectors`::
|
||||||
|
The number of NIO selectors (with an associated thread) that manage connections -- default -1 (i.e. a select heuristic decides the value; the current heuristic calculates a value based on the number of cores).
|
||||||
|
|
||||||
|
[[http-acceptors]]
|
||||||
|
=== Configuration of Acceptors
|
||||||
|
|
||||||
|
Accepting connections from remote clients may be configured as a blocking operation, or a non-blocking operation.
|
||||||
|
|
||||||
|
When accepting connections is configured as a blocking operation (the number of acceptors is greater than zero), a thread is blocked in the `accept()` call until a connection is accepted, and other acceptor threads (if any) are blocked on the lock acquired by the accepting thread just before the `accept()` call.
|
||||||
|
|
||||||
|
When the accepting thread accepts a connection, it performs a little processing of the just accepted connection, before forwarding it to other components.
|
||||||
|
|
||||||
|
During this little processing other connections may be established; if there is only one accepting thread, the newly established connections are waiting for the accepting thread to finish the processing of the previously accepted connection and call again `accept()`.
|
||||||
|
|
||||||
|
Servers that manage a very high number of connections that may (naturally) come and go, or that handle inefficient protocols that open and close connections very frequently (such as HTTP/1.0) may benefit of an increased number of acceptor threads, so that when one acceptor thread processes a just accepted connection, another acceptor thread can immediately take over accepting connections.
|
||||||
|
|
||||||
|
When accepting connections is configured as a non-blocking operation (the number of acceptors is zero), then the server socket is set in non-blocking mode and added to a NIO selector.
|
||||||
|
In this way, no dedicated acceptor threads exist: the work of accepting connections is performed by the selector thread.
|
||||||
|
|
||||||
|
[[http-selectors]]
|
||||||
|
=== Configuration of Selectors
|
||||||
|
|
||||||
|
Performing a NIO `select()` call is a blocking operation, where the selecting thread is blocked in the `select()` call until at least one connection is ready to be processed for an I/O operation.
|
||||||
|
There are 4 I/O operations: ready to be accepted, ready to be connected, ready to be read and ready to be written.
|
||||||
|
|
||||||
|
A single NIO selector can manage thousands of connections, with the assumption that not many of them will be ready at the same time.
|
||||||
|
|
||||||
|
For a single NIO selector, the ratio between the average number of selected connections over the total number of connections for every `select()` call depends heavily on the protocol but also on the application.
|
||||||
|
|
||||||
|
Multiplexed TCP protocols such as HTTP/2 tend to be busier than duplex protocols such as HTTP/1.1, leading to a higher ratio.
|
||||||
|
|
||||||
|
REST applications that exchange many little JSON messages tend to be busier than file server applications, leading to a higher ratio.
|
||||||
|
|
||||||
|
The higher the ratio, the higher the number of selectors you want to have, compatibly with the number of cores -- there is no point in having 64 selector threads on a single core hardware.
|
||||||
|
|
||||||
|
[[http2]]
|
||||||
|
== Module `http2`
|
||||||
|
|
||||||
|
The `http2` module enables support for the secure HTTP/2 protocol.
|
||||||
|
|
||||||
|
The module properties are:
|
||||||
|
|
||||||
|
----
|
||||||
|
include::{jetty-home}/modules/http2.mod[tags=documentation]
|
||||||
|
----
|
||||||
|
|
||||||
|
// tag::rate-control[]
|
||||||
|
The `jetty.http2.rateControl.maxEventsPerSecond` property controls the number of "bad" or "unnecessary" frames that a client may send before the server closes the connection (with code https://tools.ietf.org/html/rfc7540#section-7[`ENHANCE_YOUR_CALM`]) to avoid a denial of service.
|
||||||
|
|
||||||
|
For example, an attacker may send empty `SETTINGS` frames to a server in a tight loop.
|
||||||
|
While the `SETTINGS` frames don't change the server configuration and each of them is somehow harmless, the server will be very busy processing them because they are sent by the attacker one after the other, causing a CPU spike and eventually a denial of service (as all CPUs will be busy processing empty `SETTINGS` frames).
|
||||||
|
|
||||||
|
The same attack may be performed with `PRIORITY` frames, empty `DATA` frames, `PING` frames, etc.
|
||||||
|
|
||||||
|
[[http2c]]
|
||||||
|
== Module `http2c`
|
||||||
|
|
||||||
|
The `http2c` module enables support for the clear-text HTTP/2 protocol.
|
||||||
|
|
||||||
|
The module properties are:
|
||||||
|
|
||||||
|
----
|
||||||
|
include::{jetty-home}/modules/http2c.mod[tags=documentation]
|
||||||
|
----
|
||||||
|
|
||||||
|
The `jetty.http2.rateControl.maxEventsPerSecond` property controls the number of "bad" or "unnecessary" frames that a client may send before the server closes the connection (with code https://tools.ietf.org/html/rfc7540#section-7[`ENHANCE_YOUR_CALM`]) to avoid a denial of service.
|
||||||
|
|
||||||
|
For example, an attacker may send empty `SETTINGS` frames to a server in a tight loop.
|
||||||
|
While the `SETTINGS` frames don't change the server configuration and each of them is somehow harmless, the server will be very busy processing them because they are sent by the attacker one after the other, causing a CPU spike and eventually a denial of service (as all CPUs will be busy processing empty `SETTINGS` frames).
|
||||||
|
|
||||||
|
The same attack may be performed with `PRIORITY` frames, empty `DATA` frames, `PING` frames, etc.
|
||||||
|
|
||||||
|
[[http3]]
|
||||||
|
== Module `http3`
|
||||||
|
|
||||||
|
The `http3` module enables support for the HTTP/3 protocol.
|
||||||
|
|
||||||
|
The module properties are:
|
||||||
|
|
||||||
|
----
|
||||||
|
include::{jetty-home}/modules/http3.mod[tags=documentation]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[http-forwarded]]
|
||||||
|
== Module `http-forwarded`
|
||||||
|
|
||||||
|
The `http-forwarded` module provides support for processing the `Forwarded` HTTP header (defined in https://tools.ietf.org/html/rfc7239[RFC 7239]) and the now obsoleted `X-Forwarded-*` HTTP headers.
|
||||||
|
|
||||||
|
The module properties are:
|
||||||
|
|
||||||
|
----
|
||||||
|
include::{jetty-home}/modules/http-forwarded.mod[tags=documentation]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[https]]
|
||||||
|
== Module `https`
|
||||||
|
|
||||||
|
The `https` module provides the HTTP/1.1 protocol to the <<ssl,`ssl` module>>.
|
||||||
|
|
||||||
|
The module file is `$JETTY_HOME/modules/https.mod`:
|
||||||
|
|
||||||
|
----
|
||||||
|
include::{jetty-home}/modules/https.mod[]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[jmx-remote]]
|
||||||
|
== Module `jmx-remote`
|
||||||
|
|
||||||
|
The `jmx-remote` module provides remote access to JMX clients.
|
||||||
|
|
||||||
|
The module properties to configure remote JMX connector are:
|
||||||
|
|
||||||
|
----
|
||||||
|
include::{jetty-home}/modules/jmx-remote.mod[tags=documentation]
|
||||||
|
----
|
||||||
|
|
||||||
|
The system property `java.rmi.server.hostname` is specified with the usual notation, prepending a `-D` in front of the system property name.
|
||||||
|
|
||||||
|
The system property `java.rmi.server.hostname` is uncommented because it is necessary in the default configuration -- most systems do not have the local name resolution configured properly for remote access.
|
||||||
|
|
||||||
|
As an example, in a Linux machine named `beryl`, the `/etc/hosts` file may contain these entries:
|
||||||
|
|
||||||
|
----
|
||||||
|
127.0.0.1 localhost
|
||||||
|
127.0.1.1 beryl
|
||||||
|
----
|
||||||
|
|
||||||
|
If the system property `java.rmi.server.hostname` is not specified, the RMI implementation uses the host name `beryl` to figure out the IP address to store in the RMI stub, in this case `127.0.1.1`.
|
||||||
|
However, we the RMI server is configured to bind to `localhost`, i.e. `127.0.0.1`.
|
||||||
|
|
||||||
|
If the system property `java.rmi.server.hostname` is not specified, the RMI client will try to connect to `127.0.1.1` (because that's what in the RMI stub) and fail because nothing is listening on that address.
|
||||||
|
|
||||||
|
[[requestlog]]
|
||||||
|
== Module `requestlog`
|
||||||
|
|
||||||
|
The `requestlog` module provides HTTP request/response logging in the standard https://en.wikipedia.org/wiki/Common_Log_Format[NCSA format], or in a custom format of your choice.
|
||||||
|
|
||||||
|
The module properties are:
|
||||||
|
|
||||||
|
----
|
||||||
|
include::{jetty-home}/modules/requestlog.mod[tags=documentation]
|
||||||
|
----
|
||||||
|
|
||||||
|
The property `jetty.requestlog.formatString` can be customized using format codes.
|
||||||
|
|
||||||
|
javadoc::code:partial$org/eclipse/jetty/server/CustomRequestLog.java[]
|
||||||
|
|
||||||
|
[[server]]
|
||||||
|
== Module `server`
|
||||||
|
|
||||||
|
The `server` module provides generic server support, and configures generic HTTP properties that apply to all HTTP protocols, the scheduler properties and the server specific properties.
|
||||||
|
|
||||||
|
The `server` module depends on the <<threadpool,`threadpool` module>>, the <<bytebufferpool,`bytebufferpool` module>> and the xref:server/index.adoc#logging[`logging` module].
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
The `server` module configures the shared parameters for generic HTTP handling, but does not enable any specific network protocol. You have to explicitly enable the protocols you want to support by enabling, for example, the <<http,`http` module>> for clear-text HTTP/1.1 support, or the <<http2,`http2` module>> for secure HTTP/2 support, etc.
|
||||||
|
|
||||||
|
See also the xref:protocols/index.adoc[protocols section] for more information about the supported protocols.
|
||||||
|
====
|
||||||
|
|
||||||
|
[[server-http-config]]
|
||||||
|
=== HTTP Configuration Properties
|
||||||
|
|
||||||
|
The module properties to configure generic HTTP properties are listed below. Mostly they frequently apply to HTTP/1, HTTP/2 and HTTP/3, but some parameters are version specific:
|
||||||
|
|
||||||
|
----
|
||||||
|
include::{jetty-home}/modules/server.mod[tags=documentation-http-config]
|
||||||
|
----
|
||||||
|
|
||||||
|
Among the configurable properties, the most relevant are:
|
||||||
|
|
||||||
|
`jetty.httpConfig.headerCacheSize`::
|
||||||
|
The header cache is used when parsing HTTP/1 to more efficiently handle fields that are repeated in every request on a connection. If the server does not receive persistent connection or infrequent repeated fields, then there may be a performance gain in reducing the cache size. If large fields are frequently repeated, then a large cache may be beneficial.
|
||||||
|
|
||||||
|
`jetty.httpConfig.delayDispatchUntilContent`::
|
||||||
|
It is not uncommon for the network packets containing a request header to arrive before packets that contain the data of any request body. In such cases it may be beneficial for overall performance to delay dispatching the request to be handled until the first data packet arrives, as this may avoid blocking the handling thread. However, if minimum latency for receiving the request without content is important, then this parameter can be set to false.
|
||||||
|
|
||||||
|
`jetty.httpConfig.sendServerVersion`::
|
||||||
|
Whether you want to send the `Server` header in every HTTP response:
|
||||||
|
+
|
||||||
|
[,screen,subs=normal]
|
||||||
|
----
|
||||||
|
HTTP/1.1 200 OK
|
||||||
|
Content-Length: 0
|
||||||
|
Server: Jetty({version})
|
||||||
|
----
|
||||||
|
|
||||||
|
[[server-config]]
|
||||||
|
=== Server Configuration Properties
|
||||||
|
|
||||||
|
The module properties to configure the Jetty server are:
|
||||||
|
|
||||||
|
----
|
||||||
|
include::{jetty-home}/modules/server.mod[tags=documentation-server-config]
|
||||||
|
----
|
||||||
|
|
||||||
|
Among the configurable properties, the most relevant are:
|
||||||
|
|
||||||
|
`jetty.server.dumpAfterStart`::
|
||||||
|
Whether to perform a `Server.dump()` operation after the `Server` has started.
|
||||||
|
The output of the dump operation is sent to `System.err`.
|
||||||
|
See also the xref:troubleshooting/index.adoc#dump[Jetty Server Dump] section for more information.
|
||||||
|
|
||||||
|
`jetty.server.dumpBeforeStop`::
|
||||||
|
Whether to perform a `Server.dump()` operation before the `Server` stops.
|
||||||
|
The output of the dump operation is sent to `System.err`.
|
||||||
|
See also the xref:troubleshooting/index.adoc#dump[Jetty Server Dump] section for more information.
|
||||||
|
|
||||||
|
`jetty.server.stopAtShutdown`::
|
||||||
|
Whether to call `Server.stop()` through a JVM shutdown hook when the JVM exits.
|
||||||
|
|
||||||
|
[[server-compliance]]
|
||||||
|
=== Server Compliance Properties
|
||||||
|
|
||||||
|
The Jetty server strives to keep up with the latest https://en.wikipedia.org/wiki/Request_for_Comments[IETF RFC]s for compliance with internet specifications, which are periodically updated. When possible, Jetty will support backwards compatibility by providing compliance modes that can be configured to allow violations of the current specifications that may have been allowed in obsoleted specifications.
|
||||||
|
The module properties to configure the Jetty server compliance are:
|
||||||
|
|
||||||
|
----
|
||||||
|
include::{jetty-home}/modules/server.mod[tags=documentation-server-compliance]
|
||||||
|
----
|
||||||
|
|
||||||
|
Among the configurable properties, the most relevant are:
|
||||||
|
|
||||||
|
`jetty.httpConfig.compliance`::
|
||||||
|
Configures the compliance to HTTP specifications.
|
||||||
|
The value could be:
|
||||||
|
|
||||||
|
* One of the predefined link:{javadoc-url}/org/eclipse/jetty/http/HttpCompliance.html[`HttpCompliance`] constants, such as `RFC7230` or `RFC2616`.
|
||||||
|
For example: `jetty.httpConfig.compliance=RFC2616`.
|
||||||
|
* A comma-separated list of violations to allow or forbid, as specified by the link:{javadoc-url}/org/eclipse/jetty/http/HttpCompliance.html#from(java.lang.String)[`HttpCompliance.from(String)`] method.
|
||||||
|
For example, `jetty.httpConfig.compliance=RFC7230,MULTIPLE_CONTENT_LENGTHS` means that the HTTP compliance is that defined by `RFC7230`, but also allows the `HttpCompliance.Violation.MULTIPLE_CONTENT_LENGTHS`, so that requests that have multiple `Content-Length` headers are accepted (they would be rejected when using just `HttpCompliance.RFC7230`).
|
||||||
|
+
|
||||||
|
For more information about `HttpCompliance` see also xref:programming-guide:server/compliance.adoc#http[this section].
|
||||||
|
|
||||||
|
`jetty.httpConfig.uriCompliance`::
|
||||||
|
Configures the compliance to URI specifications.
|
||||||
|
The value could be:
|
||||||
|
|
||||||
|
* One of the predefined link:{javadoc-url}/org/eclipse/jetty/http/UriCompliance.html[`UriCompliance`] constants, such as `DEFAULT` or `RFC3986`.
|
||||||
|
For example: `jetty.httpConfig.compliance=RFC3986`.
|
||||||
|
* A comma-separated list of violations to allow or forbid, as specified by the link:{javadoc-url}/org/eclipse/jetty/http/UriCompliance.html#from(java.lang.String)[`UriCompliance.from(String)`] method.
|
||||||
|
For example, `jetty.httpConfig.uriCompliance=RFC3986,-AMBIGUOUS_PATH_SEPARATOR` means that the URI compliance is that defined by `RFC3986`, but also does not allow the `UriCompliance.Violation.AMBIGUOUS_PATH_SEPARATOR`, so that requests that have URIs such as `/foo/bar%2Fbaz` (where `%2F` is the URL-encoded `/` character) are rejected (they would be accepted when using just `UriCompliance.RFC3986`).
|
||||||
|
+
|
||||||
|
For more information about `UriCompliance` see also xref:programming-guide:server/compliance.adoc#uri[this section].
|
||||||
|
|
||||||
|
`jetty.httpConfig.requestCookieCompliance`::
|
||||||
|
`jetty.httpConfig.responseCookieCompliance`::
|
||||||
|
Configures the compliance to HTTP cookie specifications.
|
||||||
|
The value could be:
|
||||||
|
|
||||||
|
* One of the predefined link:{javadoc-url}/org/eclipse/jetty/http/CookieCompliance.html[`CookieCompliance`] constants, such as `RFC6265`.
|
||||||
|
For example: `jetty.httpConfig.compliance=RFC6265`.
|
||||||
|
* A comma-separated list of violations to allow or forbid, as specified by the link:{javadoc-url}/org/eclipse/jetty/http/CookieCompliance.html#from(java.lang.String)[`CookieCompliance.from(String)`] method.
|
||||||
|
For example, `jetty.httpConfig.requestCookieCompliance=RFC6265,-RESERVED_NAMES_NOT_DOLLAR_PREFIXED` means that the cookie compliance is that defined by `RFC6265`, but also does not allow the `CookieCompliance.Violation.RESERVED_NAMES_NOT_DOLLAR_PREFIXED`, so that requests that have cookie headers such as `Cookie: $foo=bar` are rejected (they would be accepted when using just `CookieCompliance.RFC6265`).
|
||||||
|
+
|
||||||
|
For more information about `CookieCompliance` see also xref:programming-guide:server/compliance.adoc#cookie[this section].
|
||||||
|
|
||||||
|
[[scheduler-config]]
|
||||||
|
=== Server Scheduler Configuration Properties
|
||||||
|
|
||||||
|
The module properties to configure the Jetty server scheduler are:
|
||||||
|
|
||||||
|
----
|
||||||
|
include::{jetty-home}/modules/server.mod[tags=documentation-scheduler-config]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[ssl]]
|
||||||
|
== Module `ssl`
|
||||||
|
|
||||||
|
The `ssl` module provides the secure connector, and allows you to configure the KeyStore properties and the TLS parameters, and depends on the <<server,`server` module>>.
|
||||||
|
|
||||||
|
[[ssl-connector]]
|
||||||
|
=== Secure Connector Properties
|
||||||
|
|
||||||
|
The module properties to configure the secure connector are:
|
||||||
|
|
||||||
|
----
|
||||||
|
include::{jetty-home}/modules/ssl.mod[tags=documentation-connector]
|
||||||
|
----
|
||||||
|
|
||||||
|
Among the configurable properties, the most relevant are:
|
||||||
|
|
||||||
|
`jetty.ssl.port`::
|
||||||
|
The network port that Jetty listens to for secure connections -- default `8443`.
|
||||||
|
`jetty.ssl.idleTimeout`::
|
||||||
|
The amount of time a connection can be idle (i.e. no bytes received and no bytes sent) until the server decides to close it to save resources -- default `30000` milliseconds.
|
||||||
|
`jetty.ssl.acceptors`::
|
||||||
|
The number of threads that compete to accept connections -- default 1. Use -1 to let the accept heuristic decides the value; the current heuristic calculates a value based on the number of cores).
|
||||||
|
Refer to <<http-acceptors,this section>> for more information about acceptor threads.
|
||||||
|
`jetty.ssl.selectors`::
|
||||||
|
The number of NIO selectors (with an associated thread) that manage connections -- default -1 (i.e. a select heuristic decides the value; the current heuristic calculates a value based on the number of cores).
|
||||||
|
Refer to <<http-selectors,this section>> for more information about selector threads.
|
||||||
|
|
||||||
|
The module properties to configure the KeyStore and TLS parameters are:
|
||||||
|
|
||||||
|
----
|
||||||
|
include::{jetty-home}/modules/ssl.mod[tags=documentation-ssl-context]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[ssl-keystore-tls]]
|
||||||
|
=== KeyStore Properties and TLS Properties
|
||||||
|
|
||||||
|
Among the configurable properties, the most relevant are:
|
||||||
|
|
||||||
|
`jetty.sslContext.keyStorePath`::
|
||||||
|
The KeyStore path on the file system, either an absolute path or a relative path to `$JETTY_BASE` -- defaults to `$JETTY_BASE/etc/keystore.p12`.
|
||||||
|
`jetty.sslContext.keyStorePassword`::
|
||||||
|
The KeyStore password, which you want to explicitly configure.
|
||||||
|
The password may be obfuscated with the xref:jaas/index.adoc#og-password[Jetty Password Tool].
|
||||||
|
|
||||||
|
If you need to configure client certificate authentication, you want to configure one of these properties (they are mutually exclusive):
|
||||||
|
|
||||||
|
`jetty.sslContext.needClientAuth`::
|
||||||
|
Whether client certificate authentication should be required.
|
||||||
|
`jetty.sslContext.wantClientAuth`::
|
||||||
|
Whether client certificate authentication should be requested.
|
||||||
|
|
||||||
|
If you configure client certificate authentication, you need to configure and distribute a client KeyStore as explained in xref:keystore/index.adoc#client-authn[this section].
|
||||||
|
|
||||||
|
[[ssl-reload]]
|
||||||
|
== Module `ssl-reload`
|
||||||
|
|
||||||
|
The `ssl-reload` module provides a periodic scanning of the directory where the KeyStore file resides.
|
||||||
|
When the scanning detects a change to the KeyStore file, the correspondent `SslContextFactory.Server` component is reloaded with the new KeyStore configuration.
|
||||||
|
|
||||||
|
The module properties are:
|
||||||
|
|
||||||
|
----
|
||||||
|
include::{jetty-home}/modules/ssl-reload.mod[tags=documentation]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[test-keystore]]
|
||||||
|
== Module `test-keystore`
|
||||||
|
|
||||||
|
The `test-keystore` module creates on-the-fly a KeyStore containing a self-signed certificate for domain `localhost`.
|
||||||
|
The KeyStore file is automatically deleted when the JVM exits, and re-created when you restart Jetty, to enforce the fact that it is a _test_ KeyStore that should not be reused if not for testing.
|
||||||
|
|
||||||
|
The module file is `$JETTY_HOME/modules/test-keystore.mod`:
|
||||||
|
|
||||||
|
----
|
||||||
|
include::{jetty-home}/modules/test-keystore.mod[]
|
||||||
|
----
|
||||||
|
|
||||||
|
Note how properties `jetty.sslContext.keyStorePath` and `jetty.sslContext.keyStorePassword` are configured, only if not already set (via the `?=` operator), directly in the module file, rather than in a `+*.ini+` file.
|
||||||
|
This is done to avoid that these properties accidentally overwrite a real KeyStore configuration.
|
||||||
|
|
||||||
|
[[threadpool]]
|
||||||
|
== Module `threadpool`
|
||||||
|
|
||||||
|
The `threadpool` module allows you to configure the server-wide thread pool.
|
||||||
|
|
||||||
|
The thread pool creates threads on demand up to `maxThreads`, and idles them out if they are not used.
|
||||||
|
|
||||||
|
Since Jetty uses the thread pool internally to execute critical tasks, it is not recommended to constrain the thread pool to small values of `maxThreads` with the purpose of limiting HTTP request concurrency, as this could very likely cause a server lockup when Jetty needs to run a critical task but there are no threads available.
|
||||||
|
Start with the default value of `maxThreads`, and tune for larger values if needed.
|
||||||
|
|
||||||
|
The module properties to configure the thread pool are:
|
||||||
|
|
||||||
|
----
|
||||||
|
include::{jetty-home}/modules/threadpool.mod[tags=documentation]
|
||||||
|
----
|
||||||
|
|
||||||
|
Among the configurable properties, the most relevant are:
|
||||||
|
|
||||||
|
`jetty.threadPool.namePrefix`::
|
||||||
|
The name prefix to use for the thread names.
|
||||||
|
|
||||||
|
`jetty.threadPool.detailedDump`::
|
||||||
|
Whether the thread pool should dump the whole stack trace of each thread, or just the topmost stack frame -- defaults to `false`.
|
||||||
|
|
||||||
|
`jetty.threadPool.idleTimeout`::
|
||||||
|
The time, in milliseconds, after which an idle thread is released from the pool -- defaults to 60000, i.e. 60 seconds.
|
||||||
|
|
||||||
|
`jetty.threadPool.maxThreads`::
|
||||||
|
The max number of threads pooled by the thread pool -- defaults to 200.
|
||||||
|
|
||||||
|
If you want to use virtual threads, introduced as a preview feature in Java 19 and Java 20, and become an official feature since Java 21, use the following modules:
|
||||||
|
|
||||||
|
* The <<threadpool-virtual,`threadpool-virtual`>> Jetty module for Java 21 or later.
|
||||||
|
* The <<threadpool-virtual-preview,`threadpool-virtual-preview`>> Jetty module for Java 19 and Java 20.
|
||||||
|
|
||||||
|
See also the xref:server/index.adoc#threadpool[section about configuring the thread pool].
|
||||||
|
|
||||||
|
[[threadpool-virtual]]
|
||||||
|
== Module `threadpool-virtual`
|
||||||
|
|
||||||
|
The `threadpool-virtual` module allows you to configure the server-wide thread pool, similarly to what you can do with the <<threadpool,`threadpool`>> Jetty module, but also specify to use virtual threads, introduced as an official feature since Java 21.
|
||||||
|
|
||||||
|
CAUTION: Only use this module if you are using Java 21 or later.
|
||||||
|
If you are using Java 19 or Java 20, use the <<threadpool-virtual-preview,`threadpool-virtual-preview`>> Jetty module instead.
|
||||||
|
|
||||||
|
Refer to the <<threadpool,`threadpool`>> Jetty module for the general features provided by that Jetty module that also this Jetty module provides.
|
||||||
|
|
||||||
|
The module properties to configure the thread pool are:
|
||||||
|
|
||||||
|
----
|
||||||
|
include::{jetty-home}/modules/threadpool-virtual.mod[tags=documentation]
|
||||||
|
----
|
||||||
|
|
||||||
|
The specific properties to configure virtual threads are:
|
||||||
|
|
||||||
|
`jetty.threadPool.virtual.namePrefix`::
|
||||||
|
The name prefix to use for the virtual thread names.
|
||||||
|
|
||||||
|
`jetty.threadPool.virtual.inheritInheritableThreadLocals`::
|
||||||
|
Whether virtual threads inherit the values of `InheritableThreadLocal` variables.
|
||||||
|
|
||||||
|
[[threadpool-virtual-preview]]
|
||||||
|
== Module `threadpool-virtual-preview`
|
||||||
|
|
||||||
|
The `threadpool-virtual-preview` module allows you to configure the server-wide thread pool, similarly to what you can do with the <<threadpool,`threadpool`>> Jetty module, but also specify to use virtual threads, introduced as a preview feature in Java 19 and in Java 20.
|
||||||
|
|
||||||
|
CAUTION: Only use this module if you are using Java 19 or Java 20.
|
||||||
|
If you are using Java 21 or later, use the <<threadpool-virtual,`threadpool-virtual`>> Jetty module instead.
|
||||||
|
|
||||||
|
NOTE: To enable preview features, this module needs to specify the `+--enable-preview+` command line option using the xref:modules/index.adoc#directive-exec[[exec\] directive], and as such it will fork another JVM.
|
||||||
|
|
||||||
|
Refer to the <<threadpool,`threadpool`>> Jetty module for the general features provided by that Jetty module that also this Jetty module provides.
|
||||||
|
|
||||||
|
The module properties to configure the thread pool are:
|
||||||
|
|
||||||
|
----
|
||||||
|
include::{jetty-home}/modules/threadpool-virtual-preview.mod[tags=documentation]
|
||||||
|
----
|
||||||
|
|
||||||
|
The specific properties to configure virtual threads are:
|
||||||
|
|
||||||
|
`jetty.threadPool.virtual.namePrefix`::
|
||||||
|
The name prefix to use for the virtual thread names.
|
||||||
|
|
||||||
|
`jetty.threadPool.virtual.allowSetThreadLocals`::
|
||||||
|
Whether virtual threads are allowed to set thread locals.
|
||||||
|
|
||||||
|
`jetty.threadPool.virtual.inheritInheritableThreadLocals`::
|
||||||
|
Whether virtual threads inherit the values of `InheritableThreadLocal` variables.
|
||||||
|
|
||||||
|
[[well-known]]
|
||||||
|
== Module `well-known`
|
||||||
|
|
||||||
|
The `well-known` Jetty module creates a `ResourceHandler` deployed at the `/.well-known` context path which serves files from a directory.
|
||||||
|
By default, the directory created at `$JETTY_BASE/.well-known` is used, but it can be configured from `well-known.ini` to anywhere in the filesystem.
|
||||||
|
Note that the `.well-known` directory may be seen as a hidden directory by the filesystem.
|
||||||
|
|
||||||
|
The concept of well-known URIs has been defined in https://datatracker.ietf.org/doc/html/rfc5785[RFC5785].
|
||||||
|
This module can be used for things like the automatic renewal of https://letsencrypt.org/[Let's Encrypt] certificates.
|
||||||
|
See https://www.iana.org/assignments/well-known-uris/well-known-uris.xhtml[IANA Well-Known URIs] for more possible examples of how this can be used.
|
||||||
|
|
||||||
|
The module properties are:
|
||||||
|
|
||||||
|
----
|
||||||
|
include::{jetty-home}/modules/well-known.mod[tags=documentation]
|
||||||
|
----
|
File diff suppressed because it is too large
Load Diff
|
@ -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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Faster Web Application Deployment
|
||||||
|
|
||||||
|
The auto discovery features of the Servlet Specification can make deployments slow and uncertain.
|
||||||
|
Auto discovery of web application configuration can be useful during the development as it allows new features and frameworks to be enabled simply by dropping in a jar file.
|
||||||
|
However for production deployment, the need to scan the contents of many jars can have a significant impact at startup time.
|
||||||
|
|
||||||
|
The `quickstart` module allows a webapp to be pre-scanned, making startup predictable and faster.
|
||||||
|
During scanning all declarative configuration (ie from web.xml, web-fragment.xml and annotations) are encoded into an effective `web.xml`, called `WEB-INF/quickstart-web.xml`, which can be inspected to understand what will be deployed.
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
Programmatic configuration is _not_ encoded into the generated `quickstart-web.xml` file.
|
||||||
|
====
|
||||||
|
|
||||||
|
With `quickstart`, webapps that took many seconds to scan and deploy can now be deployed in a few hundred milliseconds.
|
||||||
|
|
||||||
|
== Enabling
|
||||||
|
|
||||||
|
Enable the `quickstart` module for your jetty base:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ cd $JETTY-BASE
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --add-module=quickstart
|
||||||
|
----
|
||||||
|
|
||||||
|
The `$JETTY-BASE/start.d/quickstart.ini` file contains these configurable parameters:
|
||||||
|
|
||||||
|
jetty.quickstart.mode::
|
||||||
|
The values are:
|
||||||
|
|
||||||
|
AUTO:::
|
||||||
|
Allows jetty to run either with or without a `quickstart-web.xml` file.
|
||||||
|
If jetty detects the file, then it will be used, otherwise the app is started normally.
|
||||||
|
GENERATE:::
|
||||||
|
In this mode, jetty will generate a `quickstart-web.xml` file and then terminate.
|
||||||
|
Use this mode first before changing to either `AUTO` or `QUICKSTART`.
|
||||||
|
QUICKSTART:::
|
||||||
|
In this mode, if jetty does not detect a `quickstart-web.xml` file then jetty will not start.
|
||||||
|
|
||||||
|
jetty.quickstart.origin::
|
||||||
|
Use this parameter to set the name of the attribute in the `quickstart-web.xml` file that contains the origin of each element.
|
||||||
|
Knowing the descriptor or annotation from which each element derives can be useful for debugging.
|
||||||
|
Note that the origin attribute does not conform to the web xml schema, so if you deploy with xml validation, you'll see errors.
|
||||||
|
It is probably best to do a few trial runs with the attribute set, then turn it off for final generation.
|
||||||
|
|
||||||
|
jetty.quickstart.xml::
|
||||||
|
Use this parameter to change the name of the generated file.
|
||||||
|
By default this is `quickstart-web.xml` in the webapp's `WEB-INF` directory.
|
||||||
|
The file named by this parameter will always be interpreted relative to `WEB-INF`.
|
||||||
|
|
||||||
|
If your webapp is a war file, you will need to either first unpack it yourself, or use a context xml file (or code equivalent) that calls `WebAppContext.setExtractWAR(true)`.
|
||||||
|
If you allow Jetty to do the unpacking, it will use the usual mechanisms to find the location to which to unpack.
|
||||||
|
Note that by default Jetty unpacks to a temporary location which is _not_ reused between executions.
|
||||||
|
So either specify the directory to which to unpack, or make a `work` directory in your base to ensure the unpacked war is preserved and reused across restarts.
|
|
@ -0,0 +1,363 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Jetty Server
|
||||||
|
|
||||||
|
The Jetty `Server` object is the central component that links protocol connectors to web applications.
|
||||||
|
|
||||||
|
The `Server` component is defined by the xref:modules/standard.adoc#server[`server` Jetty module], that in turn depends on other Jetty modules that provide key functionalities, in particular:
|
||||||
|
|
||||||
|
* <<logging,Logging>>
|
||||||
|
* xref:modules/standard.adoc#bytebufferpool[`ByteBuffer` pooling]
|
||||||
|
* <<threadpool,`Thread` pooling>>
|
||||||
|
|
||||||
|
[[logging]]
|
||||||
|
== Logging
|
||||||
|
|
||||||
|
There are two types of logging that can be configured in Jetty:
|
||||||
|
|
||||||
|
* The logging of Jetty itself, that logs the server activity
|
||||||
|
* The HTTP request logging, that logs information about HTTP requests and responses processed by Jetty
|
||||||
|
|
||||||
|
[[logging-server]]
|
||||||
|
=== Server Logging
|
||||||
|
|
||||||
|
The Jetty code uses the http://slf4j.org/[SLF4J] API for its logging.
|
||||||
|
|
||||||
|
Thanks to the SLF4J library, the logging of the Jetty server can therefore be directed to the implementation (called SLF4J _binding_) of your choice.
|
||||||
|
|
||||||
|
The Jetty project provides an SLF4J binding (via the `jetty-slf4j-impl` Maven artifact) that is used as the default SLF4J binding.
|
||||||
|
|
||||||
|
The logging of the Jetty server itself is enabled by default with the `logging` Jetty module, which is a transitive dependency of the `server` module and therefore it is typically always enabled.
|
||||||
|
|
||||||
|
The `logging` Jetty module is a _virtual_ module (see xref:modules/index.adoc#names[this section]) and its default implementation is provided by the `logging-jetty` Jetty module, which uses the Jetty SLF4J binding.
|
||||||
|
|
||||||
|
[[logging-server-default]]
|
||||||
|
==== Default Configuration
|
||||||
|
|
||||||
|
The Jetty SLF4J binding is configured with an appender (`org.eclipse.jetty.logging.StdErrAppender`) that directs the logging to `System.err`, and reads its configuration from a file named `jetty-logging.properties` that must be found in the class-path.
|
||||||
|
|
||||||
|
The `StdErrAppender` format is:
|
||||||
|
|
||||||
|
----
|
||||||
|
<datetime>:<level>:<logger name>:<thread name>:<message>
|
||||||
|
----
|
||||||
|
|
||||||
|
where `<datetime>=yyyy-MM-dd HH:mm:ss.SSS`.
|
||||||
|
|
||||||
|
You can configure `StdErrAppender` by specifying the following properties in `jetty-logging.properties`:
|
||||||
|
|
||||||
|
org.eclipse.jetty.logging.appender.NAME_CONDENSE=<boolean>::
|
||||||
|
Specifies whether to condense logger names, so that for example `org.eclipse.jetty.util.QueuedThreadPool` becomes `oeju.QueuedThreadPool`.
|
||||||
|
Default value is `true`.
|
||||||
|
|
||||||
|
org.eclipse.jetty.logging.appender.MESSAGE_ALIGN=<integer>::
|
||||||
|
Specifies the column at which the logging `<message>` should be printed.
|
||||||
|
The value `0` specifies no alignment.
|
||||||
|
Default value is `0`.
|
||||||
|
|
||||||
|
org.eclipse.jetty.logging.appender.MESSAGE_ESCAPE=<boolean>::
|
||||||
|
Specifies whether to escape ISO control characters such as `\r` or `\n` present in the message.
|
||||||
|
Character `\r` is replaced with `<` and character `\n` is replaced with `|`; all other ISO control characters are replaced with `?`.
|
||||||
|
Default value is `false`.
|
||||||
|
|
||||||
|
org.eclipse.jetty.logging.appender.ZONE_ID=<timezone id>::
|
||||||
|
Specifies the timezone ID (such as `PST`, or `America/Los_Angeles` or `GMT-8:00`) for the `<datetime>` part of the logging line.
|
||||||
|
The empty string specifies the `UTC` timezone.
|
||||||
|
Default value is the local timezone.
|
||||||
|
|
||||||
|
The `logging-jetty` Jetty module, enabled transitively, provides the configuration file `$JETTY_BASE/resources/jetty-logging.properties` to configure the logging levels, for example:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ cd $JETTY_BASE
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --add-modules=http
|
||||||
|
----
|
||||||
|
|
||||||
|
----
|
||||||
|
$JETTY_BASE
|
||||||
|
├── resources
|
||||||
|
│ └── jetty-logging.properties
|
||||||
|
└── start.d
|
||||||
|
└── http.ini
|
||||||
|
----
|
||||||
|
|
||||||
|
.jetty-logging.properties
|
||||||
|
[,properties]
|
||||||
|
----
|
||||||
|
# Do not condense logger names.
|
||||||
|
org.eclipse.jetty.logging.appender.NAME_CONDENSE=false
|
||||||
|
|
||||||
|
# By default, log at INFO level all Jetty loggers.
|
||||||
|
org.eclipse.jetty.LEVEL=INFO
|
||||||
|
|
||||||
|
# However, the Jetty client loggers log at DEBUG level.
|
||||||
|
org.eclipse.jetty.client.LEVEL=DEBUG
|
||||||
|
----
|
||||||
|
|
||||||
|
The logging levels that you can specify in the `jetty-logging.properties` file are the usual SLF4J logging levels, `TRACE`, `DEBUG`, `INFO`, `WARN` and `ERROR`, plus two additional levels:
|
||||||
|
|
||||||
|
* `ALL`, which is an alias for `TRACE`
|
||||||
|
* `OFF`, which disables entirely the logging (not even `ERROR` level messages are logged)
|
||||||
|
|
||||||
|
When using the Jetty SLF4J binding, the logging levels can be dynamically changed via JMX, see xref:troubleshooting/index.adoc#logging[the troubleshooting section] for more information.
|
||||||
|
|
||||||
|
[[logging-server-default-rolling]]
|
||||||
|
==== Capturing Logs to a Rolling File
|
||||||
|
|
||||||
|
Having the logging output on `System.err` may be fine at development time, but you typically want the logs to be captured in a file so that they can be looked at even if you don't have a terminal (for example, you started Jetty as a service).
|
||||||
|
|
||||||
|
The `console-capture` Jetty module allows you to capture what is written to `System.out` and `System.err` and write it to a log file, by default under the `$JETTY_BASE/logs/` directory.
|
||||||
|
|
||||||
|
The `console-capture` Jetty module defines a number of properties that you can customize to control the log directory, the number of days rolled files are retained, etc.
|
||||||
|
See the xref:modules/standard.adoc#console-capture[`console-capture` module] for more information.
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
The `console-capture` Jetty module should be used only in conjunction with the `logging-jetty` module, as other SLF4J bindings such as LogBack or Log4j2 have their own, more sophisticated, rolling file appenders.
|
||||||
|
====
|
||||||
|
|
||||||
|
[[logging-server-custom]]
|
||||||
|
==== Custom Configuration
|
||||||
|
|
||||||
|
You can use a different SLF4J binding if you are more familiar with other logging libraries, or if you need custom logging appenders.
|
||||||
|
There are a number of out-of-the-box Jetty modules that you can use:
|
||||||
|
|
||||||
|
* `logging-logback`, to use the http://logback.qos.ch/[LogBack] binding
|
||||||
|
* `logging-log4j2`, to use the https://logging.apache.org/log4j/2.x/[Log4j2] binding
|
||||||
|
* `logging-log4j1`, to use the https://logging.apache.org/log4j/1.2/[Log4j1] binding (note that Log4j 1.x is end-of-life)
|
||||||
|
* `logging-jul`, to use the `java.util.logging` binding
|
||||||
|
* `logging-noop`, to use the SLF4J no-operation binding (discards all logging)
|
||||||
|
|
||||||
|
[[logging-server-custom-logback]]
|
||||||
|
==== Logging with LogBack
|
||||||
|
|
||||||
|
You can enable, for example, the `logging-logback` Jetty module in this way (from the `$JETTY_BASE` directory):
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --add-modules=logging-logback,http
|
||||||
|
----
|
||||||
|
|
||||||
|
Since LogBack is released under a license that is different from Jetty's, you will be prompted to accept the LogBack license.
|
||||||
|
Once you accept the LogBack license, you will have the following directory structure:
|
||||||
|
|
||||||
|
----
|
||||||
|
$JETTY_BASE
|
||||||
|
├── lib
|
||||||
|
│ └── logging
|
||||||
|
│ ├── logback-classic-<version>.jar
|
||||||
|
│ └── logback-core-<version>.jar
|
||||||
|
├── resources
|
||||||
|
│ └── logback.xml
|
||||||
|
└── start.d
|
||||||
|
├── http.ini
|
||||||
|
└── logging-logback.ini
|
||||||
|
----
|
||||||
|
|
||||||
|
As you can see, the Jetty module system downloaded the required LogBack `+*.jar+` files, and created a `$JETTY_BASE/resources/logback.xml` file that you can configure to customize your LogBack logging.
|
||||||
|
Please refer to the http://logback.qos.ch/manual/configuration.html[LogBack configuration manual] for more information about how to configure LogBack.
|
||||||
|
|
||||||
|
[[logging-server-custom-log4j2]]
|
||||||
|
==== Logging with Log4j2
|
||||||
|
|
||||||
|
Similarly to <<logging-server-custom-logback,logging with LogBack>>, you can enable the `logging-log4j2` Jetty module in this way (from the `$JETTY_BASE` directory):
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --add-modules=logging-log4j2,http
|
||||||
|
----
|
||||||
|
|
||||||
|
After accepting the Log4j2 license, you will have the following directory structure:
|
||||||
|
|
||||||
|
----
|
||||||
|
$JETTY_BASE
|
||||||
|
├── lib
|
||||||
|
│ └── logging
|
||||||
|
│ ├── log4j-api-<version>.jar
|
||||||
|
│ ├── log4j-core-<version>.jar
|
||||||
|
│ └── log4j-slf4j18-impl-<version>.jar
|
||||||
|
├── resources
|
||||||
|
│ └── log4j2.xml
|
||||||
|
└── start.d
|
||||||
|
├── http.ini
|
||||||
|
└── logging-log4j2.ini
|
||||||
|
----
|
||||||
|
|
||||||
|
The Jetty module system downloaded the required Log4j2 `+*.jar+` files, and created a `$JETTY_BASE/resources/log4j2.xml` file that you can configure to customize your Log4j2 logging.
|
||||||
|
|
||||||
|
[[logging-server-bridges]]
|
||||||
|
==== Bridging Logging to SLF4J
|
||||||
|
|
||||||
|
When you use libraries that provide the features you need (for example, JDBC drivers), it may be possible that those libraries use a different logging framework than SLF4J.
|
||||||
|
|
||||||
|
SLF4J provides http://www.slf4j.org/legacy.html[bridges for legacy logging APIs] that allows you to bridge logging from one of these legacy logging frameworks to SLF4J.
|
||||||
|
Once the logging is bridged to SLF4J, you can use the <<logging-server-default,default configuration>> or the <<logging-server-custom,custom configuration>> so that your logging is centralized in one place only.
|
||||||
|
|
||||||
|
Jetty provides out-of-the-box modules that you can enable to bridge logging from other logging frameworks to SLF4J.
|
||||||
|
|
||||||
|
[[logging-server-bridge-jul]]
|
||||||
|
==== Bridging `java.util.logging`
|
||||||
|
|
||||||
|
For libraries that use `java.util.logging` as their logging framework you can enable the `logging-jul-capture` Jetty module.
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --add-modules=logging-jul-capture
|
||||||
|
----
|
||||||
|
|
||||||
|
The `logging-jul-capture` Jetty module implies `--exec` and therefore spawns a second JVM (see xref:start/index.adoc#start[this section]) because it needs to provide the system property `java.util.logging.config.file` (so that `java.util.logging` can read the configuration from the specified file), and because it needs to make available on the System ClassLoader the class `org.slf4j.bridge.SLF4JBridgeHandler`.
|
||||||
|
|
||||||
|
For example, a library that uses `java.util.logging` as its logging library is the Postgresql JDBC driver.
|
||||||
|
With the `logging-jul-capture` Jetty module, the logging follows this diagram:
|
||||||
|
|
||||||
|
[plantuml]
|
||||||
|
----
|
||||||
|
skinparam backgroundColor transparent
|
||||||
|
skinparam monochrome true
|
||||||
|
skinparam shadowing false
|
||||||
|
|
||||||
|
participant "Postgresql JDBC" as postgresql
|
||||||
|
participant java.util.logging
|
||||||
|
participant SLF4JBridgeHandler
|
||||||
|
participant Jetty
|
||||||
|
participant SLF4J
|
||||||
|
participant "Jetty SLF4J Binding" as binding
|
||||||
|
|
||||||
|
|
||||||
|
postgresql -> java.util.logging
|
||||||
|
java.util.logging -> SLF4JBridgeHandler
|
||||||
|
SLF4JBridgeHandler -> SLF4J
|
||||||
|
SLF4J -> binding
|
||||||
|
Jetty -> SLF4J
|
||||||
|
SLF4J -> binding
|
||||||
|
----
|
||||||
|
|
||||||
|
Note how Jetty logs directly to SLF4J, while the Postgresql JDBC driver logs to SLF4J through the `SLF4JBridgeHandler`.
|
||||||
|
They both arrive to the SLF4J binding, in this case the Jetty SLF4J binding (but could be any other SLF4J binding such as LogBack).
|
||||||
|
|
||||||
|
[[og-logging-request]]
|
||||||
|
=== Request Logging
|
||||||
|
|
||||||
|
HTTP requests and responses can be logged to provide data that can be later analyzed with other tools, that can provide information such as the most frequently accessed request URIs, the response status codes, the request/response content lengths, geographical information about the clients, etc.
|
||||||
|
|
||||||
|
Request logging is enabled by enabling the `requestlog` Jetty module.
|
||||||
|
In the example below, both the `http` Jetty module and the `requestlog` module are enabled, so that you can make HTTP requests to the server and have them logged:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ cd $JETTY_BASE
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --add-modules=http,requestlog
|
||||||
|
----
|
||||||
|
|
||||||
|
The `$JETTY_BASE` directory looks like this:
|
||||||
|
|
||||||
|
[source]
|
||||||
|
----
|
||||||
|
$JETTY_BASE
|
||||||
|
├── logs
|
||||||
|
├── resources
|
||||||
|
│ └── jetty-logging.properties
|
||||||
|
└── start.d
|
||||||
|
├── http.ini
|
||||||
|
└── requestlog.ini
|
||||||
|
----
|
||||||
|
|
||||||
|
The `$JETTY_BASE/start.d/requestlog.ini` file is the Jetty module configuration file that allows you to configure the `requestlog` module, see xref:modules/standard.adoc#requestlog[this section] for more details.
|
||||||
|
|
||||||
|
By default the `requestlog` Jetty module produces the `$JETTY_BASE/logs/yyyy_MM_dd.request.log`, where the pattern `yyyy_MM_dd` is replaced with the current date, for example `2020_01_31`.
|
||||||
|
|
||||||
|
The format of the request log lines is the result of a _format string_ that uses formatting symbols to log relevant request/response data.
|
||||||
|
|
||||||
|
The default format is the https://en.wikipedia.org/wiki/Common_Log_Format[NCSA Format] extended with referrer data and user-agent data.
|
||||||
|
A typical log line looks like this:
|
||||||
|
|
||||||
|
[,options=nowrap]
|
||||||
|
----
|
||||||
|
192.168.0.100 - - [31/Jan/2020:20:30:40 +0000] "GET / HTTP/1.1" 200 6789 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36"
|
||||||
|
----
|
||||||
|
|
||||||
|
The line above (that uses fake values) shows `192.168.0.100` for the client IP address, a hard-coded `-` for the identity, `-` for the authenticated user name, `[31/Jan/2020:20:30:40 +0000]` for the date and time with timezone, `"GET / HTTP/1.1"` for the HTTP request line, `200` for the HTTP response status code, `6789` for the HTTP response content length, `"-"` for the referrer and `"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36"` for the user-agent.
|
||||||
|
|
||||||
|
The format string can be customized as described in xref:modules/standard.adoc#requestlog[this section].
|
||||||
|
Request log files are rolled every day, and retained for customizable number of days, by default 90 days.
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
When Jetty is behind a load balancer, you want to log the remote client IP address, not the load balancer IP address. Refer to xref:protocols/index.adoc#proxy[this section] to configure the load balancer and Jetty to retain the remote client IP address information.
|
||||||
|
====
|
||||||
|
|
||||||
|
[[threadpool]]
|
||||||
|
== Thread Pooling
|
||||||
|
|
||||||
|
Jetty uses thread pooling to efficiently execute tasks that provide Jetty functionalities.
|
||||||
|
|
||||||
|
Like any other component, the Jetty thread pool is configured and enabled via the xref:modules/standard.adoc#threadpool[`threadpool` Jetty module], that is transitively enabled by the xref:modules/standard.adoc#server[`server` Jetty module] which, in turn, is transitively enabled by a protocol module such as the xref:protocols/index.adoc#http[`http` Jetty module]:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --add-modules=http
|
||||||
|
----
|
||||||
|
|
||||||
|
The command above gives you the default configuration for the thread pool.
|
||||||
|
|
||||||
|
If you want to explicitly configure the thread pool, it is enough to explicitly specify the xref:modules/standard.adoc#threadpool[`threadpool`] module:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --add-modules=threadpool,http
|
||||||
|
----
|
||||||
|
|
||||||
|
After the command above, the `$JETTY_BASE` directory looks like this:
|
||||||
|
|
||||||
|
[source,subs=verbatim]
|
||||||
|
----
|
||||||
|
$JETTY_BASE
|
||||||
|
├── resources
|
||||||
|
│ └── jetty-logging.properties
|
||||||
|
└── start.d
|
||||||
|
├── http.ini
|
||||||
|
└── threadpool.ini
|
||||||
|
----
|
||||||
|
|
||||||
|
Now you can customize the `threadpool.ini` file to explicitly configure the thread pool.
|
||||||
|
|
||||||
|
[[threadpool-virtual]]
|
||||||
|
=== Virtual Threads Support
|
||||||
|
|
||||||
|
Virtual threads have been introduced as a preview feature in Java 19 and Java 20, and have become an official feature since Java 21.
|
||||||
|
|
||||||
|
The xref:modules/standard.adoc#threadpool-virtual-preview[`threadpool-virtual-preview`] Jetty module provides support for virtual threads in Java 19 and Java 20, and it is mutually exclusive with the `threadpool` Jetty module.
|
||||||
|
|
||||||
|
The xref:modules/standard.adoc#threadpool-virtual[`threadpool-virtual`] Jetty module provides support for virtual threads in Java 21 or later, and it is mutually exclusive with the `threadpool` Jetty module.
|
||||||
|
|
||||||
|
If you have already enabled the `threadpool` Jetty module, it is sufficient to remove it by removing the `$JETTY_BASE/start.d/threadpool.ini` file.
|
||||||
|
|
||||||
|
When using Java 21 or later, you can enable the xref:modules/standard.adoc#threadpool-virtual[`threadpool-virtual`] module:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --add-modules=threadpool-virtual,http
|
||||||
|
----
|
||||||
|
|
||||||
|
After the command above, the `$JETTY_BASE` directory looks like this:
|
||||||
|
|
||||||
|
[source,subs=verbatim]
|
||||||
|
----
|
||||||
|
$JETTY_BASE
|
||||||
|
├── resources
|
||||||
|
│ └── jetty-logging.properties
|
||||||
|
└── start.d
|
||||||
|
├── http.ini
|
||||||
|
└── threadpool-virtual.ini
|
||||||
|
----
|
||||||
|
|
||||||
|
Now you can customize the `threadpool-virtual.ini` file to explicitly configure the thread pool and the virtual threads and then start Jetty:
|
||||||
|
|
||||||
|
[jetty%nowrap]
|
||||||
|
....
|
||||||
|
[jetty]
|
||||||
|
setupArgs=--add-modules=threadpool-virtual,http
|
||||||
|
....
|
|
@ -0,0 +1,974 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= HTTP Session Management
|
||||||
|
|
||||||
|
HTTP sessions are a concept within the Servlet API which allow requests to store and retrieve information across the time a user spends in an application.
|
||||||
|
Jetty offers a number of pluggable alternatives for managing and distributing/persisting sessions.
|
||||||
|
Choosing the best alternative is an important consideration for every application as is the correct configuration to achieve optimum performance.
|
||||||
|
|
||||||
|
[[overview]]
|
||||||
|
== HTTP Session Overview
|
||||||
|
|
||||||
|
=== Terminology
|
||||||
|
|
||||||
|
Before diving into the specifics of how to plug-in and configure various alternative HTTP session management modules, let's review some useful terminology:
|
||||||
|
|
||||||
|
Session::
|
||||||
|
is a means of retaining information across requests for a particular user.
|
||||||
|
The Servlet Specification defines the semantics of sessions.
|
||||||
|
Some of the most important characteristics of sessions is that they have a unique id and that their contents cannot be shared between different contexts (although the id can be): if a session is invalidated in one context, then all other sessions that share the same id in other contexts will also be invalidated.
|
||||||
|
Sessions can expire or they can be explicitly invalidated.
|
||||||
|
|
||||||
|
SessionIdManager::
|
||||||
|
is responsible for allocating session ids.
|
||||||
|
A Jetty server can have at most 1 SessionIdManager.
|
||||||
|
|
||||||
|
HouseKeeper::
|
||||||
|
is responsible for periodically orchestrating the removal of expired sessions.
|
||||||
|
This process is referred to as <<base-scavenge,"scavenging">>.
|
||||||
|
|
||||||
|
SessionHandler::
|
||||||
|
is responsible for managing the lifecycle of sessions.
|
||||||
|
A context can have at most 1 `SessionHandler`.
|
||||||
|
|
||||||
|
SessionCache::
|
||||||
|
is a L1 cache of in-use session objects.
|
||||||
|
The `SessionCache` is used by the `SessionHandler`.
|
||||||
|
|
||||||
|
SessionDataStore::
|
||||||
|
is responsible for all clustering/persistence operations on sessions.
|
||||||
|
A `SessionCache` uses a `SessionDataStore` as a backing store.
|
||||||
|
|
||||||
|
CachingSessionDataStore::
|
||||||
|
is an L2 cache of session data.
|
||||||
|
A `SessionCache` can use a `CachingSessionDataStore` as its backing store.
|
||||||
|
|
||||||
|
More details on these concepts can be found in the xref:programming-guide:server/session.adoc[Programming Guide].
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
``SessionDataStore``s implementations interact with other, usually third party, systems responsible for storing and/or distributing session information.
|
||||||
|
Sessions can be distributed without being persisted.
|
||||||
|
They can also be persisted without being distributed.
|
||||||
|
Because persisting session information to a shared store is a very common way of distributing (also known as "clustering") sessions, in the documentation we will often refer to just "persisting".
|
||||||
|
====
|
||||||
|
|
||||||
|
[[modules]]
|
||||||
|
=== Session Modules
|
||||||
|
|
||||||
|
There are a number of modules that offer pluggable alternatives for http session management.
|
||||||
|
You can design how you want to cache and store http sessions by selecting alternative combinations of session modules.
|
||||||
|
|
||||||
|
For example, Jetty ships with two alternative implementations of the `SessionCache`:
|
||||||
|
|
||||||
|
* one that caches sessions in memory: <<cache-hash,`session-cache-hash`>>
|
||||||
|
* one that does not actually cache: <<cache-null,`session-cache-null`>>
|
||||||
|
|
||||||
|
There are at least 6 alternative implementations of the `SessionDataStore` that you can use to persist/distribute your http sessions:
|
||||||
|
|
||||||
|
* file system storage: <<filesystem,`session-store-file`>>
|
||||||
|
* relational database storage: <<jdbc,`session-store-jdbc`>>
|
||||||
|
* NoSQL database storage: <<mongo,`session-store-mongo`>>
|
||||||
|
* Google Cloud datastore storage: <<gcloud,`session-store-gcloud`>>
|
||||||
|
* Hazelcast: <<hazelcast-remote,`session-store-hazelcast-remote`>> or <<hazelcast-embedded,`session-store-hazelcast-embedded`>>
|
||||||
|
* Infinispan: <<infinispan,`session-store-infinispan-remote`>> or <<infinispan-embedded,`session-store-infinispan-embedded`>>
|
||||||
|
|
||||||
|
TIP: It is worth noting that if you do not configure _any_ session modules, Jetty will still provide HTTP sessions that are cached in memory but are never persisted.
|
||||||
|
|
||||||
|
[[base]]
|
||||||
|
== The Base Session Module
|
||||||
|
|
||||||
|
The `sessions` module is the base module that all other session modules depend upon.
|
||||||
|
As such it will be _transitively_ enabled if you enable any of the other session modules: you need to _explicitly_ enable it if you wish to _change_ any settings from their defaults.
|
||||||
|
|
||||||
|
Enabling the `sessions` module puts the `$JETTY_HOME/etc/sessions/id-manager.xml` file onto the execution path and generates a `$JETTY_BASE/start.d/sessions.ini` file.
|
||||||
|
|
||||||
|
The `id-manager.xml` file instantiates a `DefaultSessionIdManager` and `HouseKeeper`.
|
||||||
|
The former is used to generate and manage session ids whilst the latter is responsible for periodic <<base-scavenge,scavenging>> of expired sessions.
|
||||||
|
|
||||||
|
=== Configuration
|
||||||
|
|
||||||
|
The `$JETTY_BASE/start.d/sessions.ini` file contains these configuration properties:
|
||||||
|
|
||||||
|
jetty.sessionIdManager.workerName::
|
||||||
|
This uniquely identifies the jetty server instance and is applied to the `SessionIdManager`.
|
||||||
|
You can either provide a value for this property, or you can allow Jetty to try and synthesize a `workerName` - the latter option is _only_ advisable in the case of a single, non-clustered deployment.
|
||||||
|
There are two ways a default `workerName` can be synthesized:
|
||||||
|
|
||||||
|
* if running on Google AppEngine, the `workerName` will be formed by concatenating the values of the environment variables `JETTY_WORKER_INSTANCE` and `GAE_MODULE_INSTANCE`
|
||||||
|
* otherwise, the `workerName` will be formed by concatenating the environment variable `JETTY_WORKER_INSTANCE` and the literal `0`.
|
||||||
|
|
||||||
|
So, if you're not running on Google AppEngine, and you haven't configured one, the workerName will always be: `node0`.
|
||||||
|
|
||||||
|
IMPORTANT: If you have more than one Jetty instance, it is *crucial* that you configure the `workerName` differently for each instance.
|
||||||
|
|
||||||
|
jetty.sessionScavengeInterval.seconds::
|
||||||
|
This is the period in _seconds_ between runs of the `HouseKeeper`, responsible for orchestrating the removal of expired sessions.
|
||||||
|
By default it will run approximately every 600 secs (ie 10 mins).
|
||||||
|
As a rule of thumb, you should ensure that the <<base-scavenge,scavenge>> interval is shorter than the `<session-timeout>` of your sessions to ensure that they are promptly scavenged.
|
||||||
|
On the other hand, if you have a backend store configured for your sessions, <<base-scavenge,scavenging>> too frequently can increase the load on it.
|
||||||
|
|
||||||
|
CAUTION: Don't forget that the `<session-timeout>` is specified in `web.xml` in _minutes_ and the value of the `jetty.sessionScavengeInterval.seconds` is in _seconds_.
|
||||||
|
|
||||||
|
[[base-scavenge]]
|
||||||
|
=== Session Scavenging
|
||||||
|
|
||||||
|
The `HouseKeeper` is responsible for the periodic initiation of session scavenge cycles.
|
||||||
|
The `jetty.sessionScavengeInterval.seconds` property in `$JETTY_BASE/start.d/sessions.ini` controls the periodicity of the cycle.
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
The HouseKeeper semi-randomly adds an additional 10% to the configured `sessionScavengeInterval`.
|
||||||
|
This is to prevent multiple nodes in a cluster that are all started at once from syncing up scavenge cycles and placing extra load on the configured persistence mechanism.
|
||||||
|
====
|
||||||
|
|
||||||
|
A session whose expiry time has been exceeded is considered eligible for scavenging.
|
||||||
|
The session might be present in a `SessionCache` and/or present in the session persistence/clustering mechanism.
|
||||||
|
|
||||||
|
Scavenging occurs for all contexts on a server at every cycle.
|
||||||
|
The `HouseKeeper` sequentially asks the `SessionHandler` in each context to find and remove expired sessions.
|
||||||
|
The `SessionHandler` works with the `SessionDataStore` to evaluate candidates for expiry held in the `SessionCache`, and also to sweep the persistence mechanism to find expired sessions.
|
||||||
|
|
||||||
|
The sweep takes two forms: once per cycle the `SessionDataStore` searches for sessions for its own context that have expired; infrequently, the `SessionDataStore` will widen the search to expired sessions in all contexts.
|
||||||
|
The former finds sessions that are no longer in this context's `SessionCache`, and using some heuristics, are unlikely to be in the `SessionCache` of the same context on another node either.
|
||||||
|
These sessions will be loaded and fully expired, meaning that `HttpSessionListener.destroy()` will be called for them.
|
||||||
|
The latter finds sessions that have not been disposed of by scavenge cycles on any other context/node.
|
||||||
|
As these will be sessions that expired a long time ago, and may not be appropriate to load by the context doing the scavenging, these are summarily deleted without `HttpSessionListener.destroy()` being called.
|
||||||
|
|
||||||
|
A combination of these sweeps should ensure that the persistence mechanism does not fill over time with expired sessions.
|
||||||
|
|
||||||
|
As aforementioned, the sweep period needs to be short enough to find expired sessions in a timely fashion, but not so often that it overloads the persistence mechanism.
|
||||||
|
|
||||||
|
[[cache]]
|
||||||
|
== Modules for HTTP Session Caching
|
||||||
|
|
||||||
|
In this section we will look at the alternatives for the `SessionCache`, i.e. the L1 cache of in-use session objects.
|
||||||
|
Jetty ships with 2 alternatives: an in-memory cache, and a null cache.
|
||||||
|
The latter does not actually do any caching of sessions, and can be useful if you either want to minimize your support for sessions, or you are in a clustered deployment without a sticky loadbalancer.
|
||||||
|
|
||||||
|
The <<usecases,scenarios>> go into more detail on this.
|
||||||
|
|
||||||
|
[[cache-hash]]
|
||||||
|
=== Caching in Memory
|
||||||
|
|
||||||
|
If you wish to change any of the default configuration values you should enable the `session-cache-hash` xref:modules/index.adoc[module].
|
||||||
|
The name `"hash"` harks back to historical Jetty session implementations, whereby sessions were kept in memory using a HashMap.
|
||||||
|
|
||||||
|
==== Configuration
|
||||||
|
|
||||||
|
The `$JETTY_BASE/start.d/session-cache-hash.ini` contains the following configurable properties:
|
||||||
|
|
||||||
|
jetty.session.evictionPolicy::
|
||||||
|
Integer, default -1.
|
||||||
|
This controls whether session objects that are held in memory are subject to eviction from the cache.
|
||||||
|
Eviction means that the session is removed from the cache.
|
||||||
|
This can reduce the memory footprint of the cache and can be useful if you have a lot of sessions.
|
||||||
|
Eviction is usually used in conjunction with a `SessionDataStore` that persists sessions.
|
||||||
|
The eviction strategies and their corresponding values are:
|
||||||
|
-1 (NO EVICTION):::
|
||||||
|
sessions are never evicted from the cache.
|
||||||
|
The only way they leave are via expiration or invalidation.
|
||||||
|
0 (EVICT AFTER USE):::
|
||||||
|
sessions are evicted from the cache as soon as the last active request for it finishes.
|
||||||
|
The session will be passed to the `SessionDataStore` to be written out before eviction.
|
||||||
|
>= 1 (EVICT ON INACTIVITY):::
|
||||||
|
any positive number is the time in seconds after which a session that is in the cache but has not experienced any activity will be evicted.
|
||||||
|
Use the `jetty.session.saveOnInactiveEvict` property to force a session write before eviction.
|
||||||
|
|
||||||
|
NOTE: If you are not using one of the session store modules, ie one of the ``session-store-xxxx``s, then sessions will be lost when the context is stopped, or the session is evicted.
|
||||||
|
|
||||||
|
jetty.session.saveOnInactiveEvict::
|
||||||
|
Boolean, default `false`.
|
||||||
|
This controls whether a session will be persisted to the `SessionDataStore` if it is being evicted due to the EVICT ON INACTIVITY policy.
|
||||||
|
Usually sessions will be written to the `SessionDataStore` whenever the last simultaneous request exits the session.
|
||||||
|
However, as `SessionDataStores` can be configured to skip some writes (see the documentation for the `session-store-xxx` module that you are using), this option is provided to ensure that the session will be written out.
|
||||||
|
|
||||||
|
NOTE: Be careful with this option, as in clustered scenarios it would be possible to "re-animate" a session that has actually been deleted by another node.
|
||||||
|
|
||||||
|
jetty.session.saveOnCreate::
|
||||||
|
Boolean, default `false`.
|
||||||
|
Controls whether a session that is newly created will be immediately saved to the `SessionDataStore` or lazily saved as the last request for the session exits.
|
||||||
|
This can be useful if the request dispatches to another context and needs to re-use the same session id.
|
||||||
|
|
||||||
|
jetty.session.removeUnloadableSessions::
|
||||||
|
Boolean, default `false`.
|
||||||
|
Controls whether the session cache should ask a `SessionDataStore` to delete a session that cannot be restored - for example because it is corrupted.
|
||||||
|
|
||||||
|
jetty.session.flushOnResponseCommit::
|
||||||
|
Boolean, default `false`.
|
||||||
|
If true, if a session is "dirty" - ie its attributes have changed - it will be written to the `SessionDataStore` as the response is about to commit.
|
||||||
|
This ensures that all subsequent requests whether to the same or different node will see the updated session data.
|
||||||
|
If false, a dirty session will only be written to the backing store when the last simultaneous request for it leaves the session.
|
||||||
|
|
||||||
|
jetty.session.invalidateOnShutdown::
|
||||||
|
Boolean, default `false`.
|
||||||
|
If true, when a context is shutdown, all sessions in the cache are invalidated and deleted both from the cache and from the `SessionDataStore`.
|
||||||
|
|
||||||
|
[[cache-null]]
|
||||||
|
=== No Caching
|
||||||
|
|
||||||
|
You may need to use the `session-cache-null` module if your clustering setup does not have a sticky load balancer, or if you want absolutely minimal support for sessions.
|
||||||
|
If you enable this module, but you don't enable a module that provides session persistence (ie one of the `session-store-xxx` modules), then sessions will _neither_ be retained in memory _nor_ persisted.
|
||||||
|
|
||||||
|
==== Configuration
|
||||||
|
|
||||||
|
The `$JETTY_BASE/start.d/session-cache-null.ini` contains the following configurable properties:
|
||||||
|
|
||||||
|
jetty.session.saveOnCreate::
|
||||||
|
Boolean, default `false`.
|
||||||
|
Controls whether a session that is newly created will be immediately saved to the `SessionDataStore` or lazily saved as the last request for the session exits.
|
||||||
|
This can be useful if the request dispatches to another context and needs to re-use the same session id.
|
||||||
|
|
||||||
|
jetty.session.removeUnloadableSessions::
|
||||||
|
Boolean, default `false`.
|
||||||
|
Controls whether the session cache should ask a `SessionDataStore` to delete a session that cannot be restored - for example because it is corrupted.
|
||||||
|
|
||||||
|
jetty.session.flushOnResponseCommit::
|
||||||
|
Boolean, default `false`.
|
||||||
|
If true, if a session is "dirty" - ie its attributes have changed - it will be written to the backing store as the response is about to commit.
|
||||||
|
This ensures that all subsequent requests whether to the same or different node will see the updated session data.
|
||||||
|
If false, a dirty session will only be written to the backing store when the last simultaneous request for it leaves the session.
|
||||||
|
|
||||||
|
[[filesystem]]
|
||||||
|
== Modules for Persistent HTTP Sessions: File System
|
||||||
|
|
||||||
|
The `session-store-file` Jetty module supports persistent storage of session data in a filesystem.
|
||||||
|
|
||||||
|
IMPORTANT: Persisting sessions to the local file system should *never* be used in a clustered environment.
|
||||||
|
|
||||||
|
Enabling this module creates the `$JETTY_BASE/sessions` directory.
|
||||||
|
By default session data will be saved to this directory, one file representing each session.
|
||||||
|
|
||||||
|
File names follow this pattern:
|
||||||
|
|
||||||
|
`+[expiry]_[contextpath]_[virtualhost]_[id]+`
|
||||||
|
|
||||||
|
expiry::
|
||||||
|
This is the expiry time in milliseconds since the epoch.
|
||||||
|
|
||||||
|
contextpath::
|
||||||
|
This is the context path with any special characters, including `/`, replaced by the `_` underscore character.
|
||||||
|
For example, a context path of `/catalog` would become `_catalog`.
|
||||||
|
A context path of simply `/` becomes just `__`.
|
||||||
|
|
||||||
|
virtualhost::
|
||||||
|
This is the first virtual host associated with the context and has the form of 4 digits separated by `.` characters: `+[digit].[digit].[digit].[digit]+`.
|
||||||
|
If there are no virtual hosts associated with a context, then `0.0.0.0` is used.
|
||||||
|
|
||||||
|
id::
|
||||||
|
This is the unique id of the session.
|
||||||
|
|
||||||
|
Putting all of the above together as an example, a session with an id of `node0ek3vx7x2y1e7pmi3z00uqj1k0` for the context with path `/test` with no virtual hosts and an expiry of `1599558193150` would have a file name of:
|
||||||
|
|
||||||
|
`1599558193150__test_0.0.0.0_node0ek3vx7x2y1e7pmi3z00uqj1k0`
|
||||||
|
|
||||||
|
=== Configuration
|
||||||
|
|
||||||
|
The `$JETTY_BASE/start.d/sessions.ini` file contains the following properties which may be modified to customise filesystem session storage:
|
||||||
|
|
||||||
|
jetty.session.storeDir::
|
||||||
|
The default is `$JETTY_BASE/sessions`.
|
||||||
|
This is a path that defines the location for storage of session files.
|
||||||
|
|
||||||
|
jetty.session.file.deleteUnrestorableFiles::
|
||||||
|
Boolean, default `false`.
|
||||||
|
If set to `true`, unreadable files will be deleted.
|
||||||
|
This is useful to prevent repeated logging of the same error when the scavenger periodically (re-)attempts to load the corrupted information for a session in order to expire it.
|
||||||
|
|
||||||
|
jetty.session.gracePeriod.seconds::
|
||||||
|
Integer, default 3600.
|
||||||
|
Used during session <<base-scavenge,scavenging>>.
|
||||||
|
Multiples of this period are used to define how long ago a stored session must have expired before it should be <<base-scavenge,scavenged>>.
|
||||||
|
|
||||||
|
jetty.session.savePeriod.seconds::
|
||||||
|
Integer, in seconds, default is `0`.
|
||||||
|
Whenever a session is accessed by a request, its `lastAccessTime` and `expiry` are updated.
|
||||||
|
Even if your sessions are read-mostly, the `lastAccessTime` and `expiry` will always change.
|
||||||
|
For heavily-used, read-mostly sessions you can save some time by skipping some writes for sessions for which only these fields have changed (ie no session attributes changed).
|
||||||
|
The value of this property is used to skip writes for these kinds of sessions: the session will only be written out if the time since the last write exceeds the value of this property.
|
||||||
|
|
||||||
|
[WARNING]
|
||||||
|
====
|
||||||
|
You should be careful in the use of this property in clustered environments: if you set too large a value for this property, the session may not be written out sufficiently often to update its `expiry` time thus making it appear to other nodes that it has expired.
|
||||||
|
Thorough consideration of the `maxIdleTime` of the session when setting the `savePeriod` is imperative - it would be undesirable to set a `savePeriod` that is larger than the `maxIdleTime`.
|
||||||
|
====
|
||||||
|
|
||||||
|
[[jdbc]]
|
||||||
|
== Modules for Persistent HTTP Sessions: JDBC
|
||||||
|
|
||||||
|
Enabling the `session-store-jdbc` module configures Jetty to persist session data in a relational database.
|
||||||
|
|
||||||
|
=== Configuration
|
||||||
|
|
||||||
|
After enabling the module, the `$JETTY_BASE/start.d/session-store-jdbc.ini` file contains the following customizable properties:
|
||||||
|
|
||||||
|
jetty.session.gracePeriod.seconds::
|
||||||
|
Integer, default 3600.
|
||||||
|
Used during session <<base-scavenge,scavenging>>.
|
||||||
|
Multiples of this period are used to define how long ago a stored session must have expired before it should be <<base-scavenge,scavenged>>.
|
||||||
|
|
||||||
|
jetty.session.savePeriod.seconds::
|
||||||
|
Integer, in seconds, default is `0`.
|
||||||
|
Whenever a session is accessed by a request, its `lastAccessTime` and `expiry` are updated.
|
||||||
|
Even if your sessions are read-mostly, the `lastAccessTime` and `expiry` will always change.
|
||||||
|
For heavily-used, read-mostly sessions you can save some time by skipping some writes for sessions for which only these fields have changed (ie no session attributes changed).
|
||||||
|
The value of this property is used to skip writes for these kinds of sessions: the session will only be written out if the time since the last write exceeds the value of this property.
|
||||||
|
|
||||||
|
[WARNING]
|
||||||
|
====
|
||||||
|
You should be careful in the use of this property in clustered environments: if you set too large a value for this property, the session may not be written out sufficiently often to update its `expiry` time thus making it appear to other nodes that it has expired.
|
||||||
|
Thorough consideration of the `maxIdleTime` of the session when setting the `savePeriod` is imperative - it would be undesirable to set a `savePeriod` that is larger than the `maxIdleTime`.
|
||||||
|
====
|
||||||
|
|
||||||
|
db-connection-type::
|
||||||
|
Default `datasource`.
|
||||||
|
Set to either `datasource` or `driver` depending on the type of connection being used.
|
||||||
|
Depending which you select, there are additional properties available:
|
||||||
|
|
||||||
|
`datasource`:::
|
||||||
|
jetty.session.jdbc.datasourceName::::
|
||||||
|
Name of the remote datasource.
|
||||||
|
|
||||||
|
`driver`:::
|
||||||
|
jetty.session.jdbc.driverClass::::
|
||||||
|
Name of the JDBC driver that controls access to the remote database, such as `com.mysql.jdbc.Driver`
|
||||||
|
jetty.session.jdbc.driverUrl::::
|
||||||
|
URL of the database which includes the driver type, host name and port, service name and any specific attributes unique to the database, such as a username.
|
||||||
|
As an example, here is a mysql connection with the username appended: `jdbc:mysql://127.0.0.1:3306/sessions?user=sessionsadmin`.
|
||||||
|
|
||||||
|
jetty.session.jdbc.blobType::
|
||||||
|
Optional.
|
||||||
|
Default `blob` or `bytea` for Postgres.
|
||||||
|
This is the keyword used by the particular database to identify the blob data type.
|
||||||
|
If netiher default is suitable you can set this value explicitly.
|
||||||
|
|
||||||
|
jetty.session.jdbc.longType::
|
||||||
|
Optional.
|
||||||
|
Default `bigint` or `number(20)` for Oracle.
|
||||||
|
This is the keyword used by the particular database to identify the long integer data type.
|
||||||
|
Set this explicitly if neither of the default values is appropriate.
|
||||||
|
|
||||||
|
jetty.session.jdbc.stringType::
|
||||||
|
Optional.
|
||||||
|
Default `varchar`.
|
||||||
|
This is the keyword used by the particular database to identify character type.
|
||||||
|
If the default is not suitable, you can set this value explicitly.
|
||||||
|
|
||||||
|
jetty.session.jdbc.schema.schemaName::
|
||||||
|
jetty.session.jdbc.schema.catalogName::
|
||||||
|
Optional.
|
||||||
|
The exact meaning of these two properties is dependent on your database vendor, but can broadly be described as further scoping for the session table name.
|
||||||
|
See https://en.wikipedia.org/wiki/Database_schema[] and https://en.wikipedia.org/wiki/Database_catalog[].
|
||||||
|
These extra scoping names can come into play at startup time when Jetty determines if the session table already exists, or otherwise creates it on-the-fly.
|
||||||
|
If you have employed either of these concepts when you pre-created the session table, or you want to ensure that Jetty uses them when it auto-creates the session table, then you have two options: either set them explicitly, or let Jetty infer them from a database connection (obtained using either a Datasource or Driver according to the `db-connection-type` you have configured).
|
||||||
|
To set them explicitly, uncomment and supply appropriate values for the `jetty.session.jdbc.schema.schemaName` and/or `jetty.session.jdbc.schema.catalogName` properties.
|
||||||
|
Alternatively, to allow Jetty to infer them from a database connection, use the special string `INFERRED` instead.
|
||||||
|
If you leave them blank or commented out, then the sessions table will not be scoped by schema or catalog name.
|
||||||
|
|
||||||
|
jetty.session.jdbc.schema.table::
|
||||||
|
Default `JettySessions`.
|
||||||
|
This is the name of the table in which session data is stored.
|
||||||
|
|
||||||
|
jetty.session.jdbc.schema.accessTimeColumn::
|
||||||
|
Default `accessTime`.
|
||||||
|
This is the name of the column that stores the time - in ms since the epoch - at which a session was last accessed
|
||||||
|
|
||||||
|
jetty.session.jdbc.schema.contextPathColumn::
|
||||||
|
Default `contextPath`.
|
||||||
|
This is the name of the column that stores the `contextPath` of a session.
|
||||||
|
|
||||||
|
jetty.session.jdbc.schema.cookieTimeColumn::
|
||||||
|
Default `cookieTime`.
|
||||||
|
This is the name of the column that stores the time - in ms since the epoch - that the cookie was last set for a session.
|
||||||
|
|
||||||
|
jetty.session.jdbc.schema.createTimeColumn::
|
||||||
|
Default `createTime`.
|
||||||
|
This is the name of the column that stores the time - in ms since the epoch - at which a session was created.
|
||||||
|
|
||||||
|
jetty.session.jdbc.schema.expiryTimeColumn::
|
||||||
|
Default `expiryTime`.
|
||||||
|
This is name of the column that stores - in ms since the epoch - the time at which a session will expire.
|
||||||
|
|
||||||
|
jetty.session.jdbc.schema.lastAccessTimeColumn::
|
||||||
|
Default `lastAccessTime`.
|
||||||
|
This is the name of the column that stores the time - in ms since the epoch - that a session was previously accessed.
|
||||||
|
|
||||||
|
jetty.session.jdbc.schema.lastSavedTimeColumn::
|
||||||
|
Default `lastSavedTime`.
|
||||||
|
This is the name of the column that stores the time - in ms since the epoch - at which a session was last written.
|
||||||
|
|
||||||
|
jetty.session.jdbc.schema.idColumn::
|
||||||
|
Default `sessionId`.
|
||||||
|
This is the name of the column that stores the id of a session.
|
||||||
|
|
||||||
|
jetty.session.jdbc.schema.lastNodeColumn::
|
||||||
|
Default `lastNode`.
|
||||||
|
This is the name of the column that stores the `workerName` of the last node to write a session.
|
||||||
|
|
||||||
|
jetty.session.jdbc.schema.virtualHostColumn::
|
||||||
|
Default `virtualHost`.
|
||||||
|
This is the name of the column that stores the first virtual host of the context of a session.
|
||||||
|
|
||||||
|
jetty.session.jdbc.schema.maxIntervalColumn::
|
||||||
|
Default `maxInterval`.
|
||||||
|
This is the name of the column that stores the interval - in ms - during which a session can be idle before being considered expired.
|
||||||
|
|
||||||
|
jetty.session.jdbc.schema.mapColumn::
|
||||||
|
Default `map`.
|
||||||
|
This is the name of the column that stores the serialized attributes of a session.
|
||||||
|
|
||||||
|
[[mongo]]
|
||||||
|
== Modules for Persistent HTTP Sessions: MongoDB
|
||||||
|
|
||||||
|
Enabling the `session-store-mongo` module configures Jetty to store session data in MongoDB.
|
||||||
|
|
||||||
|
Because MongoDB is not a technology provided by the Eclipse Foundation, you will be prompted to assent to the licenses of the external vendor (Apache in this case) during the install.
|
||||||
|
Jars needed by MongoDB are downloaded and stored into a directory named `$JETTY_BASE/lib/nosql/`.
|
||||||
|
|
||||||
|
IMPORTANT: If you want to use updated versions of the jar files automatically downloaded by Jetty, you can place them in the associated `$JETTY_BASE/lib/` directory and use the `--skip-file-validation=<module name>` command line option to prevent errors when starting your server.
|
||||||
|
|
||||||
|
=== Configuration
|
||||||
|
|
||||||
|
The `$JETTY_BASE/start.d/session-store-mongo.ini` file contains these configurable properties:
|
||||||
|
|
||||||
|
jetty.session.mongo.dbName::
|
||||||
|
Default is "HttpSessions".
|
||||||
|
This is the name of the database in MongoDB used to store the session collection.
|
||||||
|
|
||||||
|
jetty.session.mongo.collectionName::
|
||||||
|
Default is "jettySessions".
|
||||||
|
This is the name of the collection in MongoDB used to store all of the sessions.
|
||||||
|
|
||||||
|
The connection type-::
|
||||||
|
You can connect to MongoDB either using a host/port combination, or a URI.
|
||||||
|
By default, the host/port method is selected, but you can change this by commenting out the unwanted method, and uncommenting the other one.
|
||||||
|
connection-type=address:::
|
||||||
|
Used when utilizing a direct connection to the MongoDB server.
|
||||||
|
jetty.session.mongo.host::::
|
||||||
|
Host name or address for the remote MongoDB instance.
|
||||||
|
jetty.session.mongo.port::::
|
||||||
|
Port number for the remote MongoDB instance.
|
||||||
|
connection-type=uri:::
|
||||||
|
Used when utilizing MongoURI for secured connections.
|
||||||
|
jetty.session.mongo.connectionString::::
|
||||||
|
The string defining the MongoURI value, such as `+mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]+`.
|
||||||
|
More information on how to format the MongoURI string can be found in the https://docs.mongodb.com/manual/reference/connection-string/[official documentation for mongo].
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
You will only use *one* `connection-type` at a time, either `address` or `uri`.
|
||||||
|
If both are utilized in your `session-store-mongo.ini`, only the _last_ `connection-type` configured in the file will be used.
|
||||||
|
====
|
||||||
|
|
||||||
|
jetty.session.gracePeriod.seconds::
|
||||||
|
Integer, in seconds.
|
||||||
|
Default 3600.
|
||||||
|
Used during session <<base-scavenge,scavenging>>.
|
||||||
|
Multiples of this period are used to define how long ago a stored session must have expired before it should be <<base-scavenge,scavenged>>.
|
||||||
|
|
||||||
|
jetty.session.savePeriod.seconds::
|
||||||
|
Integer, in seconds, default is `0`.
|
||||||
|
Whenever a session is accessed by a request, its `lastAccessTime` and `expiry` are updated.
|
||||||
|
Even if your sessions are read-mostly, the `lastAccessTime` and `expiry` will always change.
|
||||||
|
For heavily-used, read-mostly sessions you can save some time by skipping some writes for sessions for which only these fields have changed (ie no session attributes changed).
|
||||||
|
The value of this property is used to skip writes for these kinds of sessions: the session will only be written out if the time since the last write exceeds the value of this property.
|
||||||
|
|
||||||
|
[WARNING]
|
||||||
|
====
|
||||||
|
You should be careful in the use of this property in clustered environments: if you set too large a value for this property, the session may not be written out sufficiently often to update its `expiry` time thus making it appear to other nodes that it has expired.
|
||||||
|
Thorough consideration of the `maxIdleTime` of the session when setting the `savePeriod` is imperative - it would be undesirable to set a `savePeriod` that is larger than the `maxIdleTime`.
|
||||||
|
====
|
||||||
|
|
||||||
|
[[infinispan]]
|
||||||
|
== Modules for Persistent HTTP Sessions: Infinispan
|
||||||
|
|
||||||
|
In order to persist/cluster sessions using Infinispan, Jetty needs to know how to contact Infinispan.
|
||||||
|
There are two options: a remote Infinispan instance, or an in-process Infinispan instance.
|
||||||
|
The former is referred to as "remote" Infinispan and the latter as "embedded" Infinispan.
|
||||||
|
If you wish Jetty to be able to <<base-scavenge,scavenge>> expired sessions, you will also need to enable the appropriate `infinispan-[remote|embedded]-query` module.
|
||||||
|
|
||||||
|
[[infinispan-remote]]
|
||||||
|
=== Remote Infinispan Session Module
|
||||||
|
|
||||||
|
The `session-store-infinispan-remote` module configures Jetty to talk to an external Infinispan instance to store session data.
|
||||||
|
|
||||||
|
Because Infinispan is not a technology provided by the Eclipse Foundation, you will be prompted to assent to the licenses of the external vendor (Apache in this case).
|
||||||
|
|
||||||
|
Infinispan-specific jar files are download to the directory named `$JETTY_BASE/lib/infinispan/`.
|
||||||
|
|
||||||
|
In addition to adding these modules to the classpath of the server it also added several ini configuration files to the `$JETTY_BASE/start.d` directory.
|
||||||
|
|
||||||
|
NOTE: If you have updated versions of the jar files automatically downloaded by Jetty, you can place them in the associated `$JETTY_BASE/lib/` directory and use the `--skip-file-validation=<module name>` command line option to prevent errors when starting your server.
|
||||||
|
|
||||||
|
==== Configuration
|
||||||
|
|
||||||
|
The `$JETTY_BASE/start.d/session-store-infinispan-remote.ini` contains the following configurable properties:
|
||||||
|
|
||||||
|
jetty.session.infinispan.remoteCacheName::
|
||||||
|
Default `"sessions"`.
|
||||||
|
This is the name of the cache in Infinispan where sessions will be stored.
|
||||||
|
|
||||||
|
jetty.session.infinispan.idleTimeout.seconds::
|
||||||
|
Integer, in seconds, default `0`.
|
||||||
|
This is the amount of time, in seconds, that a session entry in Infinispan can be idle (ie neither read nor written) before Infinispan will delete its entry.
|
||||||
|
Usually, you do *not* want to set a value for this, as you want Jetty to manage all session expiration (and call any HttpSessionListeners).
|
||||||
|
You *should* enable the <<infinispan-remote-query,infinispan-remote-query>> to allow jetty to <<base-scavenge,scavenge>> for expired sessions.
|
||||||
|
If you do not, then there is the possibility that sessions can be left in Infinispan but no longer referenced by any Jetty node (so called "zombie" or "orphan" sessions), in which case you can use this feature to ensure their removal.
|
||||||
|
|
||||||
|
IMPORTANT: You should make sure that the number of seconds you specify is larger than the configured `maxIdleTime` for sessions.
|
||||||
|
|
||||||
|
jetty.session.gracePeriod.seconds::
|
||||||
|
Integer, default 3600.
|
||||||
|
Used during session <<base-scavenge,scavenging>>.
|
||||||
|
Multiples of this period are used to define how long ago a stored session must have expired before it should be <<base-scavenge,scavenged>>.
|
||||||
|
|
||||||
|
jetty.session.savePeriod.seconds::
|
||||||
|
Integer, in seconds, default is `0`.
|
||||||
|
Whenever a session is accessed by a request, its `lastAccessTime` and `expiry` are updated.
|
||||||
|
Even if your sessions are read-mostly, the `lastAccessTime` and `expiry` will always change.
|
||||||
|
For heavily-used, read-mostly sessions you can save some time by skipping some writes for sessions for which only these fields have changed (ie no session attributes changed).
|
||||||
|
The value of this property is used to skip writes for these kinds of sessions: the session will only be written out if the time since the last write exceeds the value of this property.
|
||||||
|
|
||||||
|
[WARNING]
|
||||||
|
====
|
||||||
|
You should be careful in the use of this property in clustered environments: if you set too large a value for this property, the session may not be written out sufficiently often to update its `expiry` time thus making it appear to other nodes that it has expired.
|
||||||
|
Thorough consideration of the `maxIdleTime` of the session when setting the `savePeriod` is imperative - it would be undesirable to set a `savePeriod` that is larger than the `maxIdleTime`.
|
||||||
|
====
|
||||||
|
|
||||||
|
[[infinispan-remote-query]]
|
||||||
|
=== Remote Infinispan Query Module
|
||||||
|
|
||||||
|
The `infinispan-remote-query` module allows Jetty to <<base-scavenge,scavenge>> expired sessions.
|
||||||
|
Note that this is an *additional* module, to be used in conjunction with the `session-store-infinispan-remote` module.
|
||||||
|
|
||||||
|
There are no configuration properties associated with this module.
|
||||||
|
|
||||||
|
[[infinispan-embedded]]
|
||||||
|
=== Embedded Infinispan Session Module
|
||||||
|
|
||||||
|
Enabling the `session-store-infinispan-embedded` module runs an in-process instance of Infinispan.
|
||||||
|
|
||||||
|
Because Infinispan is not a technology provided by the Eclipse Foundation, you will be prompted to assent to the licenses of the external vendor (Apache in this case).
|
||||||
|
Infinispan-specific jar files will be downloaded and saved to a directory named `$JETTY_BASE/lib/infinispan/`.
|
||||||
|
|
||||||
|
NOTE: If you have updated versions of the jar files automatically downloaded by Jetty, you can place them in the associated `$JETTY_BASE/lib/` directory and use the `--skip-file-validation=<module name>` command line option to prevent errors when starting your server.
|
||||||
|
|
||||||
|
==== Configuration
|
||||||
|
|
||||||
|
The `$JETTY_BASE/start.d/session-store-infinispan-embedded.ini` contains the following configurable properties:
|
||||||
|
|
||||||
|
jetty.session.infinispan.idleTimeout.seconds::
|
||||||
|
Integer, in seconds, default `0`.
|
||||||
|
This is the amount of time, in seconds, that a session entry in Infinispan can be idle (ie neither read nor written) before Infinispan will delete its entry.
|
||||||
|
Usually, you do *not* want to set a value for this, as you want Jetty to manage all session expiration (and call any HttpSessionListeners).
|
||||||
|
You *should* enable the <<infinispan-embedded-query,infinispan-embedded-query>> to allow Jetty to <<base-scavenge,scavenge>> for expired sessions.
|
||||||
|
If you do not, then there is the possibility that expired sessions can be left in Infinispan.
|
||||||
|
|
||||||
|
IMPORTANT: You should make sure that the number of seconds you specify is larger than the configured `maxIdleTime` for sessions.
|
||||||
|
|
||||||
|
jetty.session.gracePeriod.seconds::
|
||||||
|
Integer, default 3600.
|
||||||
|
Used during session <<base-scavenge,scavenging>>.
|
||||||
|
Multiples of this period are used to define how long ago a stored session must have expired before it should be <<base-scavenge,scavenged>>.
|
||||||
|
|
||||||
|
jetty.session.savePeriod.seconds::
|
||||||
|
Integer, in seconds, default is `0`.
|
||||||
|
Whenever a session is accessed by a request, its `lastAccessTime` and `expiry` are updated.
|
||||||
|
Even if your sessions are read-mostly, the `lastAccessTime` and `expiry` will always change.
|
||||||
|
For heavily-used, read-mostly sessions you can save some time by skipping some writes for sessions for which only these fields have changed (ie no session attributes changed).
|
||||||
|
The value of this property is used to skip writes for these kinds of sessions: the session will only be written out if the time since the last write exceeds the value of this property.
|
||||||
|
|
||||||
|
[WARNING]
|
||||||
|
====
|
||||||
|
Thorough consideration of the `maxIdleTime` of the session when setting the `savePeriod` is imperative - it would be undesirable to set a `savePeriod` that is larger than the `maxIdleTime`.
|
||||||
|
====
|
||||||
|
|
||||||
|
[[infinispan-embedded-query]]
|
||||||
|
=== Embedded Infinispan Query Module
|
||||||
|
|
||||||
|
The `infinispan-embedded-query` module allows Jetty to <<base-scavenge,scavenge>> expired sessions.
|
||||||
|
|
||||||
|
There are no configuration properties associated with this module.
|
||||||
|
|
||||||
|
=== Converting Session Format for Jetty-9.4.13
|
||||||
|
|
||||||
|
From Jetty-9.4.13 onwards, we have changed the format of the serialized session when using a remote cache (ie using hotrod).
|
||||||
|
Prior to release 9.4.13 we used the default Infinispan serialization, however this was not able to store sufficient information to allow Jetty to properly deserialize session attributes in all circumstances.
|
||||||
|
See issue https://github.com/eclipse/jetty.project/issues/2919[] for more background.
|
||||||
|
|
||||||
|
We have provided a conversion program which will convert any sessions stored in Infinispan to the new format.
|
||||||
|
|
||||||
|
IMPORTANT: We recommend that you backup your stored sessions before running the conversion program.
|
||||||
|
|
||||||
|
How to use the converter:
|
||||||
|
|
||||||
|
----
|
||||||
|
java -cp jetty-servlet-api-4.0.2.jar:jetty-util-{VERSION}.jar:jetty-server-{VERSION}.jar:infinispan-remote-9.1.0.Final.jar:jetty-infinispan-{VERSION}.jar:[other classpath] org.eclipse.jetty.session.infinispan.InfinispanSessionLegacyConverter
|
||||||
|
|
||||||
|
Usage: InfinispanSessionLegacyConverter [-Dhost=127.0.0.1] [-Dverbose=true|false] <cache-name> [check]
|
||||||
|
----
|
||||||
|
|
||||||
|
The classpath::
|
||||||
|
Must contain the servlet-api, jetty-util, jetty-server, jetty-infinispan and infinispan-remote jars. If your sessions contain attributes that use application classes, you will also need to also put those classes onto the classpath. If your session has been authenticated, you may also need to include the jetty-security and jetty-http jars on the classpath.
|
||||||
|
|
||||||
|
Parameters::
|
||||||
|
When used with no arguments the usage message is printed. When used with the `cache-name` parameter the conversion is performed. When used with both `cache-name` and `check` parameters, sessions are checked for whether or not they are converted.
|
||||||
|
-Dhost::: you can optionally provide a system property with the address of your remote Infinispan server. Defaults to the localhost.
|
||||||
|
-Dverbose::: defaults to false. If true, prints more comprehensive stacktrace information about failures. Useful to diagnose why a session is not converted.
|
||||||
|
cache-name::: the name of the remote cache containing your sessions. This is mandatory.
|
||||||
|
check::: the optional check command will verify sessions have been converted. Use it _after_ doing the conversion.
|
||||||
|
|
||||||
|
To perform the conversion, run the InfinispanSessionLegacyConverter with just the `cache-name`, and optionally the `host` system property.
|
||||||
|
The following command will attempt to convert all sessions in the cached named `my-remote-cache` on the machine `myhost`, ensuring that application classes in the `/my/custom/classes` directory are on the classpath:
|
||||||
|
|
||||||
|
----
|
||||||
|
java -cp jetty-servlet-api-4.0.2.jar:jetty-util-{VERSION}.jar:jetty-server-{VERSION}.jar:infinispan-remote-9.1.0.Final.jar:jetty-infinispan-{VERSION}.jar:/my/custom/classes org.eclipse.jetty.session.infinispan.InfinispanSessionLegacyConverter -Dhost=myhost my-remote-cache
|
||||||
|
----
|
||||||
|
|
||||||
|
If the converter fails to convert a session, an error message and stacktrace will be printed and the conversion will abort. The failed session should be untouched, however _it is prudent to take a backup of your cache before attempting the conversion_.
|
||||||
|
|
||||||
|
[[hazelcast]]
|
||||||
|
== Modules for Persistent HTTP Sessions: Hazelcast
|
||||||
|
|
||||||
|
Hazelcast can be used to cluster session information in one of two modes: either remote or embedded.
|
||||||
|
Remote mode means that Hazelcast will create a client to talk to other instances, possibly on other nodes.
|
||||||
|
Embedded mode means that Hazelcast will start a local instance and communicate with that.
|
||||||
|
|
||||||
|
[[hazelcast-remote]]
|
||||||
|
=== Remote Hazelcast Clustering
|
||||||
|
|
||||||
|
Enabling the `session-store-hazelcast-remote` module allows jetty to communicate with a remote Hazelcast instance to cluster session data.
|
||||||
|
|
||||||
|
Because Hazelcast is not a technology provided by the Eclipse Foundation, you will be prompted to assent to the licenses of the external vendor (Apache in this case).
|
||||||
|
|
||||||
|
Hazelcast-specific jar files will be downloaded and saved to a directory named `$JETTY_BASE/lib/hazelcast/`.
|
||||||
|
|
||||||
|
NOTE: If you have updated versions of the jar files automatically downloaded by Jetty, you can place them in the associated `$JETTY_BASE/lib/` directory and use the `--skip-file-validation=<module name>` command line option to prevent errors when starting your server.
|
||||||
|
|
||||||
|
==== Configuration
|
||||||
|
|
||||||
|
The `start.d/session-store-hazelcast-remote.ini` contains a list of all the configurable options for the Hazelcast module:
|
||||||
|
|
||||||
|
jetty.session.hazelcast.mapName::
|
||||||
|
The default is "jetty-distributed-session-map".
|
||||||
|
This is the name of the Map in Hazelcast where sessions will be stored.
|
||||||
|
|
||||||
|
jetty.session.hazelcast.onlyClient::
|
||||||
|
Boolean, default `true`.
|
||||||
|
The Hazelcast instance will be configured in client mode.
|
||||||
|
|
||||||
|
jetty.session.hazelcast.configurationLocation::
|
||||||
|
Optional.
|
||||||
|
This is the path to an external Hazelcast xml configuration file.
|
||||||
|
|
||||||
|
jetty.session.hazelcast.useQueries::
|
||||||
|
Boolean, default `false`.
|
||||||
|
If `true`, Jetty will use Hazelcast queries to find sessions to <<base-scavenge,scavenge>>.
|
||||||
|
If `false` sessions that are not currently in a <<cache,session cache>> cannot be <<base-scavenge,scavenged>>, and will need to be removed by some external process.
|
||||||
|
|
||||||
|
jetty.session.hazelcast.addresses::
|
||||||
|
Optional.
|
||||||
|
These are the addresses of remote Hazelcast instances with which to communicate.
|
||||||
|
|
||||||
|
jetty.session.gracePeriod.seconds::
|
||||||
|
Integer, in seconds.
|
||||||
|
Default 3600.
|
||||||
|
Used during session <<base-scavenge,scavenging>>.
|
||||||
|
Multiples of this period are used to define how long ago a stored session must have expired before it should be <<base-scavenge,scavenged>>.
|
||||||
|
|
||||||
|
jetty.session.savePeriod.seconds::
|
||||||
|
Integer, in seconds, default is `0`.
|
||||||
|
Whenever a session is accessed by a request, its `lastAccessTime` and `expiry` are updated.
|
||||||
|
Even if your sessions are read-mostly, the `lastAccessTime` and `expiry` will always change.
|
||||||
|
For heavily-used, read-mostly sessions you can save some time by skipping some writes for sessions for which only these fields have changed (ie no session attributes changed).
|
||||||
|
The value of this property is used to skip writes for these kinds of sessions: the session will only be written out if the time since the last write exceeds the value of this property.
|
||||||
|
|
||||||
|
[WARNING]
|
||||||
|
====
|
||||||
|
You should be careful in the use of this property in clustered environments: if you set too large a value for this property, the session may not be written out sufficiently often to update its `expiry` time thus making it appear to other nodes that it has expired.
|
||||||
|
Thorough consideration of the `maxIdleTime` of the session when setting the `savePeriod` is imperative - it would be undesirable to set a `savePeriod` that is larger than the `maxIdleTime`.
|
||||||
|
====
|
||||||
|
|
||||||
|
IMPORTANT: Be aware that if your session attributes contain classes from inside your webapp (or Jetty classes) then you will need to put these classes onto the classpath of all of your Hazelcast instances.
|
||||||
|
|
||||||
|
[[hazelcast-embedded]]
|
||||||
|
=== Embedded Hazelcast Clustering
|
||||||
|
|
||||||
|
This will run an in-process instance of Hazelcast.
|
||||||
|
This can be useful for example during testing.
|
||||||
|
To enable this you enable the `session-store-hazelcast-embedded` module.
|
||||||
|
|
||||||
|
Because Hazelcast is not a technology provided by the Eclipse Foundation, you will be prompted to assent to the licenses of the external vendor (Apache in this case).
|
||||||
|
|
||||||
|
Hazelcast-specific jar files will be downloaded to a directory named `$JETTY_BASE/lib/hazelcast/`.
|
||||||
|
|
||||||
|
==== Configuration
|
||||||
|
|
||||||
|
The `$JETTY_BASE/start.d/start.d/session-store-hazelcast-embedded.ini` contains a list of all the configurable options for the Hazelcast module:
|
||||||
|
|
||||||
|
jetty.session.hazelcast.mapName::
|
||||||
|
The default is "jetty-distributed-session-map".
|
||||||
|
This is the name of the Map in Hazelcast where sessions will be stored.
|
||||||
|
jetty.session.hazelcast.hazelcastInstanceName
|
||||||
|
Default is "JETTY_DISTRIBUTED_SESSION_INSTANCE".
|
||||||
|
This is the unique name of the Hazelcast instance that will be created.
|
||||||
|
|
||||||
|
jetty.session.hazelcast.configurationLocation::
|
||||||
|
Optional.
|
||||||
|
This is the path to an external Hazelcast xml configuration file.
|
||||||
|
|
||||||
|
jetty.session.hazelcast.useQueries::
|
||||||
|
Boolean, default `false'.
|
||||||
|
If `true`, Jetty will use Hazelcast queries to find expired sessions to <<base-scavenge,scavenge>>.
|
||||||
|
If `false` sessions that are not currently in a <<cache,session cache>> cannot be <<base-scavenge,scavenged>>, and will need to be removed by some external process.
|
||||||
|
|
||||||
|
jetty.session.gracePeriod.seconds::
|
||||||
|
Integer, in seconds.
|
||||||
|
Default 3600.
|
||||||
|
Used during session <<base-scavenge,scavenging>>.
|
||||||
|
Multiples of this period are used to define how long ago a stored session must have expired before it should be <<base-scavenge,scavenged>>.
|
||||||
|
|
||||||
|
jetty.session.savePeriod.seconds::
|
||||||
|
Integer, in seconds, default is `0`.
|
||||||
|
Whenever a session is accessed by a request, its `lastAccessTime` and `expiry` are updated.
|
||||||
|
Even if your sessions are read-mostly, the `lastAccessTime` and `expiry` will always change.
|
||||||
|
For heavily-used, read-mostly sessions you can save some time by skipping some writes for sessions for which only these fields have changed (ie no session attributes changed).
|
||||||
|
The value of this property is used to skip writes for these kinds of sessions: the session will only be written out if the time since the last write exceeds the value of this property.
|
||||||
|
|
||||||
|
[WARNING]
|
||||||
|
====
|
||||||
|
You should be careful in the use of this property in clustered environments: if you set too large a value for this property, the session may not be written out sufficiently often to update its `expiry` time thus making it appear to other nodes that it has expired.
|
||||||
|
Thorough consideration of the `maxIdleTime` of the session when setting the `savePeriod` is imperative - it would be undesirable to set a `savePeriod` that is larger than the `maxIdleTime`.
|
||||||
|
====
|
||||||
|
|
||||||
|
IMPORTANT: If your session attributes contain classes from inside your webapp (or jetty classes) then you will need to put these classes onto the classpath of all of your hazelcast instances. In the case of embedded hazelcast, as it is started before your webapp, it will NOT have access to your webapp's classes - you will need to extract these classes and put them onto the jetty server's classpath.
|
||||||
|
|
||||||
|
[[gcloud]]
|
||||||
|
== Modules for Persistent HTTP Sessions: Google Cloud DataStore
|
||||||
|
|
||||||
|
Jetty can store http session information into GCloud by enabling the `session-store-gcloud` module.
|
||||||
|
|
||||||
|
=== Preparation
|
||||||
|
|
||||||
|
You will first need to create a project and enable the Google Cloud api: https://cloud.google.com/docs/authentication#preparation[].
|
||||||
|
Take note of the project id that you create in this step as you need to supply it in later steps.
|
||||||
|
|
||||||
|
=== Communicating with GCloudDataStore
|
||||||
|
|
||||||
|
==== When Running Jetty Outside of Google Infrastructure
|
||||||
|
|
||||||
|
Before running Jetty, you will need to choose one of the following methods to set up the local environment to enable remote GCloud DataStore communications.
|
||||||
|
|
||||||
|
1. Using the GCloud SDK:
|
||||||
|
* Ensure you have the GCloud SDK installed: https://cloud.google.com/sdk/?hl=en[]
|
||||||
|
* Use the GCloud tool to set up the project you created in the preparation step: `gcloud config set project PROJECT_ID`
|
||||||
|
* Use the GCloud tool to authenticate a google account associated with the project created in the preparation step: `gcloud auth login ACCOUNT`
|
||||||
|
|
||||||
|
2. Using environment variables
|
||||||
|
* Define the environment variable `GCLOUD_PROJECT` with the project id you created in the preparation step.
|
||||||
|
* Generate a JSON https://cloud.google.com/storage/docs/authentication?hl=en#service_accounts[service account key] and then define the environment variable `GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/key.json`
|
||||||
|
|
||||||
|
==== When Running Jetty Inside of Google Infrastructure
|
||||||
|
|
||||||
|
The Google deployment tools will automatically configure the project and authentication information for you.
|
||||||
|
|
||||||
|
=== Configuring Indexes for Session Data
|
||||||
|
|
||||||
|
Using some special, composite indexes can speed up session search operations, although it may make write operations slower.
|
||||||
|
By default, indexes will _not_ be used.
|
||||||
|
In order to use them, you will need to manually upload a file that defines the indexes.
|
||||||
|
This file is named `index.yaml` and you can find it in your distribution in `$JETTY_BASE/etc/sessions/gcloud/index.yaml`.
|
||||||
|
|
||||||
|
Follow the instructions https://cloud.google.com/datastore/docs/tools/#the_development_workflow_using_gcloud[here] to upload the pre-generated `index.yaml` file.
|
||||||
|
|
||||||
|
=== Communicating with the GCloudDataStore Emulator
|
||||||
|
|
||||||
|
To enable communication using the GCloud Emulator:
|
||||||
|
|
||||||
|
* Ensure you have the GCloud SDK installed: https://cloud.google.com/sdk/?hl=en[]
|
||||||
|
* Follow the instructions https://cloud.google.com/datastore/docs/tools/datastore-emulator[here] on how to start the GCloud datastore emulator, and how to propagate the environment variables that it creates to the terminal in which you run Jetty.
|
||||||
|
|
||||||
|
=== Enabling the Google Cloud DataStore Module
|
||||||
|
|
||||||
|
The `session-store-gcloud` module provides GCloud support for storing session data.
|
||||||
|
|
||||||
|
Because the Google Cloud DataStore is not a technology provided by the Eclipse Foundation, when enabling the module you will be prompted to assent to the licenses of the external vendor.
|
||||||
|
|
||||||
|
As GCloud requires certain Java Commons Logging features to work correctly, Jetty routes these through SLF4J.
|
||||||
|
By default Jetty implements the SLF4J api, but you can choose a different logging implementation by following the instructions xref:server/index.adoc#logging[here]
|
||||||
|
|
||||||
|
IMPORTANT: If you want to use updated versions of the jar files automatically downloaded during the module enablement, you can place them in the associated `$JETTY_BASE/lib/` directory and use the `--skip-file-validation=<module name>` command line option to prevent errors when starting your server.
|
||||||
|
|
||||||
|
== Configuration
|
||||||
|
|
||||||
|
The `$JETTY_BASE/start.d/session-store-gcloud.ini` file contains all of the configurable properties for the `session-store-gcloud` module:
|
||||||
|
|
||||||
|
jetty.session.gcloud.maxRetries::
|
||||||
|
Integer.
|
||||||
|
Default 5.
|
||||||
|
Maximum number of retries to connect to GCloud DataStore to write a session.
|
||||||
|
|
||||||
|
jetty.session.gcloud.backoffMs::
|
||||||
|
Integer in milliseconds.
|
||||||
|
Default 1000.
|
||||||
|
Number of milliseconds between successive attempts to connect to the GCloud DataStore to write a session.
|
||||||
|
|
||||||
|
jetty.session.gracePeriod.seconds::
|
||||||
|
Integer, in seconds.
|
||||||
|
Default 3600.
|
||||||
|
Used during session <<base-scavenge,scavenging>>.
|
||||||
|
Multiples of this period are used to define how long ago a stored session must have expired before it should be <<base-scavenge,scavenged>>.
|
||||||
|
|
||||||
|
jetty.session.savePeriod.seconds::
|
||||||
|
Integer, in seconds, default is `0`.
|
||||||
|
Whenever a session is accessed by a request, its `lastAccessTime` and `expiry` are updated.
|
||||||
|
Even if your sessions are read-mostly, the `lastAccessTime` and `expiry` will always change.
|
||||||
|
For heavily-used, read-mostly sessions you can save some time by skipping some writes for sessions for which only these fields have changed (ie no session attributes changed).
|
||||||
|
The value of this property is used to skip writes for these kinds of sessions: the session will only be written out if the time since the last write exceeds the value of this property.
|
||||||
|
|
||||||
|
[WARNING]
|
||||||
|
====
|
||||||
|
You should be careful in the use of this property in clustered environments: if you set too large a value for this property, the session may not be written out sufficiently often to update its `expiry` time thus making it appear to other nodes that it has expired.
|
||||||
|
Thorough consideration of the `maxIdleTime` of the session when setting the `savePeriod` is imperative - it would be undesirable to set a `savePeriod` that is larger than the `maxIdleTime`.
|
||||||
|
====
|
||||||
|
|
||||||
|
jetty.session.gcloud.namespace::
|
||||||
|
Optional.
|
||||||
|
Sets the namespace for GCloud Datastore to use.
|
||||||
|
If set, partitions the visibility of session data between webapps, which is helpful for multi-tenant deployments.
|
||||||
|
More information can be found https://cloud.google.com/datastore/docs/concepts/multitenancy[here.]
|
||||||
|
|
||||||
|
Configuration of the stored session object and its fields names-::
|
||||||
|
You should very rarely, if ever, need to change these defaults.
|
||||||
|
jetty.session.gcloud.model.kind:::
|
||||||
|
The default is "GCloudSession".
|
||||||
|
This is the type of the object that is stored in GCloud.
|
||||||
|
jetty.session.gcloud.model.id:::
|
||||||
|
The default is "id".
|
||||||
|
This is the session id.
|
||||||
|
jetty.session.gcloud.model.contextPath:::
|
||||||
|
The default is "contextPath".
|
||||||
|
This is the canonicalized context path of the context to which the session belongs.
|
||||||
|
jetty.session.gcloud.model.vhost:::
|
||||||
|
The default is "vhost".
|
||||||
|
This is the canonicalized virtual host of the context to which the session belongs.
|
||||||
|
jetty.session.gcloud.model.accessed:::
|
||||||
|
The default is "accessed".
|
||||||
|
This is the current access time of the session.
|
||||||
|
jetty.session.gcloud.model.lastAccessed:::
|
||||||
|
The default is "lastAccessed".
|
||||||
|
This is the last access time of the session.
|
||||||
|
jetty.session.gcloud.model.createTime:::
|
||||||
|
The default is "createTime".
|
||||||
|
This is the time, in ms since the epoch, at which the session was created.
|
||||||
|
jetty.session.gcloud.model.cookieSetTime:::
|
||||||
|
The default is "cookieSetTime".
|
||||||
|
This is the time at which the session cookie was last set.
|
||||||
|
jetty.session.gcloud.model.lastNode:::
|
||||||
|
The default is "lastNode".
|
||||||
|
This is the `workerName` of the last node to manage the session.
|
||||||
|
jetty.session.gcloud.model.expiry:::
|
||||||
|
The default is "expiry".
|
||||||
|
This is the time, in ms since the epoch, at which the session will expire.
|
||||||
|
jetty.session.gcloud.model.maxInactive:::
|
||||||
|
The default is "maxInactive".
|
||||||
|
This is the session timeout in ms.
|
||||||
|
jetty.session.gcloud.model.attributes:::
|
||||||
|
The default is "attributes".
|
||||||
|
This is a map of all the session attributes.
|
||||||
|
|
||||||
|
[[memcached]]
|
||||||
|
|
||||||
|
== Modules for Persistent HTTP Sessions: The L2 Session Data Cache
|
||||||
|
|
||||||
|
If your chosen persistence technology is slow, it can be helpful to locally cache the session data.
|
||||||
|
The `CachingSessionDataStore` is a special type of `SessionDataStore` that locally caches session data, which makes reads faster. It writes-through to your chosen type of `SessionDataStore` when session data changes.
|
||||||
|
|
||||||
|
=== MemcachedSessionDataMap
|
||||||
|
|
||||||
|
The `MemcachedSessionDataMap` uses `memcached` to perform caching of `SessionData`.
|
||||||
|
|
||||||
|
To enable it with the Jetty distribution, enable the `session-store-cache` module, along with your chosen `session-store-xxxx` module.
|
||||||
|
|
||||||
|
==== Configuration
|
||||||
|
|
||||||
|
The `$JETTY_BASE/start.d/session-store-cache.ini` contains the following configurable properties:
|
||||||
|
|
||||||
|
jetty.session.memcached.host::
|
||||||
|
Default value is `localhost`.
|
||||||
|
This is the host on which the memcached server resides.
|
||||||
|
|
||||||
|
jetty.session.memcached.port::
|
||||||
|
Default value is `11211`.
|
||||||
|
This is the port on which the memcached server is listening.
|
||||||
|
|
||||||
|
jetty.session.memcached.expirySec::
|
||||||
|
Default value `0`.
|
||||||
|
This is the length of time in seconds that an item can remain in the memcached cache, where 0 indicates indefinitely.
|
||||||
|
|
||||||
|
jetty.session.memcached.heartbeats::
|
||||||
|
Default value `true`.
|
||||||
|
Whether the memcached system should generate heartbeats.
|
||||||
|
|
||||||
|
[[usecases]]
|
||||||
|
== Session Scenarios
|
||||||
|
|
||||||
|
=== Minimizing Support for Sessions
|
||||||
|
|
||||||
|
The standard support for webapps in Jetty will use sessions cached in memory, but not persisted/clustered, with a scavenge for expired sessions that occurs every 10 minutes.
|
||||||
|
If you wish to pare back support for sessions because you know your app doesn't use them (or use JSPs that use them), then you can do the following:
|
||||||
|
|
||||||
|
* enable the <<base,base sessions module>> and <<base,configure the scavenge interval>> to 0 to prevent scavenging
|
||||||
|
* enable the <<cache-null,null session cache module>> to prevent sessions being cached in memory
|
||||||
|
|
||||||
|
If you wish to do any further minimization, you should consult the xref:programming-guide:server/session.adoc[Programming Guide].
|
||||||
|
|
||||||
|
=== Clustering with a Sticky Load Balancer
|
||||||
|
|
||||||
|
Preferably, your cluster will utilize a sticky load balancer.
|
||||||
|
This will route requests for the same session to the same Jetty instance.
|
||||||
|
In this case, the <<cache-hash,`DefaultSessionCache`>> can be used to keep in-use session objects <<cache-hash,in memory>>.
|
||||||
|
You can fine-tune the cache by controlling how long session objects remain in memory with the <<cache-hash,eviction policy settings>>.
|
||||||
|
|
||||||
|
If you have a large number of sessions or very large session objects, then you may want to manage your memory allocation by controlling the amount of time session objects spend in the cache.
|
||||||
|
The `EVICT_ON_SESSION_EXIT` eviction policy will remove a session object from the cache as soon as the last simultaneous request referencing it exits.
|
||||||
|
Alternatively, the `EVICT_ON_INACTIVITY` policy will remove a session object from the cache after a configurable amount of time has passed without a request referencing it.
|
||||||
|
|
||||||
|
If your sessions are very long lived and infrequently referenced, you might use the `EVICT_ON_INACTIVITY_POLICY` to control the size of the cache.
|
||||||
|
|
||||||
|
If your sessions are small, or relatively few or stable in number or they are read-mostly, then you might select the `NEVER_EVICT` policy.
|
||||||
|
With this policy, session objects will remain in the cache until they either expire or are explicitly invalidated.
|
||||||
|
|
||||||
|
If you have a high likelihood of simultaneous requests for the same session object, then the `EVICT_ON_SESSION_EXIT` policy will ensure the session object stays in the cache as long as it is needed.
|
||||||
|
|
||||||
|
=== Clustering Without a Sticky Load Balancer
|
||||||
|
|
||||||
|
Without a sticky load balancer requests for the same session may arrive on any node in the cluster.
|
||||||
|
This means it is likely that the copy of the session object in any `SessionCache` is likely to be out-of-date, as the session was probably last accessed on a different node.
|
||||||
|
In this case, your choices are to use either the <<cache-null,`NullSessionCache`>> or to de-tune the <<cache-hash,`DefaultSessionCache`>>.
|
||||||
|
If you use the `NullSessionCache` all session object caching is avoided.
|
||||||
|
This means that every time a request references a session it must be read in from persistent storage.
|
||||||
|
It also means that there can be no sharing of session objects for multiple requests for the same session: each will have their own independent session object.
|
||||||
|
Furthermore, the outcome of session writes are indeterminate because the Servlet Specification does not mandate ACID transactions for sessions.
|
||||||
|
|
||||||
|
If you use the `DefaultSessionCache`, there is a risk that the caches on some nodes will contain out-of-date session information as simultaneous requests for the same session are scattered over the cluster.
|
||||||
|
To mitigate this somewhat you can use the `EVICT_ON_SESSION_EXIT` eviction policy: this will ensure that the session is removed from the cache as soon as the last simultaneous request for it exits.
|
||||||
|
Again, due to the lack of session transactionality, the ordering outcome of write operations cannot be guaranteed.
|
||||||
|
As the session is cached while at least one request is accessing it, it is possible for multiple simultaneous requests to share the same session object.
|
||||||
|
|
||||||
|
=== Handling Corrupted or Unreadable Session Data
|
||||||
|
|
||||||
|
For various reasons it might not be possible for the `SessionDataStore` to re-read a stored session.
|
||||||
|
One scenario is that the session stores a serialized object in its attributes, and after a re-deployment there in an incompatible class change.
|
||||||
|
Setting the `$JETTY_BASE/start.d/session-cache-hash.ini` or `$JETTY_BASE/start.d/session-cache-null.ini` property `jetty.session.removeUnloadableSessions` to `true` will allow the unreadable session to be removed from persistent storage.
|
||||||
|
This can be useful for preventing the <<base-scavenge,scavenger>> from continually generating errors on the same expired, but un-readable session.
|
|
@ -0,0 +1,622 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Jetty Start Mechanism
|
||||||
|
|
||||||
|
NOTE: Make sure you have read the xref:arch/index.adoc[Jetty architecture section] if you are not familiar with the terms used in this section.
|
||||||
|
|
||||||
|
The Jetty start mechanism is invoked by executing `$JETTY_HOME/start.jar`, from within a `$JETTY_BASE` directory, with zero or more command line options:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ cd $JETTY_BASE
|
||||||
|
$ java -jar $JETTY_HOME/start.jar ...
|
||||||
|
----
|
||||||
|
|
||||||
|
The Jetty start mechanism has two main modes of operation:
|
||||||
|
|
||||||
|
* The _tool_ mode, detailed in <<configure,this section>>, when it is used as a command line tool to configure the `$JETTY_BASE` directory by enabling modules, creating sub-directories and files, downloading files, etc.
|
||||||
|
In this mode, the JVM started with `java -jar $JETTY_HOME/start.jar` performs the specified command and then exits.
|
||||||
|
* The _start_ mode, detailed in <<start,this section>>, when it is used to start the JVM that runs Jetty with the specified configuration.
|
||||||
|
In this mode, the JVM started with `java -jar $JETTY_HOME/start.jar` starts Jetty and does not exit until stopped, for example by hitting kbd:[Ctrl+C] on the terminal.
|
||||||
|
|
||||||
|
Refer to the <<reference,Jetty start mechanism reference section>> for the complete list of the available command line options.
|
||||||
|
|
||||||
|
You want to use the Jetty start mechanism to <<configure,configure your $JETTY_BASE>> and then to <<start,start Jetty>>.
|
||||||
|
|
||||||
|
[[configure]]
|
||||||
|
== Configuring $JETTY_BASE
|
||||||
|
|
||||||
|
Within the Jetty start mechanism, the source of configurations is layered in this order, from higher priority to lower priority:
|
||||||
|
|
||||||
|
* The command line options.
|
||||||
|
* The `$JETTY_BASE` directory, and its files.
|
||||||
|
* The directory specified with the `--include-jetty-dir` option, and its files.
|
||||||
|
* The `$JETTY_HOME` directory, and its files.
|
||||||
|
|
||||||
|
[[configure-enable]]
|
||||||
|
=== Enabling Modules
|
||||||
|
|
||||||
|
You can enable Jetty modules persistently across restarts with the `--add-modules` command:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --add-modules=server,http
|
||||||
|
----
|
||||||
|
|
||||||
|
The Jetty start mechanism will look for the specified modules following the order specified above.
|
||||||
|
In the common case (without a `--include-jetty-dir` directory), it will look in `$JETTY_BASE/modules/` first and then in `$JETTY_HOME/modules/`.
|
||||||
|
|
||||||
|
Since the `server` and `http` modules are standard Jetty modules, they are present in `$JETTY_HOME/modules/` and loaded from there.
|
||||||
|
|
||||||
|
When you enable a Jetty module, the Jetty start mechanism:
|
||||||
|
|
||||||
|
* Creates the correspondent `+$JETTY_BASE/start.d/*.ini+` module configuration file.
|
||||||
|
The content of these `+*.ini+` files is copied from the `[ini-template]` section of the correspondent `+*.mod+` file.
|
||||||
|
* Executes the directives specified in `[files]` section (if present) of the `+*.mod+` file.
|
||||||
|
This may simply create a file or a directory, or download files from the Internet.
|
||||||
|
This step is performed transitively for all module dependencies.
|
||||||
|
|
||||||
|
For example, enabling the `server` and `http` modules results in the `$JETTY_BASE` directory to have the following structure:
|
||||||
|
|
||||||
|
----
|
||||||
|
$JETTY_BASE
|
||||||
|
├── resources
|
||||||
|
│ └── jetty-logging.properties
|
||||||
|
└── start.d
|
||||||
|
├── http.ini
|
||||||
|
└── server.ini
|
||||||
|
----
|
||||||
|
|
||||||
|
The `$JETTY_BASE/resources/jetty-logging.properties` is created by the `[files]` directives of the `logging-jetty` module, which is a transitive dependency of the `server` module.
|
||||||
|
|
||||||
|
[[configure-disable]]
|
||||||
|
=== Disabling Modules
|
||||||
|
|
||||||
|
A module is enabled because the correspondent `+$JETTY_BASE/start.d/*.ini+` file contains a `--module=<name>` directive.
|
||||||
|
|
||||||
|
Commenting out the `--module=<name>` directive effectively disables the module.
|
||||||
|
|
||||||
|
Deleting the correspondent `+$JETTY_BASE/start.d/*.ini+` file also disables the module.
|
||||||
|
|
||||||
|
[[configure-edit-ini]]
|
||||||
|
=== Editing `+*.ini+` Files
|
||||||
|
|
||||||
|
You can now edit the `+$JETTY_BASE/start.d/*.ini+` configuration files, typically by uncommenting properties to change their default value.
|
||||||
|
|
||||||
|
The `+$JETTY_BASE/start.d/*.ini+` configuration file may be missing, if the correspondent module is a transitive dependency.
|
||||||
|
You can easily generate the configuration file by explicitly enabling the module, for example to generate the `$JETTY_BASE/start.d/logging-jetty.ini` configuration file you would issue the following command (the module order does not matter):
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --add-modules=server,http,logging-jetty
|
||||||
|
----
|
||||||
|
|
||||||
|
The `$JETTY_BASE` directory structure is now:
|
||||||
|
|
||||||
|
[source,subs=quotes]
|
||||||
|
----
|
||||||
|
$JETTY_BASE
|
||||||
|
├── resources
|
||||||
|
│ └── jetty-logging.properties
|
||||||
|
└── start.d
|
||||||
|
├── http.ini
|
||||||
|
├── ##logging-jetty.ini##
|
||||||
|
└── server.ini
|
||||||
|
----
|
||||||
|
|
||||||
|
You want to edit the `+$JETTY_BASE/start.d/*.ini+` configuration files so that the configuration is applied every time Jetty is started (or re-started).
|
||||||
|
|
||||||
|
For example, `$JETTY_BASE/start.d/http.ini` contains the following property, commented out:
|
||||||
|
|
||||||
|
.http.ini
|
||||||
|
----
|
||||||
|
# jetty.http.port=8080
|
||||||
|
----
|
||||||
|
|
||||||
|
You can change the clear-text HTTP port Jetty listens to by uncommenting that property and changing its value:
|
||||||
|
|
||||||
|
.http.ini
|
||||||
|
----
|
||||||
|
jetty.http.port=9876
|
||||||
|
----
|
||||||
|
|
||||||
|
When Jetty is started (or re-started) this configuration is applied and Jetty will listen for clear-text HTTP/1.1 on port `9876`.
|
||||||
|
|
||||||
|
[[configure-enable-command-line]]
|
||||||
|
=== Enabling Modules on Command Line
|
||||||
|
|
||||||
|
You can also enable a module transiently, only for the current execution of the `java -jar $JETTY_HOME/start.jar` command.
|
||||||
|
|
||||||
|
If you have an empty `$JETTY_BASE`, the following command enables the `server` and `http` modules, but does not create any `+$JETTY_BASE/start.d/*.ini+` files.
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --module=server,http
|
||||||
|
----
|
||||||
|
|
||||||
|
Since there are no `+$JETTY_BASE/start.d/*.ini+` files, you can only customize the properties via the command line, for example:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --module=server,http jetty.http.port=9876
|
||||||
|
----
|
||||||
|
|
||||||
|
Enabling modules on the command line is useful to verify that the modules work as expected, or to try different configurations.
|
||||||
|
|
||||||
|
NOTE: It is possible to enable some module persistently via `--add-modules` and some other module transiently via `--module`.
|
||||||
|
|
||||||
|
Remember that once the current execution terminates, the modules enabled transiently on the command line via `--module` and their configuration are not saved and will not be enabled on the next execution (unless you specify them again on the command line).
|
||||||
|
|
||||||
|
[[configure-custom-module]]
|
||||||
|
=== Adding Your Own Modules
|
||||||
|
|
||||||
|
NOTE: Refer to the xref:modules/custom.adoc[custom module section] for the details about how to create your own modules.
|
||||||
|
|
||||||
|
You can add your own modules by adding a `+$JETTY_BASE/modules/*.mod+` file.
|
||||||
|
|
||||||
|
For example, you may want to add a Postgres JDBC driver to the server class-path, to avoid that each deployed web application bring its own version. This allows you to control the exact Postgres JDBC driver version for all web applications.
|
||||||
|
|
||||||
|
Create the `$JETTY_BASE/modules/postgresql.mod` file:
|
||||||
|
|
||||||
|
.postgresql.mod
|
||||||
|
----
|
||||||
|
include::code:example$jetty-modules/postgresql.mod[]
|
||||||
|
----
|
||||||
|
|
||||||
|
Then enable it:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --add-modules=postgresql
|
||||||
|
----
|
||||||
|
|
||||||
|
Enabling the `postgresql` module will execute the `[files]` directive (downloading the `+*.jar+` file from Maven Central if not already present) and create the `$JETTY_BASE/start.d/postgresql.ini` with the content of the `[ini-template]` section.
|
||||||
|
|
||||||
|
The `[lib]` section ensures that the specified file is in the server class-path when Jetty is started.
|
||||||
|
|
||||||
|
You can <<configure-display,display the Jetty configuration>> to verify that the server class-path is correct.
|
||||||
|
|
||||||
|
[[configure-custom-module-exec]]
|
||||||
|
=== Custom Module with JVM Options
|
||||||
|
|
||||||
|
Using a custom Jetty module, you can customize the JVM startup options.
|
||||||
|
|
||||||
|
This is useful if you need to start Jetty and want to specify JVM options such as:
|
||||||
|
|
||||||
|
* `+-Xmx+`, to specify the max heap size
|
||||||
|
* `+-Xlog:gc+`, to specify the GC log file and options
|
||||||
|
* `+-javaagent+`, to specify Java agents
|
||||||
|
* `+-XX:+` options, for example to specify the GC implementation
|
||||||
|
* `+--enable-preview+`, to enable Java preview features
|
||||||
|
|
||||||
|
Start by creating `$JETTY_BASE/modules/jvm.mod`:
|
||||||
|
|
||||||
|
.jvm.mod
|
||||||
|
----
|
||||||
|
include::code:example$jetty-modules/jvm.mod[]
|
||||||
|
----
|
||||||
|
|
||||||
|
Enable it:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --add-modules=jvm
|
||||||
|
----
|
||||||
|
|
||||||
|
Since the module defines an `[exec]` section, it will fork _another_ JVM when Jetty is started.
|
||||||
|
|
||||||
|
This means that when you start Jetty, there will be _two_ JVMs running: one created by you when you run `java -jar $JETTY_HOME/start.jar`, and another forked by the Jetty start mechanism with the JVM options you specified (that cannot be applied to an already running JVM).
|
||||||
|
|
||||||
|
Again, you can <<configure-dry-run,display the JVM command line>> to verify that it is correct.
|
||||||
|
|
||||||
|
[TIP]
|
||||||
|
====
|
||||||
|
The second JVM forked by the Jetty start mechanism when one of the modules requires forking, for example a module that contains an `[exec]` section, may not be desirable, and may be avoided as explained in <<configure-dry-run,this section>>.
|
||||||
|
====
|
||||||
|
|
||||||
|
[[configure-display]]
|
||||||
|
=== Displaying the Configuration
|
||||||
|
|
||||||
|
Once you have enabled and configured the `$JETTY_BASE`, you can display the configuration to verify that it is correct.
|
||||||
|
|
||||||
|
Using the standard `server` and `http` Jetty modules, and the `postgresql` and `jvm` custom Jetty module defined above, you obtain:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --list-config
|
||||||
|
----
|
||||||
|
|
||||||
|
[jetty%nowrap]
|
||||||
|
....
|
||||||
|
[jetty]
|
||||||
|
setupModules=code:example$jetty-modules/jvm.mod,code:example$jetty-modules/postgresql.mod
|
||||||
|
setupArgs=--add-modules=server,http,postgresql,jvm
|
||||||
|
args=--list-config
|
||||||
|
....
|
||||||
|
|
||||||
|
Note how the configuration displayed above includes:
|
||||||
|
|
||||||
|
* In the list of enabled modules, the `postgresql` and `jvm` modules
|
||||||
|
* In the list of JVM arguments, those specified by the `jvm` module
|
||||||
|
* In the server class-path, the `+*.jar+` file specified by the `postgresql` module
|
||||||
|
|
||||||
|
[[configure-dry-run]]
|
||||||
|
=== Displaying the JVM Command Line
|
||||||
|
|
||||||
|
The Jetty start mechanism can display a full JVM command line that will start Jetty with the configuration you specified, with the `--dry-run` option:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --dry-run
|
||||||
|
----
|
||||||
|
|
||||||
|
The full JVM command line generated by `--dry-run` can be split in various parts that can be used individually, for example in scripts.
|
||||||
|
|
||||||
|
Furthermore, Jetty modules may specify the `--exec` option that will fork a second JVM to start Jetty, which may not be desirable.
|
||||||
|
Some option, such as `--jpms`, imply `--exec`, as it won't be possible to modify the module-path in the already started JVM.
|
||||||
|
|
||||||
|
To start Jetty without forking a second JVM, the `--dry-run` option can be used to generate a command line that is then executed so that starting Jetty only spawns one JVM.
|
||||||
|
|
||||||
|
IMPORTANT: You can use the `--dry-run` option as explained below to avoid forking a second JVM when using modules that have the `[exec]` section, or the `--exec` option, or when using the `--jpms` option.
|
||||||
|
|
||||||
|
For example, using the `--dry-run` option with the `jvm.mod` introduced in <<configure-custom-module-exec,this section>> produces the following command line:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --dry-run
|
||||||
|
----
|
||||||
|
|
||||||
|
[jetty%nowrap]
|
||||||
|
....
|
||||||
|
[jetty]
|
||||||
|
setupModules=code:example$jetty-modules/jvm.mod
|
||||||
|
setupArgs=--add-modules=http,jvm
|
||||||
|
args=--dry-run
|
||||||
|
replace=( ),$1\\\n
|
||||||
|
....
|
||||||
|
|
||||||
|
You can then run the generated command line.
|
||||||
|
|
||||||
|
For example, in the Linux `bash` shell you can run it by wrapping it into `$(\...)`:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ $(java -jar $JETTY_HOME/start.jar --dry-run)
|
||||||
|
----
|
||||||
|
|
||||||
|
The `--dry-run` option is quite flexible and below you can find a few examples of how to use it to avoid forking a second JVM, or generating scripts or creating an arguments file that can be passed to (a possibly alternative) `java` executable.
|
||||||
|
|
||||||
|
To display the `java` executable used to start Jetty:
|
||||||
|
|
||||||
|
[source,subs=quotes]
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --dry-run=##java##
|
||||||
|
----
|
||||||
|
|
||||||
|
[jetty%nowrap]
|
||||||
|
....
|
||||||
|
[jetty]
|
||||||
|
setupArgs=--add-modules=http
|
||||||
|
args=--dry-run=java
|
||||||
|
....
|
||||||
|
|
||||||
|
To display the JVM options:
|
||||||
|
|
||||||
|
[source,subs=quotes]
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --dry-run=##opts##
|
||||||
|
----
|
||||||
|
|
||||||
|
[jetty%nowrap]
|
||||||
|
....
|
||||||
|
[jetty]
|
||||||
|
setupModules=code:example$jetty-modules/jvm.mod
|
||||||
|
setupArgs=--add-modules=http,jvm
|
||||||
|
args=--dry-run=opts
|
||||||
|
replace=( ),$1\\\n
|
||||||
|
....
|
||||||
|
|
||||||
|
To display the JVM class-path:
|
||||||
|
|
||||||
|
[source,subs=quotes]
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --dry-run=##path##
|
||||||
|
----
|
||||||
|
|
||||||
|
[jetty%nowrap]
|
||||||
|
....
|
||||||
|
[jetty]
|
||||||
|
setupModules=code:example$jetty-modules/postgresql.mod
|
||||||
|
setupArgs=--add-modules=http,jvm
|
||||||
|
args=--dry-run=path
|
||||||
|
replace=( |:),$1\\\n
|
||||||
|
....
|
||||||
|
|
||||||
|
To display the JVM class-path and module-path, if you want to xref:start/start-jpms.adoc[start Jetty using JPMS] with the `--jpms` option:
|
||||||
|
|
||||||
|
[source,subs=quotes]
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar ##--jpms## --dry-run=##path##
|
||||||
|
----
|
||||||
|
|
||||||
|
[jetty%nowrap]
|
||||||
|
....
|
||||||
|
[jetty]
|
||||||
|
setupModules=code:example$jetty-modules/postgresql.mod
|
||||||
|
setupArgs=--add-modules=http,jvm
|
||||||
|
args=--jpms --dry-run=path
|
||||||
|
replace=( |:),$1\\\n
|
||||||
|
....
|
||||||
|
|
||||||
|
To display the JVM main class:
|
||||||
|
|
||||||
|
[source,subs=quotes]
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --dry-run=##main##
|
||||||
|
----
|
||||||
|
|
||||||
|
[jetty%nowrap]
|
||||||
|
....
|
||||||
|
[jetty]
|
||||||
|
setupArgs=--add-modules=http
|
||||||
|
args=--dry-run=main
|
||||||
|
....
|
||||||
|
|
||||||
|
To display the JVM main class when xref:start/start-jpms.adoc[starting Jetty using JPMS]:
|
||||||
|
|
||||||
|
[source,subs=quotes]
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --jpms --dry-run=##main##
|
||||||
|
----
|
||||||
|
|
||||||
|
[jetty%nowrap]
|
||||||
|
....
|
||||||
|
[jetty]
|
||||||
|
setupArgs=--add-modules=http
|
||||||
|
args=--jpms --dry-run=main
|
||||||
|
....
|
||||||
|
|
||||||
|
The main class is typically Jetty's `XmlConfiguration` class that accepts, as program arguments, a list of properties and a list of Jetty XML files to process.
|
||||||
|
The Jetty XML files compose together the Jetty components that are then configured with the values from the command line properties.
|
||||||
|
|
||||||
|
To display the program arguments passed to the main class:
|
||||||
|
|
||||||
|
[source,subs=quotes]
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --dry-run=##args##
|
||||||
|
----
|
||||||
|
|
||||||
|
[jetty%nowrap]
|
||||||
|
....
|
||||||
|
[jetty]
|
||||||
|
setupModules=code:example$jetty-modules/postgresql.mod
|
||||||
|
setupArgs=--add-modules=http
|
||||||
|
args=--dry-run=args
|
||||||
|
replace=( ),$1\\\n
|
||||||
|
....
|
||||||
|
|
||||||
|
Note how the program arguments are a list of properties in the form `<name>=<value>` and a list of Jetty XML files.
|
||||||
|
|
||||||
|
The various parts of the full JVM command line can be combined to leverage the arguments file feature (that is, specify the JVM options in a file rather than on the command line) that is built-in in the `java` executable:
|
||||||
|
|
||||||
|
[source,subs=quotes]
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --dry-run=##opts,path,main,args## > /tmp/jvm_cmd_line.txt
|
||||||
|
$ /some/other/java @/tmp/jvm_cmd_line.txt
|
||||||
|
----
|
||||||
|
|
||||||
|
Using `--dry-run=opts,path,main,args` can be used to avoid that the Jetty start mechanism forks a second JVM when using modules that require forking:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java $(java -jar $JETTY_HOME/start.jar --dry-run=opts,path,main,args)
|
||||||
|
----
|
||||||
|
|
||||||
|
The output of different `--dry-run` executions can be creatively combined in a shell script:
|
||||||
|
|
||||||
|
[source,subs=quotes]
|
||||||
|
----
|
||||||
|
$ OPTS=$(java -jar start.jar --dry-run=##opts,path##)
|
||||||
|
$ MAIN=$(java -jar start.jar --dry-run=##main##)
|
||||||
|
$ ARGS=$(java -jar start.jar --dry-run=##args##)
|
||||||
|
$ java $OPTS -Dextra=opt $MAIN $ARGS extraProp=value extra.xml
|
||||||
|
----
|
||||||
|
|
||||||
|
[[start]]
|
||||||
|
== Starting Jetty
|
||||||
|
|
||||||
|
After you have configured the `$JETTY_BASE` directory, as explained in <<configure,this section>>, you can start Jetty as a standalone server.
|
||||||
|
|
||||||
|
In the _start_ mode, the Jetty start mechanism computes a JVM command line with JVM options, system properties, class-path, module-path, main class and program arguments, and then executes it, forking a new JVM if necessary.
|
||||||
|
|
||||||
|
The Jetty start mechanism performs these steps:
|
||||||
|
|
||||||
|
. Loads all the Jetty modules files (that have extension `+*.mod+`) from the `modules/` subdirectory of each configuration source directory (see <<configure,this section>> for the list of configuration sources).
|
||||||
|
In this way, a Jetty module graph can be built in memory, where the module dependencies form the edges of the graph and each node contains the metadata information declared by each module (for example, the libraries that it needs, the XML files to process, and so on), in preparation for the next step.
|
||||||
|
. Reads the Jetty module configuration files (that have extension `+*.ini+`) from the `start.d/` subdirectory of each configuration source directory and from the command line.
|
||||||
|
This step produces a list of _enabled_ modules; for each enabled module all its dependencies are transitively resolved by navigating the graph built in the previous steps.
|
||||||
|
. Processes the list of enabled (explicitly and transitively) modules, gathering the list of libraries to add to the class-path, the JPMS directives to add to the command line, the properties and XML files to add as program arguments, etc., so that a full JVM command line can be generated.
|
||||||
|
. Executes the command line, either in-JVM or by forking a second JVM (if the `--exec` option is present or implied by other options such as `--jpms`), and waits for the JVM, or the forked JVM, to exit.
|
||||||
|
|
||||||
|
[[start-class-path]]
|
||||||
|
=== Server Class-Path
|
||||||
|
|
||||||
|
When the Jetty server is started in-JVM, the server class-path gathered by processing the enabled modules is organized in a `URLClassLoader`, the Jetty Start ClassLoader, that is a child of the System ClassLoader:
|
||||||
|
|
||||||
|
[plantuml]
|
||||||
|
----
|
||||||
|
skinparam backgroundColor transparent
|
||||||
|
skinparam monochrome true
|
||||||
|
skinparam shadowing false
|
||||||
|
skinparam roundCorner 10
|
||||||
|
|
||||||
|
rectangle "Jetty Start JVM" {
|
||||||
|
rectangle "System ClassLoader" as system
|
||||||
|
rectangle "Jetty Start ClassLoader" as start
|
||||||
|
|
||||||
|
note right of system: start.jar
|
||||||
|
note right of start: jetty-server.jar\njetty-http.jar\njetty-io.jar\njetty-util.jar\njetty-xml.jar\netc.
|
||||||
|
|
||||||
|
system <-- start
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|
||||||
|
The System ClassLoader only has `$JETTY_HOME/start.jar` in its class-path, since the JVM was started with `java -jar $JETTY_HOME/start.jar`.
|
||||||
|
The Jetty Start ClassLoader has in its class-path the `+*.jar+` files gathered by processing the enabled modules, typically from `+$JETTY_HOME/lib/jetty-*.jar+`, but possibly also from `+$JETTY_BASE/lib/*.jar+` if custom modules extend the server class-path with their own `+*.jar+` files.
|
||||||
|
|
||||||
|
When the Jetty server is started in a forked JVM, there will be two JVMs: one started by you with `java -jar $JETTY_HOME/start.jar` and one forked by the Jetty start mechanism.
|
||||||
|
In the forked JVM, the System ClassLoader has the server class-path and/or module-path in its class-path, since the forked JVM is started with `+java --class-path $JETTY_HOME/lib/jetty-server-<version>.jar:...+`:
|
||||||
|
|
||||||
|
[plantuml]
|
||||||
|
----
|
||||||
|
skinparam backgroundColor transparent
|
||||||
|
skinparam monochrome true
|
||||||
|
skinparam shadowing false
|
||||||
|
skinparam roundCorner 10
|
||||||
|
|
||||||
|
rectangle "Jetty Start JVM" as startJVM {
|
||||||
|
rectangle "System ClassLoader" as system1
|
||||||
|
note right of system1: start.jar
|
||||||
|
}
|
||||||
|
|
||||||
|
rectangle "Forked JVM" as forkedJVM {
|
||||||
|
rectangle "System ClassLoader" as system2
|
||||||
|
|
||||||
|
note right of system2: jetty-server.jar\njetty-http.jar\njetty-io.jar\njetty-util.jar\njetty-xml.jar\netc.
|
||||||
|
}
|
||||||
|
|
||||||
|
startJVM --> forkedJVM: " waits for"
|
||||||
|
----
|
||||||
|
|
||||||
|
It is worth mentioning that there are two standard Jetty modules that allow you to easily add entries to the Jetty server class-path:
|
||||||
|
|
||||||
|
* The `resources` module, that adds the `$JETTY_BASE/resources/` directory to the server class-path.
|
||||||
|
This is useful if you have third party libraries that lookup resources from the class-path: just put those resources in the `$JETTY_BASE/resources/` directory. +
|
||||||
|
Logging libraries often perform class-path lookup of their configuration files (for example, `log4j.properties`, `log4j.xml`, `logging.properties`, and `logback.xml`), so `$JETTY_BASE/resources/` is the ideal place to add those files. +
|
||||||
|
* The the `ext` module, that adds all the `+*.jar+` files under the `$JETTY_BASE/lib/ext/` directory, and subdirectories recursively, to the server class-path. +
|
||||||
|
+
|
||||||
|
[CAUTION]
|
||||||
|
====
|
||||||
|
On one hand, the `ext` module provides a handy place to put third party libraries and their dependencies; on the other hand, the `$JETTY_BASE/lib/ext/` directory may become a confused mixture of many `+*.jar+` files from different third party libraries.
|
||||||
|
|
||||||
|
Prefer to group third party libraries and their dependencies into their own directories using xref:modules/custom.adoc[custom modules], or at least group them into `$JETTY_BASE/lib/ext/` subdirectories such as `$JETTY_BASE/lib/ext/util/` or `$JETTY_BASE/lib/ext/acme/`.
|
||||||
|
====
|
||||||
|
|
||||||
|
[[start-xml]]
|
||||||
|
=== Assembling Jetty Components
|
||||||
|
|
||||||
|
The Jetty start mechanism eventually invokes, by default, main class `org.eclipse.jetty.xml.XmlConfiguration`, passing properties and xref:xml/index.adoc[Jetty XML files] as program arguments.
|
||||||
|
|
||||||
|
The Jetty XML files are nothing more than Java code in XML format.
|
||||||
|
|
||||||
|
The XML files are processed to instantiate Jetty components such as `org.eclipse.jetty.server.Server` or `org.eclipse.jetty.util.ssl.SslContextFactory`.
|
||||||
|
The components are then assembled together to provide the configured Jetty features.
|
||||||
|
|
||||||
|
The Jetty XML files are parametrized using properties; a property is just a name/value pair.
|
||||||
|
|
||||||
|
This parametrization of the XML files allows an XML file that resides in `$JETTY_HOME/etc/` to _declare_ a property such as `jetty.http.port`, and allow this property to be set in a `$JETTY_BASE/start.d/http.ini` file, so that you don't need to change the XML files in `$JETTY_HOME`, but only change files in your `$JETTY_BASE`.
|
||||||
|
|
||||||
|
You can write your own xref:modules/custom.adoc[custom modules] with your own Jetty XML files, and your own properties, to further customize Jetty.
|
||||||
|
|
||||||
|
[[stop]]
|
||||||
|
== Stopping Jetty
|
||||||
|
|
||||||
|
When Jetty is started, the Jetty components that you have configured by enabling Jetty modules are assembled and started.
|
||||||
|
|
||||||
|
If you have started Jetty from a terminal, you can exit the Jetty JVM by hitting kbd:[Ctrl+C] on the same terminal.
|
||||||
|
|
||||||
|
Similarly, from a different terminal, you can exit the Jetty JVM using `kill -INT <pid>` or `kill -TERM <pid>`.
|
||||||
|
|
||||||
|
In the three cases above, the JVM is exited, but by default Jetty components are not stopped.
|
||||||
|
If you want to stop the Jetty components, to stop Jetty more gracefully, you can start Jetty with this property:
|
||||||
|
|
||||||
|
[source,subs=quotes]
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar ##jetty.server.stopAtShutdown=true##
|
||||||
|
----
|
||||||
|
|
||||||
|
This property can also be set in `$JETTY_BASE/start.d/server.ini` so that it is persistently configured across Jetty restarts (see also xref:modules/standard.adoc#server[the `server` module]).
|
||||||
|
|
||||||
|
The `jetty.server.stopAtShutdown` property configures a JVM shutdown hook that is run, stopping the `Server` instance, when the JVM exits.
|
||||||
|
|
||||||
|
Obviously, the JVM can also be stopped with `kill -KILL <pid>` that exits the process abruptly without running the JVM shutdown hooks.
|
||||||
|
|
||||||
|
[[stop-remote]]
|
||||||
|
=== Stopping Jetty from Remote
|
||||||
|
|
||||||
|
You can configure a Jetty server so that it can be stopped by remote clients using a command sent through a TCP socket.
|
||||||
|
|
||||||
|
You can start Jetty with the following properties:
|
||||||
|
|
||||||
|
* `stop.host`, the host name Jetty will bind to listen for stop commands. Defaults to `127.0.0.1` which means that the stop command can be issued only clients that run on the same host as Jetty.
|
||||||
|
* `stop.port`, the port number Jetty will listen to for stop commands. Defaults to `-1`, which means that Jetty will not listen to any port.
|
||||||
|
* `stop.key`, the password to verify when a stop command is received. Defaults to a password that is randomly generated and printed when Jetty starts.
|
||||||
|
|
||||||
|
[source,subs=quotes]
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar ##stop.port=8181##
|
||||||
|
----
|
||||||
|
|
||||||
|
[jetty%nowrap]
|
||||||
|
....
|
||||||
|
[jetty]
|
||||||
|
setupArgs=--add-modules=http
|
||||||
|
args=stop.port=8181
|
||||||
|
highlight=(?i)stop.key
|
||||||
|
....
|
||||||
|
|
||||||
|
In the example above, Jetty is started with just the `stop.port` property, and the `stop.key` is printed on the terminal when Jetty starts.
|
||||||
|
|
||||||
|
CAUTION: You can choose your own `stop.key`, but make sure it's a strong password.
|
||||||
|
|
||||||
|
A remote client can now use the Jetty start mechanism to stop the remote Jetty server:
|
||||||
|
|
||||||
|
[source,subs=normal]
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar ##--stop## stop.port=8181 stop.key=<stop.key>
|
||||||
|
----
|
||||||
|
|
||||||
|
Note the `--stop` command along with the `stop.port` and `stop.key` properties.
|
||||||
|
The `stop.key` must be the same as the one of remote Jetty server, either the one you chose, or the one printed on the terminal when Jetty starts.
|
||||||
|
|
||||||
|
Remote clients can wait for the remote Jetty server to shut down by specifying the `stop.wait` property with the number of seconds to wait:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --stop stop.port=8181 stop.key=<stop.key> stop.wait=15
|
||||||
|
----
|
||||||
|
|
||||||
|
If the time specified elapses, without the confirmation that the remote Jetty server stopped, then the `--stop` command exits with a non-zero return code.
|
||||||
|
|
||||||
|
== Start Mechanism Logging
|
||||||
|
|
||||||
|
The steps performed by the Jetty start mechanism are logged by the `StartLog` class, that outputs directly, by default, to `System.err`.
|
||||||
|
|
||||||
|
This is necessary to avoid that the Jetty start mechanism depend on logging libraries that may clash with those defined by Jetty logging modules, when Jetty is started in-VM.
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
This section is about the logging performed by the Jetty start mechanism _before_ it configures and starts Jetty.
|
||||||
|
See the xref:server/index.adoc#logging[logging section] for information about logging when Jetty starts.
|
||||||
|
====
|
||||||
|
|
||||||
|
You can enable DEBUG level logging with the `--debug` command line option, for both the _tool_ and _start_ modes:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --debug ...
|
||||||
|
----
|
||||||
|
|
||||||
|
You can send the start log output to a file, by default relative to `$JETTY_BASE`, with the `--start-log-file=<file>` option:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --debug --start-log-file=start.log ...
|
||||||
|
----
|
||||||
|
|
||||||
|
This is useful for capturing startup issues where the Jetty-specific logger has not yet kicked in due to a possible startup configuration error.
|
||||||
|
|
||||||
|
[[reference]]
|
||||||
|
== Usage Reference
|
||||||
|
|
||||||
|
[jetty]
|
||||||
|
....
|
||||||
|
[jetty]
|
||||||
|
args=--help
|
||||||
|
....
|
|
@ -0,0 +1,94 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Starting Jetty using JPMS
|
||||||
|
|
||||||
|
Jetty modules are proper https://en.wikipedia.org/wiki/Java_Platform_Module_System[JPMS] modules: each Jetty module has a `module-info.class` file.
|
||||||
|
This makes possible to run Jetty from the module-path, rather than the class-path.
|
||||||
|
|
||||||
|
To start Jetty on the module-path rather than the class-path, it is enough to add the `--jpms` option to the command line, for example:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --jpms
|
||||||
|
----
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
The `--jpms` option implies the `--exec` option.
|
||||||
|
|
||||||
|
When running on the module-path using the `--jpms` option, the Jetty start mechanism will fork a second JVM passing it the right JVM options to run on the module-path.
|
||||||
|
|
||||||
|
Therefore, you will have two JVMs running: one that runs `start.jar` and one that runs Jetty on the module-path.
|
||||||
|
====
|
||||||
|
|
||||||
|
When Jetty is started in JPMS mode, all JPMS modules in the module-path are added to the set of JPMS _root modules_ through the JVM option `--add-modules ALL_MODULE_PATH`.
|
||||||
|
|
||||||
|
For a `+*.jar+` file that is not a JPMS module, but is on the module-path, the JVM will assume internally it is an automatic JPMS module, with a JPMS module name derived from the `+*.jar+` file name.
|
||||||
|
|
||||||
|
Rather than adding the `--jpms` option to the command line, you can use a custom Jetty module to centralize your JPMS configuration, where you can specify additional JPMS directives.
|
||||||
|
|
||||||
|
Create the `$JETTY_BASE/modules/jpms.mod` file:
|
||||||
|
|
||||||
|
.jpms.mod
|
||||||
|
----
|
||||||
|
include::code:example$jetty-modules/jpms.mod[]
|
||||||
|
----
|
||||||
|
|
||||||
|
The `[ini]` section with `--jpms` is equivalent to passing the `--jpms` option to the command line (see also xref:modules/index.adoc#directive-ini[this section]).
|
||||||
|
|
||||||
|
The `[jpms]` section allows you to specify additional JPMS configuration, for example additional `--add-modules` options, or `--add-opens` options, etc. (see also xref:modules/index.adoc#directive-jpms[this section]).
|
||||||
|
|
||||||
|
Then enable it:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --add-modules=jpms
|
||||||
|
----
|
||||||
|
|
||||||
|
Now you can start Jetty without extra command line options, and it will start in JPMS mode because you have enabled the `jpms` module.
|
||||||
|
|
||||||
|
[[advanced]]
|
||||||
|
== Advanced JPMS Configuration
|
||||||
|
|
||||||
|
Web applications may need additional services from the Servlet Container, such as JDBC `DataSource` references or JTA `UserTransaction` references.
|
||||||
|
|
||||||
|
For example, for JDBC it is typical to store, in JNDI, a reference to the connection pool's `DataSource` or directly a reference to the JDBC driver's `DataSource` (for example, `org.postgresql.ds.PGConnectionPoolDataSource`).
|
||||||
|
Jetty needs to be able to instantiate those classes and therefore needs to be able to load those classes and all their super-classes, among which includes `javax.sql.DataSource`.
|
||||||
|
|
||||||
|
When Jetty runs on the class-path, this is easily achieved by using a xref:modules/custom.adoc[custom module] as explained in xref:start/index.adoc#configure-custom-module[this section].
|
||||||
|
|
||||||
|
However, when running on the module-path, things are quite different.
|
||||||
|
|
||||||
|
When Jetty tries to load, for example, class `org.postgresql.ds.PGConnectionPoolDataSource`, it must be in a JPMS module that is resolved in the run-time module graph.
|
||||||
|
Furthermore, any dependency, for example classes from the `java.sql` JPMS module, must also be in a module present in the resolved module graph.
|
||||||
|
|
||||||
|
Thanks to the fact that when Jetty starts in JPMS mode the `--add-modules ALL_MODULE_PATH` option is added to the JVM command line, every `+*.jar+` file in the module-path is also present in the module graph.
|
||||||
|
|
||||||
|
There are now two cases for the `postgresql-<version>.jar` file: either it is a proper JPMS module, or it is an automatic JPMS module (either an explicit automatic JPMS module with the `Automatic-Module-Name` attribute in the manifest, or an implicit automatic JPMS module whose name is derived from the `+*.jar+` file name).
|
||||||
|
|
||||||
|
If the `postgresql-<version>.jar` file is a proper JPMS module, then there is nothing more that you should do: the `postgresql-<version>.jar` file is in the module-path, and all the modules in the module-path are in the module graph, and any dependency declared in the `module-info.class` will be added to the module graph.
|
||||||
|
|
||||||
|
Otherwise, `postgresql-<version>.jar` file is an automatic module, and will likely have a dependency on the JDK-bundled `java.sql` JPMS module.
|
||||||
|
However, the `java.sql` JPMS module is not in the module graph, because automatic modules do not have a way to declare their dependencies.
|
||||||
|
|
||||||
|
For this reason, you have to manually add the `java.sql` dependency to the module graph.
|
||||||
|
Using the `postgresql.mod` introduced in xref:start/index.adoc#configure-custom-module[this section] as an example, modify your custom module in the following way:
|
||||||
|
|
||||||
|
.postgresql.mod
|
||||||
|
----
|
||||||
|
...
|
||||||
|
|
||||||
|
[jpms]
|
||||||
|
add-modules: java.sql
|
||||||
|
----
|
||||||
|
|
||||||
|
The `[jpms]` section is only used when Jetty is started on the module-path.
|
|
@ -0,0 +1,232 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Troubleshooting
|
||||||
|
|
||||||
|
To troubleshoot Jetty when used as a production server, there are two main tools: the Jetty Server Dump and enabling DEBUG level logging.
|
||||||
|
|
||||||
|
Jetty is based on components organized as a tree, with the `Server` instance at the root of the tree.
|
||||||
|
|
||||||
|
As explained in the xref:jmx/index.adoc[JMX section], these components can be exported as JMX MBeans and therefore be accessible from JMX Consoles such as Java Missions Control (JMC).
|
||||||
|
|
||||||
|
Being able to take a snapshot of the state of Jetty while it is running is the most useful information that can be attached when reporting an issue.
|
||||||
|
Such state includes:
|
||||||
|
|
||||||
|
* The thread pool configuration and its current state, including how many threads are in use, and their stack trace.
|
||||||
|
* The TLS configuration.
|
||||||
|
* The I/O configuration and its current state, including the ports Jetty listens to, how many connections are currently open, and he state of each connection, and the state of the request/response handling for each connection.
|
||||||
|
* The `Handler` structure and its configuration.
|
||||||
|
* The web applications deployed and their configurations, including the class loader information.
|
||||||
|
|
||||||
|
The prerequisite for troubleshooting is to enable JMX, so that Jetty -- possibly a production server -- can be accessed from a remote location to obtain the information exported via JMX, and possibly be able to reconfigure Jetty to solve the issue.
|
||||||
|
|
||||||
|
IMPORTANT: Make sure you read about how to secure the access to Jetty when using xref:jmx/index.adoc#remote[remote JMX].
|
||||||
|
|
||||||
|
[[dump]]
|
||||||
|
== Server Dump
|
||||||
|
|
||||||
|
The Jetty Server Dump is obtained by invoking, via JMX, the `Server.dump()` operation, as shown below.
|
||||||
|
|
||||||
|
image::jmc-server-dump.png[]
|
||||||
|
|
||||||
|
Find the `Server` MBean in the MBean Tree, under `org.eclipse.jetty.server:type=server,id=0`.
|
||||||
|
Then click on the "Operations" tab, select the `dump()` operation, and then click the `Execute` button.
|
||||||
|
In the bottom panel you will see the result of the invocation, that you can copy into a text editor and save to your file system.
|
||||||
|
|
||||||
|
[CAUTION]
|
||||||
|
====
|
||||||
|
Taking a Jetty Server Dump is a relatively expensive operation, as it dumps the state of all connections (which can be thousands), and the state of all threads.
|
||||||
|
|
||||||
|
The result of the invocation may produce a large string, possibly few MiB, that may impact the server memory usage.
|
||||||
|
|
||||||
|
Furthermore, dumping the state of the I/O Jetty components takes a little CPU time off the handling of the actual I/O, possibly slowing it down temporarily.
|
||||||
|
|
||||||
|
While the slow-down caused by taking the Jetty Server Dump may be noticeable on highly loaded systems, it is typically a very small price to pay to obtain the information about the Jetty state that may be critical to the resolution of an issue.
|
||||||
|
====
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
The format of the Jetty Server Dump output is subject to change at any time, as Jetty developers modify the Jetty code and decide to include more state, or remove state that is no longer relevant.
|
||||||
|
|
||||||
|
The Jetty Server Dump is organized in a tree whose structure is similar to the runtime Jetty component tree.
|
||||||
|
|
||||||
|
At the end of the dump output there is a legend that explains the type of tree node: whether it is a node that represent a _managed_ component, or an _array_ node (or a _map_ node) that represent some component state, etc.
|
||||||
|
====
|
||||||
|
|
||||||
|
[[dump-start-stop]]
|
||||||
|
=== Dump at Server Start/Stop
|
||||||
|
|
||||||
|
The `Server.dump()` operation may also be invoked just after the `Server` starts (to log the state of the freshly started server), and just before the `Server` stops (which may be useful to log the state of server that is not working properly).
|
||||||
|
|
||||||
|
You can temporarily enable the Jetty Server Dump at start time by overriding the `jetty.server.dumpAfterStart` property on the command line:
|
||||||
|
|
||||||
|
[source,subs=quotes]
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar *jetty.server.dumpAfterStart=true*
|
||||||
|
----
|
||||||
|
|
||||||
|
To make this change persistent across server restarts, see the xref:modules/standard.adoc#server[`server` module] configuration for more information about how to configure the server to dump at start/stop time.
|
||||||
|
|
||||||
|
[[dump-detailed]]
|
||||||
|
=== Detailed ThreadPool Information
|
||||||
|
|
||||||
|
By default, the dump of the thread pool will only dump the topmost stack frame of each thread.
|
||||||
|
It is possible to configure the thread pool to dump the whole stack trace for each thread; while this may be a little more expensive, it provides complete information about the state of each thread, which may be important to diagnose the issue.
|
||||||
|
|
||||||
|
See the xref:modules/standard.adoc#threadpool[`threadpool` module] configuration for more information about how to configure the thread pool to dump detailed thread information.
|
||||||
|
|
||||||
|
Detailed thread pool information can also be turned on/off on-the-fly via JMX, by finding the `ThreadPool` MBean under `org.eclipse.jetty.util.thread:type=queuedthreadpool,id=0`, then selecting the `detailedDump` attribute and setting it to `true`. You can now perform the `Server.dump()` operation as explained above, and then set `detailedDump` back to `false`.
|
||||||
|
|
||||||
|
[[dump-example]]
|
||||||
|
=== Dump Example
|
||||||
|
|
||||||
|
Below you can find a simple example of a Jetty Server Dump, with annotations for the principal components:
|
||||||
|
|
||||||
|
[jetty.small%nowrap]
|
||||||
|
....
|
||||||
|
[jetty]
|
||||||
|
setupArgs=--add-modules=http
|
||||||
|
args=jetty.http.selectors=1 jetty.http.acceptors=1 jetty.threadPool.minThreads=4 jetty.server.dumpAfterStart=true
|
||||||
|
delete=^[0-9]\{4}
|
||||||
|
callouts= <$N>,Server@,= QueuedThreadPool,HandlerList@,= ServerConnector,ManagedSelector@,keys @,startJarLoader@,unmanaged
|
||||||
|
....
|
||||||
|
ifdef::run-jetty[]
|
||||||
|
<1> The `Server` instance at the root of the tree
|
||||||
|
<2> The thread pool component
|
||||||
|
<3> The root of the `Handler` structure
|
||||||
|
<4> The connector listening on port `8080` for the HTTP/1.1 protocol
|
||||||
|
<5> A selector component that manages connections
|
||||||
|
<6> The connections currently managed by the selector component
|
||||||
|
<7> The server `ClassLoader` and its classpath
|
||||||
|
<8> The legend for the dump nodes
|
||||||
|
endif::[]
|
||||||
|
|
||||||
|
[[logging]]
|
||||||
|
== Enabling DEBUG Logging
|
||||||
|
|
||||||
|
Enabling DEBUG level logging for the `org.eclipse.jetty` logger name provides the maximum amount of information to troubleshoot Jetty issues.
|
||||||
|
|
||||||
|
Refer to the xref:server/index.adoc#logging[logging section] for more information about how to configure logging in Jetty.
|
||||||
|
|
||||||
|
[CAUTION]
|
||||||
|
====
|
||||||
|
Enabling DEBUG level logging for `org.eclipse.jetty` is very, *very* expensive.
|
||||||
|
|
||||||
|
Your server could be slowed down to almost a halt, especially if it is under heavy load.
|
||||||
|
Furthermore, the log file could quickly fill up the entire filesystem (unless configured to roll over), so you want to be really careful using DEBUG logging.
|
||||||
|
|
||||||
|
For production servers, consider using the <<dump,Jetty Server Dump>> first, and enable DEBUG logging only as a last resort.
|
||||||
|
====
|
||||||
|
|
||||||
|
However, sometimes issues are such that only DEBUG logging can really tell what's going on in the system, and enabling DEBUG logging is your best chance to figure the issue out.
|
||||||
|
Below you can find few suggestions that can help you reduce the impact when you have to enable DEBUG logging.
|
||||||
|
|
||||||
|
[[logging-backend]]
|
||||||
|
=== Jetty Behind a Load Balancer
|
||||||
|
|
||||||
|
If Jetty instances are behind a load balancer, you may configure the load balancer to send less load to a particular Jetty instance, and enable DEBUG logging in that instance only.
|
||||||
|
|
||||||
|
[[logging-jmx]]
|
||||||
|
=== Enabling DEBUG Logging for a Short Time
|
||||||
|
|
||||||
|
In certain cases the issue can be reproduced reliably, but only in the production environment.
|
||||||
|
|
||||||
|
You can use JMX to temporarily enable DEBUG logging, reproduce the issue, and then disable DEBUG logging.
|
||||||
|
|
||||||
|
Alternatively, if you cannot reliably reproduce the issue, but you _know_ it is happening, you can temporarily enable DEBUG logging for a small period of time, let's say 10-60 seconds, and then disable DEBUG logging.
|
||||||
|
|
||||||
|
Changing the log level at runtime is a feature of the logging implementation that you are using.
|
||||||
|
|
||||||
|
The Jetty SLF4J implementation, used by default, exposes via JMX method `boolean JettyLoggerFactoryMBean.setLoggerLevel(String loggerName, String levelName)` that you can invoke via a JMX console to change the level for the specified logger name.
|
||||||
|
The method returns `true` if the logger level was successfully changed.
|
||||||
|
|
||||||
|
For example, you can pass the string `org.eclipse.jetty` as the first parameter, and the string `DEBUG` (upper case) as the second parameter.
|
||||||
|
You can then use the string `INFO` or `WARN` (upper case) to restore the logging level to its previous value.
|
||||||
|
|
||||||
|
[[logging-subpackages]]
|
||||||
|
=== Enabling DEBUG Logging for SubPackages
|
||||||
|
|
||||||
|
Enabling DEBUG logging for the `org.eclipse.jetty` logger name implies that all children logger names, recursively, inherit the DEBUG level.
|
||||||
|
|
||||||
|
Processing a single HTTP request involves many Jetty components: the I/O subsystem (under `org.eclipse.jetty.io`), the thread pool (under `org.eclipse.jetty.util`), the HTTP/1.1 parsing (under `org.eclipse.jetty.http`), etc.
|
||||||
|
|
||||||
|
If you can cut the amount of DEBUG logging to just what you need to troubleshoot the issue, the impact of enabling DEBUG logging will be much less than enabling it for all Jetty components.
|
||||||
|
|
||||||
|
For example, if you need to troubleshoot a client that sends bad HTTP/1.1 requests, it may be enough to enable only the `org.eclipse.jetty.http` logger name, therefore saving the large amount of DEBUG logging produced by the I/O subsystem and by the thread pool.
|
||||||
|
|
||||||
|
In another case, you may need to troubleshoot only HTTP/2 requests, and therefore enabling only the `org.eclipse.jetty.http2` logger name could be enough.
|
||||||
|
|
||||||
|
[[debugging]]
|
||||||
|
== Remote Debugging
|
||||||
|
|
||||||
|
The Java Virtual Machines allows remote processes on different hosts to connect for debugging purposes, by using specific command line options.
|
||||||
|
|
||||||
|
[CAUTION]
|
||||||
|
====
|
||||||
|
While it is possible to enable remote debugging on a Jetty server, it is typically not recommended for security and performance reasons.
|
||||||
|
Only enable remote debugging on a Jetty server as a last resort to troubleshoot issues that could not be troubleshot otherwise.
|
||||||
|
====
|
||||||
|
|
||||||
|
You can easily create a custom Jetty module (see xref:modules/custom.adoc[this section]) with the following content:
|
||||||
|
|
||||||
|
.remote-debug.mod
|
||||||
|
----
|
||||||
|
include::code:example$jetty-modules/remote-debug.mod[]
|
||||||
|
----
|
||||||
|
|
||||||
|
The `[exec]` directive (documented xref:modules/index.adoc#directive-exec[here]) is necessary to pass the `-agentlib:jdwp` JVM option to the forked JVM that runs Jetty, so that you can attach with a debugger.
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
The `address` parameter of the `-agentlib:jdwp` command line option specifies the network address and port the Jetty JVM listens on for remote debugging.
|
||||||
|
|
||||||
|
Please refer to the https://docs.oracle.com/en/java/javase/17/docs/specs/jpda/conninv.html[Java Debug Wire Protocol documentation] for additional information about the `-agentlib:jdwp` command line option and its parameters.
|
||||||
|
====
|
||||||
|
|
||||||
|
You can now enable the `remote-debug` Jetty module with the following command issued from the `$JETTY_BASE` directory:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ java -jar $JETTY_HOME/start.jar --add-modules=server,remote-debug
|
||||||
|
----
|
||||||
|
|
||||||
|
The command above minimally adds a Jetty server without connectors (via the `server` Jetty module) and the `remote-debug` Jetty module, and produces the following `$JETTY_BASE` directory structure:
|
||||||
|
|
||||||
|
[source,subs=normal]
|
||||||
|
----
|
||||||
|
$JETTY_BASE
|
||||||
|
├── modules
|
||||||
|
│ └── remote-debug.mod
|
||||||
|
├── resources
|
||||||
|
│ └── jetty-logging.properties
|
||||||
|
└── start.d
|
||||||
|
├── ##remote-debug.ini##
|
||||||
|
└── server.ini
|
||||||
|
----
|
||||||
|
|
||||||
|
You can easily disable the `remote-debug` Jetty module as explained in xref:start/index.adoc#configure-disable[this section].
|
||||||
|
|
||||||
|
Alternatively, you can enable the `remote-debug` module on the command line, as explained in xref:start/index.adoc#configure-enable-command-line[this section].
|
||||||
|
|
||||||
|
Starting the Jetty server with the `remote-debug` module enabled yields:
|
||||||
|
|
||||||
|
[jetty%nowrap]
|
||||||
|
....
|
||||||
|
[jetty]
|
||||||
|
setupModules=code:example$jetty-modules/remote-debug.mod
|
||||||
|
setupArgs=--add-modules=server,remote-debug
|
||||||
|
highlight=5005
|
||||||
|
....
|
||||||
|
|
||||||
|
Note how the JVM is listening on port `5005` to allow remote debuggers to connect.
|
||||||
|
|
||||||
|
If you want to avoid to fork a second JVM to pass the `-agentlib:jdwp` JVM option, please read xref:start/index.adoc#configure-dry-run[this section].
|
|
@ -0,0 +1,518 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Jetty XML
|
||||||
|
|
||||||
|
// TODO: merge this small section into the syntax and maybe call it "Jetty XML Reference".
|
||||||
|
|
||||||
|
The Jetty XML format is a straightforward mapping of XML elements to Java APIs so that any object can be instantiated and getters, setters, and methods can be called.
|
||||||
|
|
||||||
|
The Jetty XML format is very similar to that of frameworks like Spring or Plexus, although it predates all of them and it's typically more powerful as it can invoke any Java API.
|
||||||
|
|
||||||
|
The Jetty XML format is used in xref:modules/index.adoc[Jetty modules] to create the Jetty server components, as well as in xref:deploy/index.adoc[Jetty XML context files] to configure web applications, but it can be used to call any Java API.
|
||||||
|
|
||||||
|
[[syntax]]
|
||||||
|
== Jetty XML Syntax
|
||||||
|
|
||||||
|
The Jetty XML syntax defines XML element that allow you to call any Java API and that allow you to interact in a simpler way with the xref:modules/index.adoc[Jetty module system] and the xref:deploy/index.adoc[Jetty deploy system].
|
||||||
|
|
||||||
|
The Jetty XML elements define attributes such as `id`, `name`, `class`, etc. that may be replaced by correspondent elements, so that these XML documents are equivalent:
|
||||||
|
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure>
|
||||||
|
<Get id="stderr" class="java.lang.System" name="err">
|
||||||
|
<Call name="println" arg="HELLO" />
|
||||||
|
</Get>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure>
|
||||||
|
<Get>
|
||||||
|
<Id>stderr</Id>
|
||||||
|
<Name>err</Name>
|
||||||
|
<Class>java.lang.System</Class>
|
||||||
|
<Call>
|
||||||
|
<Name>println</Name>
|
||||||
|
<Arg>HELLO</Arg>
|
||||||
|
</Call>
|
||||||
|
</Get>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
The version using attributes is typically shorter and nicer to read, but sometimes the attribute value cannot be a literal string (for example, it could be the value of a system property) and that's where elements gives you the required flexibility.
|
||||||
|
|
||||||
|
[[syntax-configure]]
|
||||||
|
=== `<Configure>`
|
||||||
|
|
||||||
|
Element `Configure` must be the root element of the XML document.
|
||||||
|
|
||||||
|
The following Jetty XML creates an empty `String` and assigns it the id `mystring`:
|
||||||
|
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure id="mystring" class="java.lang.String" />
|
||||||
|
----
|
||||||
|
|
||||||
|
This is equivalent to the following Java code:
|
||||||
|
|
||||||
|
[,java]
|
||||||
|
----
|
||||||
|
var mystring = new String();
|
||||||
|
----
|
||||||
|
|
||||||
|
If an object with the id `mystring` already exists, then it is not created again but rather just referenced.
|
||||||
|
|
||||||
|
Within element `<Configure>`, the created object (if any) is in <<syntax-scope,scope>> and may be the implicit target of other, nested, elements.
|
||||||
|
|
||||||
|
Typically the `<Configure>` element is used to configure a `Server` instance or `ContextHandler` subclasses such as `WebAppContext` that represent web applications.
|
||||||
|
|
||||||
|
[[syntax-arg]]
|
||||||
|
=== `<Arg>`
|
||||||
|
|
||||||
|
Element `Arg` is used to pass arguments to <<syntax-new,constructors>> and <<syntax-call,method calls>>.
|
||||||
|
|
||||||
|
The following example creates a minimal Jetty `Server`:
|
||||||
|
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure class="org.eclipse.jetty.server.Server">
|
||||||
|
<Arg type="int">8080</Arg>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
Arguments may have a `type` attribute that explicitly performs <<syntax-types,type coercion>>.
|
||||||
|
|
||||||
|
Arguments may also have a `name` attribute, which is matched with the corresponding Java annotation in the source class, that helps to identify arguments:
|
||||||
|
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure class="org.eclipse.jetty.server.Server">
|
||||||
|
<Arg name="port" type="int">8080</Arg>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
[[syntax-new]]
|
||||||
|
=== `<New>`
|
||||||
|
|
||||||
|
Element `<New>` creates a new object of the type specified by the mandatory `class` attribute.
|
||||||
|
A sequence of `Arg` elements, that must be contiguous and before other elements, may be present to specify the constructor arguments.
|
||||||
|
|
||||||
|
Within element `<New>` the newly created object is in <<syntax-scope,scope>> and may be the implicit target of other, nested, elements.
|
||||||
|
|
||||||
|
The following example creates an `ArrayList`:
|
||||||
|
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure>
|
||||||
|
<New id="mylist" class="java.util.ArrayList">
|
||||||
|
<Arg type="int">16</Arg>
|
||||||
|
</New>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
This is equivalent to the following Java code:
|
||||||
|
|
||||||
|
[,java]
|
||||||
|
----
|
||||||
|
var mylist = new ArrayList(16);
|
||||||
|
----
|
||||||
|
|
||||||
|
[[syntax-call]]
|
||||||
|
=== `<Call>`
|
||||||
|
|
||||||
|
Element `<Call>` invokes a method specified by the mandatory `name` attribute.
|
||||||
|
A sequence of `Arg` elements, that must be contiguous and before other elements, may be present to specify the method arguments.
|
||||||
|
|
||||||
|
Within element `<Call>` the return value, if the return type is not `void`, is in <<syntax-scope,scope>> and may be the implicit target of other, nested, elements.
|
||||||
|
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure>
|
||||||
|
<New class="java.util.ArrayList">
|
||||||
|
<Call name="listIterator">
|
||||||
|
<Arg type="int">0</Arg>
|
||||||
|
</Call>
|
||||||
|
<Call name="next" />
|
||||||
|
</New>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
This is equivalent to the following Java code:
|
||||||
|
|
||||||
|
[,java]
|
||||||
|
----
|
||||||
|
new ArrayList().listIterator(0).next();
|
||||||
|
----
|
||||||
|
|
||||||
|
It is possible to call `static` methods by specifying the `class` attribute:
|
||||||
|
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure>
|
||||||
|
<Call id="myhost" name="getByName" class="java.net.InetAddress">
|
||||||
|
<Arg>jdk.java.net</Arg>
|
||||||
|
</Call>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
This is equivalent to the following Java code:
|
||||||
|
|
||||||
|
[,java]
|
||||||
|
----
|
||||||
|
var myhost = InetAddress.getByName("jdk.java.net");
|
||||||
|
----
|
||||||
|
|
||||||
|
The `class` attribute (or `<Class>` element) can also be used to specify the Java class or interface to use to lookup the non-``static`` method name.
|
||||||
|
This is necessary when the object in scope, onto which the `<Call>` would be applied, is an instance of a class that is not visible to Jetty classes, or not accessible because it is not `public`.
|
||||||
|
For example:
|
||||||
|
|
||||||
|
[,xml,subs=normal]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure>
|
||||||
|
<Call class="java.util.concurrent.Executors" name="newSingleThreadScheduledExecutor">
|
||||||
|
#<Call class="java.util.concurrent.ExecutorService" name="shutdown" />#
|
||||||
|
</Call>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
In the example above, `Executors.newSingleThreadScheduledExecutor()` returns an object whose class is a private JDK implementation class.
|
||||||
|
Without an explicit `class` attribute (or `<Class>` element), it is not possible to invoke the method `shutdown()` when it is obtained via reflection from the private JDK implementation class, because while the method is `public`, the private JDK implementation class is not, therefore this exception is thrown:
|
||||||
|
|
||||||
|
[source]
|
||||||
|
----
|
||||||
|
java.lang.IllegalAccessException: class org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration (in module org.eclipse.jetty.xml) cannot access a member of class java.util.concurrent.Executors$DelegatedExecutorService (in module java.base) with modifiers "public"
|
||||||
|
----
|
||||||
|
|
||||||
|
The solution is to explicitly use the `class` attribute (or `<Class>` element) of the `<Call>` element that is invoking the `shutdown()` method, specifying a publicly accessible class or interface that the object in scope extends or implements (in the example above `java.util.concurrent.ExecutorService`).
|
||||||
|
|
||||||
|
[[syntax-get]]
|
||||||
|
=== `<Get>`
|
||||||
|
|
||||||
|
Element `<Get>` retrieves the value of a JavaBean property specified by the mandatory `name` attribute.
|
||||||
|
|
||||||
|
If the JavaBean property is `foo` (or `Foo`), `<Get>` first attempts to invoke _method_ `getFoo()` or _method_ `isFoo()`; failing that, attempts to retrieve the value from _field_ `foo` (or `Foo`).
|
||||||
|
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure id="server" class="org.eclipse.jetty.server.Server">
|
||||||
|
<!-- Invokes getter method server.getVersion() -->
|
||||||
|
<Get id="version" name="version" />
|
||||||
|
|
||||||
|
<!-- Gets the System.err field -->
|
||||||
|
<Get class="java.lang.System" name="err">
|
||||||
|
<Call name="println">
|
||||||
|
<Arg>Jetty</Arg>
|
||||||
|
</Call>
|
||||||
|
</Get>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
The `class` attribute (or `<Class>` element) allows to perform `static` calls, or to lookup the getter method from the specified class, as described in the <<syntax-call,`<Call>` section>>.
|
||||||
|
|
||||||
|
[[syntax-set]]
|
||||||
|
=== `<Set>`
|
||||||
|
|
||||||
|
Element `<Set>` stores the value of a JavaBean property specified by the mandatory `name` attribute.
|
||||||
|
|
||||||
|
If the JavaBean property is `foo` (or `Foo`), `<Set>` first attempts to invoke _method_ `setFoo(...)` with the value in the <<syntax-scope,scope>> as argument; failing that, attempts to store the value in the scope to _field_ `foo` (or `Foo`).
|
||||||
|
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure id="server" class="org.eclipse.jetty.server.Server">
|
||||||
|
<!-- The value in the <Set> scope is the string "true" -->
|
||||||
|
<Set name="dryRun">true</Set>
|
||||||
|
|
||||||
|
<!-- The value in the <Set> scope is the instance created by <New> -->
|
||||||
|
<Set name="requestLog">
|
||||||
|
<New class="org.eclipse.jetty.server.CustomRequestLog" />
|
||||||
|
</Set>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
The `class` attribute (or `<Class>` element) allows to perform `static` calls, or to lookup the setter method from the specified class, as described in the <<syntax-call,`<Call>` section>>.
|
||||||
|
|
||||||
|
[[syntax-map]]
|
||||||
|
=== `<Map>` and `<Entry>`
|
||||||
|
|
||||||
|
Element `<Map>` allows the creation of a new `java.util.Map` implementation, specified by the `class` attribute -- by default a `HashMap`.
|
||||||
|
|
||||||
|
The map entries are specified with a sequence of `<Entry>` elements, each with exactly 2 `<Item>` elements, for example:
|
||||||
|
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure>
|
||||||
|
<Map class="java.util.concurrent.ConcurrentHashMap">
|
||||||
|
<Entry>
|
||||||
|
<Item>host</Item>
|
||||||
|
<Item>
|
||||||
|
<Call class="java.net.InetAddress" name="getByName">
|
||||||
|
<Arg>localhost</Arg>
|
||||||
|
</Call>
|
||||||
|
</Item>
|
||||||
|
</Entry>
|
||||||
|
</Map>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
[[syntax-put]]
|
||||||
|
=== `<Put>`
|
||||||
|
|
||||||
|
Element `<Put>` is a convenience element that puts a key/value pair into objects that implement `java.util.Map`.
|
||||||
|
You can only specify the key value via the `name` attribute, so the key can only be a literal string (for keys that are not literal strings, use the `<Call>` element).
|
||||||
|
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure>
|
||||||
|
<New class="java.util.Properties">
|
||||||
|
<Put name="host">
|
||||||
|
<Call class="java.net.InetAddress" name="getByName">
|
||||||
|
<Arg>localhost</Arg>
|
||||||
|
</Call>
|
||||||
|
</Put>
|
||||||
|
</New>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
[[syntax-array]]
|
||||||
|
=== `<Array>` and `<Item>`
|
||||||
|
|
||||||
|
Element `<Array>` creates a new array, whose component type may be specified by the `type` attribute, or by a `Type` child element.
|
||||||
|
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure>
|
||||||
|
<Array type="java.lang.Object">
|
||||||
|
<Item /> <!-- null -->
|
||||||
|
<Item>literalString</Item>
|
||||||
|
<Item type="String"></Item> <!-- empty string -->
|
||||||
|
<Item type="Double">1.0D</Item>
|
||||||
|
<Item>
|
||||||
|
<New class="java.lang.Exception" />
|
||||||
|
</Item>
|
||||||
|
</Array>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
[[syntax-ref]]
|
||||||
|
=== `<Ref>`
|
||||||
|
|
||||||
|
Element `<Ref>` allows you to reference an object via the `refid` attribute`, putting it into <<syntax-scope,scope>> so that nested elements can operate on it.
|
||||||
|
You must give a unique `id` attribute to the objects you want to reference.
|
||||||
|
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<!-- The Jetty Server has id="server" -->
|
||||||
|
<Configure id="server" class="org.eclipse.jetty.server.Server">
|
||||||
|
<Get class="java.lang.System" name="err">
|
||||||
|
<!-- Here the System.err field is in scope, but you
|
||||||
|
want to operate on the server to get its version -->
|
||||||
|
<Ref refid="server">
|
||||||
|
<!-- Store the server version under id="myversion" -->
|
||||||
|
<Get id="myversion" name="version" />
|
||||||
|
</Ref>
|
||||||
|
|
||||||
|
<Call name="println">
|
||||||
|
<!-- Reference the server version stored above -->
|
||||||
|
<Arg>Server version is: <Ref refid="myversion" /></Arg>
|
||||||
|
</Call>
|
||||||
|
</Get>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
[[syntax-property]]
|
||||||
|
=== `<Property>`
|
||||||
|
|
||||||
|
Element `<Property>` retrieves the value of the Jetty module property specified by the `name` attribute, and it is mostly used when creating xref:modules/custom.adoc[custom Jetty modules] or when using xref:deploy/index.adoc#jetty[Jetty context XML files].
|
||||||
|
|
||||||
|
The `deprecated` attribute allows you to specify a comma separated list of old, deprecated, property names for backward compatibility.
|
||||||
|
|
||||||
|
The `default` attribute allows you to specify a default value for the property, if it has not been explicitly defined.
|
||||||
|
|
||||||
|
For example, you may want to configure the context path of your web application in this way:
|
||||||
|
|
||||||
|
[,xml,subs=normal]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
|
||||||
|
<Set name="contextPath">
|
||||||
|
#<Property name="com.myapps.mywiki.context.path" default="/wiki" />#
|
||||||
|
</Set>
|
||||||
|
<Set name="war">/opt/myapps/mywiki.war</Set>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
The `contextPath` value is resolved by looking for the Jetty module property `com.myapps.mywiki.context.path`; if this property is not set, then the default value of `/wiki` is used.
|
||||||
|
|
||||||
|
[[syntax-system-property]]
|
||||||
|
=== `<SystemProperty>`
|
||||||
|
|
||||||
|
Element `<SystemProperty>` retrieves the value of the JVM system property specified by the `name` attribute, via `System.getProperty(...)`.
|
||||||
|
|
||||||
|
The `deprecated` attribute allows you to specify a comma separated list of old, deprecated, system property names for backward compatibility.
|
||||||
|
|
||||||
|
The `default` attribute allows you to specify a default value for the system property value, if it has not been explicitly defined.
|
||||||
|
|
||||||
|
The following example creates a minimal Jetty `Server` that listens on a port specified by the `com.acme.http.port` system property:
|
||||||
|
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure id="server" class="org.eclipse.jetty.server.Server">
|
||||||
|
<Arg type="int">
|
||||||
|
<SystemProperty name="com.acme.http.port" default="8080" />
|
||||||
|
</Arg>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
[[syntax-env]]
|
||||||
|
=== `<Env>`
|
||||||
|
|
||||||
|
Element `<Env>` retrieves the value of the environment variable specified by the `name` attribute, via `System.getenv(...)`.
|
||||||
|
|
||||||
|
The `deprecated` attribute allows you to specify a comma separated list of old, deprecated, environment variable names for backward compatibility.
|
||||||
|
|
||||||
|
The `default` attribute allows you to specify a default value for the environment variable value, if it has not been explicitly defined.
|
||||||
|
|
||||||
|
The following example creates a minimal Jetty `Server` that listens on a port specified by the `COM_ACME_HTTP_PORT` environment variable:
|
||||||
|
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure id="server" class="org.eclipse.jetty.server.Server">
|
||||||
|
<Arg type="int">
|
||||||
|
<Env name="COM_ACME_HTTP_PORT" default="8080" />
|
||||||
|
</Arg>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
[[syntax-types]]
|
||||||
|
=== Type Coercion
|
||||||
|
|
||||||
|
Elements that have the `type` attribute explicitly perform the type coercion of the string value present in the XML document to the Java type specified by the `type` attribute.
|
||||||
|
|
||||||
|
Supported types are the following:
|
||||||
|
|
||||||
|
* all primitive types and their boxed equivalents, for example `type="int"` but also `type="Integer"` (short form) and `type="java.lang.Integer"` (fully qualified form)
|
||||||
|
* `java.lang.String`, in both short form and fully qualified form
|
||||||
|
* `java.net.URL`, in both short form and fully qualified form
|
||||||
|
* `java.net.InetAddress`, in both short form and fully qualified form
|
||||||
|
|
||||||
|
[[syntax-scope]]
|
||||||
|
=== Scopes
|
||||||
|
|
||||||
|
Elements that create new objects or that return a value create a _scope_.
|
||||||
|
Within these elements there may be nested elements that will operate on that scope, i.e. on the new object or returned value.
|
||||||
|
|
||||||
|
The following example illustrates how scopes work:
|
||||||
|
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
|
||||||
|
|
||||||
|
<Configure id="server" class="org.eclipse.jetty.server.Server">
|
||||||
|
<Arg type="int">8080</Arg>
|
||||||
|
<!-- Here the Server object has been created and is in scope -->
|
||||||
|
|
||||||
|
<!-- Calls the setter on the Server object that is in scope -->
|
||||||
|
<Set name="stopTimeout">5000</Set>
|
||||||
|
|
||||||
|
<!-- Creates a new object -->
|
||||||
|
<New id="httpConfig" class="org.eclipse.jetty.server.HttpConfiguration">
|
||||||
|
<!-- Here the HttpConfiguration just created is in a nested scope -->
|
||||||
|
|
||||||
|
<!-- Calls the setter on the HttpConfiguration object that is in scope -->
|
||||||
|
<Set name="secureScheme">https</Set>
|
||||||
|
</New>
|
||||||
|
|
||||||
|
<!-- Calls the getter on the Server object that is in scope -->
|
||||||
|
<Get name="ThreadPool">
|
||||||
|
<!-- Here the ThreadPool object returned by the getter is in a nested scope -->
|
||||||
|
|
||||||
|
<!-- Calls the setter on the ThreadPool object that is in scope -->
|
||||||
|
<Set name="maxThreads" type="int">256</Set>
|
||||||
|
</Get>
|
||||||
|
|
||||||
|
<!-- Gets the System.err field -->
|
||||||
|
<Get class="java.lang.System" name="err">
|
||||||
|
<!-- Here the System.err object is in scope -->
|
||||||
|
|
||||||
|
<!-- Equivalent to: var myversion = server.getVersion() -->
|
||||||
|
<Ref refid="server">
|
||||||
|
<!-- Here the "server" object is in scope -->
|
||||||
|
<Get id="myversion" name="version" />
|
||||||
|
</Ref>
|
||||||
|
|
||||||
|
<!-- Calls println() on the System.err object -->
|
||||||
|
<Call name="println">
|
||||||
|
<Arg>Server version is: <Ref refid="myversion" /></Arg>
|
||||||
|
</Call>
|
||||||
|
</Get>
|
||||||
|
</Configure>
|
||||||
|
----
|
||||||
|
|
||||||
|
// TODO: port the documentation from old_docs/jetty-xml/*.adoc
|
|
@ -0,0 +1,46 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
.xref:programming-guide:index.adoc[]
|
||||||
|
* xref:client/index.adoc[]
|
||||||
|
** xref:client/io-arch.adoc[]
|
||||||
|
** xref:client/http.adoc[]
|
||||||
|
** xref:client/http2.adoc[]
|
||||||
|
** xref:client/http3.adoc[]
|
||||||
|
** xref:client/websocket.adoc[]
|
||||||
|
* xref:server/index.adoc[]
|
||||||
|
** xref:server/compliance.adoc[]
|
||||||
|
** xref:server/http.adoc[]
|
||||||
|
** xref:server/http2.adoc[]
|
||||||
|
** xref:server/http3.adoc[]
|
||||||
|
** xref:server/session.adoc[]
|
||||||
|
** xref:server/websocket.adoc[]
|
||||||
|
** xref:server/fastcgi.adoc[]
|
||||||
|
** xref:server/io-arch.adoc[]
|
||||||
|
* Maven and Jetty
|
||||||
|
** xref:maven-jetty/jetty-maven-helloworld.adoc[]
|
||||||
|
** xref:maven-jetty/jetty-maven-plugin.adoc[]
|
||||||
|
** xref:maven-jetty/jetty-jspc-maven-plugin.adoc[]
|
||||||
|
* Jetty Architecture
|
||||||
|
** xref:arch/bean.adoc[]
|
||||||
|
** xref:arch/threads.adoc[]
|
||||||
|
** xref:arch/io.adoc[]
|
||||||
|
** xref:arch/listener.adoc[]
|
||||||
|
** xref:arch/jmx.adoc[]
|
||||||
|
* xref:troubleshooting/index.adoc[]
|
||||||
|
** xref:troubleshooting/logging.adoc[]
|
||||||
|
** xref:troubleshooting/thread-dump.adoc[]
|
||||||
|
** xref:troubleshooting/component-dump.adoc[]
|
||||||
|
** xref:troubleshooting/debugging.adoc[]
|
||||||
|
* Migration Guides
|
||||||
|
** xref:migration/94-to-10.adoc[]
|
|
@ -0,0 +1,160 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Jetty Component Architecture
|
||||||
|
|
||||||
|
Applications that use the Jetty libraries (both client and server) create objects from Jetty classes and compose them together to obtain the desired functionalities.
|
||||||
|
|
||||||
|
A client application creates a `ClientConnector` instance, a `HttpClientTransport` instance and an `HttpClient` instance and compose them to have a working HTTP client that uses to call third party services.
|
||||||
|
|
||||||
|
A server application creates a `ThreadPool` instance, a `Server` instance, a `ServerConnector` instance, a `Handler` instance and compose them together to expose an HTTP service.
|
||||||
|
|
||||||
|
Internally, the Jetty libraries create even more instances of other components that also are composed together with the main ones created by applications.
|
||||||
|
|
||||||
|
The end result is that an application based on the Jetty libraries is a _tree_ of components.
|
||||||
|
In server application the root of the component tree is a `Server` instance, while in client applications the root of the component tree is an `HttpClient` instance.
|
||||||
|
|
||||||
|
Having all the Jetty components in a tree is beneficial in a number of use cases.
|
||||||
|
It makes possible to register the components in the tree as xref:arch/jmx.adoc[JMX MBeans] so that a JMX console can look at the internal state of the components.
|
||||||
|
It also makes possible to xref:troubleshooting/component-dump.adoc[dump the component tree] (and therefore each component's internal state) to a log file or to the console for xref:troubleshooting/index.adoc[troubleshooting purposes].
|
||||||
|
// TODO: add a section on Dumpable?
|
||||||
|
|
||||||
|
[[lifecycle]]
|
||||||
|
== Jetty Component Lifecycle
|
||||||
|
|
||||||
|
Jetty components typically have a life cycle: they can be started and stopped.
|
||||||
|
The Jetty components that have a life cycle implement the `org.eclipse.jetty.util.component.LifeCycle` interface.
|
||||||
|
|
||||||
|
Jetty components that contain other components implement the `org.eclipse.jetty.util.component.Container` interface and typically extend the `org.eclipse.jetty.util.component.ContainerLifeCycle` class.
|
||||||
|
`ContainerLifeCycle` can contain these type of components, also called __bean__s:
|
||||||
|
|
||||||
|
* _managed_ beans, `LifeCycle` instances whose life cycle is tied to the life cycle of their container
|
||||||
|
* _unmanaged_ beans, `LifeCycle` instances whose life cycle is _not_ tied to the life cycle of their container
|
||||||
|
* _POJO_ (Plain Old Java Object) beans, instances that do not implement `LifeCycle`
|
||||||
|
|
||||||
|
`ContainerLifeCycle` uses the following logic to determine if a bean should be _managed_, _unmanaged_ or _POJO_:
|
||||||
|
|
||||||
|
* the bean implements `LifeCycle`
|
||||||
|
** the bean is not started, add it as _managed_
|
||||||
|
** the bean is started, add it as _unmanaged_
|
||||||
|
* the bean does not implement `LifeCycle`, add it as _POJO_
|
||||||
|
|
||||||
|
When a `ContainerLifeCycle` is started, it also starts recursively all its managed beans (if they implement `LifeCycle`); unmanaged beans are not started during the `ContainerLifeCycle` start cycle.
|
||||||
|
Likewise, stopping a `ContainerLifeCycle` stops recursively and in reverse order all its managed beans; unmanaged beans are not stopped during the `ContainerLifeCycle` stop cycle.
|
||||||
|
|
||||||
|
Components can also be started and stopped individually, therefore activating or deactivating the functionalities that they offer.
|
||||||
|
|
||||||
|
Applications should first compose components in the desired structure, and then start the root component:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/ComponentDocs.java[tags=start]
|
||||||
|
----
|
||||||
|
|
||||||
|
The component tree is the following:
|
||||||
|
|
||||||
|
[,screen]
|
||||||
|
----
|
||||||
|
Root
|
||||||
|
├── Monitor (MANAGED)
|
||||||
|
└── Service (MANAGED)
|
||||||
|
└── ScheduledExecutorService (POJO)
|
||||||
|
----
|
||||||
|
|
||||||
|
When the `Root` instance is created, also the `Monitor` instance is created and added as bean, so `Monitor` is the first bean of `Root`.
|
||||||
|
`Monitor` is a _managed_ bean, because it has been explicitly added to `Root` via `ContainerLifeCycle.addManaged(...)`.
|
||||||
|
|
||||||
|
Then, the application creates a `Service` instance and adds it to `Root` via `ContainerLifeCycle.addBean(...)`, so `Service` is the second bean of `Root`.
|
||||||
|
`Service` is a _managed_ bean too, because it is a `LifeCycle` and at the time it was added to `Root` is was not started.
|
||||||
|
|
||||||
|
The `ScheduledExecutorService` within `Service` does not implement `LifeCycle` so it is added as a _POJO_ to `Service`.
|
||||||
|
|
||||||
|
It is possible to stop and re-start any component in a tree, for example:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/ComponentDocs.java[tags=restart]
|
||||||
|
----
|
||||||
|
|
||||||
|
`Service` can be stopped independently of `Root`, and re-started.
|
||||||
|
Starting and stopping a non-root component does not alter the structure of the component tree, just the state of the subtree starting from the component that has been stopped and re-started.
|
||||||
|
|
||||||
|
`Container` provides an API to find beans in the component tree:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/ComponentDocs.java[tags=getBeans]
|
||||||
|
----
|
||||||
|
|
||||||
|
You can add your own beans to the component tree at application startup time, and later find them from your application code to access their services.
|
||||||
|
|
||||||
|
[TIP]
|
||||||
|
====
|
||||||
|
The component tree should be used for long-lived or medium-lived components such as thread pools, web application contexts, etc.
|
||||||
|
|
||||||
|
It is not recommended adding to, and removing from, the component tree short-lived objects such as HTTP requests or TCP connections, for performance reasons.
|
||||||
|
|
||||||
|
If you need component tree features such as automatic xref:arch/jmx.adoc[export to JMX] or xref:troubleshooting/component-dump.adoc[dump capabilities] for short-lived objects, consider having a long-lived container in the component tree instead.
|
||||||
|
You can make the long-lived container efficient at adding/removing the short-lived components using a data structure that is not part of the component tree, and make the long-lived container handle the JMX and dump features for the short-lived components.
|
||||||
|
====
|
||||||
|
|
||||||
|
[[listener]]
|
||||||
|
== Jetty Component Listeners
|
||||||
|
|
||||||
|
A component that extends `AbstractLifeCycle` inherits the possibility to add/remove event _listeners_ for various events emitted by components.
|
||||||
|
|
||||||
|
A component that implements `java.util.EventListener` that is added to a `ContainerLifeCycle` is also registered as an event listener.
|
||||||
|
|
||||||
|
The following sections describe in details the various listeners available in the Jetty component architecture.
|
||||||
|
|
||||||
|
[[listener-lifecycle]]
|
||||||
|
=== LifeCycle.Listener
|
||||||
|
|
||||||
|
A `LifeCycle.Listener` emits events for life cycle events such as starting, stopping and failures:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/ComponentDocs.java[tags=lifecycleListener]
|
||||||
|
----
|
||||||
|
|
||||||
|
For example, a life cycle listener attached to a `Server` instance could be used to create (for the _started_ event) and delete (for the _stopped_ event) a file containing the process ID of the JVM that runs the `Server`.
|
||||||
|
|
||||||
|
[[listener-container]]
|
||||||
|
=== Container.Listener
|
||||||
|
|
||||||
|
A component that implements `Container` is a container for other components and `ContainerLifeCycle` is the typical implementation.
|
||||||
|
|
||||||
|
A `Container` emits events when a component (also called _bean_) is added to or removed from the container:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/ComponentDocs.java[tags=containerListener]
|
||||||
|
----
|
||||||
|
|
||||||
|
A `Container.Listener` added as a bean will also be registered as a listener:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/ComponentDocs.java[tags=containerSiblings]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[listener-inherited]]
|
||||||
|
=== Container.InheritedListener
|
||||||
|
|
||||||
|
A `Container.InheritedListener` is a listener that will be added to all descendants that are also ``Container``s.
|
||||||
|
|
||||||
|
Listeners of this type may be added to the component tree root only, but will be notified of every descendant component that is added to or removed from the component tree (not only first level children).
|
||||||
|
|
||||||
|
The primary use of `Container.InheritedListener` within the Jetty Libraries is `MBeanContainer` from the xref:arch/jmx.adoc[Jetty JMX support].
|
||||||
|
|
||||||
|
`MBeanContainer` listens for every component added to the tree, converts it to an MBean and registers it to the MBeanServer; for every component removed from the tree, it unregisters the corresponding MBean from the MBeanServer.
|
|
@ -0,0 +1,201 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Jetty I/O Architecture
|
||||||
|
|
||||||
|
Jetty libraries (both client and server) use Java NIO to handle I/O, so that at its core Jetty I/O is completely non-blocking.
|
||||||
|
|
||||||
|
[[selector-manager]]
|
||||||
|
== Jetty I/O: `SelectorManager`
|
||||||
|
|
||||||
|
The core class of Jetty I/O is link:{javadoc-url}/org/eclipse/jetty/io/SelectorManager.html[`SelectorManager`].
|
||||||
|
|
||||||
|
`SelectorManager` manages internally a configurable number of link:{javadoc-url}/org/eclipse/jetty/io/ManagedSelector.html[`ManagedSelector`]s.
|
||||||
|
Each `ManagedSelector` wraps an instance of `java.nio.channels.Selector` that in turn manages a number of `java.nio.channels.SocketChannel` instances.
|
||||||
|
|
||||||
|
NOTE: TODO: add image
|
||||||
|
|
||||||
|
`SocketChannel` instances can be created by clients when connecting to a server and by a server when accepting connections from clients.
|
||||||
|
In both cases the `SocketChannel` instance is passed to `SelectorManager` (which passes it to `ManagedSelector` and eventually to `java.nio.channels.Selector`) to be registered for use within Jetty.
|
||||||
|
|
||||||
|
It is possible for an application to create the `SocketChannel` instances outside Jetty, even perform some initial network traffic also outside Jetty (for example for authentication purposes), and then pass the `SocketChannel` instance to `SelectorManager` for use within Jetty.
|
||||||
|
|
||||||
|
This example shows how a client can connect to a server:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/SelectorManagerDocs.java[tags=connect]
|
||||||
|
----
|
||||||
|
|
||||||
|
This example shows how a server accepts a client connection:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/SelectorManagerDocs.java[tags=accept]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[endpoint-connection]]
|
||||||
|
== Jetty I/O: `EndPoint` and `Connection`
|
||||||
|
|
||||||
|
``SocketChannel``s that are passed to `SelectorManager` are wrapped into two related components: an link:{javadoc-url}/org/eclipse/jetty/io/EndPoint.html[`EndPoint`] and a link:{javadoc-url}/org/eclipse/jetty/io/Connection.html[`Connection`].
|
||||||
|
|
||||||
|
`EndPoint` is the Jetty abstraction for a `SocketChannel` or a `DatagramChannel`: you can read bytes from an `EndPoint`, you can write bytes to an `EndPoint` , you can close an `EndPoint`, etc.
|
||||||
|
|
||||||
|
`Connection` is the Jetty abstraction that is responsible to read bytes from the `EndPoint` and to deserialize the read bytes into objects.
|
||||||
|
For example, an HTTP/1.1 server-side `Connection` implementation is responsible to deserialize HTTP/1.1 request bytes into an HTTP request object.
|
||||||
|
Conversely, an HTTP/1.1 client-side `Connection` implementation is responsible to deserialize HTTP/1.1 response bytes into an HTTP response object.
|
||||||
|
|
||||||
|
`Connection` is the abstraction that implements the reading side of a specific protocol such as HTTP/1.1, or HTTP/2, or HTTP/3, or WebSocket: it is able to read incoming communication in that protocol.
|
||||||
|
|
||||||
|
The writing side for a specific protocol _may_ be implemented in the `Connection` but may also be implemented in other components, although eventually the bytes to be written will be written through the `EndPoint`.
|
||||||
|
|
||||||
|
While there are primarily only two implementations of `EndPoint`,link:{javadoc-url}/org/eclipse/jetty/io/SocketChannelEndPoint.html[`SocketChannelEndPoint`] for TCP and link:{javadoc-url}/org/eclipse/jetty/io/DatagramChannelEndPoint.html[`DatagramChannelEndPoint`] for UDP (used both on the client-side and on the server-side), there are many implementations of `Connection`, typically two for each protocol (one for the client-side and one for the server-side).
|
||||||
|
|
||||||
|
The `EndPoint` and `Connection` pairs can be chained, for example in case of encrypted communication using the TLS protocol.
|
||||||
|
There is an `EndPoint` and `Connection` TLS pair where the `EndPoint` reads the encrypted bytes from the socket and the `Connection` decrypts them; next in the chain there is an `EndPoint` and `Connection` pair where the `EndPoint` "reads" decrypted bytes (provided by the previous `Connection`) and the `Connection` deserializes them into specific protocol objects (for example HTTP/2 frame objects).
|
||||||
|
|
||||||
|
Certain protocols, such as WebSocket, start the communication with the server using one protocol (for example, HTTP/1.1), but then change the communication to use another protocol (for example, WebSocket).
|
||||||
|
`EndPoint` supports changing the `Connection` object on-the-fly via `EndPoint.upgrade(Connection)`.
|
||||||
|
This allows to use the HTTP/1.1 `Connection` during the initial communication and later to replace it with a WebSocket `Connection`.
|
||||||
|
|
||||||
|
// TODO: add a section on `UpgradeFrom` and `UpgradeTo`?
|
||||||
|
|
||||||
|
`SelectorManager` is an abstract class because while it knows how to create concrete `EndPoint` instances, it does not know how to create protocol specific `Connection` instances.
|
||||||
|
|
||||||
|
Creating `Connection` instances is performed on the server-side by link:{javadoc-url}/org/eclipse/jetty/server/ConnectionFactory.html[`ConnectionFactory`]s and on the client-side by link:{javadoc-url}/org/eclipse/jetty/io/ClientConnectionFactory.html[`ClientConnectionFactory`]s.
|
||||||
|
|
||||||
|
On the server-side, the component that aggregates a `SelectorManager` with a set of ``ConnectionFactory``s is link:{javadoc-url}/org/eclipse/jetty/server/ServerConnector.html[`ServerConnector`] for TCP sockets, link:{javadoc-url}/org/eclipse/jetty/quic/server/QuicServerConnector.html[`QuicServerConnector`] for QUIC sockets, and link:{JDURL}/org/eclipse/jetty/unixdomain/server/UnixDomainServerConnector.html[`UnixDomainServerConnector`] for Unix-Domain sockets (see the xref:server/io-arch.adoc[server-side architecture section] for more information).
|
||||||
|
|
||||||
|
On the client-side, the components that aggregates a `SelectorManager` with a set of ``ClientConnectionFactory``s are link:{javadoc-url}/org/eclipse/jetty/client/HttpClientTransport.html[`HttpClientTransport`] subclasses (see the xref:client/io-arch.adoc[client-side architecture section] for more information).
|
||||||
|
|
||||||
|
[[endpoint]]
|
||||||
|
== Jetty I/O: `EndPoint`
|
||||||
|
|
||||||
|
The Jetty I/O library use Java NIO to handle I/O, so that I/O is non-blocking.
|
||||||
|
|
||||||
|
At the Java NIO level, in order to be notified when a `SocketChannel` or `DatagramChannel` has data to be read, the `SelectionKey.OP_READ` flag must be set.
|
||||||
|
|
||||||
|
In the Jetty I/O library, you can call `EndPoint.fillInterested(Callback)` to declare interest in the "read" (also called "fill") event, and the `Callback` parameter is the object that is notified when such an event occurs.
|
||||||
|
|
||||||
|
At the Java NIO level, a `SocketChannel` or `DatagramChannel` is always writable, unless it becomes congested.
|
||||||
|
In order to be notified when a channel uncongests and it is therefore writable again, the `SelectionKey.OP_WRITE` flag must be set.
|
||||||
|
|
||||||
|
In the Jetty I/O library, you can call `EndPoint.write(Callback, ByteBuffer...)` to write the ``ByteBuffer``s and the `Callback` parameter is the object that is notified when the whole write is finished (i.e. _all_ ``ByteBuffer``s have been fully written, even if they are delayed by congestion/uncongestion).
|
||||||
|
|
||||||
|
The `EndPoint` APIs abstract out the Java NIO details by providing non-blocking APIs based on `Callback` objects for I/O operations.
|
||||||
|
The `EndPoint` APIs are typically called by `Connection` implementations, see <<connection,this section>>.
|
||||||
|
|
||||||
|
[[connection]]
|
||||||
|
== Jetty I/O: `Connection`
|
||||||
|
|
||||||
|
`Connection` is the abstraction that deserializes incoming bytes into objects, for example an HTTP request object or a WebSocket frame object, that can be used by more abstract layers.
|
||||||
|
|
||||||
|
`Connection` instances have two lifecycle methods:
|
||||||
|
|
||||||
|
* `Connection.onOpen()`, invoked when the `Connection` is associated with the `EndPoint`
|
||||||
|
* `Connection.onClose(Throwable)`, invoked when the `Connection` is disassociated from the `EndPoint`, where the `Throwable` parameter indicates whether the disassociation was normal (when the parameter is `null`) or was due to an error (when the parameter is not `null`)
|
||||||
|
|
||||||
|
When a `Connection` is first created, it is not registered for any Java NIO event.
|
||||||
|
It is therefore typical to implement `onOpen()` to call `EndPoint.fillInterested(Callback)` so that the `Connection` declares interest for read events and it is invoked (via the `Callback`) when the read event happens.
|
||||||
|
|
||||||
|
Abstract class `AbstractConnection` partially implements `Connection` and provides simpler APIs.
|
||||||
|
The example below shows a typical implementation that extends `AbstractConnection`:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/SelectorManagerDocs.java[tags=connection]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[connection-listener]]
|
||||||
|
=== Jetty I/O: `Connection.Listener`
|
||||||
|
|
||||||
|
TODO
|
||||||
|
|
||||||
|
[[echo]]
|
||||||
|
== Jetty I/O: TCP Network Echo
|
||||||
|
|
||||||
|
With the concepts above it is now possible to write a simple, fully non-blocking, `Connection` implementation that simply echoes the bytes that it reads back to the other peer.
|
||||||
|
|
||||||
|
A naive, but wrong, implementation may be the following:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/SelectorManagerDocs.java[tags=echo-wrong]
|
||||||
|
----
|
||||||
|
|
||||||
|
WARNING: The implementation above is wrong and leads to `StackOverflowError`.
|
||||||
|
|
||||||
|
The problem with this implementation is that if the writes always complete synchronously (i.e. without being delayed by TCP congestion), you end up with this sequence of calls:
|
||||||
|
|
||||||
|
----
|
||||||
|
Connection.onFillable()
|
||||||
|
EndPoint.write()
|
||||||
|
Connection.succeeded()
|
||||||
|
Connection.onFillable()
|
||||||
|
EndPoint.write()
|
||||||
|
Connection.succeeded()
|
||||||
|
...
|
||||||
|
----
|
||||||
|
|
||||||
|
which leads to `StackOverflowError`.
|
||||||
|
|
||||||
|
This is a typical side effect of asynchronous programming using non-blocking APIs, and happens in the Jetty I/O library as well.
|
||||||
|
|
||||||
|
NOTE: The callback is invoked synchronously for efficiency reasons.
|
||||||
|
Submitting the invocation of the callback to an `Executor` to be invoked in a different thread would cause a context switch and make simple writes extremely inefficient.
|
||||||
|
|
||||||
|
This side effect of asynchronous programming leading to `StackOverflowError` is so common that the Jetty libraries have a generic solution for it: a specialized `Callback` implementation named `org.eclipse.jetty.util.IteratingCallback` that turns recursion into iteration, therefore avoiding the `StackOverflowError`.
|
||||||
|
|
||||||
|
`IteratingCallback` is a `Callback` implementation that should be passed to non-blocking APIs such as `EndPoint.write(Callback, ByteBuffer...)` when they are performed in a loop.
|
||||||
|
|
||||||
|
`IteratingCallback` works by starting the loop with `IteratingCallback.iterate()`.
|
||||||
|
In turn, this calls `IteratingCallback.process()`, an abstract method that must be implemented with the code that should be executed for each loop.
|
||||||
|
|
||||||
|
Method `process()` must return:
|
||||||
|
|
||||||
|
* `Action.SCHEDULED`, to indicate whether the loop has performed a non-blocking, possibly asynchronous, operation
|
||||||
|
* `Action.IDLE`, to indicate that the loop should temporarily be suspended to be resumed later
|
||||||
|
* `Action.SUCCEEDED` to indicate that the loop exited successfully
|
||||||
|
|
||||||
|
Any exception thrown within `process()` exits the loops with a failure.
|
||||||
|
|
||||||
|
Now that you know how `IteratingCallback` works, a correct implementation for the echo `Connection` is the following:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/SelectorManagerDocs.java[tags=echo-correct]
|
||||||
|
----
|
||||||
|
|
||||||
|
When `onFillable()` is called, for example the first time that bytes are available from the network, the iteration is started.
|
||||||
|
Starting the iteration calls `process()`, where a buffer is allocated and filled with bytes read from the network via `EndPoint.fill(ByteBuffer)`; the buffer is subsequently written back via `EndPoint.write(Callback, ByteBuffer...)` -- note that the callback passed to `EndPoint.write()` is `this`, i.e. the `IteratingCallback` itself; finally `Action.SCHEDULED` is returned, returning from the `process()` method.
|
||||||
|
|
||||||
|
At this point, the call to `EndPoint.write(Callback, ByteBuffer...)` may have completed synchronously; `IteratingCallback` would know that and call `process()` again; within `process()`, the buffer has already been allocated so it will be reused, saving further allocations; the buffer will be filled and possibly written again; `Action.SCHEDULED` is returned again, returning again from the `process()` method.
|
||||||
|
|
||||||
|
At this point, the call to `EndPoint.write(Callback, ByteBuffer...)` may have not completed synchronously, so `IteratingCallback` will not call `process()` again; the processing thread is free to return to the Jetty I/O system where it may be put back into the thread pool.
|
||||||
|
If this was the only active network connection, the system would now be idle, with no threads blocked, waiting that the `write()` completes. This thread-less wait is one of the most important features that make non-blocking asynchronous servers more scalable: they use less resources.
|
||||||
|
|
||||||
|
Eventually, the Jetty I/O system will notify that the `write()` completed; this notifies the `IteratingCallback` that can now resume the loop and call `process()` again.
|
||||||
|
|
||||||
|
When `process()` is called, it is possible that zero bytes are read from the network; in this case, you want to deallocate the buffer since the other peer may never send more bytes for the `Connection` to read, or it may send them after a long pause -- in both cases we do not want to retain the memory allocated by the buffer; next, you want to call `fillInterested()` to declare again interest for read events, and return `Action.IDLE` since there is nothing to write back and therefore the loop may be suspended.
|
||||||
|
When more bytes are again available to be read from the network, `onFillable()` will be called again and that will start the iteration again.
|
||||||
|
|
||||||
|
Another possibility is that during `process()` the read returns `-1` indicating that the other peer has closed the connection; this means that there will not be more bytes to read and the loop can be exited, so you return `Action.SUCCEEDED`; `IteratingCallback` will then call `onCompleteSuccess()` where you can close the `EndPoint`.
|
||||||
|
|
||||||
|
The last case is that during `process()` an exception is thrown, for example by `EndPoint.fill(ByteBuffer)` or, in more advanced implementations, by code that parses the bytes that have been read and finds them unacceptable; any exception thrown within `process()` will be caught by `IteratingCallback` that will exit the loop with a failure and call `onCompleteFailure(Throwable)` with the exception that has been thrown, where you can close the `EndPoint`, passing the exception that is the reason for closing prematurely the `EndPoint`.
|
||||||
|
|
||||||
|
[IMPORTANT]
|
||||||
|
====
|
||||||
|
Asynchronous programming is hard.
|
||||||
|
|
||||||
|
Rely on the Jetty classes to implement `Connection` to avoid mistakes that will be difficult to diagnose and reproduce.
|
||||||
|
====
|
|
@ -0,0 +1,300 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Jetty JMX Support
|
||||||
|
|
||||||
|
The Java Management Extensions (JMX) APIs are standard API for managing and monitoring resources such as applications, devices, services, and the Java Virtual Machine itself.
|
||||||
|
|
||||||
|
The JMX API includes remote access, so a remote management console such as https://openjdk.java.net/projects/jmc/[Java Mission Control] can interact with a running application for these purposes.
|
||||||
|
|
||||||
|
Jetty architecture is based on xref:arch/bean.adoc[components] organized in a tree. Every time a component is added to or removed from the component tree, an event is emitted, and xref:arch/bean.adoc#listener-container[Container.Listener] implementations can listen to those events and perform additional actions.
|
||||||
|
|
||||||
|
`org.eclipse.jetty.jmx.MBeanContainer` listens to those events and registers/unregisters the Jetty components as MBeans into the platform MBeanServer.
|
||||||
|
|
||||||
|
The Jetty components are annotated with <<annotation,Jetty JMX annotations>> so that they can provide specific JMX metadata such as attributes and operations that should be exposed via JMX.
|
||||||
|
|
||||||
|
Therefore, when a component is added to the component tree, `MBeanContainer` is notified, it creates the MBean from the component POJO and registers it to the `MBeanServer`.
|
||||||
|
Similarly, when a component is removed from the tree, `MBeanContainer` is notified, and unregisters the MBean from the `MBeanServer`.
|
||||||
|
|
||||||
|
The Maven coordinates for the Jetty JMX support are:
|
||||||
|
|
||||||
|
[,xml,subs=normal]
|
||||||
|
----
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-jmx</artifactId>
|
||||||
|
<version>{version}</version>
|
||||||
|
</dependency>
|
||||||
|
----
|
||||||
|
|
||||||
|
== Enabling JMX Support
|
||||||
|
|
||||||
|
Enabling JMX support is always recommended because it provides valuable information about the system, both for monitoring purposes and for troubleshooting purposes in case of problems.
|
||||||
|
|
||||||
|
To enable JMX support on the server:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/JMXDocs.java[tags=server]
|
||||||
|
----
|
||||||
|
|
||||||
|
Similarly on the client:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/JMXDocs.java[tags=client]
|
||||||
|
----
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
The MBeans exported to the platform MBeanServer can only be accessed locally (from the same machine), not from remote machines.
|
||||||
|
|
||||||
|
This means that this configuration is enough for development, where you have easy access (with graphical user interface) to the machine where Jetty runs, but it is typically not enough when the machine where Jetty runs is remote, or only accessible via SSH or otherwise without graphical user interface support.
|
||||||
|
In these cases, you have to enable <<remote,JMX Remote Access>>.
|
||||||
|
====
|
||||||
|
|
||||||
|
// TODO: add a section about how to expose logging once #4830 is fixed.
|
||||||
|
|
||||||
|
[[remote]]
|
||||||
|
== Enabling JMX Remote Access
|
||||||
|
|
||||||
|
There are two ways of enabling remote connectivity so that JMC can connect to the remote JVM to visualize MBeans.
|
||||||
|
|
||||||
|
* Use the `com.sun.management.jmxremote` system property on the command line.
|
||||||
|
Unfortunately, this solution does not work well with firewalls and is not flexible.
|
||||||
|
* Use Jetty's `ConnectorServer` class.
|
||||||
|
|
||||||
|
`org.eclipse.jetty.jmx.ConnectorServer` will use by default RMI to allow connection from remote clients, and it is a wrapper around the standard JDK class `JMXConnectorServer`, which is the class that provides remote access to JMX clients.
|
||||||
|
|
||||||
|
Connecting to the remote JVM is a two step process:
|
||||||
|
|
||||||
|
* First, the client will connect to the RMI _registry_ to download the RMI stub for the `JMXConnectorServer`; this RMI stub contains the IP address and port to connect to the RMI server, i.e. the remote `JMXConnectorServer`.
|
||||||
|
* Second, the client uses the RMI stub to connect to the RMI _server_ (i.e. the remote `JMXConnectorServer`) typically on an address and port that may be different from the RMI registry address and port.
|
||||||
|
|
||||||
|
The host and port configuration for the RMI registry and the RMI server is specified by a `JMXServiceURL`.
|
||||||
|
The string format of an RMI `JMXServiceURL` is:
|
||||||
|
|
||||||
|
[,screen]
|
||||||
|
----
|
||||||
|
service:jmx:rmi://<rmi_server_host>:<rmi_server_port>/jndi/rmi://<rmi_registry_host>:<rmi_registry_port>/jmxrmi
|
||||||
|
----
|
||||||
|
|
||||||
|
Default values are:
|
||||||
|
|
||||||
|
[,screen]
|
||||||
|
----
|
||||||
|
rmi_server_host = localhost
|
||||||
|
rmi_server_port = 1099
|
||||||
|
rmi_registry_host = localhost
|
||||||
|
rmi_registry_port = 1099
|
||||||
|
----
|
||||||
|
|
||||||
|
With the default configuration, only clients that are local to the server machine can connect to the RMI registry and RMI server - this is done for security reasons.
|
||||||
|
With this configuration it would still be possible to access the MBeans from remote using a <<remote-ssh-tunnel,SSH tunnel>>.
|
||||||
|
|
||||||
|
By specifying an appropriate `JMXServiceURL`, you can fine tune the network interfaces the RMI registry and the RMI server bind to, and the ports that the RMI registry and the RMI server listen to.
|
||||||
|
The RMI server and RMI registry hosts and ports can be the same (as in the default configuration) because RMI is able to multiplex traffic arriving to a port to multiple RMI objects.
|
||||||
|
|
||||||
|
If you need to allow JMX remote access through a firewall, you must open both the RMI registry and the RMI server ports.
|
||||||
|
|
||||||
|
`JMXServiceURL` common examples:
|
||||||
|
|
||||||
|
[,screen]
|
||||||
|
----
|
||||||
|
service:jmx:rmi:///jndi/rmi:///jmxrmi
|
||||||
|
rmi_server_host = local host address
|
||||||
|
rmi_server_port = randomly chosen
|
||||||
|
rmi_registry_host = local host address
|
||||||
|
rmi_registry_port = 1099
|
||||||
|
|
||||||
|
service:jmx:rmi://0.0.0.0:1099/jndi/rmi://0.0.0.0:1099/jmxrmi
|
||||||
|
rmi_server_host = any address
|
||||||
|
rmi_server_port = 1099
|
||||||
|
rmi_registry_host = any address
|
||||||
|
rmi_registry_port = 1099
|
||||||
|
|
||||||
|
service:jmx:rmi://localhost:1100/jndi/rmi://localhost:1099/jmxrmi
|
||||||
|
rmi_server_host = loopback address
|
||||||
|
rmi_server_port = 1100
|
||||||
|
rmi_registry_host = loopback address
|
||||||
|
rmi_registry_port = 1099
|
||||||
|
----
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
When `ConnectorServer` is started, its RMI stub is exported to the RMI registry.
|
||||||
|
The RMI stub contains the IP address and port to connect to the RMI object, but the IP address is typically the machine host name, not the host specified in the `JMXServiceURL`.
|
||||||
|
|
||||||
|
To control the IP address stored in the RMI stub you need to set the system property `java.rmi.server.hostname` with the desired value.
|
||||||
|
This is especially important when binding the RMI server host to the loopback address for security reasons. See also <<remote-ssh-tunnel,JMX Remote Access via SSH Tunnel.>>
|
||||||
|
====
|
||||||
|
|
||||||
|
To allow JMX remote access, create and configure a `ConnectorServer`:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/JMXDocs.java[tags=remote]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[remote-authorization]]
|
||||||
|
=== JMX Remote Access Authorization
|
||||||
|
|
||||||
|
The standard `JMXConnectorServer` provides several options to authorize access, for example via JAAS or via configuration files.
|
||||||
|
For a complete guide to controlling authentication and authorization in JMX, see https://docs.oracle.com/en/java/javase/11/management/[the official JMX documentation].
|
||||||
|
|
||||||
|
In the sections below we detail one way to setup JMX authentication and authorization, using configuration files for users, passwords and roles:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/JMXDocs.java[tags=remoteAuthorization]
|
||||||
|
----
|
||||||
|
|
||||||
|
The `users.access` file format is defined in the `$JAVA_HOME/conf/management/jmxremote.access` file.
|
||||||
|
A simplified version is the following:
|
||||||
|
|
||||||
|
.users.access
|
||||||
|
[,screen]
|
||||||
|
----
|
||||||
|
user1 readonly
|
||||||
|
user2 readwrite
|
||||||
|
----
|
||||||
|
|
||||||
|
The `users.password` file format is defined in the `$JAVA_HOME/conf/management/jmxremote.password.template` file.
|
||||||
|
A simplified version is the following:
|
||||||
|
|
||||||
|
.users.password
|
||||||
|
[,screen]
|
||||||
|
----
|
||||||
|
user1 password1
|
||||||
|
user2 password2
|
||||||
|
----
|
||||||
|
|
||||||
|
CAUTION: The `users.access` and `users.password` files are not standard `*.properties` files -- the user must be separated from the role or password by a space character.
|
||||||
|
|
||||||
|
=== Securing JMX Remote Access with TLS
|
||||||
|
|
||||||
|
The JMX communication via RMI happens by default in clear-text.
|
||||||
|
|
||||||
|
It is possible to configure the `ConnectorServer` with a `SslContextFactory` so that the JMX communication via RMI is encrypted:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/JMXDocs.java[tags=tlsRemote]
|
||||||
|
----
|
||||||
|
|
||||||
|
It is possible to use the same `SslContextFactory.Server` used to configure the Jetty `ServerConnector` that supports TLS also for the JMX communication via RMI.
|
||||||
|
|
||||||
|
The keystore must contain a valid certificate signed by a Certification Authority.
|
||||||
|
|
||||||
|
The RMI mechanic is the usual one: the RMI client (typically a monitoring console) will connect first to the RMI registry (using TLS), download the RMI server stub that contains the address and port of the RMI server to connect to, then connect to the RMI server (using TLS).
|
||||||
|
|
||||||
|
This also mean that if the RMI registry and the RMI server are on different hosts, the RMI client must have available the cryptographic material to validate both hosts.
|
||||||
|
|
||||||
|
Having certificates signed by a Certification Authority simplifies by a lot the configuration needed to get the JMX communication over TLS working properly.
|
||||||
|
|
||||||
|
If that is not the case (for example the certificate is self-signed), then you need to specify the required system properties that allow RMI (especially when acting as an RMI client) to retrieve the cryptographic material necessary to establish the TLS connection.
|
||||||
|
|
||||||
|
For example, trying to connect using the JDK standard `JMXConnector` with both the RMI server and the RMI registry via TLS to `domain.com` with a self-signed certificate:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/JMXDocs.java[tags=tlsJMXConnector]
|
||||||
|
----
|
||||||
|
|
||||||
|
Similarly, to launch JMC:
|
||||||
|
|
||||||
|
[,screen]
|
||||||
|
----
|
||||||
|
$ jmc -vmargs -Djavax.net.ssl.trustStore=/path/to/trustStore -Djavax.net.ssl.trustStorePassword=secret
|
||||||
|
----
|
||||||
|
|
||||||
|
IMPORTANT: These system properties are required when launching the `ConnectorServer` too, on the server, because it acts as an RMI client with respect to the RMI registry.
|
||||||
|
|
||||||
|
[[remote-ssh-tunnel]]
|
||||||
|
=== JMX Remote Access with Port Forwarding via SSH Tunnel
|
||||||
|
|
||||||
|
You can access JMX MBeans on a remote machine when the RMI ports are not open, for example because of firewall policies, but you have SSH access to the machine using local port forwarding via an SSH tunnel.
|
||||||
|
|
||||||
|
In this case you want to configure the `ConnectorServer` with a `JMXServiceURL` that binds the RMI server and the RMI registry to the loopback interface only: `service:jmx:rmi://localhost:1099/jndi/rmi://localhost:1099/jmxrmi`.
|
||||||
|
|
||||||
|
Then you setup the local port forwarding with the SSH tunnel:
|
||||||
|
|
||||||
|
[,screen]
|
||||||
|
----
|
||||||
|
$ ssh -L 1099:localhost:1099 <user>@<machine_host>
|
||||||
|
----
|
||||||
|
|
||||||
|
Now you can use JConsole or JMC to connect to `localhost:1099` on your local computer.
|
||||||
|
The traffic will be forwarded to `machine_host` and when there, SSH will forward the traffic to `localhost:1099`, which is exactly where the `ConnectorServer` listens.
|
||||||
|
|
||||||
|
When you configure `ConnectorServer` in this way, you must set the system property `-Djava.rmi.server.hostname=localhost`, on the server.
|
||||||
|
This is required because when the RMI server is exported, its address and port are stored in the RMI stub. You want the address in the RMI stub to be `localhost` so that when the RMI stub is downloaded to the remote client, the RMI communication will go through the SSH tunnel.
|
||||||
|
|
||||||
|
[[annotation]]
|
||||||
|
== Jetty JMX Annotations
|
||||||
|
|
||||||
|
The Jetty JMX support, and in particular `MBeanContainer`, is notified every time a bean is added to the component tree.
|
||||||
|
|
||||||
|
The bean is scanned for Jetty JMX annotations to obtain JMX metadata: the JMX attributes and JMX operations.
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/JMXDocs.java[tags=jmxAnnotation]
|
||||||
|
----
|
||||||
|
|
||||||
|
The JMX metadata and the bean are wrapped by an instance of `org.eclipse.jetty.jmx.ObjectMBean` that exposes the JMX metadata and, upon request from JMX consoles, invokes methods on the bean to get/set attribute values and perform operations.
|
||||||
|
|
||||||
|
You can provide a custom subclass of `ObjectMBean` to further customize how the bean is exposed to JMX.
|
||||||
|
|
||||||
|
The custom `ObjectMBean` subclass must respect the following naming convention: `<package>.jmx.<class>MBean`.
|
||||||
|
For example, class `com.acme.Foo` may have a custom `ObjectMBean` subclass named `com.acme.**jmx**.Foo**MBean**`.
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/JMXDocs.java[tags=jmxCustomMBean]
|
||||||
|
----
|
||||||
|
|
||||||
|
The custom `ObjectMBean` subclass is also scanned for Jetty JMX annotations and overrides the JMX metadata obtained by scanning the bean class.
|
||||||
|
This allows to annotate only the custom `ObjectMBean` subclass and keep the bean class free of the Jetty JMX annotations.
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/JMXDocs.java[tags=jmxCustomMBeanOverride]
|
||||||
|
----
|
||||||
|
|
||||||
|
The scan for Jetty JMX annotations is performed on the bean class and all the interfaces implemented by the bean class, then on the super-class and all the interfaces implemented by the super-class and so on until `java.lang.Object` is reached.
|
||||||
|
For each type -- class or interface, the corresponding `+*.jmx.*MBean+` is looked up and scanned as well with the same algorithm.
|
||||||
|
For each type, the scan looks for the class-level annotation `@ManagedObject`.
|
||||||
|
If it is found, the scan looks for method-level `@ManagedAttribute` and `@ManagedOperation` annotations; otherwise it skips the current type and moves to the next type to scan.
|
||||||
|
|
||||||
|
=== @ManagedObject
|
||||||
|
|
||||||
|
The `@ManagedObject` annotation is used on a class at the top level to indicate that it should be exposed as an MBean.
|
||||||
|
It has only one attribute to it which is used as the description of the MBean.
|
||||||
|
|
||||||
|
=== @ManagedAttribute
|
||||||
|
|
||||||
|
The `@ManagedAttribute` annotation is used to indicate that a given method is exposed as a JMX attribute.
|
||||||
|
This annotation is placed always on the getter method of a given attribute.
|
||||||
|
Unless the `readonly` attribute is set to `true` in the annotation, a corresponding setter is looked up following normal naming conventions.
|
||||||
|
For example if this annotation is on a method called `String getFoo()` then a method called `void setFoo(String)` would be looked up, and if found wired as the setter for the JMX attribute.
|
||||||
|
|
||||||
|
=== @ManagedOperation
|
||||||
|
|
||||||
|
The `@ManagedOperation` annotation is used to indicate that a given method is exposed as a JMX operation.
|
||||||
|
A JMX operation has an _impact_ that can be `INFO` if the operation returns a value without modifying the object, `ACTION` if the operation does not return a value but modifies the object, and "ACTION_INFO" if the operation both returns a value and modifies the object.
|
||||||
|
If the _impact_ is not specified, it has the default value of `UNKNOWN`.
|
||||||
|
|
||||||
|
=== @Name
|
||||||
|
|
||||||
|
The `@Name` annotation is used to assign a name and description to parameters in method signatures so that when rendered by JMX consoles it is clearer what the parameter meaning is.
|
|
@ -0,0 +1,27 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Jetty Listeners
|
||||||
|
|
||||||
|
The Jetty architecture is based on xref:arch/bean.adoc[components], typically organized in a component tree.
|
||||||
|
These components have an internal state that varies with the component life cycle (that is, whether the component is started or stopped), as well as with the component use at runtime.
|
||||||
|
The typical example is a thread pool, whose internal state -- such as the number of pooled threads or the job queue size -- changes as the thread pool is used by the running client or server.
|
||||||
|
|
||||||
|
In many cases, the component state change produces an event that is broadcast to listeners.
|
||||||
|
Applications can register listeners to these components to be notified of the events they produce.
|
||||||
|
|
||||||
|
This section lists the listeners available in the Jetty components, but the events and listener APIs are discussed in the component specific sections.
|
||||||
|
|
||||||
|
* xref:arch/bean.adoc#listener[]
|
||||||
|
* xref:arch/io.adoc#connection-listener[]
|
||||||
|
* xref:server/http.adoc#channel-events[]
|
|
@ -0,0 +1,229 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Jetty Threading Architecture
|
||||||
|
|
||||||
|
Writing a performant client or server is difficult, because it should:
|
||||||
|
|
||||||
|
* Scale well with the number of processors.
|
||||||
|
* Be efficient at using processor caches to avoid https://en.wikipedia.org/wiki/Parallel_slowdown[parallel slowdown].
|
||||||
|
* Support multiple network protocols that may have very different requirements; for example, multiplexed protocols such as HTTP/2 introduce new challenges that are not present in non-multiplexed protocols such as HTTP/1.1.
|
||||||
|
* Support different application threading models; for example, if a Jetty server invokes server-side application code that is allowed to call blocking APIs, then the Jetty server should not be affected by how long the blocking API call takes, and should be able to process other connections or other requests in a timely fashion.
|
||||||
|
|
||||||
|
[[execution-strategy]]
|
||||||
|
== Execution Strategies
|
||||||
|
|
||||||
|
The Jetty threading architecture can be modeled with a producer/consumer pattern, where produced tasks needs to be consumed efficiently.
|
||||||
|
|
||||||
|
For example, Jetty produces (among others) these tasks:
|
||||||
|
|
||||||
|
* A task that wraps a NIO selection event, see the xref:arch/io.adoc[Jetty I/O architecture].
|
||||||
|
* A task that wraps the invocation of application code that may block (for example, the invocation of a Servlet to handle an HTTP request).
|
||||||
|
|
||||||
|
A task is typically a `Runnable` object that may implement `org.eclipse.jetty.util.thread.Invocable` to indicate the behavior of the task (in particular, whether the task may block or not).
|
||||||
|
|
||||||
|
Once a task has been produced, it may be consumed using these modes:
|
||||||
|
|
||||||
|
* <<execution-strategy-pc,`Produce-Consume`>>
|
||||||
|
* <<execution-strategy-pec,`Produce-Execute-Consume`>>
|
||||||
|
* <<execution-strategy-epc,`Execute-Produce-Consume`>>
|
||||||
|
|
||||||
|
[[execution-strategy-pc]]
|
||||||
|
=== Produce-Consume
|
||||||
|
In the `Produce-Consume` mode, the producer thread loops to produce a task that is run directly by the `Producer Thread`.
|
||||||
|
|
||||||
|
[plantuml]
|
||||||
|
----
|
||||||
|
skinparam backgroundColor transparent
|
||||||
|
|
||||||
|
compact concise "Producer Thread" as PT
|
||||||
|
hide time-axis
|
||||||
|
|
||||||
|
@PT
|
||||||
|
0 is T1 #lightgreen
|
||||||
|
1 is "Run T1" #dodgerblue
|
||||||
|
5 is T2 #lightgreen
|
||||||
|
6 is "Run T2" #dodgerblue
|
||||||
|
8 is T3 #lightgreen
|
||||||
|
9 is "Run T3" #dodgerblue
|
||||||
|
12 is {hidden}
|
||||||
|
----
|
||||||
|
|
||||||
|
If the task is a NIO selection event, then this mode is the thread-per-selector mode which is very CPU core cache efficient, but suffers from the http://en.wikipedia.org/wiki/Head-of-line_blocking[head-of-line blocking]: if one of the tasks blocks or runs slowly, then subsequent tasks cannot be produced (and therefore cannot be consumed either) and will pay in latency the cost of running previous, possibly unrelated, tasks.
|
||||||
|
|
||||||
|
This mode should only be used if the produced task is known to never block, or if the system tolerates well (or does not care about) head-of-line blocking.
|
||||||
|
|
||||||
|
[[execution-strategy-pec]]
|
||||||
|
=== Produce-Execute-Consume
|
||||||
|
In the `Produce-Execute-Consume` mode, the `Producer Thread` loops to produce tasks that are submitted to a `java.util.concurrent.Executor` to be run by ``Worker Thread``s different from the `Producer Thread`.
|
||||||
|
|
||||||
|
[plantuml]
|
||||||
|
----
|
||||||
|
skinparam backgroundColor transparent
|
||||||
|
|
||||||
|
compact concise "Producer Thread" as PT
|
||||||
|
compact concise "Worker Thread 1" as WT1
|
||||||
|
compact concise "Worker Thread 2" as WT2
|
||||||
|
compact concise "Worker Thread 3" as WT3
|
||||||
|
hide time-axis
|
||||||
|
|
||||||
|
@PT
|
||||||
|
0 is T1 #lightgreen
|
||||||
|
1 is T2 #lightgreen
|
||||||
|
2 is T3 #lightgreen
|
||||||
|
3 is T4 #lightgreen
|
||||||
|
4 is {hidden}
|
||||||
|
|
||||||
|
@WT1
|
||||||
|
1 is "Run T1" #dodgerblue
|
||||||
|
5 is {hidden}
|
||||||
|
|
||||||
|
@WT2
|
||||||
|
2 is "Run T2" #dodgerblue
|
||||||
|
4 is "Run T4" #dodgerblue
|
||||||
|
8 is {hidden}
|
||||||
|
|
||||||
|
@WT3
|
||||||
|
3 is "Run T3" #dodgerblue
|
||||||
|
6 is {hidden}
|
||||||
|
----
|
||||||
|
|
||||||
|
The `Executor` implementation typically adds the task to a queue, and dequeues the task when there is a worker thread available to run it.
|
||||||
|
|
||||||
|
This mode solves the head-of-line blocking discussed in the <<execution-strategy-pc,`Produce-Consume` section>>, but suffers from other issues:
|
||||||
|
|
||||||
|
* It is not CPU core cache efficient, as the data available to the producer thread will need to be accessed by another thread that likely is going to run on a CPU core that will not have that data in its caches.
|
||||||
|
* If the tasks take time to be run, the `Executor` queue may grow indefinitely.
|
||||||
|
* A small latency is added to every task: the time it waits in the `Executor` queue.
|
||||||
|
|
||||||
|
[[execution-strategy-epc]]
|
||||||
|
=== Execute-Produce-Consume
|
||||||
|
In the `Execute-Produce-Consume` mode, the producer thread `Thread 1` loops to produce a task, then submits one internal task to an `Executor` to take over production on thread `Thread 2`, and then runs the task in `Thread 1`, and so on.
|
||||||
|
|
||||||
|
[plantuml]
|
||||||
|
----
|
||||||
|
skinparam backgroundColor transparent
|
||||||
|
|
||||||
|
compact concise "Thread 1" as WT1
|
||||||
|
compact concise "Thread 2" as WT2
|
||||||
|
compact concise "Thread 3" as WT3
|
||||||
|
compact concise "Thread 4" as WT4
|
||||||
|
hide time-axis
|
||||||
|
|
||||||
|
@WT1
|
||||||
|
0 is T1 #lightgreen
|
||||||
|
1 is "Run T1" #dodgerblue
|
||||||
|
5 is {hidden}
|
||||||
|
|
||||||
|
@WT2
|
||||||
|
1 is T2 #lightgreen
|
||||||
|
2 is "Run T2" #dodgerblue
|
||||||
|
4 is T5 #lightgreen
|
||||||
|
5 is "Run T5" #dodgerblue
|
||||||
|
10 is {hidden}
|
||||||
|
|
||||||
|
@WT3
|
||||||
|
2 is T3 #lightgreen
|
||||||
|
3 is "Run T3" #dodgerblue
|
||||||
|
6 is {hidden}
|
||||||
|
|
||||||
|
@WT4
|
||||||
|
3 is T4 #lightgreen
|
||||||
|
4 is "Run T4" #dodgerblue
|
||||||
|
8 is {hidden}
|
||||||
|
|
||||||
|
----
|
||||||
|
|
||||||
|
This mode may operate like <<execution-strategy-pc,`Produce-Consume`>> when the take over production task run, for example, by thread `Thread 3` takes time to be executed (for example, in a busy server): then thread `Thread 2` will produce one task and run it, then produce another task and run it, etc. -- `Thread 2` behaves exactly like the `Produce-Consume` mode.
|
||||||
|
By the time thread `Thread 3` takes over task production from `Thread 2`, all the work might already be done.
|
||||||
|
|
||||||
|
This mode may also operate similarly to <<execution-strategy-pec,`Produce-Execute-Consume`>> when the take over production task always finds a free CPU core immediately (for example, in a mostly idle server): thread `Thread 1` will produce a task, yield production to `Thread 2` while `Thread 1` is running the task; `Thread 2` will produce a task, yield production to `Thread 3` while `Thread 2` is running the task, etc.
|
||||||
|
|
||||||
|
Differently from `Produce-Execute-Consume`, here production happens on different threads, but the advantage is that the task is run by the same thread that produced it (which is CPU core cache efficient).
|
||||||
|
|
||||||
|
[[execution-strategy-adaptive]]
|
||||||
|
=== Adaptive Execution Strategy
|
||||||
|
The modes of task consumption discussed above are captured by the `org.eclipse.jetty.util.thread.ExecutionStrategy` interface, with an additional implementation that also takes into account the behavior of the task when the task implements `Invocable`.
|
||||||
|
|
||||||
|
For example, a task that declares itself as non-blocking can be consumed using the `Produce-Consume` mode, since there is no risk to stop production because the task will not block.
|
||||||
|
|
||||||
|
Conversely, a task that declares itself as blocking will stop production, and therefore must be consumed using either the `Produce-Execute-Consume` mode or the `Execute-Produce-Consume` mode.
|
||||||
|
Deciding between these two modes depends on whether there is a free thread immediately available to take over production, and this is captured by the `org.eclipse.jetty.util.thread.TryExecutor` interface.
|
||||||
|
|
||||||
|
An implementation of `TryExecutor` can be asked whether a thread can be immediately and exclusively allocated to run a task, as opposed to a normal `Executor` that can only queue the task in the expectation that there will be a thread available in the near future to run the task.
|
||||||
|
|
||||||
|
The concept of task consumption modes, coupled with `Invocable` tasks that expose their own behavior, coupled with a `TryExecutor` that guarantees whether production can be immediately taken over are captured by the default Jetty execution strategy, named `org.eclipse.jetty.util.thread.AdaptiveExecutionStrategy`.
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
`AdaptiveExecutionStrategy` was previously named `EatWhatYouKill`, named after a hunting proverb in the sense that one should produce (kill) only what it consumes (eats).
|
||||||
|
====
|
||||||
|
|
||||||
|
[[thread-pool]]
|
||||||
|
== Thread Pool
|
||||||
|
Jetty's xref:arch/threads.adoc[threading architecture] requires a more sophisticated thread pool than what offered by Java's `java.util.concurrent.ExecutorService`.
|
||||||
|
|
||||||
|
Jetty's default thread pool implementation is link:{javadoc-url}/org/eclipse/jetty/util/thread/QueuedThreadPool.html[`QueuedThreadPool`].
|
||||||
|
|
||||||
|
`QueuedThreadPool` integrates with the xref:arch/bean.adoc[Jetty component model], implements `Executor`, provides a `TryExecutor` implementation (discussed in the <<execution-strategy-adaptive,adaptive execution strategy section>>), and supports <<thread-pool-virtual-threads,virtual threads>> (introduced as a preview feature in Java 19 and Java 20, and as an official feature since Java 21).
|
||||||
|
|
||||||
|
`QueuedThreadPool` can be configured with a `maxThreads` value.
|
||||||
|
|
||||||
|
However, some of the Jetty components (such as the xref:arch/io.adoc#selector-manager[selectors]) permanently steal threads for their internal use, or rather `QueuedThreadPool` leases some threads to these components.
|
||||||
|
These threads are reported by `QueuedThreadPool.leasedThreads` and are not available to run application code.
|
||||||
|
|
||||||
|
`QueuedThreadPool` can be configured with a `reservedThreads` value.
|
||||||
|
This value represents the maximum number of threads that can be reserved and used by the `TryExecutor` implementation.
|
||||||
|
A negative value for `QueuedThreadPool.reservedThreads` means that the actual value will be heuristically derived from the number of CPU cores and `QueuedThreadPool.maxThreads`.
|
||||||
|
A value of zero for `QueuedThreadPool.reservedThreads` means that reserved threads are disabled, and therefore the <<execution-strategy-epc,`Execute-Produce-Consume` mode>> is never used -- the <<execution-strategy-pec,`Produce-Execute-Consume` mode>> is always used instead.
|
||||||
|
|
||||||
|
`QueuedThreadPool` always maintains the number of threads between `QueuedThreadPool.minThreads` and `QueuedThreadPool.maxThreads`; during load spikes the number of thread grows to meet the load demand, and when the load on the system diminishes or the system goes idle, the number of threads shrinks.
|
||||||
|
|
||||||
|
Shrinking `QueuedThreadPool` is important in particular in containerized environments, where typically you want to return the memory occupied by the threads to the operative system.
|
||||||
|
The shrinking of the `QueuedThreadPool` is controlled by two parameters: `QueuedThreadPool.idleTimeout` and `QueuedThreadPool.maxEvictCount`.
|
||||||
|
|
||||||
|
`QueuedThreadPool.idleTimeout` indicates how long a thread should stay around when it is idle, waiting for tasks to execute.
|
||||||
|
The longer the threads stay around, the more ready they are in case of new load spikes on the system; however, they consume resources: a Java platform thread typically allocates 1 MiB of native memory.
|
||||||
|
|
||||||
|
`QueuedThreadPool.maxEvictCount` controls how many idle threads are evicted for one `QueuedThreadPool.idleTimeout` period.
|
||||||
|
The larger this value is, the quicker the threads are evicted when the `QueuedThreadPool` is idle or has less load, and their resources returned to the operative system; however, large values may result in too much thread thrashing: the `QueuedThreadPool` shrinks too fast and must re-create a lot of threads in case of a new load spike on the system.
|
||||||
|
|
||||||
|
A good balance between `QueuedThreadPool.idleTimeout` and `QueuedThreadPool.maxEvictCount` depends on the load profile of your system, and it is often tuned via trial and error.
|
||||||
|
|
||||||
|
[[thread-pool-virtual-threads]]
|
||||||
|
=== Virtual Threads
|
||||||
|
Virtual threads have been introduced in Java 19 and Java 20 as a preview feature, and have become an official feature since Java 21.
|
||||||
|
|
||||||
|
NOTE: In Java versions where virtual threads are a preview feature, remember to add `+--enable-preview+` to the JVM command line options to use virtual threads.
|
||||||
|
|
||||||
|
`QueuedThreadPool` can be configured to use virtual threads by specifying the virtual threads `Executor`:
|
||||||
|
|
||||||
|
[,java]
|
||||||
|
----
|
||||||
|
QueuedThreadPool threadPool = new QueuedThreadPool();
|
||||||
|
threadPool.setVirtualThreadsExecutor(Executors.newVirtualThreadPerTaskExecutor());
|
||||||
|
----
|
||||||
|
|
||||||
|
[CAUTION]
|
||||||
|
====
|
||||||
|
Jetty cannot enforce that the `Executor` passed to `setVirtualThreadsExecutor(Executor)` uses virtual threads, so make sure to specify a _virtual_ threads `Executor` and not a normal `Executor` that uses platform threads.
|
||||||
|
====
|
||||||
|
|
||||||
|
`AdaptiveExecutionStrategy` makes use of this setting when it determines that a task should be run with the <<execution-strategy-pec,`Produce-Execute-Consume` mode>>: rather than submitting the task to `QueuedThreadPool` to be run in a platform thread, it submits the task to the virtual threads `Executor`.
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
Enabling virtual threads in `QueuedThreadPool` will default the number of reserved threads to zero, unless the number of reserved threads is explicitly configured to a positive value.
|
||||||
|
|
||||||
|
Defaulting the number of reserved threads to zero ensures that the <<execution-strategy-pec,Produce-Execute-Consume mode>> is always used, which means that virtual threads will always be used for blocking tasks.
|
||||||
|
====
|
|
@ -0,0 +1,951 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= HTTP Client
|
||||||
|
|
||||||
|
[[intro]]
|
||||||
|
== HttpClient Introduction
|
||||||
|
|
||||||
|
The Jetty HTTP client module provides easy-to-use APIs and utility classes to perform HTTP (or HTTPS) requests.
|
||||||
|
|
||||||
|
Jetty's HTTP client is non-blocking and asynchronous.
|
||||||
|
It offers an asynchronous API that never blocks for I/O, making it very efficient in thread utilization and well suited for high performance scenarios such as load testing or parallel computation.
|
||||||
|
|
||||||
|
However, when all you need to do is to perform a `GET` request to a resource, Jetty's HTTP client offers also a synchronous API; a programming interface where the thread that issued the request blocks until the request/response conversation is complete.
|
||||||
|
|
||||||
|
Jetty's HTTP client supports different <<transport,transports protocols>>: HTTP/1.1, HTTP/2, HTTP/3 and FastCGI. This means that the semantic of an HTTP request such as: " ``GET`` the resource ``/index.html`` " can be carried over the network in different formats.
|
||||||
|
The most common and default format is HTTP/1.1. That said, Jetty's HTTP client can carry the same request using the HTTP/2 format, the HTTP/3 format, or the FastCGI format.
|
||||||
|
|
||||||
|
Furthermore, every transport protocol can be sent either over the network or via Unix-Domain sockets.
|
||||||
|
Supports for Unix-Domain sockets requires Java 16 or later, since Unix-Domain sockets support has been introduced in OpenJDK with https://openjdk.java.net/jeps/380[JEP 380].
|
||||||
|
|
||||||
|
The <<transport-fcgi,FastCGI transport>> is heavily used in Jetty's xref:server/fastcgi.adoc[FastCGI support] that allows Jetty to work as a reverse proxy to PHP (exactly like Apache or Nginx do) and therefore be able to serve, for example, WordPress websites, often in conjunction with Unix-Domain sockets (although it's possible to use FastCGI via network too).
|
||||||
|
|
||||||
|
The HTTP/2 transport allows Jetty's HTTP client to perform requests using HTTP/2 to HTTP/2 enabled web sites, see also Jetty's xref:client/http2.adoc[HTTP/2 support].
|
||||||
|
|
||||||
|
The HTTP/3 transport allows Jetty's HTTP client to perform requests using HTTP/3 to HTTP/3 enabled web sites, see also Jetty's xref:client/http3.adoc[HTTP/3 support].
|
||||||
|
|
||||||
|
Out of the box features that you get with the Jetty HTTP client include:
|
||||||
|
|
||||||
|
* Redirect support -- redirect codes such as 302 or 303 are automatically followed.
|
||||||
|
* Cookies support -- cookies sent by servers are stored and sent back to servers in matching requests.
|
||||||
|
* Authentication support -- HTTP "Basic", "Digest" and "SPNEGO" authentications are supported, others are pluggable.
|
||||||
|
* Forward proxy support -- HTTP proxying and SOCKS4 proxying.
|
||||||
|
|
||||||
|
[[start]]
|
||||||
|
== Starting HttpClient
|
||||||
|
|
||||||
|
The Jetty artifact that provides the main HTTP client implementation is `jetty-client`.
|
||||||
|
The Maven artifact coordinates are the following:
|
||||||
|
|
||||||
|
[,xml,subs=normal]
|
||||||
|
----
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-client</artifactId>
|
||||||
|
<version>{version}</version>
|
||||||
|
</dependency>
|
||||||
|
----
|
||||||
|
|
||||||
|
The main class is named `org.eclipse.jetty.client.HttpClient`.
|
||||||
|
|
||||||
|
You can think of a `HttpClient` instance as a browser instance.
|
||||||
|
Like a browser it can make requests to different domains, it manages redirects, cookies and authentication, you can configure it with a proxy, and it provides you with the responses to the requests you make.
|
||||||
|
|
||||||
|
In order to use `HttpClient`, you must instantiate it, configure it, and then start it:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=start]
|
||||||
|
----
|
||||||
|
|
||||||
|
You may create multiple instances of `HttpClient`, but typically one instance is enough for an application.
|
||||||
|
There are several reasons for having multiple `HttpClient` instances including, but not limited to:
|
||||||
|
|
||||||
|
* You want to specify different configuration parameters (for example, one instance is configured with a forward proxy while another is not).
|
||||||
|
* You want the two instances to behave like two different browsers and hence have different cookies, different authentication credentials, etc.
|
||||||
|
* You want to use <<transport,different transports>>.
|
||||||
|
|
||||||
|
Like browsers, HTTPS requests are supported out-of-the-box (see <<configuration-tls,this section>> for the TLS configuration), as long as the server provides a valid certificate.
|
||||||
|
In case the server does not provide a valid certificate (or in case it is self-signed) you want to customize ``HttpClient``'s TLS configuration as described in <<configuration-tls,this section>>.
|
||||||
|
|
||||||
|
[[stop]]
|
||||||
|
== Stopping HttpClient
|
||||||
|
|
||||||
|
It is recommended that when your application stops, you also stop the `HttpClient` instance (or instances) that you are using.
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=stop]
|
||||||
|
----
|
||||||
|
|
||||||
|
Stopping `HttpClient` makes sure that the memory it holds (for example, authentication credentials, cookies, etc.) is released, and that the thread pool and scheduler are properly stopped allowing all threads used by `HttpClient` to exit.
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
You cannot call `HttpClient.stop()` from one of its own threads, as it would cause a deadlock.
|
||||||
|
It is recommended that you stop `HttpClient` from an unrelated thread, or from a newly allocated thread, for example:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=stopFromOtherThread]
|
||||||
|
----
|
||||||
|
====
|
||||||
|
|
||||||
|
[[arch]]
|
||||||
|
== HttpClient Architecture
|
||||||
|
|
||||||
|
A `HttpClient` instance can be thought as a browser instance, and it manages the following components:
|
||||||
|
|
||||||
|
* a `CookieStore` (see <<cookie,this section>>).
|
||||||
|
* a `AuthenticationStore` (see <<authentication,this section>>).
|
||||||
|
* a `ProxyConfiguration` (see <<proxy,this section>>).
|
||||||
|
* a set of _destinations_.
|
||||||
|
|
||||||
|
A _destination_ is the client-side component that represents an _origin_ server, and manages a queue of requests for that origin, and a <<connection-pool,pool of TCP connections>> to that origin.
|
||||||
|
|
||||||
|
An _origin_ may be simply thought as the tuple `(scheme, host, port)` and it is where the client connects to in order to communicate with the server.
|
||||||
|
However, this is not enough.
|
||||||
|
|
||||||
|
If you use `HttpClient` to write a proxy you may have different clients that want to contact the same server.
|
||||||
|
In this case, you may not want to use the same proxy-to-server connection to proxy requests for both clients, for example for authentication reasons: the server may associate the connection with authentication credentials and you do not want to use the same connection for two different users that have different credentials.
|
||||||
|
Instead, you want to use different connections for different clients and this can be achieved by "tagging" a destination with a tag object that represents the remote client (for example, it could be the remote client IP address).
|
||||||
|
|
||||||
|
Two origins with the same `(scheme, host, port)` but different `tag` create two different destinations and therefore two different connection pools.
|
||||||
|
However, also this is not enough.
|
||||||
|
|
||||||
|
It is possible for a server to speak different protocols on the same `port`.
|
||||||
|
A connection may start by speaking one protocol, for example HTTP/1.1, but then be upgraded to speak a different protocol, for example HTTP/2. After a connection has been upgraded to a second protocol, it cannot speak the first protocol anymore, so it can only be used to communicate using the second protocol.
|
||||||
|
|
||||||
|
Two origins with the same `(scheme, host, port)` but different `protocol` create two different destinations and therefore two different connection pools.
|
||||||
|
|
||||||
|
Therefore an origin is identified by the tuple `(scheme, host, port, tag, protocol)`.
|
||||||
|
|
||||||
|
[[connection-pool]]
|
||||||
|
== HttpClient Connection Pooling
|
||||||
|
|
||||||
|
A destination manages a `org.eclipse.jetty.client.ConnectionPool`, where connections to a particular origin are pooled for performance reasons:
|
||||||
|
opening a connection is a costly operation and it's better to reuse them for multiple requests.
|
||||||
|
|
||||||
|
NOTE: Remember that to select a specific destination you must select a specific origin, and that an origin is identified by the tuple `(scheme, host, port, tag, protocol)`, so you can have multiple destinations for the same `host` and `port`.
|
||||||
|
|
||||||
|
You can access the `ConnectionPool` in this way:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=getConnectionPool]
|
||||||
|
----
|
||||||
|
|
||||||
|
Jetty's client library provides the following `ConnectionPool` implementations:
|
||||||
|
|
||||||
|
* `DuplexConnectionPool`, historically the first implementation, only used by the HTTP/1.1 transport.
|
||||||
|
* `MultiplexConnectionPool`, the generic implementation valid for any transport where connections are reused with a MRU (most recently used) algorithm (that is, the connections most recently returned to the connection pool are the more likely to be used again).
|
||||||
|
* `RoundRobinConnectionPool`, similar to `MultiplexConnectionPool` but where connections are reused with a round-robin algorithm.
|
||||||
|
|
||||||
|
The `ConnectionPool` implementation can be customized for each destination in by setting a `ConnectionPool.Factory` on the `HttpClientTransport`:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=setConnectionPool]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[request-processing]]
|
||||||
|
== HttpClient Request Processing
|
||||||
|
|
||||||
|
[plantuml]
|
||||||
|
----
|
||||||
|
skinparam backgroundColor transparent
|
||||||
|
skinparam monochrome true
|
||||||
|
skinparam shadowing false
|
||||||
|
|
||||||
|
participant Application
|
||||||
|
participant Request
|
||||||
|
participant HttpClient
|
||||||
|
participant Destination
|
||||||
|
participant ConnectionPool
|
||||||
|
participant Connection
|
||||||
|
|
||||||
|
Application -> HttpClient : newRequest()
|
||||||
|
HttpClient -> Request **
|
||||||
|
Application -> Request : send()
|
||||||
|
Request -> HttpClient : send()
|
||||||
|
HttpClient -> Destination ** : get or create
|
||||||
|
Destination -> ConnectionPool ** : create
|
||||||
|
HttpClient -> Destination : send(Request)
|
||||||
|
Destination -> Destination : enqueue(Request)
|
||||||
|
Destination -> ConnectionPool : acquire()
|
||||||
|
ConnectionPool -> Connection ** : create
|
||||||
|
Destination -> Destination : dequeue(Request)
|
||||||
|
Destination -> Connection : send(Request)
|
||||||
|
----
|
||||||
|
|
||||||
|
When a request is sent, an origin is computed from the request; `HttpClient` uses that origin to find (or create if it does not exist) the correspondent destination.
|
||||||
|
The request is then queued onto the destination, and this causes the destination to ask its connection pool for a free connection.
|
||||||
|
If a connection is available, it is returned, otherwise a new connection is created.
|
||||||
|
Once the destination has obtained the connection, it dequeues the request and sends it over the connection.
|
||||||
|
|
||||||
|
The first request to a destination triggers the opening of the first connection.
|
||||||
|
A second request with the same origin sent _after_ the first request/response cycle is completed may reuse the same connection, depending on the connection pool implementation.
|
||||||
|
A second request with the same origin sent _concurrently_ with the first request will likely cause the opening of a second connection, depending on the connection pool implementation.
|
||||||
|
The configuration parameter `HttpClient.maxConnectionsPerDestination` (see also the <<configuration,configuration section>>) controls the max number of connections that can be opened for a destination.
|
||||||
|
|
||||||
|
NOTE: If opening connections to a given origin takes a long time, then requests for that origin will queue up in the corresponding destination until the connections are established.
|
||||||
|
|
||||||
|
Each connection can handle a limited number of concurrent requests.
|
||||||
|
For HTTP/1.1, this number is always `1`: there can only be one outstanding request for each connection.
|
||||||
|
For HTTP/2 this number is determined by the server `max_concurrent_stream` setting (typically around `100`, i.e. there can be up to `100` outstanding requests for every connection).
|
||||||
|
|
||||||
|
When a destination has maxed out its number of connections, and all connections have maxed out their number of outstanding requests, more requests sent to that destination will be queued.
|
||||||
|
When the request queue is full, the request will be failed.
|
||||||
|
The configuration parameter `HttpClient.maxRequestsQueuedPerDestination` (see also the <<configuration,configuration section>>) controls the max number of requests that can be queued for a destination.
|
||||||
|
|
||||||
|
[[api]]
|
||||||
|
== HttpClient API Usage
|
||||||
|
|
||||||
|
`HttpClient` provides two types of APIs: a blocking API and a non-blocking API.
|
||||||
|
|
||||||
|
[[blocking]]
|
||||||
|
=== HttpClient Blocking APIs
|
||||||
|
|
||||||
|
The simpler way to perform a HTTP request is the following:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=simpleBlockingGet]
|
||||||
|
----
|
||||||
|
|
||||||
|
The method `HttpClient.GET(...)` performs a HTTP `GET` request to the given URI and returns a `ContentResponse` when the request/response conversation completes successfully.
|
||||||
|
|
||||||
|
The `ContentResponse` object contains the HTTP response information: status code, headers and possibly content.
|
||||||
|
The content length is limited by default to 2 MiB; for larger content see <<content-response,the section on response content handling>>.
|
||||||
|
|
||||||
|
If you want to customize the request, for example by issuing a `HEAD` request instead of a `GET`, and simulating a browser user agent, you can do it in this way:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=headFluent]
|
||||||
|
----
|
||||||
|
|
||||||
|
This is a shorthand for:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=headNonFluent]
|
||||||
|
----
|
||||||
|
|
||||||
|
You first create a request object using `httpClient.newRequest(...)`, and then you customize it using the fluent API style (that is, a chained invocation of methods on the request object).
|
||||||
|
When the request object is customized, you call `request.send()` that produces the `ContentResponse` when the request/response conversation is complete.
|
||||||
|
|
||||||
|
IMPORTANT: The `Request` object, despite being mutable, cannot be reused for other requests.
|
||||||
|
This is true also when trying to send two or more identical requests: you have to create two or more `Request` objects.
|
||||||
|
|
||||||
|
Simple `POST` requests also have a shortcut method:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=postFluent]
|
||||||
|
----
|
||||||
|
|
||||||
|
The `POST` parameter values added via the `param()` method are automatically URL-encoded.
|
||||||
|
|
||||||
|
Jetty's `HttpClient` automatically follows redirects, so it handles the typical web pattern http://en.wikipedia.org/wiki/Post/Redirect/Get[POST/Redirect/GET], and the response object contains the content of the response of the `GET` request.
|
||||||
|
Following redirects is a feature that you can enable/disable on a per-request basis or globally.
|
||||||
|
|
||||||
|
File uploads also require one line, and make use of `java.nio.file` classes:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=fileFluent]
|
||||||
|
----
|
||||||
|
|
||||||
|
It is possible to impose a total timeout for the request/response conversation using the `Request.timeout(...)` method as follows:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=totalTimeout]
|
||||||
|
----
|
||||||
|
|
||||||
|
In the example above, when the 5 seconds expire, the request/response cycle is aborted and a `java.util.concurrent.TimeoutException` is thrown.
|
||||||
|
|
||||||
|
[[non-blocking]]
|
||||||
|
=== HttpClient Non-Blocking APIs
|
||||||
|
|
||||||
|
So far we have shown how to use Jetty HTTP client in a blocking style -- that is, the thread that issues the request blocks until the request/response conversation is complete.
|
||||||
|
|
||||||
|
This section will look at Jetty's `HttpClient` non-blocking, asynchronous APIs that are perfectly suited for large content downloads, for parallel processing of requests/responses and in cases where performance and efficient thread and resource utilization is a key factor.
|
||||||
|
|
||||||
|
The asynchronous APIs rely heavily on listeners that are invoked at various stages of request and response processing.
|
||||||
|
These listeners are implemented by applications and may perform any kind of logic.
|
||||||
|
The implementation invokes these listeners in the same thread that is used to process the request or response.
|
||||||
|
Therefore, if the application code in these listeners takes a long time to execute, the request or response processing is delayed until the listener returns.
|
||||||
|
|
||||||
|
If you need to execute application code that takes long time inside a listener, you must spawn your own thread.
|
||||||
|
|
||||||
|
Request and response processing are executed by two different threads and therefore may happen concurrently.
|
||||||
|
A typical example of this concurrent processing is an echo server, where a large upload may be concurrent with the large download echoed back.
|
||||||
|
|
||||||
|
NOTE: Remember that responses may be processed and completed _before_ requests; a typical example is a large upload that triggers a quick response, for example an error, by the server: the response may arrive and be completed while the request content is still being uploaded.
|
||||||
|
|
||||||
|
The application thread that calls `Request.send(Response.CompleteListener)` performs the <<request-processing,processing of the request>> until either the request is fully sent over the network or until it would block on I/O, then it returns (and therefore never blocks).
|
||||||
|
If it would block on I/O, the thread asks the I/O system to emit an event when the I/O will be ready to continue, then returns.
|
||||||
|
When such an event is fired, a thread taken from the `HttpClient` thread pool will resume the processing of the request.
|
||||||
|
|
||||||
|
Response are processed from the I/O thread taken from the `HttpClient` thread pool that processes the event that bytes are ready to be read.
|
||||||
|
Response processing continues until either the response is fully processed or until it would block for I/O.
|
||||||
|
If it would block for I/O, the thread asks the I/O system to emit an event when the I/O will be ready to continue, then returns.
|
||||||
|
When such an event is fired, a (possibly different) thread taken from the `HttpClient` thread pool will resume the processing of the response.
|
||||||
|
|
||||||
|
When the request and the response are both fully processed, the thread that finished the last processing (usually the thread that processes the response, but may also be the thread that processes the request -- if the request takes more time than the response to be processed) is used to dequeue the next request for the same destination and to process it.
|
||||||
|
|
||||||
|
A simple non-blocking `GET` request that discards the response content can be written in this way:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=simpleNonBlocking]
|
||||||
|
----
|
||||||
|
|
||||||
|
Method `Request.send(Response.CompleteListener)` returns `void` and does not block; the `Response.CompleteListener` lambda provided as a parameter is notified when the request/response conversation is complete, and the `Result` parameter allows you to access the request and response objects as well as failures, if any.
|
||||||
|
|
||||||
|
You can impose a total timeout for the request/response conversation in the same way used by the synchronous API:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=nonBlockingTotalTimeout]
|
||||||
|
----
|
||||||
|
|
||||||
|
The example above will impose a total timeout of 3 seconds on the request/response conversation.
|
||||||
|
|
||||||
|
The HTTP client APIs use listeners extensively to provide hooks for all possible request and response events:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=listeners]
|
||||||
|
----
|
||||||
|
|
||||||
|
This makes Jetty HTTP client suitable for HTTP load testing because, for example, you can accurately time every step of the request/response conversation (thus knowing where the request/response time is really spent).
|
||||||
|
|
||||||
|
Have a look at the link:{javadoc-url}/org/eclipse/jetty/client/api/Request.Listener.html[`Request.Listener`] class to know about request events, and to the link:{javadoc-url}/org/eclipse/jetty/client/api/Response.Listener.html[`Response.Listener`] class to know about response events.
|
||||||
|
|
||||||
|
[[content-request]]
|
||||||
|
=== Request Content Handling
|
||||||
|
|
||||||
|
Jetty's `HttpClient` provides a number of utility classes off the shelf to handle request content.
|
||||||
|
|
||||||
|
You can provide request content as `String`, `byte[]`, `ByteBuffer`, `java.nio.file.Path`, `InputStream`, and provide your own implementation of `org.eclipse.jetty.client.api.Request.Content`.
|
||||||
|
Here’s an example that provides the request content using `java.nio.file.Paths`:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=pathRequestContent]
|
||||||
|
----
|
||||||
|
|
||||||
|
Alternatively, you can use `FileInputStream` via the `InputStreamRequestContent` utility class:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=inputStreamRequestContent]
|
||||||
|
----
|
||||||
|
|
||||||
|
Since `InputStream` is blocking, then also the send of the request will block if the input stream blocks, even in case of usage of the non-blocking `HttpClient` APIs.
|
||||||
|
|
||||||
|
If you have already read the content in memory, you can pass it as a `byte[]` (or a `String`) using the `BytesRequestContent` (or `StringRequestContent`) utility class:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=bytesStringRequestContent]
|
||||||
|
----
|
||||||
|
|
||||||
|
If the request content is not immediately available, but your application will be notified of the content to send, you can use `AsyncRequestContent` in this way:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=asyncRequestContent]
|
||||||
|
----
|
||||||
|
|
||||||
|
While the request content is awaited and consequently uploaded by the client application, the server may be able to respond (at least with the response headers) completely asynchronously.
|
||||||
|
In this case, `Response.Listener` callbacks will be invoked before the request is fully sent.
|
||||||
|
This allows fine-grained control of the request/response conversation: for example the server may reject contents that are too big, send a response to the client, which in turn may stop the content upload.
|
||||||
|
|
||||||
|
Another way to provide request content is by using an `OutputStreamRequestContent`, which allows applications to write request content when it is available to the `OutputStream` provided by `OutputStreamRequestContent`:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=outputStreamRequestContent]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[content-response]]
|
||||||
|
=== Response Content Handling
|
||||||
|
|
||||||
|
Jetty's `HttpClient` allows applications to handle response content in different ways.
|
||||||
|
|
||||||
|
You can buffer the response content in memory; this is done when using the <<blocking,blocking APIs>> and the content is buffered within a `ContentResponse` up to 2 MiB.
|
||||||
|
|
||||||
|
If you want to control the length of the response content (for example limiting to values smaller than the default of 2 MiB), then you can use a `org.eclipse.jetty.client.util.FutureResponseListener` in this way:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=futureResponseListener]
|
||||||
|
----
|
||||||
|
|
||||||
|
If the response content length is exceeded, the response will be aborted, and an exception will be thrown by method `get(...)`.
|
||||||
|
|
||||||
|
You can buffer the response content in memory also using the <<non-blocking,non-blocking APIs>>, via the `BufferingResponseListener` utility class:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=bufferingResponseListener]
|
||||||
|
----
|
||||||
|
|
||||||
|
If you want to avoid buffering, you can wait for the response and then stream the content using the `InputStreamResponseListener` utility class:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=inputStreamResponseListener]
|
||||||
|
----
|
||||||
|
|
||||||
|
Finally, let's look at the advanced usage of the response content handling.
|
||||||
|
|
||||||
|
The response content is provided by the `HttpClient` implementation to application listeners following a reactive model similar to that of `java.util.concurrent.Flow`.
|
||||||
|
|
||||||
|
The listener that follows this model is `Response.DemandedContentListener`.
|
||||||
|
|
||||||
|
After the response headers have been processed by the `HttpClient` implementation, `Response.DemandedContentListener.onBeforeContent(response, demand)` is invoked.
|
||||||
|
This allows the application to control whether to demand the first content or not.
|
||||||
|
The default implementation of this method calls `demand.accept(1)`, which demands one chunk of content to the implementation.
|
||||||
|
The implementation will deliver the chunk of content as soon as it is available.
|
||||||
|
|
||||||
|
The chunks of content are delivered to the application by invoking `Response.DemandedContentListener.onContent(response, demand, buffer, callback)`.
|
||||||
|
Applications implement this method to process the content bytes in the `buffer`.
|
||||||
|
Succeeding the `callback` signals to the implementation that the application has consumed the `buffer` so that the implementation can dispose/recycle the `buffer`.
|
||||||
|
Failing the `callback` signals to the implementation to fail the response (no more content will be delivered, and the _response failed_ event will be emitted).
|
||||||
|
|
||||||
|
IMPORTANT: Succeeding the `callback` must be done only after the `buffer` bytes have been consumed.
|
||||||
|
When the `callback` is succeeded, the `HttpClient` implementation may reuse the `buffer` and overwrite the bytes with different bytes; if the application looks at the `buffer` _after_ having succeeded the `callback` is may see other, unrelated, bytes.
|
||||||
|
|
||||||
|
The application uses the `demand` object to demand more content chunks.
|
||||||
|
Applications will typically demand for just one more content via `demand.accept(1)`, but may decide to demand for more via `demand.accept(2)` or demand "infinitely" once via `demand.accept(Long.MAX_VALUE)`.
|
||||||
|
Applications that demand for more than 1 chunk of content must be prepared to receive all the content that they have demanded.
|
||||||
|
|
||||||
|
Demanding for content and consuming the content are orthogonal activities.
|
||||||
|
|
||||||
|
An application can demand "infinitely" and store aside the pairs `(buffer, callback)` to consume them later.
|
||||||
|
If not done carefully, this may lead to excessive memory consumption, since the ``buffer``s are not consumed.
|
||||||
|
Succeeding the ``callback``s will result in the ``buffer``s to be disposed/recycled and may be performed at any time.
|
||||||
|
|
||||||
|
An application can also demand one chunk of content, consume it (by succeeding the associated `callback`) and then _not_ demand for more content until a later time.
|
||||||
|
|
||||||
|
Subclass `Response.AsyncContentListener` overrides the behavior of `Response.DemandedContentListener`; when an application implementing its `onContent(response, buffer, callback)` succeeds the `callback`, it will have _both_ the effect of disposing/recycling the `buffer` _and_ the effect of demanding one more chunk of content.
|
||||||
|
|
||||||
|
Subclass `Response.ContentListener` overrides the behavior of `Response.AsyncContentListener`; when an application implementing its `onContent(response, buffer)` returns from the method itself, it will _both_ the effect of disposing/recycling the `buffer` _and_ the effect of demanding one more chunk of content.
|
||||||
|
|
||||||
|
Previous examples of response content handling were inefficient because they involved copying the `buffer` bytes, either to accumulate them aside so that the application could use them when the request was completed, or because they were provided to an API such as `InputStream` that made use of `byte[]` (and therefore a copy from `ByteBuffer` to `byte[]` is necessary).
|
||||||
|
|
||||||
|
An application that implements a forwarder between two servers can be implemented efficiently by handling the response content without copying the `buffer` bytes as in the following example:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=demandedContentListener]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[configuration]]
|
||||||
|
== HttpClient Configuration
|
||||||
|
|
||||||
|
`HttpClient` has a quite large number of configuration parameters.
|
||||||
|
Please refer to the `HttpClient` link:{javadoc-url}/org/eclipse/jetty/client/HttpClient.html[javadocs] for the complete list of configurable parameters.
|
||||||
|
|
||||||
|
The most common parameters are:
|
||||||
|
|
||||||
|
* `HttpClient.idleTimeout`: same as `ClientConnector.idleTimeout` described in xref:client/io-arch.adoc#network[this section].
|
||||||
|
* `HttpClient.connectBlocking`: same as `ClientConnector.connectBlocking` described in xref:client/io-arch.adoc#network[this section].
|
||||||
|
* `HttpClient.connectTimeout`: same as `ClientConnector.connectTimeout` described in xref:client/io-arch.adoc#network[this section].
|
||||||
|
* `HttpClient.maxConnectionsPerDestination`: the max number of TCP connections that are opened for a particular destination (defaults to 64).
|
||||||
|
* `HttpClient.maxRequestsQueuedPerDestination`: the max number of requests queued (defaults to 1024).
|
||||||
|
|
||||||
|
[[configuration-tls]]
|
||||||
|
=== HttpClient TLS Configuration
|
||||||
|
|
||||||
|
`HttpClient` supports HTTPS requests out-of-the-box like a browser does.
|
||||||
|
|
||||||
|
The support for HTTPS request is provided by a `SslContextFactory.Client` instance, typically configured in the `ClientConnector`.
|
||||||
|
If not explicitly configured, the `ClientConnector` will allocate a default one when started.
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=tlsExplicit]
|
||||||
|
----
|
||||||
|
|
||||||
|
The default `SslContextFactory.Client` verifies the certificate sent by the server by verifying the validity of the certificate with respect to the certificate chain, the expiration date, the server host name, etc.
|
||||||
|
This means that requests to public websites that have a valid certificate (such as `+https://google.com+`) will work out-of-the-box, without the need to specify a KeyStore or a TrustStore.
|
||||||
|
|
||||||
|
However, requests made to sites that return an invalid or a self-signed certificate will fail (like they will in a browser).
|
||||||
|
An invalid certificate may be expired or have the wrong server host name; a self-signed certificate has a certificate chain that cannot be verified.
|
||||||
|
|
||||||
|
The validation of the server host name present in the certificate is important, to guarantee that the client is connected indeed with the intended server.
|
||||||
|
|
||||||
|
The validation of the server host name is performed at two levels: at the TLS level (in the JDK) and, optionally, at the application level.
|
||||||
|
|
||||||
|
By default, the validation of the server host name at the TLS level is enabled, while it is disabled at the application level.
|
||||||
|
|
||||||
|
You can configure the `SslContextFactory.Client` to skip the validation of the server host name at the TLS level:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=tlsNoValidation]
|
||||||
|
----
|
||||||
|
|
||||||
|
When you disable the validation of the server host name at the TLS level, you are strongly recommended to enable it at the application level, otherwise you may risk to connect to a server different from the one you intend to connect to:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tags=tlsAppValidation]
|
||||||
|
----
|
||||||
|
|
||||||
|
You may have the validation of the server host name enabled at both the TLS level and application level, typically when you want to further restrict the client to connect only to a smaller set of server hosts than those allowed in the certificate sent by the server.
|
||||||
|
|
||||||
|
Please refer to the `SslContextFactory.Client` link:{javadoc-url}/org/eclipse/jetty/util/ssl/SslContextFactory.Client.html[javadocs] for the complete list of configurable parameters.
|
||||||
|
|
||||||
|
[[configuration-tls-truststore]]
|
||||||
|
==== HttpClient TLS TrustStore Configuration
|
||||||
|
TODO
|
||||||
|
|
||||||
|
[[configuration-tls-client-certs]]
|
||||||
|
==== HttpClient TLS Client Certificates Configuration
|
||||||
|
TODO
|
||||||
|
|
||||||
|
[[cookie]]
|
||||||
|
== HttpClient Cookie Support
|
||||||
|
|
||||||
|
Jetty's `HttpClient` supports cookies out of the box.
|
||||||
|
|
||||||
|
The `HttpClient` instance receives cookies from HTTP responses and stores them in a `java.net.CookieStore`, a class that is part of the JDK.
|
||||||
|
When new requests are made, the cookie store is consulted and if there are matching cookies (that is, cookies that are not expired and that match domain and path of the request) then they are added to the requests.
|
||||||
|
|
||||||
|
Applications can programmatically access the cookie store to find the cookies that have been set:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=getCookies]
|
||||||
|
----
|
||||||
|
|
||||||
|
Applications can also programmatically set cookies as if they were returned from a HTTP response:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=setCookie]
|
||||||
|
----
|
||||||
|
|
||||||
|
Cookies may be added explicitly only for a particular request:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=requestCookie]
|
||||||
|
----
|
||||||
|
|
||||||
|
You can remove cookies that you do not want to be sent in future HTTP requests:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=removeCookie]
|
||||||
|
----
|
||||||
|
|
||||||
|
If you want to totally disable cookie handling, you can install a `HttpCookieStore.Empty`.
|
||||||
|
This must be done when `HttpClient` is used in a proxy application, in this way:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=emptyCookieStore]
|
||||||
|
----
|
||||||
|
|
||||||
|
You can enable cookie filtering by installing a cookie store that performs the filtering logic in this way:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=filteringCookieStore]
|
||||||
|
----
|
||||||
|
|
||||||
|
The example above will retain only cookies that come from the `google.com` domain or sub-domains.
|
||||||
|
|
||||||
|
// TODO: move this section to server-side
|
||||||
|
=== Special Characters in Cookies
|
||||||
|
|
||||||
|
Jetty is compliant with https://tools.ietf.org/html/rfc6265[RFC6265], and as such care must be taken when setting a cookie value that includes special characters such as `;`.
|
||||||
|
|
||||||
|
Previously, `Version=1` cookies defined in https://tools.ietf.org/html/rfc2109[RFC2109] (and continued in https://tools.ietf.org/html/rfc2965[RFC2965]) allowed for special/reserved characters to be enclosed within double quotes when declared in a `Set-Cookie` response header:
|
||||||
|
|
||||||
|
[,screen]
|
||||||
|
----
|
||||||
|
Set-Cookie: foo="bar;baz";Version=1;Path="/secur"
|
||||||
|
----
|
||||||
|
|
||||||
|
This was added to the HTTP Response as follows:
|
||||||
|
|
||||||
|
[,java]
|
||||||
|
----
|
||||||
|
protected void service(HttpServletRequest request, HttpServletResponse response)
|
||||||
|
{
|
||||||
|
javax.servlet.http.Cookie cookie = new Cookie("foo", "bar;baz");
|
||||||
|
cookie.setPath("/secure");
|
||||||
|
response.addCookie(cookie);
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|
||||||
|
The introduction of RFC6265 has rendered this approach no longer possible; users are now required to encode cookie values that use these special characters.
|
||||||
|
This can be done utilizing `javax.servlet.http.Cookie` as follows:
|
||||||
|
|
||||||
|
[,java]
|
||||||
|
----
|
||||||
|
javax.servlet.http.Cookie cookie = new Cookie("foo", URLEncoder.encode("bar;baz", "UTF-8"));
|
||||||
|
----
|
||||||
|
|
||||||
|
Jetty validates all cookie names and values being added to the `HttpServletResponse` via the `addCookie(Cookie)` method.
|
||||||
|
If an illegal value is discovered Jetty will throw an `IllegalArgumentException` with the details.
|
||||||
|
|
||||||
|
[[authentication]]
|
||||||
|
== HttpClient Authentication Support
|
||||||
|
|
||||||
|
Jetty's `HttpClient` supports the `BASIC` and `DIGEST` authentication mechanisms defined by https://tools.ietf.org/html/rfc7235[RFC 7235], as well as the SPNEGO authentication mechanism defined in https://tools.ietf.org/html/rfc4559[RFC 4559].
|
||||||
|
|
||||||
|
The HTTP _conversation_, the sequence of related HTTP requests, for a request that needs authentication is the following:
|
||||||
|
|
||||||
|
[plantuml]
|
||||||
|
----
|
||||||
|
skinparam backgroundColor transparent
|
||||||
|
skinparam monochrome true
|
||||||
|
skinparam shadowing false
|
||||||
|
|
||||||
|
participant Application
|
||||||
|
participant HttpClient
|
||||||
|
participant Server
|
||||||
|
|
||||||
|
Application -> Server : GET /path
|
||||||
|
Server -> HttpClient : 401 + WWW-Authenticate
|
||||||
|
HttpClient -> Server : GET + Authentication
|
||||||
|
Server -> Application : 200 OK
|
||||||
|
----
|
||||||
|
|
||||||
|
Upon receiving a HTTP 401 response code, `HttpClient` looks at the `WWW-Authenticate` response header (the server _challenge_) and then tries to match configured authentication credentials to produce an `Authentication` header that contains the authentication credentials to access the resource.
|
||||||
|
|
||||||
|
You can configure authentication credentials in the `HttpClient` instance as follows:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=addAuthentication]
|
||||||
|
----
|
||||||
|
|
||||||
|
``Authentication``s are matched against the server challenge first by mechanism (e.g. `BASIC` or `DIGEST`), then by realm and then by URI.
|
||||||
|
|
||||||
|
If an `Authentication` match is found, the application does not receive events related to the HTTP 401 response.
|
||||||
|
These events are handled internally by `HttpClient` which produces another (internal) request similar to the original request but with an additional `Authorization` header.
|
||||||
|
|
||||||
|
If the authentication is successful, the server responds with a HTTP 200 and `HttpClient` caches the `Authentication.Result` so that subsequent requests for a matching URI will not incur in the additional rountrip caused by the HTTP 401 response.
|
||||||
|
|
||||||
|
It is possible to clear ``Authentication.Result``s in order to force authentication again:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=clearResults]
|
||||||
|
----
|
||||||
|
|
||||||
|
Authentication results may be preempted to avoid the additional roundtrip due to the server challenge in this way:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=preemptedResult]
|
||||||
|
----
|
||||||
|
|
||||||
|
In this way, requests for the given URI are enriched immediately with the `Authorization` header, and the server should respond with HTTP 200 (and the resource content) rather than with the 401 and the challenge.
|
||||||
|
|
||||||
|
It is also possible to preempt the authentication for a single request only, in this way:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=requestPreemptedResult]
|
||||||
|
----
|
||||||
|
|
||||||
|
See also the <<proxy-authentication,proxy authentication section>> for further information about how authentication works with HTTP proxies.
|
||||||
|
|
||||||
|
[[authentication-spnego]]
|
||||||
|
=== HttpClient SPNEGO Authentication Support
|
||||||
|
TODO
|
||||||
|
|
||||||
|
[[proxy]]
|
||||||
|
== HttpClient Proxy Support
|
||||||
|
|
||||||
|
Jetty's `HttpClient` can be configured to use proxies to connect to destinations.
|
||||||
|
|
||||||
|
These types of proxies are available out of the box:
|
||||||
|
|
||||||
|
* HTTP proxy (provided by class `org.eclipse.jetty.client.HttpProxy`)
|
||||||
|
* SOCKS 4 proxy (provided by class `org.eclipse.jetty.client.Socks4Proxy`)
|
||||||
|
* <<proxy-socks5,SOCKS 5 proxy>> (provided by class `org.eclipse.jetty.client.Socks5Proxy`)
|
||||||
|
|
||||||
|
Other implementations may be written by subclassing `ProxyConfiguration.Proxy`.
|
||||||
|
|
||||||
|
The following is a typical configuration:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=proxy]
|
||||||
|
----
|
||||||
|
|
||||||
|
You specify the proxy host and proxy port, and optionally also the addresses that you do not want to be proxied, and then add the proxy configuration on the `ProxyConfiguration` instance.
|
||||||
|
|
||||||
|
Configured in this way, `HttpClient` makes requests to the HTTP proxy (for plain-text HTTP requests) or establishes a tunnel via HTTP `CONNECT` (for encrypted HTTPS requests).
|
||||||
|
|
||||||
|
Proxying is supported for any version of the HTTP protocol.
|
||||||
|
|
||||||
|
[[proxy-socks5]]
|
||||||
|
=== SOCKS5 Proxy Support
|
||||||
|
|
||||||
|
SOCKS 5 (defined in https://datatracker.ietf.org/doc/html/rfc1928[RFC 1928]) offers choices for authentication methods and supports IPv6 (things that SOCKS 4 does not support).
|
||||||
|
|
||||||
|
A typical SOCKS 5 proxy configuration with the username/password authentication method is the following:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=proxySocks5]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[proxy-authentication]]
|
||||||
|
=== HTTP Proxy Authentication Support
|
||||||
|
|
||||||
|
Jetty's `HttpClient` supports HTTP proxy authentication in the same way it supports <<authentication,server authentication>>.
|
||||||
|
|
||||||
|
In the example below, the HTTP proxy requires `BASIC` authentication, but the server requires `DIGEST` authentication, and therefore:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=proxyAuthentication]
|
||||||
|
----
|
||||||
|
|
||||||
|
The HTTP conversation for successful authentications on both the proxy and the server is the following:
|
||||||
|
|
||||||
|
[plantuml]
|
||||||
|
----
|
||||||
|
skinparam backgroundColor transparent
|
||||||
|
skinparam monochrome true
|
||||||
|
skinparam shadowing false
|
||||||
|
|
||||||
|
participant Application
|
||||||
|
participant HttpClient
|
||||||
|
participant Proxy
|
||||||
|
participant Server
|
||||||
|
|
||||||
|
Application -> Proxy : GET /path
|
||||||
|
Proxy -> HttpClient : 407 + Proxy-Authenticate
|
||||||
|
HttpClient -> Proxy : GET /path + Proxy-Authorization
|
||||||
|
Proxy -> Server : GET /path
|
||||||
|
Server -> Proxy : 401 + WWW-Authenticate
|
||||||
|
Proxy -> HttpClient : 401 + WWW-Authenticate
|
||||||
|
HttpClient -> Proxy : GET /path + Proxy-Authorization + Authorization
|
||||||
|
Proxy -> Server : GET /path + Authorization
|
||||||
|
Server -> Proxy : 200 OK
|
||||||
|
Proxy -> HttpClient : 200 OK
|
||||||
|
HttpClient -> Application : 200 OK
|
||||||
|
----
|
||||||
|
|
||||||
|
The application does not receive events related to the responses with code 407 and 401 since they are handled internally by `HttpClient`.
|
||||||
|
|
||||||
|
Similarly to the <<authentication,authentication section>>, the proxy authentication result and the server authentication result can be preempted to avoid, respectively, the 407 and 401 roundtrips.
|
||||||
|
|
||||||
|
[[transport]]
|
||||||
|
== HttpClient Pluggable Transports
|
||||||
|
|
||||||
|
Jetty's `HttpClient` can be configured to use different transport protocols to carry the semantic of HTTP requests and responses.
|
||||||
|
|
||||||
|
This means that the intention of a client to request resource `/index.html` using the `GET` method can be carried over the network in different formats.
|
||||||
|
|
||||||
|
An `HttpClient` transport is the component that is in charge of converting a high-level, semantic, HTTP requests such as " ``GET`` resource ``/index.html`` " into the specific format understood by the server (for example, HTTP/2 or HTTP/3), and to convert the server response from the specific format (HTTP/2 or HTTP/3) into high-level, semantic objects that can be used by applications.
|
||||||
|
|
||||||
|
The most common protocol format is HTTP/1.1, a textual protocol with lines separated by `\r\n`:
|
||||||
|
|
||||||
|
[,screen]
|
||||||
|
----
|
||||||
|
GET /index.html HTTP/1.1\r\n
|
||||||
|
Host: domain.com\r\n
|
||||||
|
...
|
||||||
|
\r\n
|
||||||
|
----
|
||||||
|
|
||||||
|
However, the same request can be made using FastCGI, a binary protocol:
|
||||||
|
|
||||||
|
[,screen]
|
||||||
|
----
|
||||||
|
x01 x01 x00 x01 x00 x08 x00 x00
|
||||||
|
x00 x01 x01 x00 x00 x00 x00 x00
|
||||||
|
x01 x04 x00 x01 xLL xLL x00 x00
|
||||||
|
x0C x0B D O C U M E
|
||||||
|
N T _ U R I / i
|
||||||
|
n d e x . h t m
|
||||||
|
l
|
||||||
|
...
|
||||||
|
----
|
||||||
|
|
||||||
|
Similarly, HTTP/2 is a binary protocol that transports the same information in a yet different format via TCP, while HTTP/3 is a binary protocol that transports the same information in yet another format via UDP.
|
||||||
|
|
||||||
|
A protocol may be _negotiated_ between client and server.
|
||||||
|
A request for a resource may be sent using one protocol (for example, HTTP/1.1), but the response may arrive in a different protocol (for example, HTTP/2).
|
||||||
|
|
||||||
|
`HttpClient` supports these static transports, each speaking only one protocol:
|
||||||
|
|
||||||
|
* <<transport-http11,HTTP/1.1>> (both clear-text and TLS encrypted)
|
||||||
|
* <<transport-http2,HTTP/2>> (both clear-text and TLS encrypted)
|
||||||
|
* <<transport-http3,HTTP/3>> (only encrypted via QUIC+TLS)
|
||||||
|
* <<transport-fcgi,FastCGI>> (both clear-text and TLS encrypted)
|
||||||
|
|
||||||
|
`HttpClient` also supports one <<transport-dynamic,dynamic transport>>, that can speak different protocols and can select the right protocol by negotiating it with the server or by explicit indication from applications.
|
||||||
|
|
||||||
|
Furthermore, every transport protocol can be sent either over the network or via Unix-Domain sockets.
|
||||||
|
Supports for Unix-Domain sockets requires Java 16 or later, since Unix-Domain sockets support has been introduced in OpenJDK with https://openjdk.java.net/jeps/380[JEP 380].
|
||||||
|
|
||||||
|
Applications are typically not aware of the actual protocol being used.
|
||||||
|
This allows them to write their logic against a high-level API that hides the details of the specific protocol being used over the network.
|
||||||
|
|
||||||
|
[[transport-http11]]
|
||||||
|
=== HTTP/1.1 Transport
|
||||||
|
|
||||||
|
HTTP/1.1 is the default transport.
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=defaultTransport]
|
||||||
|
----
|
||||||
|
|
||||||
|
If you want to customize the HTTP/1.1 transport, you can explicitly configure it in this way:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=http11Transport]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[transport-http2]]
|
||||||
|
=== HTTP/2 Transport
|
||||||
|
|
||||||
|
The HTTP/2 transport can be configured in this way:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=http2Transport]
|
||||||
|
----
|
||||||
|
|
||||||
|
`HTTP2Client` is the lower-level client that provides an API based on HTTP/2 concepts such as _sessions_, _streams_ and _frames_ that are specific to HTTP/2. See xref:client/http2.adoc[the HTTP/2 client section] for more information.
|
||||||
|
|
||||||
|
`HttpClientTransportOverHTTP2` uses `HTTP2Client` to format high-level semantic HTTP requests (like "GET resource /index.html") into the HTTP/2 specific format.
|
||||||
|
|
||||||
|
[[transport-http3]]
|
||||||
|
=== HTTP/3 Transport
|
||||||
|
|
||||||
|
The HTTP/3 transport can be configured in this way:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=http3Transport]
|
||||||
|
----
|
||||||
|
|
||||||
|
`HTTP3Client` is the lower-level client that provides an API based on HTTP/3 concepts such as _sessions_, _streams_ and _frames_ that are specific to HTTP/3. See xref:client/http3.adoc[the HTTP/3 client section] for more information.
|
||||||
|
|
||||||
|
`HttpClientTransportOverHTTP3` uses `HTTP3Client` to format high-level semantic HTTP requests (like "GET resource /index.html") into the HTTP/3 specific format.
|
||||||
|
|
||||||
|
[[transport-fcgi]]
|
||||||
|
=== FastCGI Transport
|
||||||
|
|
||||||
|
The FastCGI transport can be configured in this way:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=fcgiTransport]
|
||||||
|
----
|
||||||
|
|
||||||
|
In order to make requests using the FastCGI transport, you need to have a FastCGI server such as https://en.wikipedia.org/wiki/PHP#PHPFPM[PHP-FPM] (see also link:http://php.net/manual/en/install.fpm.php).
|
||||||
|
|
||||||
|
The FastCGI transport is primarily used by Jetty's xref:server/fastcgi.adoc[FastCGI support] to serve PHP pages (WordPress for example).
|
||||||
|
|
||||||
|
[[transport-dynamic]]
|
||||||
|
=== Dynamic Transport
|
||||||
|
|
||||||
|
The static transports work well if you know in advance the protocol you want to speak with the server, or if the server only supports one protocol (such as FastCGI).
|
||||||
|
|
||||||
|
With the advent of HTTP/2 and HTTP/3, however, servers are now able to support multiple protocols, at least both HTTP/1.1 and HTTP/2.
|
||||||
|
|
||||||
|
The HTTP/2 protocol is typically negotiated between client and server.
|
||||||
|
This negotiation can happen via ALPN, a TLS extension that allows the client to tell the server the list of protocol that the client supports, so that the server can pick one of the client supported protocols that also the server supports; or via HTTP/1.1 upgrade by means of the `Upgrade` header.
|
||||||
|
|
||||||
|
Applications can configure the dynamic transport with one or more _application_ protocols such as HTTP/1.1 or HTTP/2. The implementation will take care of using TLS for HTTPS URIs, using ALPN if necessary, negotiating protocols, upgrading from one protocol to another, etc.
|
||||||
|
|
||||||
|
By default, the dynamic transport only speaks HTTP/1.1:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=dynamicDefault]
|
||||||
|
----
|
||||||
|
|
||||||
|
The dynamic transport can be configured with just one protocol, making it equivalent to the corresponding static transport:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=dynamicOneProtocol]
|
||||||
|
----
|
||||||
|
|
||||||
|
The dynamic transport, however, has been implemented to support multiple transports, in particular both HTTP/1.1 and HTTP/2:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=dynamicH1H2]
|
||||||
|
----
|
||||||
|
|
||||||
|
NOTE: The order in which the protocols are specified to `HttpClientTransportDynamic` indicates what is the client preference.
|
||||||
|
|
||||||
|
IMPORTANT: When using TLS (i.e. URIs with the `https` scheme), the application protocol is _negotiated_ between client and server via ALPN, and it is the server that decides what is the application protocol to use for the communication, regardless of the client preference.
|
||||||
|
|
||||||
|
When clear-text communication is used (i.e. URIs with the `http` scheme) there is no application protocol negotiation, and therefore the application must know _a priori_ whether the server supports the protocol or not.
|
||||||
|
For example, if the server only supports clear-text HTTP/2, and `HttpClientTransportDynamic` is configured as in the example above, the client will send, by default, a clear-text HTTP/1.1 request to a clear-text HTTP/2 only server, which will result in a communication failure.
|
||||||
|
|
||||||
|
Provided that the server supports both HTTP/1.1 and HTTP/2 clear-text, client applications can explicitly hint the version they want to use:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=dynamicClearText]
|
||||||
|
----
|
||||||
|
|
||||||
|
In case of TLS encrypted communication using the `https` scheme, things are a little more complicated.
|
||||||
|
|
||||||
|
If the client application explicitly specifies the HTTP version, then ALPN is not used by the client.
|
||||||
|
By specifying the HTTP version explicitly, the client application has prior-knowledge of what HTTP version the server supports, and therefore ALPN is not needed.
|
||||||
|
If the server does not support the HTTP version chosen by the client, then the communication will fail.
|
||||||
|
|
||||||
|
If the client application does not explicitly specify the HTTP version, then ALPN will be used by the client.
|
||||||
|
If the server also supports ALPN, then the protocol will be negotiated via ALPN and the server will choose the protocol to use.
|
||||||
|
If the server does not support ALPN, the client will try to use the first protocol configured in `HttpClientTransportDynamic`, and the communication may succeed or fail depending on whether the server supports the protocol chosen by the client.
|
||||||
|
|
||||||
|
[[transport-unix-domain]]
|
||||||
|
=== Unix-Domain Configuration
|
||||||
|
|
||||||
|
All the transports can be configured with a `ClientConnector`, the component that is responsible for the transmission of the bytes generated by the transport to the server.
|
||||||
|
|
||||||
|
By default, `ClientConnector` uses TCP networking to send bytes to the server and receive bytes from the server.
|
||||||
|
|
||||||
|
When you are using Java 16 or later, `ClientConnector` also support xref:client/io-arch.adoc#unix-domain[Unix-Domain sockets], and every transport can be configured to use Unix-Domain sockets instead of TCP networking.
|
||||||
|
|
||||||
|
To configure Unix-Domain sockets, you can create a `ClientConnector` instance in the following way:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=unixDomain]
|
||||||
|
----
|
||||||
|
|
||||||
|
[IMPORTANT]
|
||||||
|
====
|
||||||
|
You can use Unix-Domain sockets support only when you run your client application with Java 16 or later.
|
||||||
|
====
|
||||||
|
|
||||||
|
You can configure a Jetty server to use Unix-Domain sockets, as explained in xref:server/http.adoc#connector[this section].
|
|
@ -0,0 +1,223 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= HTTP/2 Client Library
|
||||||
|
|
||||||
|
In the vast majority of cases, client applications should use the generic, high-level, xref:client/http.adoc[HTTP client library] that also provides HTTP/2 support via the pluggable xref:client/http.adoc#transport-http2[HTTP/2 transport] or the xref:client/http.adoc#transport-dynamic[dynamic transport].
|
||||||
|
|
||||||
|
The high-level HTTP library supports cookies, authentication, redirection, connection pooling and a number of other features that are absent in the low-level HTTP/2 library.
|
||||||
|
|
||||||
|
The HTTP/2 client library has been designed for those applications that need low-level access to HTTP/2 features such as _sessions_, _streams_ and _frames_, and this is quite a rare use case.
|
||||||
|
|
||||||
|
See also the correspondent xref:server/http2.adoc[HTTP/2 server library].
|
||||||
|
|
||||||
|
[[intro]]
|
||||||
|
== Introducing HTTP2Client
|
||||||
|
|
||||||
|
The Maven artifact coordinates for the HTTP/2 client library are the following:
|
||||||
|
|
||||||
|
[,xml,subs=normal]
|
||||||
|
----
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty.http2</groupId>
|
||||||
|
<artifactId>http2-client</artifactId>
|
||||||
|
<version>{version}</version>
|
||||||
|
</dependency>
|
||||||
|
----
|
||||||
|
|
||||||
|
The main class is named `org.eclipse.jetty.http2.client.HTTP2Client`, and must be created, configured and started before use:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http2/HTTP2ClientDocs.java[tags=start]
|
||||||
|
----
|
||||||
|
|
||||||
|
When your application stops, or otherwise does not need `HTTP2Client` anymore, it should stop the `HTTP2Client` instance (or instances) that were started:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http2/HTTP2ClientDocs.java[tags=stop]
|
||||||
|
----
|
||||||
|
|
||||||
|
`HTTP2Client` allows client applications to connect to an HTTP/2 server.
|
||||||
|
A _session_ represents a single TCP connection to an HTTP/2 server and is defined by class `org.eclipse.jetty.http2.api.Session`.
|
||||||
|
A _session_ typically has a long life -- once the TCP connection is established, it remains open until it is not used anymore (and therefore it is closed by the idle timeout mechanism), until a fatal error occurs (for example, a network failure), or if one of the peers decides unilaterally to close the TCP connection.
|
||||||
|
|
||||||
|
HTTP/2 is a multiplexed protocol: it allows multiple HTTP/2 requests to be sent on the same TCP connection, or _session_.
|
||||||
|
Each request/response cycle is represented by a _stream_.
|
||||||
|
Therefore, a single _session_ manages multiple concurrent _streams_.
|
||||||
|
A _stream_ has typically a very short life compared to the _session_: a _stream_ only exists for the duration of the request/response cycle and then disappears.
|
||||||
|
|
||||||
|
[[flow-control]]
|
||||||
|
== HTTP/2 Flow Control
|
||||||
|
|
||||||
|
The HTTP/2 protocol is _flow controlled_ (see https://tools.ietf.org/html/rfc7540#section-5.2[the specification]).
|
||||||
|
This means that a sender and a receiver maintain a _flow control window_ that tracks the number of data bytes sent and received, respectively.
|
||||||
|
When a sender sends data bytes, it reduces its flow control window.
|
||||||
|
When a receiver receives data bytes, it also reduces its flow control window, and then passes the received data bytes to the application.
|
||||||
|
The application consumes the data bytes and tells back the receiver that it has consumed the data bytes.
|
||||||
|
The receiver then enlarges the flow control window, and arranges to send a message to the sender with the number of bytes consumed, so that the sender can enlarge its flow control window.
|
||||||
|
|
||||||
|
A sender can send data bytes up to its whole flow control window, then it must stop sending until it receives a message from the receiver that the data bytes have been consumed, which enlarges the flow control window, which allows the sender to send more data bytes.
|
||||||
|
|
||||||
|
HTTP/2 defines _two_ flow control windows: one for each _session_, and one for each _stream_.
|
||||||
|
Let's see with an example how they interact, assuming that in this example the session flow control window is 120 bytes and the stream flow control window is 100 bytes.
|
||||||
|
|
||||||
|
The sender opens a session, and then opens `stream_1` on that session, and sends `80` data bytes.
|
||||||
|
At this point the session flow control window is `40` bytes (`120 - 80`), and ``stream_1``'s flow control window is `20` bytes (`100 - 80`).
|
||||||
|
The sender now opens `stream_2` on the same session and sends `40` data bytes.
|
||||||
|
At this point, the session flow control window is `0` bytes (`40 - 40`), while ``stream_2``'s flow control window is `60` (`100 - 40`).
|
||||||
|
Since now the session flow control window is `0`, the sender cannot send more data bytes, neither on `stream_1` nor on `stream_2` despite both have their stream flow control windows greater than `0`.
|
||||||
|
|
||||||
|
The receiver consumes ``stream_2``'s `40` data bytes and sends a message to the sender with this information.
|
||||||
|
At this point, the session flow control window is `40` (`0 40`), ``stream_1``'s flow control window is still `20` and ``stream_2``'s flow control window is `100` (`60 40`).
|
||||||
|
If the sender opens `stream_3` and would like to send 50 data bytes, it would only be able to send `40` because that is the maximum allowed by the session flow control window at this point.
|
||||||
|
|
||||||
|
It is therefore very important that applications notify the fact that they have consumed data bytes as soon as possible, so that the implementation (the receiver) can send a message to the sender (in the form of a `WINDOW_UPDATE` frame) with the information to enlarge the flow control window, therefore reducing the possibility that sender stalls due to the flow control windows being reduced to `0`.
|
||||||
|
|
||||||
|
How a client application should handle HTTP/2 flow control is discussed in details in <<response,this section>>.
|
||||||
|
|
||||||
|
[[connect]]
|
||||||
|
== Connecting to the Server
|
||||||
|
|
||||||
|
The first thing an application should do is to connect to the server and obtain a `Session`.
|
||||||
|
The following example connects to the server on a clear-text port:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http2/HTTP2ClientDocs.java[tags=clearTextConnect]
|
||||||
|
----
|
||||||
|
|
||||||
|
The following example connects to the server on an encrypted port:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http2/HTTP2ClientDocs.java[tags=encryptedConnect]
|
||||||
|
----
|
||||||
|
|
||||||
|
IMPORTANT: Applications must know in advance whether they want to connect to a clear-text or encrypted port, and pass the `SslContextFactory` parameter accordingly to the `connect(...)` method.
|
||||||
|
|
||||||
|
[[configure]]
|
||||||
|
== Configuring the Session
|
||||||
|
|
||||||
|
The `connect(...)` method takes a `Session.Listener` parameter.
|
||||||
|
This listener's `onPreface(...)` method is invoked just before establishing the connection to the server to gather the client configuration to send to the server.
|
||||||
|
Client applications can override this method to change the default configuration:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http2/HTTP2ClientDocs.java[tags=configure]
|
||||||
|
----
|
||||||
|
|
||||||
|
The `Session.Listener` is notified of session events originated by the server such as receiving a `SETTINGS` frame from the server, or the server closing the connection, or the client timing out the connection due to idleness.
|
||||||
|
Please refer to the `Session.Listener` link:{javadoc-url}/org/eclipse/jetty/http2/api/Session.Listener.html[javadocs] for the complete list of events.
|
||||||
|
|
||||||
|
Once a `Session` has been established, the communication with the server happens by exchanging _frames_, as specified in the https://tools.ietf.org/html/rfc7540#section-4[HTTP/2 specification].
|
||||||
|
|
||||||
|
[[request]]
|
||||||
|
== Sending a Request
|
||||||
|
|
||||||
|
Sending an HTTP request to the server, and receiving a response, creates a _stream_ that encapsulates the exchange of HTTP/2 frames that compose the request and the response.
|
||||||
|
|
||||||
|
In order to send an HTTP request to the server, the client must send a `HEADERS` frame.
|
||||||
|
`HEADERS` frames carry the request method, the request URI and the request headers.
|
||||||
|
Sending the `HEADERS` frame opens the `Stream`:
|
||||||
|
|
||||||
|
[,java,indent=0,subs=normal]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http2/HTTP2ClientDocs.java[tags=newStream]
|
||||||
|
----
|
||||||
|
|
||||||
|
Note how `Session.newStream(...)` takes a `Stream.Listener` parameter.
|
||||||
|
This listener is notified of stream events originated by the server such as receiving `HEADERS` or `DATA` frames that are part of the response, discussed in more details in the <<response,section below>>.
|
||||||
|
Please refer to the `Stream.Listener` link:{javadoc-url}/org/eclipse/jetty/http2/api/Stream.Listener.html[javadocs] for the complete list of events.
|
||||||
|
|
||||||
|
HTTP requests may have content, which is sent using the `Stream` APIs:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http2/HTTP2ClientDocs.java[tags=newStreamWithData]
|
||||||
|
----
|
||||||
|
|
||||||
|
IMPORTANT: When sending two `DATA` frames consecutively, the second call to `Stream.data(...)` must be done only when the first is completed, or a `WritePendingException` will be thrown.
|
||||||
|
Use the `Callback` APIs or `CompletableFuture` APIs to ensure that the second `Stream.data(...)` call is performed when the first completed successfully.
|
||||||
|
|
||||||
|
[[response]]
|
||||||
|
== Receiving a Response
|
||||||
|
|
||||||
|
Response events are delivered to the `Stream.Listener` passed to `Session.newStream(...)`.
|
||||||
|
|
||||||
|
An HTTP response is typically composed of a `HEADERS` frame containing the HTTP status code and the response headers, and optionally one or more `DATA` frames containing the response content bytes.
|
||||||
|
|
||||||
|
The HTTP/2 protocol also supports response trailers (that is, headers that are sent after the response content) that also are sent using a `HEADERS` frame.
|
||||||
|
|
||||||
|
A client application can therefore receive the HTTP/2 frames sent by the server by implementing the relevant methods in `Stream.Listener`:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http2/HTTP2ClientDocs.java[tags=responseListener]
|
||||||
|
----
|
||||||
|
|
||||||
|
NOTE: Returning from the `onData(...)` method implicitly demands for more `DATA` frames (unless the one just delivered was the last).
|
||||||
|
Additional `DATA` frames may be delivered immediately if they are available or later, asynchronously, when they arrive.
|
||||||
|
|
||||||
|
Applications that consume the content buffer within `onData(...)` (for example, writing it to a file, or copying the bytes to another storage) should succeed the callback as soon as they have consumed the content buffer.
|
||||||
|
This allows the implementation to reuse the buffer, reducing the memory requirements needed to handle the content buffers.
|
||||||
|
|
||||||
|
Alternatively, a client application may store away _both_ the buffer and the callback to consume the buffer bytes later, or pass _both_ the buffer and the callback to another asynchronous API (this is typical in proxy applications).
|
||||||
|
|
||||||
|
IMPORTANT: Completing the `Callback` is very important not only to allow the implementation to reuse the buffer, but also tells the implementation to enlarge the stream and session flow control windows so that the sender will be able to send more `DATA` frames without stalling.
|
||||||
|
|
||||||
|
Applications can also precisely control _when_ to demand more `DATA` frames, by implementing the `onDataDemanded(...)` method instead of `onData(...)`:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/HTTP2Docs.java[tags=dataDemanded]
|
||||||
|
----
|
||||||
|
|
||||||
|
IMPORTANT: Applications that implement `onDataDemanded(...)` must remember to call `Stream.demand(...)`.
|
||||||
|
If they don't, the implementation will not deliver `DATA` frames and the application will stall threadlessly until an idle timeout fires to close the stream or the session.
|
||||||
|
|
||||||
|
[[reset]]
|
||||||
|
== Resetting a Request or Response
|
||||||
|
|
||||||
|
In HTTP/2, clients and servers have the ability to tell to the other peer that they are not interested anymore in either the request or the response, using a `RST_STREAM` frame.
|
||||||
|
|
||||||
|
The `HTTP2Client` APIs allow client applications to send and receive this "reset" frame:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http2/HTTP2ClientDocs.java[tags=reset]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[push]]
|
||||||
|
== Receiving HTTP/2 Pushes
|
||||||
|
|
||||||
|
HTTP/2 servers have the ability to push resources related to a primary resource.
|
||||||
|
When an HTTP/2 server pushes a resource, it sends to the client a `PUSH_PROMISE` frame that contains the request URI and headers that a client would use to request explicitly that resource.
|
||||||
|
|
||||||
|
Client applications can be configured to tell the server to never push resources, see <<configure,this section>>.
|
||||||
|
|
||||||
|
Client applications can listen to the push events, and act accordingly:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http2/HTTP2ClientDocs.java[tags=push]
|
||||||
|
----
|
||||||
|
|
||||||
|
If a client application does not want to handle a particular HTTP/2 push, it can just reset the pushed stream to tell the server to stop sending bytes for the pushed stream:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http2/HTTP2ClientDocs.java[tags=pushReset]
|
||||||
|
----
|
|
@ -0,0 +1,155 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= HTTP/3 Client Library
|
||||||
|
|
||||||
|
In the vast majority of cases, client applications should use the generic, high-level, xref:client/http.adoc[HTTP client library] that also provides HTTP/3 support via the pluggable xref:client/http.adoc#transport-http3[HTTP/3 transport] or the xref:client/http.adoc#transport-dynamic[dynamic transport].
|
||||||
|
|
||||||
|
The high-level HTTP library supports cookies, authentication, redirection, connection pooling and a number of other features that are absent in the low-level HTTP/3 library.
|
||||||
|
|
||||||
|
The HTTP/3 client library has been designed for those applications that need low-level access to HTTP/3 features such as _sessions_, _streams_ and _frames_, and this is quite a rare use case.
|
||||||
|
|
||||||
|
See also the correspondent xref:server/http3.adoc[HTTP/3 server library].
|
||||||
|
|
||||||
|
[[intro]]
|
||||||
|
== Introducing HTTP3Client
|
||||||
|
|
||||||
|
The Maven artifact coordinates for the HTTP/3 client library are the following:
|
||||||
|
|
||||||
|
[,xml,subs=normal]
|
||||||
|
----
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty.http3</groupId>
|
||||||
|
<artifactId>http3-client</artifactId>
|
||||||
|
<version>{version}</version>
|
||||||
|
</dependency>
|
||||||
|
----
|
||||||
|
|
||||||
|
The main class is named `org.eclipse.jetty.http3.client.HTTP3Client`, and must be created, configured and started before use:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http3/HTTP3ClientDocs.java[tags=start]
|
||||||
|
----
|
||||||
|
|
||||||
|
When your application stops, or otherwise does not need `HTTP3Client` anymore, it should stop the `HTTP3Client` instance (or instances) that were started:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http3/HTTP3ClientDocs.java[tags=stop]
|
||||||
|
----
|
||||||
|
|
||||||
|
`HTTP3Client` allows client applications to connect to an HTTP/3 server.
|
||||||
|
A _session_ represents a single connection to an HTTP/3 server and is defined by class `org.eclipse.jetty.http3.api.Session`.
|
||||||
|
A _session_ typically has a long life -- once the connection is established, it remains active until it is not used anymore (and therefore it is closed by the idle timeout mechanism), until a fatal error occurs (for example, a network failure), or if one of the peers decides unilaterally to close the connection.
|
||||||
|
|
||||||
|
HTTP/3 is a multiplexed protocol because it relies on the multiplexing capabilities of QUIC, the protocol based on UDP that transports HTTP/3 frames.
|
||||||
|
Thanks to multiplexing, multiple HTTP/3 requests are sent on the same QUIC connection, or _session_.
|
||||||
|
Each request/response cycle is represented by a _stream_.
|
||||||
|
Therefore, a single _session_ manages multiple concurrent _streams_.
|
||||||
|
A _stream_ has typically a very short life compared to the _session_: a _stream_ only exists for the duration of the request/response cycle and then disappears.
|
||||||
|
|
||||||
|
// TODO: flow control?
|
||||||
|
//[[pg-client-http3-flow-control]]
|
||||||
|
//==== HTTP/3 Flow Control
|
||||||
|
|
||||||
|
//include::../../http3.adoc[tag=flowControl]
|
||||||
|
|
||||||
|
//How a client application should handle HTTP/3 flow control is discussed in details in <<response,this section>>.
|
||||||
|
|
||||||
|
[[connect]]
|
||||||
|
== Connecting to the Server
|
||||||
|
|
||||||
|
The first thing an application should do is to connect to the server and obtain a `Session`.
|
||||||
|
The following example connects to the server:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http3/HTTP3ClientDocs.java[tags=connect]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[configure]]
|
||||||
|
== Configuring the Session
|
||||||
|
|
||||||
|
The `connect(...)` method takes a `Session.Client.Listener` parameter.
|
||||||
|
This listener's `onPreface(...)` method is invoked just before establishing the connection to the server to gather the client configuration to send to the server.
|
||||||
|
Client applications can override this method to change the default configuration:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http3/HTTP3ClientDocs.java[tags=configure]
|
||||||
|
----
|
||||||
|
|
||||||
|
The `Session.Client.Listener` is notified of session events originated by the server such as receiving a `SETTINGS` frame from the server, or the server closing the connection, or the client timing out the connection due to idleness.
|
||||||
|
Please refer to the `Session.Client.Listener` link:{javadoc-url}/org/eclipse/jetty/http3/api/Session.Client.Listener.html[javadocs] for the complete list of events.
|
||||||
|
|
||||||
|
Once a `Session` has been established, the communication with the server happens by exchanging _frames_.
|
||||||
|
|
||||||
|
[[request]]
|
||||||
|
== Sending a Request
|
||||||
|
|
||||||
|
Sending an HTTP request to the server, and receiving a response, creates a _stream_ that encapsulates the exchange of HTTP/3 frames that compose the request and the response.
|
||||||
|
|
||||||
|
In order to send an HTTP request to the server, the client must send a `HEADERS` frame.
|
||||||
|
`HEADERS` frames carry the request method, the request URI and the request headers.
|
||||||
|
Sending the `HEADERS` frame opens the `Stream`:
|
||||||
|
|
||||||
|
[,java,indent=0,subs=normal]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http3/HTTP3ClientDocs.java[tags=newStream]
|
||||||
|
----
|
||||||
|
|
||||||
|
Note how `Session.newRequest(...)` takes a `Stream.Client.Listener` parameter.
|
||||||
|
This listener is notified of stream events originated by the server such as receiving `HEADERS` or `DATA` frames that are part of the response, discussed in more details in the <<response,section below>>.
|
||||||
|
Please refer to the `Stream.Client.Listener` link:{javadoc-url}/org/eclipse/jetty/http3/api/Stream.Client.Listener.html[javadocs] for the complete list of events.
|
||||||
|
|
||||||
|
HTTP requests may have content, which is sent using the `Stream` APIs:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http3/HTTP3ClientDocs.java[tags=newStreamWithData]
|
||||||
|
----
|
||||||
|
|
||||||
|
IMPORTANT: When sending two `DATA` frames consecutively, the second call to `Stream.data(...)` must be done only when the first is completed, or a `WritePendingException` will be thrown.
|
||||||
|
Use the `CompletableFuture` APIs to ensure that the second `Stream.data(...)` call is performed when the first completed successfully.
|
||||||
|
|
||||||
|
[[response]]
|
||||||
|
== Receiving a Response
|
||||||
|
|
||||||
|
Response events are delivered to the `Stream.Client.Listener` passed to `Session.newRequest(...)`.
|
||||||
|
|
||||||
|
An HTTP response is typically composed of a `HEADERS` frame containing the HTTP status code and the response headers, and optionally one or more `DATA` frames containing the response content bytes.
|
||||||
|
|
||||||
|
The HTTP/3 protocol also supports response trailers (that is, headers that are sent after the response content) that also are sent using a `HEADERS` frame.
|
||||||
|
|
||||||
|
A client application can therefore receive the HTTP/3 frames sent by the server by implementing the relevant methods in `Stream.Client.Listener`:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http3/HTTP3ClientDocs.java[tags=responseListener]
|
||||||
|
----
|
||||||
|
|
||||||
|
// TODO: flow control?
|
||||||
|
//include::../../http3.adoc[tag=apiFlowControl]
|
||||||
|
|
||||||
|
[[reset]]
|
||||||
|
== Resetting a Request or Response
|
||||||
|
|
||||||
|
In HTTP/3, clients and servers have the ability to tell to the other peer that they are not interested anymore in either the request or the response, by resetting the stream.
|
||||||
|
|
||||||
|
The `HTTP3Client` APIs allow client applications to send and receive this "reset" event:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/http3/HTTP3ClientDocs.java[tags=reset]
|
||||||
|
----
|
|
@ -0,0 +1,30 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Client Libraries
|
||||||
|
|
||||||
|
The Eclipse Jetty Project provides client-side libraries that allow you to embed a client in your applications.
|
||||||
|
A typical example is a client application that needs to contact a third party service via HTTP (for example a REST service).
|
||||||
|
Another example is a proxy application that receives HTTP requests and forwards them as FCGI requests to a PHP application such as WordPress, or receives HTTP/1.1 requests and converts them to HTTP/2 or HTTP/3.
|
||||||
|
Yet another example is a client application that needs to receive events from a WebSocket server.
|
||||||
|
|
||||||
|
The client libraries are designed to be non-blocking and offer both synchronous and asynchronous APIs and come with many configuration options.
|
||||||
|
|
||||||
|
These are the available client libraries:
|
||||||
|
|
||||||
|
* xref:client/http.adoc[The High-Level HTTP Client Library] for HTTP/1.1, HTTP/2 and FastCGI
|
||||||
|
* xref:client/http2.adoc[The Low-Level HTTP/2 Client Library] for low-level HTTP/2
|
||||||
|
* xref:client/http3.adoc[The Low-Level HTTP/3 Client Library] for low-level HTTP/3
|
||||||
|
* xref:client/websocket.adoc[The WebSocket client library]
|
||||||
|
|
||||||
|
If you are interested in the low-level details of how the Eclipse Jetty client libraries work, or are interested in writing a custom protocol, look at the xref:client/io-arch.adoc[Client I/O Architecture].
|
|
@ -0,0 +1,148 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= I/O Architecture
|
||||||
|
|
||||||
|
The Jetty client libraries provide the basic components and APIs to implement a network client.
|
||||||
|
|
||||||
|
They build on the common xref:arch/io.adoc[Jetty I/O Architecture] and provide client specific concepts (such as establishing a connection to a server).
|
||||||
|
|
||||||
|
There are conceptually two layers that compose the Jetty client libraries:
|
||||||
|
|
||||||
|
. <<network,The network layer>>, that handles the low level I/O and deals with buffers, threads, etc.
|
||||||
|
. <<protocol,The protocol layer>>, that handles the parsing of bytes read from the network and the generation of bytes to write to the network.
|
||||||
|
|
||||||
|
[[network]]
|
||||||
|
== Network Layer
|
||||||
|
|
||||||
|
The Jetty client libraries use the common I/O design described in xref:arch/io.adoc[this section].
|
||||||
|
The main client-side component is the link:{javadoc-url}/org/eclipse/jetty/io/ClientConnector.html[`ClientConnector`].
|
||||||
|
|
||||||
|
The `ClientConnector` primarily wraps the link:{javadoc-url}/org/eclipse/jetty/io/SelectorManager.html[`SelectorManager`] and aggregates other four components:
|
||||||
|
|
||||||
|
* a thread pool (in form of an `java.util.concurrent.Executor`)
|
||||||
|
* a scheduler (in form of `org.eclipse.jetty.util.thread.Scheduler`)
|
||||||
|
* a byte buffer pool (in form of `org.eclipse.jetty.io.ByteBufferPool`)
|
||||||
|
* a TLS factory (in form of `org.eclipse.jetty.util.ssl.SslContextFactory.Client`)
|
||||||
|
|
||||||
|
The `ClientConnector` is where you want to set those components after you have configured them.
|
||||||
|
If you don't explicitly set those components on the `ClientConnector`, then appropriate defaults will be chosen when the `ClientConnector` starts.
|
||||||
|
|
||||||
|
The simplest example that creates and starts a `ClientConnector` is the following:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/ClientConnectorDocs.java[tags=simplest]
|
||||||
|
----
|
||||||
|
|
||||||
|
A more typical example:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/ClientConnectorDocs.java[tags=typical]
|
||||||
|
----
|
||||||
|
|
||||||
|
A more advanced example that customizes the `ClientConnector` by overriding some of its methods:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/ClientConnectorDocs.java[tags=advanced]
|
||||||
|
----
|
||||||
|
|
||||||
|
Since `ClientConnector` is the component that handles the low-level network, it is also the component where you want to configure the low-level network configuration.
|
||||||
|
|
||||||
|
The most common parameters are:
|
||||||
|
|
||||||
|
* `ClientConnector.selectors`: the number of ``java.nio.Selector``s components (defaults to `1`) that are present to handle the ``SocketChannel``s opened by the `ClientConnector`.
|
||||||
|
You typically want to increase the number of selectors only for those use cases where each selector should handle more than few hundreds _concurrent_ socket events.
|
||||||
|
For example, one selector typically runs well for `250` _concurrent_ socket events; as a rule of thumb, you can multiply that number by `10` to obtain the number of opened sockets a selector can handle (`2500`), based on the assumption that not all the `2500` sockets will be active _at the same time_.
|
||||||
|
* `ClientConnector.idleTimeout`: the duration of time after which `ClientConnector` closes a socket due to inactivity (defaults to `30` seconds).
|
||||||
|
This is an important parameter to configure, and you typically want the client idle timeout to be shorter than the server idle timeout, to avoid race conditions where the client attempts to use a socket just before the client-side idle timeout expires, but the server-side idle timeout has already expired and the is already closing the socket.
|
||||||
|
* `ClientConnector.connectBlocking`: whether the operation of connecting a socket to the server (i.e. `SocketChannel.connect(SocketAddress)`) must be a blocking or a non-blocking operation (defaults to `false`).
|
||||||
|
For `localhost` or same datacenter hosts you want to set this parameter to
|
||||||
|
`true` because DNS resolution will be immediate (and likely never fail).
|
||||||
|
For generic Internet hosts (e.g. when you are implementing a web spider) you want to set this parameter to `false`.
|
||||||
|
* `ClientConnector.connectTimeout`: the duration of time after which `ClientConnector` aborts a connection attempt to the server (defaults to `5` seconds).
|
||||||
|
This time includes the DNS lookup time _and_ the TCP connect time.
|
||||||
|
|
||||||
|
Please refer to the `ClientConnector` link:{javadoc-url}/org/eclipse/jetty/io/ClientConnector.html[javadocs] for the complete list of configurable parameters.
|
||||||
|
|
||||||
|
[[unix-domain]]
|
||||||
|
=== Unix-Domain Support
|
||||||
|
|
||||||
|
https://openjdk.java.net/jeps/380[JEP 380] introduced Unix-Domain sockets support in Java 16, on all operative systems.
|
||||||
|
|
||||||
|
`ClientConnector` can be configured to support Unix-Domain sockets in the following way:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/ClientConnectorDocs.java[tags=unixDomain]
|
||||||
|
----
|
||||||
|
|
||||||
|
[IMPORTANT]
|
||||||
|
====
|
||||||
|
You can use Unix-Domain sockets support only when you run your client application with Java 16 or later.
|
||||||
|
====
|
||||||
|
|
||||||
|
[[protocol]]
|
||||||
|
== Protocol Layer
|
||||||
|
|
||||||
|
The protocol layer builds on top of the network layer to generate the bytes to be written to the network and to parse the bytes read from the network.
|
||||||
|
|
||||||
|
Recall from xref:arch/io.adoc#connection[this section] that Jetty uses the `Connection` abstraction to produce and interpret the network bytes.
|
||||||
|
|
||||||
|
On the client side, a `ClientConnectionFactory` implementation is the component that creates `Connection` instances based on the protocol that the client wants to "speak" with the server.
|
||||||
|
|
||||||
|
Applications use `ClientConnector.connect(SocketAddress, Map<String, Object>)` to establish a TCP connection to the server, and must tell `ClientConnector` how to create the `Connection` for that particular TCP connection, and how to notify back the application when the connection creation succeeds or fails.
|
||||||
|
|
||||||
|
This is done by passing a link:{javadoc-url}/org/eclipse/jetty/io/ClientConnectionFactory.html[`ClientConnectionFactory`] (that creates `Connection` instances) and a link:{javadoc-url}/org/eclipse/jetty/util/Promise.html[`Promise`] (that is notified of connection creation success or failure) in the context `Map` as follows:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/ClientConnectorDocs.java[tags=connect]
|
||||||
|
----
|
||||||
|
|
||||||
|
When a `Connection` is created successfully, its `onOpen()` method is invoked, and then the promise is completed successfully.
|
||||||
|
|
||||||
|
It is now possible to write a super-simple `telnet` client that reads and writes string lines:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/ClientConnectorDocs.java[tags=telnet]
|
||||||
|
----
|
||||||
|
|
||||||
|
Note how a very basic "telnet" API that applications could use is implemented in the form of the `onLine(Consumer<String>)` for the non-blocking receiving side and `writeLine(String, Callback)` for the non-blocking sending side.
|
||||||
|
Note also how the `onFillable()` method implements some basic "parsing" by looking up the `\n` character in the buffer.
|
||||||
|
|
||||||
|
NOTE: The "telnet" client above looks like a super-simple HTTP client because HTTP/1.0 can be seen as a line-based protocol.
|
||||||
|
HTTP/1.0 was used just as an example, but we could have used any other line-based protocol such as https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol[SMTP], provided that the server was able to understand it.
|
||||||
|
|
||||||
|
This is very similar to what the Jetty client implementation does for real network protocols.
|
||||||
|
Real network protocols are of course more complicated and so is the implementation code that handles them, but the general ideas are similar.
|
||||||
|
|
||||||
|
The Jetty client implementation provides a number of `ClientConnectionFactory` implementations that can be composed to produce and interpret the network bytes.
|
||||||
|
|
||||||
|
For example, it is simple to modify the above example to use the TLS protocol so that you will be able to connect to the server on port `443`, typically reserved for the encrypted HTTP protocol.
|
||||||
|
|
||||||
|
The differences between the clear-text version and the TLS encrypted version are minimal:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/ClientConnectorDocs.java[tags=tlsTelnet]
|
||||||
|
----
|
||||||
|
|
||||||
|
The differences with the clear-text version are only:
|
||||||
|
|
||||||
|
* Change the port from `80` to `443`.
|
||||||
|
* Wrap the `ClientConnectionFactory` with `SslClientConnectionFactory`.
|
||||||
|
* Unwrap the `SslConnection` to access `TelnetConnection`.
|
|
@ -0,0 +1,503 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= WebSocket Client
|
||||||
|
|
||||||
|
Jetty's `WebSocketClient` is a more powerful alternative to the WebSocket client provided by the standard JSR 356 `javax.websocket` APIs.
|
||||||
|
|
||||||
|
Similarly to Jetty's xref:client/http.adoc[`HttpClient`], the `WebSocketClient` is non-blocking and asynchronous, making it very efficient in resource utilization.
|
||||||
|
A synchronous, blocking, API is also offered for simpler cases.
|
||||||
|
|
||||||
|
Since the first step of establishing a WebSocket communication is an HTTP request, `WebSocketClient` makes use of `HttpClient` and therefore depends on it.
|
||||||
|
|
||||||
|
The Maven artifact coordinates are the following:
|
||||||
|
|
||||||
|
[,xml,subs=normal]
|
||||||
|
----
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty.websocket</groupId>
|
||||||
|
<artifactId>websocket-jetty-client</artifactId>
|
||||||
|
<version>{version}</version>
|
||||||
|
</dependency>
|
||||||
|
----
|
||||||
|
|
||||||
|
[[start]]
|
||||||
|
== Starting WebSocketClient
|
||||||
|
|
||||||
|
The main class is `org.eclipse.jetty.websocket.client.WebSocketClient`; you instantiate it, configure it, and then start it like may other Jetty components.
|
||||||
|
This is a minimal example:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/websocket/WebSocketClientDocs.java[tags=start]
|
||||||
|
----
|
||||||
|
|
||||||
|
However, it is recommended that you explicitly pass an `HttpClient` instance to `WebSocketClient` so that you can have control over the HTTP configuration as well:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/websocket/WebSocketClientDocs.java[tags=startWithHttpClient]
|
||||||
|
----
|
||||||
|
|
||||||
|
You may create multiple instances of `WebSocketClient`, but typically one instance is enough for most applications.
|
||||||
|
Creating multiple instances may be necessary for example when you need to specify different configuration parameters for different instances.
|
||||||
|
For example, you may need different instances when you need to configure the `HttpClient` differently: different transports, different proxies, different cookie stores, different authentications, etc.
|
||||||
|
|
||||||
|
The configuration that is not WebSocket specific (such as idle timeout, etc.) should be directly configured on the associated `HttpClient` instance.
|
||||||
|
|
||||||
|
The WebSocket specific configuration can be configured directly on the `WebSocketClient` instance.
|
||||||
|
Configuring the `WebSocketClient` allows to give default values to various parameters, whose values may be overridden more specifically, as described in <<session-configure,this section>>.
|
||||||
|
|
||||||
|
Refer to the `WebSocketClient` link:{javadoc-url}/org/eclipse/jetty/websocket/client/WebSocketClient.html[javadocs] for the setter methods available to customize the WebSocket specific configuration.
|
||||||
|
|
||||||
|
[[stop]]
|
||||||
|
== Stopping WebSocketClient
|
||||||
|
|
||||||
|
It is recommended that when your application stops, you also stop the `WebSocketClient` instance (or instances) that you are using.
|
||||||
|
|
||||||
|
Similarly to xref:client/http.adoc#stop[stopping `HttpClient`], you want to stop `WebSocketClient` from a thread that is not owned by `WebSocketClient` itself, for example:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/websocket/WebSocketClientDocs.java[tags=stop]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[connect]]
|
||||||
|
== Connecting to a Remote Host
|
||||||
|
|
||||||
|
A WebSocket client may initiate the communication with the server either <<connect-http11,using HTTP/1.1>> or <<connect-http2,using HTTP/2>>.
|
||||||
|
The two mechanism are quite different and detailed in the following sections.
|
||||||
|
|
||||||
|
[[connect-http11]]
|
||||||
|
=== Using HTTP/1.1
|
||||||
|
|
||||||
|
Initiating a WebSocket communication with a server using HTTP/1.1 is detailed in https://tools.ietf.org/html/rfc6455#section-1.8[RFC 6455].
|
||||||
|
|
||||||
|
A WebSocket client first establishes a TCP connection to the server, then sends an HTTP/1.1 _upgrade_ request.
|
||||||
|
|
||||||
|
If the server supports upgrading to WebSocket, it responds with HTTP status code `101`, and then switches the communication over that connection, either incoming or outgoing, to happen using the WebSocket protocol.
|
||||||
|
|
||||||
|
When the client receives the HTTP status code `101`, it switches the communication over that connection, either incoming or outgoing, to happen using the WebSocket protocol.
|
||||||
|
|
||||||
|
[plantuml]
|
||||||
|
----
|
||||||
|
skinparam backgroundColor transparent
|
||||||
|
skinparam monochrome true
|
||||||
|
skinparam shadowing false
|
||||||
|
|
||||||
|
participant ClientEndPoint
|
||||||
|
participant WebSocketClient
|
||||||
|
participant HttpClient
|
||||||
|
participant Server
|
||||||
|
participant ServerEndPoint
|
||||||
|
|
||||||
|
WebSocketClient -> HttpClient : connect()
|
||||||
|
HttpClient -> Server : TCP/TLS connect
|
||||||
|
HttpClient -> Server : GET / HTTP/1.1\nUpgrade: websocket
|
||||||
|
Server -> ServerEndPoint ** : create
|
||||||
|
Server -> HttpClient : HTTP/1.1 101\nUpgrade: websocket
|
||||||
|
HttpClient -> WebSocketClient
|
||||||
|
WebSocketClient -> ClientEndPoint ** : create
|
||||||
|
ClientEndPoint -> WebSocketClient : WebSocket Frame A
|
||||||
|
WebSocketClient -> Server : WebSocket Frame A
|
||||||
|
Server -> ServerEndPoint : WebSocket Frame A
|
||||||
|
ServerEndPoint -> Server : WebSocket Frame B
|
||||||
|
Server -> WebSocketClient : WebSocket Frame B
|
||||||
|
WebSocketClient -> ClientEndPoint : WebSocket Frame B
|
||||||
|
----
|
||||||
|
|
||||||
|
In code:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/websocket/WebSocketClientDocs.java[tags=connectHTTP11]
|
||||||
|
----
|
||||||
|
|
||||||
|
`WebSocketClient.connect()` links the client-side WebSocket _endpoint_ to a specific server URI, and returns a `CompletableFuture` of an `org.eclipse.jetty.websocket.api.Session`.
|
||||||
|
|
||||||
|
The endpoint offers APIs to _receive_ WebSocket data (or errors) from the server, while the session offers APIs to _send_ WebSocket data to the server.
|
||||||
|
|
||||||
|
[[connect-http2]]
|
||||||
|
=== Using HTTP/2
|
||||||
|
|
||||||
|
Initiating a WebSocket communication with a server using HTTP/1.1 is detailed in https://tools.ietf.org/html/rfc8441[RFC 8441].
|
||||||
|
|
||||||
|
A WebSocket client establishes a TCP connection to the server or reuses an existing one currently used for HTTP/2, then sends an HTTP/2 _connect_ request over an HTTP/2 stream.
|
||||||
|
|
||||||
|
If the server supports upgrading to WebSocket, it responds with HTTP status code `200`, then switches the communication over that stream, either incoming or outgoing, to happen using HTTP/2 `DATA` frames wrapping WebSocket frames.
|
||||||
|
|
||||||
|
When the client receives the HTTP status code `200`, it switches the communication over that stream, either incoming or outgoing, to happen using HTTP/2 `DATA` frames wrapping WebSocket frames.
|
||||||
|
|
||||||
|
From an external point of view, it will look like client is sending chunks of an infinite HTTP/2 request upload, and the server is sending chunks of an infinite HTTP/2 response download, as they will exchange HTTP/2 `DATA` frames; but the HTTP/2 `DATA` frames will contain each one or more WebSocket frames that both client and server know how to deliver to the respective WebSocket endpoints.
|
||||||
|
|
||||||
|
When either WebSocket endpoint decides to terminate the communication, the HTTP/2 stream will be closed as well.
|
||||||
|
|
||||||
|
[plantuml]
|
||||||
|
----
|
||||||
|
skinparam backgroundColor transparent
|
||||||
|
skinparam monochrome true
|
||||||
|
skinparam shadowing false
|
||||||
|
|
||||||
|
participant ClientEndPoint
|
||||||
|
participant WebSocketClient
|
||||||
|
participant HttpClient
|
||||||
|
participant Server
|
||||||
|
participant ServerEndPoint
|
||||||
|
|
||||||
|
WebSocketClient -> HttpClient : connect()
|
||||||
|
HttpClient --> Server : TCP/TLS connect
|
||||||
|
HttpClient -> Server : HEADERS\n:method: CONNECT\n:protocol: websocket
|
||||||
|
Server -> ServerEndPoint ** : create
|
||||||
|
Server -> HttpClient : HEADERS\n:status: 200\n: websocket
|
||||||
|
HttpClient -> WebSocketClient
|
||||||
|
WebSocketClient -> ClientEndPoint ** : create
|
||||||
|
ClientEndPoint -> HttpClient : WebSocket Frame A
|
||||||
|
HttpClient -> Server : DATA\nWebSocket Frame A
|
||||||
|
Server -> ServerEndPoint : WebSocket Frame A
|
||||||
|
ServerEndPoint -> Server : WebSocket Frame B
|
||||||
|
Server -> HttpClient : DATA\nWebSocket Frame B
|
||||||
|
HttpClient -> ClientEndPoint : WebSocket Frame B
|
||||||
|
----
|
||||||
|
|
||||||
|
In code:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/websocket/WebSocketClientDocs.java[tags=connectHTTP2]
|
||||||
|
----
|
||||||
|
|
||||||
|
Alternatively, you can use the xref:client/http.adoc#transport-dynamic[dynamic `HttpClient` transport]:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/websocket/WebSocketClientDocs.java[tags=connectHTTP2Dynamic]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[connect-custom-http-request]]
|
||||||
|
=== Customizing the Initial HTTP Request
|
||||||
|
|
||||||
|
Sometimes you need to add custom cookies, or other HTTP headers, or specify a WebSocket sub-protocol to the HTTP request that initiates the WebSocket communication.
|
||||||
|
|
||||||
|
You can do this by using overloaded versions of the `WebSocketClient.connect(...)` method:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/websocket/WebSocketClientDocs.java[tags=customHTTPRequest]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[connect-inspect-http-response]]
|
||||||
|
=== Inspecting the Initial HTTP Response
|
||||||
|
|
||||||
|
If you want to inspect the HTTP response returned by the server as a reply to the HTTP request that initiates the WebSocket communication, you may provide a `JettyUpgradeListener`:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/client/websocket/WebSocketClientDocs.java[tags=inspectHTTPResponse]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[architecture]]
|
||||||
|
== Jetty WebSocket Architecture
|
||||||
|
|
||||||
|
The Jetty WebSocket architecture is organized around the concept of a logical _connection_ between the client and the server.
|
||||||
|
|
||||||
|
The connection may be physical, when connecting to the server using HTTP/1.1, as the WebSocket bytes are carried directly by the TCP connection.
|
||||||
|
|
||||||
|
The connection may be virtual, when connecting to the server using HTTP/2, as the WebSocket bytes are wrapped into HTTP/2 `DATA` frames of an HTTP/2 stream.
|
||||||
|
In this case, a single TCP connection may carry several WebSocket virtual connections, each wrapped in its own HTTP/2 stream.
|
||||||
|
|
||||||
|
Each side of a WebSocket connection, either client or server, is made of two entities:
|
||||||
|
|
||||||
|
* A <<endpoints,WebSocket _endpoint_>>, the entity that _receives_ WebSocket events.
|
||||||
|
* A <<session,WebSocket _session_>>, the entity that offers an API to _send_ WebSocket data (and to close the WebSocket connection), as well as to configure WebSocket connection parameters.
|
||||||
|
|
||||||
|
[[endpoints]]
|
||||||
|
== WebSocket Endpoints
|
||||||
|
|
||||||
|
A WebSocket endpoint is the entity that receives WebSocket events.
|
||||||
|
|
||||||
|
The WebSocket events are the following:
|
||||||
|
|
||||||
|
* The _connect_ event.
|
||||||
|
This event is emitted when the WebSocket communication has been successfully established.
|
||||||
|
Applications interested in the connect event receive the WebSocket _session_ so that they can use it to send data to the remote peer.
|
||||||
|
* The _close_ event.
|
||||||
|
This event is emitted when the WebSocket communication has been closed.
|
||||||
|
Applications interested in the close event receive a WebSocket status code and an optional close reason message.
|
||||||
|
* The _error_ event.
|
||||||
|
This event is emitted when the WebSocket communication encounters a fatal error, such as an I/O error (for example, the network connection has been broken), or a protocol error (for example, the remote peer sends an invalid WebSocket frame).
|
||||||
|
Applications interested in the error event receive a `Throwable` that represent the error.
|
||||||
|
* The _message_ event.
|
||||||
|
The message event is emitted when a WebSocket message is received.
|
||||||
|
Only one thread at a time will be delivering a message event to the `onMessage` method; the next message event will not be delivered until the previous call to the `onMessage` method has exited.
|
||||||
|
Endpoints will always be notified of message events in the same order they were received over the network.
|
||||||
|
The message event can be of two types:
|
||||||
|
** Textual message event.
|
||||||
|
Applications interested in this type of messages receive a `String` representing the UTF-8 bytes received.
|
||||||
|
** Binary message event.
|
||||||
|
Applications interested in this type of messages receive a `byte[]` representing the raw bytes received.
|
||||||
|
|
||||||
|
[[endpoints-listener]]
|
||||||
|
=== Listener Endpoints
|
||||||
|
|
||||||
|
A WebSocket endpoint may implement the `org.eclipse.jetty.websocket.api.WebSocketListener` interface to receive WebSocket events:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/WebSocketDocs.java[tags=listenerEndpoint]
|
||||||
|
----
|
||||||
|
<1> Your listener class implements `WebSocketListener`.
|
||||||
|
|
||||||
|
==== Message Streaming Reads
|
||||||
|
|
||||||
|
If you need to deal with large WebSocket messages, you may reduce the memory usage by streaming the message content.
|
||||||
|
For large WebSocket messages, the memory usage may be large due to the fact that the text or the bytes must be accumulated until the message is complete before delivering the message event.
|
||||||
|
|
||||||
|
To stream textual or binary messages, you must implement interface `org.eclipse.jetty.websocket.api.WebSocketPartialListener` instead of `WebSocketListener`.
|
||||||
|
|
||||||
|
Interface `WebSocketPartialListener` exposes one method for textual messages, and one method to binary messages that receive _chunks_ of, respectively, text and bytes that form the whole WebSocket message.
|
||||||
|
|
||||||
|
You may accumulate the chunks yourself, or process each chunk as it arrives, or stream the chunks elsewhere, for example:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/WebSocketDocs.java[tags=streamingListenerEndpoint]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[endpoints-annotated]]
|
||||||
|
=== Annotated Endpoints
|
||||||
|
|
||||||
|
A WebSocket endpoint may annotate methods with `org.eclipse.jetty.websocket.api.annotations.*` annotations to receive WebSocket events.
|
||||||
|
Each annotated method may take an optional `Session` argument as its first parameter:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/WebSocketDocs.java[tags=annotatedEndpoint]
|
||||||
|
----
|
||||||
|
<1> Use the `@WebSocket` annotation at the class level to make it a WebSocket endpoint.
|
||||||
|
<2> Use the `@OnWebSocketConnect` annotation for the _connect_ event.
|
||||||
|
As this is the first event notified to the endpoint, you can configure the `Session` object.
|
||||||
|
<3> Use the `@OnWebSocketClose` annotation for the _close_ event.
|
||||||
|
The method may take an optional `Session` as first parameter.
|
||||||
|
<4> Use the `@OnWebSocketError` annotation for the _error_ event.
|
||||||
|
The method may take an optional `Session` as first parameter.
|
||||||
|
<5> Use the `@OnWebSocketMessage` annotation for the _message_ event, both for textual and binary messages.
|
||||||
|
The method may take an optional `Session` as first parameter.
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
For binary messages, you may declare the annotated method with either or these two signatures:
|
||||||
|
|
||||||
|
[,java]
|
||||||
|
----
|
||||||
|
@OnWebSocketMessage
|
||||||
|
public void methodName(byte[] bytes, int offset, int length) { ... }
|
||||||
|
----
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
[,java]
|
||||||
|
----
|
||||||
|
@OnWebSocketMessage
|
||||||
|
public void methodName(ByteBuffer buffer) { ... }
|
||||||
|
----
|
||||||
|
====
|
||||||
|
|
||||||
|
==== Message Streaming Reads
|
||||||
|
|
||||||
|
If you need to deal with large WebSocket messages, you may reduce the memory usage by streaming the message content.
|
||||||
|
|
||||||
|
To stream textual or binary messages, you still use the `@OnWebSocketMessage` annotation, but you change the signature of the method to take, respectively a `Reader` and an `InputStream`:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/WebSocketDocs.java[tags=streamingAnnotatedEndpoint]
|
||||||
|
----
|
||||||
|
|
||||||
|
[CAUTION]
|
||||||
|
====
|
||||||
|
`Reader` or `InputStream` only offer blocking APIs, so if the remote peers are slow in sending the large WebSocket messages, reading threads may be blocked in `Reader.read(char[])` or `InputStream.read(byte[])`, possibly exhausting the thread pool.
|
||||||
|
====
|
||||||
|
|
||||||
|
[[session]]
|
||||||
|
== WebSocket Session
|
||||||
|
|
||||||
|
A WebSocket session is the entity that offers an API to send data to the remote peer, to close the WebSocket connection, and to configure WebSocket connection parameters.
|
||||||
|
|
||||||
|
[[session-configure]]
|
||||||
|
=== Configuring the Session
|
||||||
|
|
||||||
|
You may configure the WebSocket session behavior using the `org.eclipse.jetty.websocket.api.Session` APIs.
|
||||||
|
You want to do this as soon as you have access to the `Session` object, typically from the <<endpoints,_connect_ event>> handler:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/WebSocketDocs.java[tags=sessionConfigure]
|
||||||
|
----
|
||||||
|
|
||||||
|
The settings that can be configured include:
|
||||||
|
|
||||||
|
maxBinaryMessageSize::
|
||||||
|
the maximum size in bytes of a binary message (which may be composed of multiple frames) that can be received.
|
||||||
|
|
||||||
|
maxTextMessageSize::
|
||||||
|
the maximum size in bytes of a text message (which may be composed of multiple frames) that can be received.
|
||||||
|
|
||||||
|
maxFrameSize::
|
||||||
|
the maximum payload size in bytes of any WebSocket frame that can be received.
|
||||||
|
|
||||||
|
inputBufferSize::
|
||||||
|
the input (read from network/transport layer) buffer size in bytes; it has no relationship with the WebSocket frame size or message size.
|
||||||
|
|
||||||
|
outputBufferSize::
|
||||||
|
the output (write to network/transport layer) buffer size in bytes; it has no relationship to the WebSocket frame size or message size.
|
||||||
|
|
||||||
|
autoFragment::
|
||||||
|
whether WebSocket frames are automatically fragmented to respect the maximum frame size.
|
||||||
|
|
||||||
|
idleTimeout::
|
||||||
|
the duration that a WebSocket connection may remain idle (that is, there is no network traffic, neither in read nor in write) before being closed by the implementation.
|
||||||
|
|
||||||
|
Please refer to the `Session` link:{javadoc-url}/org/eclipse/jetty/websocket/api/Session.html[javadocs] for the complete list of configuration APIs.
|
||||||
|
|
||||||
|
[[session-send]]
|
||||||
|
=== Sending Data
|
||||||
|
|
||||||
|
To send data to the remote peer, you need to obtain the `RemoteEndpoint` object from the `Session`, and then use its API to send data.
|
||||||
|
|
||||||
|
`RemoteEndpoint` offers two styles of APIs to send data:
|
||||||
|
|
||||||
|
* Blocking APIs, where the call returns when the data has been sent, or throws an `IOException` if the data cannot be sent.
|
||||||
|
* Non-blocking APIs, where a callback object is notified when the data has been sent, or when there was a failure sending the data.
|
||||||
|
|
||||||
|
[[session-send-blocking]]
|
||||||
|
==== Blocking APIs
|
||||||
|
|
||||||
|
`RemoteEndpoint` blocking APIs throw `IOException`:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/WebSocketDocs.java[tags=sendBlocking]
|
||||||
|
----
|
||||||
|
|
||||||
|
Blocking APIs are simpler to use since they can be invoked one after the other sequentially.
|
||||||
|
|
||||||
|
[CAUTION]
|
||||||
|
====
|
||||||
|
Sending large messages to the remote peer may cause the sending thread to block, possibly exhausting the thread pool.
|
||||||
|
Consider using non-blocking APIs for large messages.
|
||||||
|
====
|
||||||
|
|
||||||
|
[[session-send-non-blocking]]
|
||||||
|
==== Non-Blocking APIs
|
||||||
|
|
||||||
|
`RemoteEndpoint` non-blocking APIs have an additional callback parameter:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/WebSocketDocs.java[tags=sendNonBlocking]
|
||||||
|
----
|
||||||
|
<1> Non-blocking APIs require a `WriteCallback` parameter.
|
||||||
|
<2> Note how the second send must be performed from inside the callback.
|
||||||
|
<3> Sequential sends may throw `WritePendingException`.
|
||||||
|
|
||||||
|
Non-blocking APIs are more difficult to use since you are required to meet the following condition:
|
||||||
|
|
||||||
|
[IMPORTANT]
|
||||||
|
====
|
||||||
|
You cannot initiate another send of any kind until the previous send is completed.
|
||||||
|
|
||||||
|
For example, if you have initiated a text send, you cannot initiate a binary send, until the previous send has completed.
|
||||||
|
|
||||||
|
Furthermore, if you have initiated a non-blocking send, you cannot initiate a blocking send, until the previous send has completed.
|
||||||
|
====
|
||||||
|
|
||||||
|
This requirement is necessary to avoid unbounded buffering that could lead to ``OutOfMemoryError``s.
|
||||||
|
|
||||||
|
[CAUTION]
|
||||||
|
====
|
||||||
|
We strongly recommend that you follow the condition above.
|
||||||
|
|
||||||
|
However, there may be cases where you want to explicitly control the number of outgoing buffered messages using `RemoteEndpoint.setMaxOutgoingFrames(int)`.
|
||||||
|
|
||||||
|
Remember that trying to control the number of outgoing buffered messages is very difficult and tricky; you may set `maxOutgoingFrames=4` and have a situation where 6 threads try to concurrently send messages: threads 1 to 4 will be able to successfully buffer their messages, thread 5 may fail, but thread 6 may succeed because one of the previous threads completed its send.
|
||||||
|
At this point you have an out-of-order message delivery that could be unexpected and very difficult to troubleshoot because it will happen non-deterministically.
|
||||||
|
====
|
||||||
|
|
||||||
|
While non-blocking APIs are more difficult to use, they don't block the sender thread and therefore use less resources, which in turn typically allows for greater scalability under load: with respect to blocking APIs, non-blocking APIs need less resources to cope with the same load.
|
||||||
|
|
||||||
|
[[session-send-stream]]
|
||||||
|
==== Streaming Send APIs
|
||||||
|
|
||||||
|
If you need to send large WebSocket messages, you may reduce the memory usage by streaming the message content.
|
||||||
|
|
||||||
|
Both blocking and non-blocking APIs offer `sendPartial*(...)` methods that allow you to send a chunk of the whole message at a time, therefore reducing the memory usage since it is not necessary to have the whole message `String` or `byte[]` in memory to send it.
|
||||||
|
|
||||||
|
Streaming sends using blocking APIs is quite simple:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/WebSocketDocs.java[tags=streamSendBlocking]
|
||||||
|
----
|
||||||
|
|
||||||
|
Streaming sends using non-blocking APIs is more complicated, as you should wait (without blocking!) for the callbacks to complete.
|
||||||
|
|
||||||
|
Fortunately, Jetty provides you with the `IteratingCallback` utility class (described in more details xref:arch/io.adoc#echo[in this section]) which greatly simplify the use of non-blocking APIs:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/WebSocketDocs.java[tags=streamSendNonBlocking]
|
||||||
|
----
|
||||||
|
<1> Implementing `WriteCallback` allows to pass `this` to `sendPartialBytes(...)`.
|
||||||
|
<2> The `process()` method is called iteratively when each `sendPartialBytes(...)` is completed.
|
||||||
|
<3> Send the message chunks.
|
||||||
|
|
||||||
|
[[session-ping]]
|
||||||
|
=== Sending Ping/Pong
|
||||||
|
|
||||||
|
The WebSocket protocol defines two special frame, named `Ping` and `Pong` that may be interesting to applications for these use cases:
|
||||||
|
|
||||||
|
* Calculate the round-trip time with the remote peer.
|
||||||
|
* Keep the connection from being closed due to idle timeout -- a heartbeat-like mechanism.
|
||||||
|
|
||||||
|
To handle `Ping`/`Pong` events, you may implement interface `org.eclipse.jetty.websocket.api.WebSocketPingPongListener`.
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
`Ping`/`Pong` events are not supported when using annotations.
|
||||||
|
====
|
||||||
|
|
||||||
|
`Ping` frames may contain opaque application bytes, and the WebSocket implementation replies to them with a `Pong` frame containing the same bytes:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/WebSocketDocs.java[tags=pingPongListener]
|
||||||
|
----
|
||||||
|
<1> The WebSocket endpoint class must implement `WebSocketPingPongListener`
|
||||||
|
|
||||||
|
[[session-close]]
|
||||||
|
=== Closing the Session
|
||||||
|
|
||||||
|
When you want to terminate the communication with the remote peer, you close the `Session`:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/WebSocketDocs.java[tags=sessionClose]
|
||||||
|
----
|
||||||
|
|
||||||
|
Closing a WebSocket `Session` carries a status code and a reason message that the remote peer can inspect in the _close_ event handler (see <<endpoints,this section>>).
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
The reason message is optional, and may be truncated to fit into the WebSocket frame sent to the client.
|
||||||
|
It is best to use short tokens such as `"shutdown"`, or `"idle_timeout"`, etc. or even application specific codes such as `"0001"` or `"00AF"` that can be converted by the application into more meaningful messages.
|
||||||
|
====
|
|
@ -0,0 +1,25 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
[reftext=Programming Guide]
|
||||||
|
= Jetty {page-version} Programming Guide
|
||||||
|
|
||||||
|
The Eclipse Jetty Programming Guide targets developers who want to use the Eclipse Jetty libraries in their applications.
|
||||||
|
|
||||||
|
The Eclipse Jetty libraries provide the client-side and server-side APIs to work with various web protocols such as HTTP/1.1, HTTP/2, HTTP/3, WebSocket and FastCGI.
|
||||||
|
|
||||||
|
You may use the xref:client/index.adoc[Eclipse Jetty client-side library] in your application to make calls to third party REST services, or to other REST microservices in your system.
|
||||||
|
|
||||||
|
Likewise, you may use the xref:server/index.adoc[Eclipse Jetty server-side library] to quickly create an HTTP or REST service without having to create a web application archive file (a `+*.war+` file) and without having to deploy it a Jetty standalone server that you would have to download and install.
|
||||||
|
|
||||||
|
This guide will walk you through the design of the Eclipse Jetty libraries and how to use its classes to write your applications.
|
|
@ -0,0 +1,244 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Jetty Jspc Maven Plugin
|
||||||
|
|
||||||
|
This plugin will help you pre-compile your jsps and works in conjunction with the Maven war plugin to put them inside an assembled war.
|
||||||
|
|
||||||
|
[[jspc-config]]
|
||||||
|
== Configuration
|
||||||
|
|
||||||
|
Here's the basic setup required to put the jspc plugin into your build:
|
||||||
|
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-jspc-maven-plugin</artifactId>
|
||||||
|
<version>{VERSION}</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>jspc</id>
|
||||||
|
<goals>
|
||||||
|
<goal>jspc</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
----
|
||||||
|
|
||||||
|
The configurable parameters are as follows:
|
||||||
|
|
||||||
|
webXmlFragment::
|
||||||
|
Default value: `$\{project.basedir}/target/webfrag.xml`
|
||||||
|
+
|
||||||
|
File into which to generate the servlet declarations.
|
||||||
|
Will be merged with an existing `web.xml`.
|
||||||
|
webAppSourceDirectory::
|
||||||
|
Default value: `$\{project.basedir}/src/main/webapp`
|
||||||
|
+
|
||||||
|
Root of resources directory where jsps, tags etc are located.
|
||||||
|
webXml::
|
||||||
|
Default value: `$\{project.basedir}/src/main/webapp/WEB-INF/web.xml`
|
||||||
|
+
|
||||||
|
The web.xml file to use to merge with the generated fragments.
|
||||||
|
includes::
|
||||||
|
Default value: `**\/*.jsp, **\/*.jspx`
|
||||||
|
+
|
||||||
|
The comma separated list of patterns for file extensions to be processed.
|
||||||
|
excludes::
|
||||||
|
Default value: `**\/.svn\/**`
|
||||||
|
+
|
||||||
|
The comma separated list of patterns for file extensions to be skipped.
|
||||||
|
classesDirectory::
|
||||||
|
Default value: `$\{project.build.outputDirectory}`
|
||||||
|
+
|
||||||
|
Location of classes for the webapp.
|
||||||
|
generatedClasses::
|
||||||
|
Default value: `$\{project.build.outputDirectory}`
|
||||||
|
+
|
||||||
|
Location to put the generated classes for the jsps.
|
||||||
|
insertionMarker::
|
||||||
|
Default value: _none_
|
||||||
|
+
|
||||||
|
A marker string in the src `web.xml` file which indicates where to merge in the generated web.xml fragment.
|
||||||
|
Note that the marker string will NOT be preserved during the insertion.
|
||||||
|
Can be left blank, in which case the generated fragment is inserted just before the line containing `</web-app>`.
|
||||||
|
useProvidedScope::
|
||||||
|
Default value: false
|
||||||
|
+
|
||||||
|
If true, jars of dependencies marked with <scope>provided</scope> will be placed on the compilation classpath.
|
||||||
|
mergeFragment::
|
||||||
|
Default value: true
|
||||||
|
+
|
||||||
|
Whether or not to merge the generated fragment file with the source web.xml.
|
||||||
|
The merged file will go into the same directory as the webXmlFragment.
|
||||||
|
keepSources::
|
||||||
|
Default value: false
|
||||||
|
+
|
||||||
|
If true, the generated .java files are not deleted at the end of processing.
|
||||||
|
scanAllDirectories::
|
||||||
|
Default value: true
|
||||||
|
+
|
||||||
|
Determines if dirs on the classpath should be scanned as well as jars.
|
||||||
|
If true, this allows scanning for tlds of dependent projects that
|
||||||
|
are in the reactor as unassembled jars.
|
||||||
|
scanManifest::
|
||||||
|
Default value: true
|
||||||
|
+
|
||||||
|
Determines if the manifest of JAR files found on the classpath should be scanned.
|
||||||
|
sourceVersion::
|
||||||
|
Introduced in Jetty 9.3.6.
|
||||||
|
Java version of jsp source files.
|
||||||
|
Defaults to 1.7.
|
||||||
|
targetVersion::
|
||||||
|
Introduced in Jetty 9.3.6.
|
||||||
|
Java version of class files generated from jsps.
|
||||||
|
Defaults to 1.7.
|
||||||
|
tldJarNamePatterns::
|
||||||
|
Default value: `.*taglibs[^/]*\.jar|.*jstl-impl[^/]*\.jar$`
|
||||||
|
+
|
||||||
|
Patterns of jars on the 'system' (ie container) path that contain tlds.
|
||||||
|
Use | to separate each pattern.
|
||||||
|
jspc::
|
||||||
|
Default value: the `org.apache.jasper.JspC` instance being configured.
|
||||||
|
+
|
||||||
|
The JspC class actually performs the pre-compilation.
|
||||||
|
All setters on the JspC class are available.
|
||||||
|
You can download the javadoc https://repo1.maven.org/maven2/org/glassfish/web/javax.servlet.jsp/2.3.2/javax.servlet.jsp-2.3.2-javadoc.jar[here].
|
||||||
|
|
||||||
|
Taking all the default settings, here's how to configure the war plugin to use the generated `web.xml` that includes all of the jsp servlet declarations:
|
||||||
|
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-war-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<webXml>${project.basedir}/target/web.xml</webXml>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
----
|
||||||
|
|
||||||
|
[[jspc-production-precompile]]
|
||||||
|
== Precompiling only for Production Build
|
||||||
|
|
||||||
|
As compiling jsps is usually done during preparation for a production release and not usually done during development, it is more convenient to put the plugin setup inside a <profile> which which can be deliberately invoked during prep for production.
|
||||||
|
|
||||||
|
For example, the following profile will only be invoked if the flag `-Dprod` is present on the run line:
|
||||||
|
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<profiles>
|
||||||
|
<profile>
|
||||||
|
<id>prod</id>
|
||||||
|
<activation>
|
||||||
|
<property><name>prod</name></property>
|
||||||
|
</activation>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-jspc-maven-plugin</artifactId>
|
||||||
|
<version>{VERSION}</version>
|
||||||
|
<!-- put your configuration in here -->
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-war-plugin</artifactId>
|
||||||
|
<!-- put your configuration in here -->
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</profile>
|
||||||
|
</profiles>
|
||||||
|
----
|
||||||
|
|
||||||
|
The following invocation would cause your code to be compiled, the jsps to be compiled, the <servlet> and <servlet-mapping>s inserted in the `web.xml` and your webapp assembled into a war:
|
||||||
|
|
||||||
|
----
|
||||||
|
$ mvn -Dprod package
|
||||||
|
----
|
||||||
|
|
||||||
|
[[jspc-overlay-precompile]]
|
||||||
|
== Precompiling Jsps with Overlaid Wars
|
||||||
|
|
||||||
|
Precompiling jsps with an overlaid war requires a bit more configuration.
|
||||||
|
This is because you need to separate the steps of unpacking the overlaid war and then repacking the final target war so the jetty-jspc-maven-plugin has the opportunity to access the overlaid resources.
|
||||||
|
|
||||||
|
In the example we'll show, we will use an overlaid war.
|
||||||
|
The overlaid war will provide the `web.xml` file but the jsps will be in `src/main/webapp` (i.e. part of the project that uses the overlay).
|
||||||
|
We will unpack the overlaid war file, compile the jsps and merge their servlet definitions into the extracted `web.xml`, then pack everything into a war.
|
||||||
|
|
||||||
|
Here's an example configuration of the war plugin that separate those phases into an unpack phase, and then a packing phase:
|
||||||
|
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-war-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>unpack</id>
|
||||||
|
<goals><goal>exploded</goal></goals>
|
||||||
|
<phase>generate-resources</phase>
|
||||||
|
<configuration>
|
||||||
|
<webappDirectory>target/foo</webappDirectory>
|
||||||
|
<overlays>
|
||||||
|
<overlay />
|
||||||
|
<overlay>
|
||||||
|
<groupId>org.eclipse.jetty.demos</groupId>
|
||||||
|
<artifactId>demo-jetty-webapp</artifactId>
|
||||||
|
</overlay>
|
||||||
|
</overlays>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
<execution>
|
||||||
|
<id>pack</id>
|
||||||
|
<goals><goal>war</goal></goals>
|
||||||
|
<phase>package</phase>
|
||||||
|
<configuration>
|
||||||
|
<warSourceDirectory>target/foo</warSourceDirectory>
|
||||||
|
<webXml>target/web.xml</webXml>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
----
|
||||||
|
|
||||||
|
Now you also need to configure the `jetty-jspc-maven-plugin` so that it can use the web.xml that was extracted by the war unpacking and merge in the generated definitions of the servlets.
|
||||||
|
This is in `target/foo/WEB-INF/web.xml`.
|
||||||
|
Using the default settings, the `web.xml` merged with the jsp servlet definitions will be put into `target/web.xml`.
|
||||||
|
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-jspc-maven-plugin</artifactId>
|
||||||
|
<version>{VERSION}</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>jspc</id>
|
||||||
|
<goals>
|
||||||
|
<goal>jspc</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<webXml>target/foo/WEB-INF/web.xml</webXml>
|
||||||
|
<includes>**/*.foo</includes>
|
||||||
|
<excludes>**/*.fff</excludes>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
----
|
|
@ -0,0 +1,308 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Using Maven
|
||||||
|
|
||||||
|
http://maven.apache.org/[Apache Maven] is a software project management and comprehension tool.
|
||||||
|
Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information.
|
||||||
|
|
||||||
|
It is an ideal tool to build a web application project, and such projects can use the xref:maven-jetty/jetty-maven-plugin.adoc[jetty-maven-plugin] to easily run the web application and save time in development.
|
||||||
|
You can also use Maven to build, test and run a project which embeds Jetty.
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
Use of Maven and the jetty-maven-plugin is *not* required.
|
||||||
|
Using Maven for Jetty implementations is a popular choice, but users encouraged to manage their projects in whatever way suits their needs.
|
||||||
|
Other popular tools include Ant and Gradle.
|
||||||
|
====
|
||||||
|
|
||||||
|
First we'll have a look at a very simple HelloWorld java application that embeds Jetty, then a simple webapp which makes use of the xref:maven-jetty/jetty-maven-plugin.adoc[jetty-maven-plugin] to speed up the development cycle.
|
||||||
|
|
||||||
|
[[configuring-embedded-jetty-with-maven]]
|
||||||
|
== Using Embedded Jetty with Maven
|
||||||
|
|
||||||
|
Maven uses convention over configuration, so it is best to use the project structure Maven recommends.
|
||||||
|
You can use _http://maven.apache.org/guides/introduction/introduction-to-archetypes.html[archetypes]_ to quickly setup Maven projects, but we will set up the structure manually for this simple tutorial example:
|
||||||
|
|
||||||
|
----
|
||||||
|
> mkdir JettyMavenHelloWorld
|
||||||
|
> cd JettyMavenHelloWorld
|
||||||
|
> mkdir -p src/main/java/org/example
|
||||||
|
----
|
||||||
|
|
||||||
|
[[creating-helloworld-class]]
|
||||||
|
=== Creating the HelloWorld Class
|
||||||
|
|
||||||
|
Use an editor to create the file `src/main/java/org/example/HelloWorld.java` with the following contents:
|
||||||
|
|
||||||
|
[,java]
|
||||||
|
----
|
||||||
|
package org.example;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import java.io.IOException;
|
||||||
|
import org.eclipse.jetty.server.Server;
|
||||||
|
import org.eclipse.jetty.server.Request;
|
||||||
|
import org.eclipse.jetty.server.handler.AbstractHandler;
|
||||||
|
|
||||||
|
public class HelloWorld extends AbstractHandler
|
||||||
|
{
|
||||||
|
public void handle(String target,
|
||||||
|
Request baseRequest,
|
||||||
|
HttpServletRequest request,
|
||||||
|
HttpServletResponse response)
|
||||||
|
throws IOException, ServletException
|
||||||
|
{
|
||||||
|
response.setContentType("text/html;charset=utf-8");
|
||||||
|
response.setStatus(HttpServletResponse.SC_OK);
|
||||||
|
baseRequest.setHandled(true);
|
||||||
|
response.getWriter().println("<h1>Hello World</h1>");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception
|
||||||
|
{
|
||||||
|
Server server = new Server(8080);
|
||||||
|
server.setHandler(new HelloWorld());
|
||||||
|
|
||||||
|
server.start();
|
||||||
|
server.join();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|
||||||
|
[[creating-embedded-pom-descriptor]]
|
||||||
|
=== Creating the POM Descriptor
|
||||||
|
|
||||||
|
The `pom.xml` file declares the project name and its dependencies.
|
||||||
|
Use an editor to create the file `pom.xml` in the `JettyMavenHelloWorld` directory with the following contents:
|
||||||
|
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<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>
|
||||||
|
<groupId>org.example</groupId>
|
||||||
|
<artifactId>hello-world</artifactId>
|
||||||
|
<version>0.1-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<name>Jetty HelloWorld</name>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<!-- Adapt this to a version found on
|
||||||
|
https://repo1.maven.org/maven2/org/eclipse/jetty/jetty-maven-plugin/
|
||||||
|
-->
|
||||||
|
<jettyVersion>{VERSION}</jettyVersion>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-server</artifactId>
|
||||||
|
<version>$\{jettyVersion}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>exec-maven-plugin</artifactId>
|
||||||
|
<version>1.1</version>
|
||||||
|
<executions>
|
||||||
|
<execution><goals><goal>java</goal></goals></execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<mainClass>org.example.HelloWorld</mainClass>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
|
----
|
||||||
|
|
||||||
|
[[buildng-and-running-embedded-helloworld]]
|
||||||
|
=== Building and Running Embedded HelloWorld
|
||||||
|
|
||||||
|
You can now compile and execute the HelloWorld class by using these commands:
|
||||||
|
|
||||||
|
----
|
||||||
|
> mvn clean compile exec:java
|
||||||
|
----
|
||||||
|
|
||||||
|
You can point your browser to `http://localhost:8080` to see the _Hello World_ page.
|
||||||
|
You can observe what Maven is doing for you behind the scenes by using the `mvn dependency:tree` command, which reveals the transitive dependency resolved and downloaded as:
|
||||||
|
|
||||||
|
----
|
||||||
|
> mvn dependency:tree
|
||||||
|
[INFO] Scanning for projects...
|
||||||
|
...
|
||||||
|
[INFO]
|
||||||
|
[INFO] ------------------------------------------------------------------------
|
||||||
|
[INFO] Building Jetty HelloWorld 0.1-SNAPSHOT
|
||||||
|
[INFO] ------------------------------------------------------------------------
|
||||||
|
[INFO]
|
||||||
|
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ hello-world ---
|
||||||
|
...
|
||||||
|
[INFO] org.example:hello-world:jar:0.1-SNAPSHOT
|
||||||
|
[INFO] \- org.eclipse.jetty:jetty-server:jar:9.3.9.v20160517:compile
|
||||||
|
[INFO] +- javax.servlet:javax.servlet-api:jar:3.1.0:compile
|
||||||
|
[INFO] +- org.eclipse.jetty:jetty-http:jar:9.3.9.v20160517:compile
|
||||||
|
[INFO] | \- org.eclipse.jetty:jetty-util:jar:9.3.9.v20160517:compile
|
||||||
|
[INFO] \- org.eclipse.jetty:jetty-io:jar:9.3.9.v20160517:compile
|
||||||
|
[INFO] ------------------------------------------------------------------------
|
||||||
|
[INFO] BUILD SUCCESS
|
||||||
|
[INFO] ------------------------------------------------------------------------
|
||||||
|
[INFO] Total time: 4.145 s
|
||||||
|
[INFO] Finished at: 2016-08-01T13:46:42-04:00
|
||||||
|
[INFO] Final Memory: 15M/209M
|
||||||
|
[INFO] ------------------------------------------------------------------------
|
||||||
|
----
|
||||||
|
|
||||||
|
[[developing-standard-webapp-with-jetty-and-maven]]
|
||||||
|
== Developing a Standard WebApp with Jetty and Maven
|
||||||
|
|
||||||
|
The previous section demonstrated how to use Maven with an application that embeds Jetty.
|
||||||
|
Now we will examine instead how to develop a standard webapp with Maven and Jetty.
|
||||||
|
First create the Maven structure (you can use the maven webapp archetype instead if you prefer):
|
||||||
|
|
||||||
|
----
|
||||||
|
> mkdir JettyMavenHelloWarApp
|
||||||
|
> cd JettyMavenHelloWebApp
|
||||||
|
> mkdir -p src/main/java/org/example
|
||||||
|
> mkdir -p src/main/webapp/WEB-INF
|
||||||
|
----
|
||||||
|
|
||||||
|
[[creating-servlet]]
|
||||||
|
=== Creating a Servlet
|
||||||
|
|
||||||
|
Use an editor to create the file `src/main/java/org/example/HelloServlet.java` with the following contents:
|
||||||
|
|
||||||
|
[,java]
|
||||||
|
----
|
||||||
|
package org.example;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
public class HelloServlet extends HttpServlet
|
||||||
|
{
|
||||||
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
|
||||||
|
{
|
||||||
|
response.setContentType("text/html");
|
||||||
|
response.setStatus(HttpServletResponse.SC_OK);
|
||||||
|
response.getWriter().println("<h1>Hello Servlet</h1>");
|
||||||
|
response.getWriter().println("session=" + request.getSession(true).getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|
||||||
|
You need to declare this servlet in the deployment descriptor, so create the file `src/main/webapp/WEB-INF/web.xml` and add the following contents:
|
||||||
|
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<?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"
|
||||||
|
metadata-complete="false"
|
||||||
|
version="3.1">
|
||||||
|
|
||||||
|
<servlet>
|
||||||
|
<servlet-name>Hello</servlet-name>
|
||||||
|
<servlet-class>org.example.HelloServlet</servlet-class>
|
||||||
|
</servlet>
|
||||||
|
<servlet-mapping>
|
||||||
|
<servlet-name>Hello</servlet-name>
|
||||||
|
<url-pattern>/hello/*</url-pattern>
|
||||||
|
</servlet-mapping>
|
||||||
|
|
||||||
|
</web-app>
|
||||||
|
----
|
||||||
|
|
||||||
|
[[creating-plugin-pom-descriptor]]
|
||||||
|
=== Creating the POM Descriptor
|
||||||
|
|
||||||
|
The `pom.xml` file declares the project name and its dependencies.
|
||||||
|
Use an editor to create the file `pom.xml` with the following contents in the `JettyMavenHelloWarApp` directory, noting particularly the declaration of the xref:maven-jetty/jetty-maven-plugin.adoc[jetty-maven-plugin]:
|
||||||
|
|
||||||
|
[,xml]
|
||||||
|
----
|
||||||
|
<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>
|
||||||
|
<groupId>org.example</groupId>
|
||||||
|
<artifactId>hello-world</artifactId>
|
||||||
|
<version>0.1-SNAPSHOT</version>
|
||||||
|
<packaging>war</packaging>
|
||||||
|
<name>Jetty HelloWorld WebApp</name>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<jettyVersion>{VERSION}</jettyVersion>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.servlet</groupId>
|
||||||
|
<artifactId>javax.servlet-api</artifactId>
|
||||||
|
<version>3.1.0</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-maven-plugin</artifactId>
|
||||||
|
<version>$\{jettyVersion}</version>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
|
----
|
||||||
|
|
||||||
|
[[building-and-running-web-application]]
|
||||||
|
=== Building and Running the Web Application
|
||||||
|
|
||||||
|
Now you can both build and run the web application without needing to assemble it into a war by using the xref:maven-jetty/jetty-maven-plugin.adoc[jetty-maven-plugin] via the command:
|
||||||
|
|
||||||
|
----
|
||||||
|
> mvn jetty:run
|
||||||
|
----
|
||||||
|
|
||||||
|
You can see the static and dynamic content at `http://localhost:8080/hello`
|
||||||
|
|
||||||
|
There are a great deal of configuration options available for the jetty-maven-plugin to help you build and run your webapp.
|
||||||
|
The full reference is at xref:maven-jetty/jetty-maven-plugin.adoc[Configuring the Jetty Maven Plugin].
|
||||||
|
|
||||||
|
[[building-war-file]]
|
||||||
|
=== Building a WAR file
|
||||||
|
|
||||||
|
You can create a Web Application Archive (WAR) file from the project with the command:
|
||||||
|
|
||||||
|
----
|
||||||
|
> mvn package
|
||||||
|
----
|
||||||
|
|
||||||
|
The resulting war file is in the `target` directory and may be deployed on any standard servlet server, including xref:operations-guide:deploy/index.adoc[Jetty].
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,130 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Migrating from Jetty 9.4.x to Jetty 10.0.x
|
||||||
|
|
||||||
|
[[java-version]]
|
||||||
|
== Required Java Version Changes
|
||||||
|
|
||||||
|
[cols="1,1", options="header"]
|
||||||
|
|===
|
||||||
|
| Jetty 9.4.x | Jetty 10.0.x
|
||||||
|
| Java 8 | Java 11
|
||||||
|
|===
|
||||||
|
|
||||||
|
[[websocket]]
|
||||||
|
== WebSocket Migration Guide
|
||||||
|
|
||||||
|
Migrating from Jetty 9.4.x to Jetty 10.0.x requires changes in the coordinates of the Maven artifact dependencies for WebSocket. Some of these classes have also changed name and package. This is not a comprehensive list of changes but should cover the most common changes encountered during migration.
|
||||||
|
|
||||||
|
[[websocket-maven-artifact-changes]]
|
||||||
|
=== Maven Artifacts Changes
|
||||||
|
|
||||||
|
[cols="1a,1a", options="header"]
|
||||||
|
|===
|
||||||
|
| Jetty 9.4.x | Jetty 10.0.x
|
||||||
|
|
||||||
|
| `org.eclipse.jetty.websocket:**websocket-api**`
|
||||||
|
| `org.eclipse.jetty.websocket:**websocket-jetty-api**`
|
||||||
|
|
||||||
|
| `org.eclipse.jetty.websocket:**websocket-server**`
|
||||||
|
| `org.eclipse.jetty.websocket:**websocket-jetty-server**`
|
||||||
|
|
||||||
|
| `org.eclipse.jetty.websocket:**websocket-client**`
|
||||||
|
| `org.eclipse.jetty.websocket:**websocket-jetty-client**`
|
||||||
|
|
||||||
|
| `org.eclipse.jetty.websocket:**javax-websocket-server-impl**`
|
||||||
|
| `org.eclipse.jetty.websocket:**websocket-javax-server**`
|
||||||
|
|
||||||
|
| `org.eclipse.jetty.websocket:**javax-websocket-client-impl**`
|
||||||
|
| `org.eclipse.jetty.websocket:**websocket-javax-client**`
|
||||||
|
|
||||||
|
|===
|
||||||
|
|
||||||
|
[[websocket-class-name-changes]]
|
||||||
|
=== Class Names Changes
|
||||||
|
|
||||||
|
[cols="1a,1a", options="header"]
|
||||||
|
|===
|
||||||
|
| Jetty 9.4.x | Jetty 10.0.x
|
||||||
|
|
||||||
|
| `org.eclipse.jetty.websocket.**server.NativeWebSocketServletContainerInitializer**`
|
||||||
|
| `org.eclipse.jetty.websocket.**server.config.JettyWebSocketServletContainerInitializer**`
|
||||||
|
|
||||||
|
| `org.eclipse.jetty.websocket.**jsr356.server.deploy.WebSocketServerContainerInitializer**`
|
||||||
|
| `org.eclipse.jetty.websocket.**javax.server.config.JavaxWebSocketServletContainerInitializer**`
|
||||||
|
|
||||||
|
| `org.eclipse.jetty.websocket.**servlet.WebSocketCreator**`
|
||||||
|
| `org.eclipse.jetty.websocket.**server.JettyWebSocketCreator**`
|
||||||
|
|
||||||
|
| `org.eclipse.jetty.websocket.**servlet.ServletUpgradeRequest**`
|
||||||
|
| `org.eclipse.jetty.websocket.**server.JettyServerUpgradeRequest**`
|
||||||
|
|
||||||
|
| `org.eclipse.jetty.websocket.**servlet.ServletUpgradeResponse**`
|
||||||
|
| `org.eclipse.jetty.websocket.**server.JettyServerUpgradeResponse**`
|
||||||
|
|
||||||
|
| `org.eclipse.jetty.websocket.**servlet.WebSocketServlet**`
|
||||||
|
| `org.eclipse.jetty.websocket.**server.JettyWebSocketServlet**`
|
||||||
|
|
||||||
|
| `org.eclipse.jetty.websocket.**servlet.WebSocketServletFactory**`
|
||||||
|
| `org.eclipse.jetty.websocket.**server.JettyWebSocketServletFactory**`
|
||||||
|
|===
|
||||||
|
|
||||||
|
[[websocket-example-code]]
|
||||||
|
=== Example Code
|
||||||
|
|
||||||
|
[cols="1a,1a", options="header"]
|
||||||
|
|===
|
||||||
|
| Jetty 9.4.x
|
||||||
|
| Jetty 10.0.x
|
||||||
|
|
||||||
|
|
|
||||||
|
[,java]
|
||||||
|
----
|
||||||
|
public class ExampleWebSocketServlet extends WebSocketServlet
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void configure(WebSocketServletFactory factory)
|
||||||
|
{
|
||||||
|
factory.setCreator(new WebSocketCreator()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp)
|
||||||
|
{
|
||||||
|
return new ExampleEndpoint();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|
||||||
|
|
|
||||||
|
[,java]
|
||||||
|
----
|
||||||
|
public class ExampleWebSocketServlet extends JettyWebSocketServlet
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void configure(JettyWebSocketServletFactory factory)
|
||||||
|
{
|
||||||
|
factory.setCreator(new JettyWebSocketCreator()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public Object createWebSocket(JettyServerUpgradeRequest req, JettyServerUpgradeResponse resp)
|
||||||
|
{
|
||||||
|
return new ExampleEndpoint();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|===
|
|
@ -0,0 +1,121 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Server Compliance Modes
|
||||||
|
|
||||||
|
The Jetty server strives to keep up with the latest https://en.wikipedia.org/wiki/Request_for_Comments[IETF RFC]s for compliance with internet specifications, which are periodically updated.
|
||||||
|
|
||||||
|
When possible, Jetty will support backwards compatibility by providing compliance modes that can be configured to allow violations of the current specifications that may have been allowed in obsoleted specifications.
|
||||||
|
|
||||||
|
There are compliance modes provided for:
|
||||||
|
|
||||||
|
* <<http,HTTP Compliance>>
|
||||||
|
* <<uri,URI Compliance>>
|
||||||
|
* <<cookie,Cookie Compliance>>
|
||||||
|
|
||||||
|
Compliance modes can be configured to allow violations from the RFC requirements, or in some cases to allow additional behaviors that Jetty has implemented in excess of the RFC (for example, to allow <<uri,ambiguous URIs>>).
|
||||||
|
|
||||||
|
For example, the HTTP RFCs require that request HTTP methods are https://datatracker.ietf.org/doc/html/rfc7230#section-3.1.1[case sensitive], however Jetty can allow case-insensitive HTTP methods by including the link:{javadoc-url}/org/eclipse/jetty/http/HttpCompliance.Violation.html#CASE_INSENSITIVE_METHOD[`HttpCompliance.Violation.CASE_INSENSITIVE_METHOD`] in the link:{javadoc-url}/org/eclipse/jetty/http/HttpCompliance.html[`HttpCompliance`] set of allowed violations.
|
||||||
|
|
||||||
|
[[http]]
|
||||||
|
== HTTP Compliance Modes
|
||||||
|
|
||||||
|
In 1995, when Jetty was first implemented, there were no RFC specification of HTTP, only a W3C specification for https://www.w3.org/Protocols/HTTP/AsImplemented.html[HTTP/0.9], which has since been obsoleted or augmented by:
|
||||||
|
|
||||||
|
* https://datatracker.ietf.org/doc/html/rfc1945[RFC 1945] for HTTP/1.0 in 1996
|
||||||
|
* https://datatracker.ietf.org/doc/html/rfc2068[RFC 2068] for HTTP/1.1 in 1997
|
||||||
|
* https://datatracker.ietf.org/doc/html/rfc2616[RFC 2616] for HTTP/1.1 bis in 1999
|
||||||
|
* https://datatracker.ietf.org/doc/html/rfc7230[RFC 7230], https://datatracker.ietf.org/doc/html/rfc7231[RFC 7231], https://datatracker.ietf.org/doc/html/rfc7232[RFC 7232], https://datatracker.ietf.org/doc/html/rfc7233[RFC 7233], https://datatracker.ietf.org/doc/html/rfc7234[RFC 7234], https://datatracker.ietf.org/doc/html/rfc7235[RFC 7235] again for HTTP/1.1 in 2014
|
||||||
|
* https://datatracker.ietf.org/doc/html/rfc7540[RFC 7540] for HTTP/2.0 in 2015
|
||||||
|
|
||||||
|
In addition to these evolving requirements, some earlier version of Jetty did not completely or strictly implement the RFC at the time (for example, case-insensitive HTTP methods).
|
||||||
|
Therefore, upgrading to a newer Jetty version may cause runtime behavior differences that may break your applications.
|
||||||
|
|
||||||
|
The link:{javadoc-url}/org/eclipse/jetty/http/HttpCompliance.Violation.html[`HttpCompliance.Violation`] enumeration defines the RFC requirements that may be optionally enforced by Jetty, to support legacy deployments. These possible violations are grouped into modes by the link:{javadoc-url}/org/eclipse/jetty/http/HttpCompliance.html[`HttpCompliance`] class, which also defines several named modes that support common deployed sets of violations (with the default being link:{javadoc-url}/org/eclipse/jetty/http/HttpCompliance.html#RFC7230[`HttpCompliance.RFC7230`]).
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=httpCompliance]
|
||||||
|
----
|
||||||
|
|
||||||
|
If you want to customize the violations that you want to allow, you can create your own mode using the link:{javadoc-url}/org/eclipse/jetty/http/HttpCompliance.html#from(java.lang.String)[`HttpCompliance.from(String)`] method:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=httpComplianceCustom]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[uri]]
|
||||||
|
== URI Compliance Modes
|
||||||
|
|
||||||
|
Universal Resource Locators (URLs) where initially formalized in 1994 in https://datatracker.ietf.org/doc/html/rfc1738[RFC 1738] and then refined in 1995 with relative URLs by https://datatracker.ietf.org/doc/html/rfc1808[RFC 1808].
|
||||||
|
|
||||||
|
In 1998, URLs were generalized to Universal Resource Identifiers (URIs) by https://datatracker.ietf.org/doc/html/rfc2396[RFC 2396], which also introduced features such a https://datatracker.ietf.org/doc/html/rfc2396#section-3.3[path parameter]s.
|
||||||
|
|
||||||
|
This was then obsoleted in 2005 by https://datatracker.ietf.org/doc/html/rfc3986[RFC 3986] which removed the definition for path parameters.
|
||||||
|
|
||||||
|
Unfortunately by this stage the existence and use of such parameters had already been codified in the Servlet specification.
|
||||||
|
For example, the relative URI `/foo/bar;JSESSIONID=a8b38cd02b1c` would define the path parameter `JSESSIONID` for the path segment `bar`, but the most recent RFC does not specify a formal definition of what this relative URI actually means.
|
||||||
|
|
||||||
|
The current situation is that there may be URIs that are entirely valid for https://datatracker.ietf.org/doc/html/rfc3986[RFC 3986], but are ambiguous when handled by the Servlet APIs:
|
||||||
|
|
||||||
|
* A URI with `..` _and_ path parameters such as `/some/..;/path` is not https://datatracker.ietf.org/doc/html/rfc3986#section-5.2[_resolved_] by RFC 3986, since the resolution process only applies to the exact segment `..`, not to `..;`.
|
||||||
|
However, once the path parameters are removed by the Servlet APIs, the resulting `/some/../path` can easily be resolved to `/path`, rather than be treated as a path that has `..;` as a segment.
|
||||||
|
* A URI such as `/some/%2e%2e/path` is not resolved by RFC 3986, yet when URL-decoded by the Servlet APIs will result in `/some/../path` which can easily be resolved to `/path`, rather than be treated as a path that has `..` as a segment.
|
||||||
|
* A URI with empty segments like `/some//../path` may be correctly resolved to `/some/path` (the `..` removes the previous empty segment) by the Servlet APIs.
|
||||||
|
However, if the URI raw path is passed to some other APIs (for example, file system APIs) it can be interpreted as `/path` because the empty segment `//` is discarded and treated as `/`, and the `..` thus removes the `/some` segment.
|
||||||
|
|
||||||
|
In order to avoid ambiguous URIs, Jetty imposes additional URI requirements in excess of what is required by https://datatracker.ietf.org/doc/html/rfc3986[RFC 3986] compliance.
|
||||||
|
|
||||||
|
These additional requirements may optionally be violated and are defined by the link:{javadoc-url}/org/eclipse/jetty/http/UriCompliance.Violation.html[`UriCompliance.Violation`] enumeration.
|
||||||
|
|
||||||
|
These violations are then grouped into modes by the link:{javadoc-url}/org/eclipse/jetty/http/UriCompliance.html[`UriCompliance`] class, which also defines several named modes that support common deployed sets of violations, with the default being link:{javadoc-url}/org/eclipse/jetty/http/UriCompliance.html#DEFAULT[`UriCompliance.DEFAULT`].
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=uriCompliance]
|
||||||
|
----
|
||||||
|
|
||||||
|
If you want to customize the violations that you want to allow, you can create your own mode using the link:{javadoc-url}/org/eclipse/jetty/http/UriCompliance.html#from(java.lang.String)[`UriCompliance.from(String)`] method:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=uriComplianceCustom]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[cookie]]
|
||||||
|
== Cookie Compliance Modes
|
||||||
|
|
||||||
|
The standards for Cookies have varied greatly over time from a non-specified but de-facto standard (implemented by the first browsers), through https://tools.ietf.org/html/rfc2965[RFC 2965] and currently to https://tools.ietf.org/html/rfc6265[RFC 6265].
|
||||||
|
|
||||||
|
The link:{javadoc-url}/org/eclipse/jetty/http/CookieCompliance.Violation.html[CookieCompliance.Violation] enumeration defines the RFC requirements that may be optionally enforced by Jetty when parsing the `Cookie` HTTP header in requests and when generating the `Set-Cookie` HTTP header in responses.
|
||||||
|
|
||||||
|
These violations are then grouped into modes by the link:{javadoc-url}/org/eclipse/jetty/http/CookieCompliance.html[`CookieCompliance`] class, which also defines several named modes that support common deployed sets of violations, with the default being link:{javadoc-url}/org/eclipse/jetty/http/CookieCompliance.html#RFC6265[`CookieCompliance.RFC6265`].
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=cookieCompliance]
|
||||||
|
----
|
||||||
|
|
||||||
|
If you want to customize the violations that you want to allow, you can create your own mode using the link:{javadoc-url}/org/eclipse/jetty/http/CookieCompliance.html#from(java.lang.String)[`CookieCompliance.from(String)`] method:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=cookieComplianceCustom]
|
||||||
|
----
|
|
@ -0,0 +1,16 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= FastCGI Server Libraries
|
||||||
|
|
||||||
|
TODO
|
|
@ -0,0 +1,960 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= HTTP Server Libraries
|
||||||
|
|
||||||
|
Web application development typically involves writing your web applications, packaging them into a web application archive, the `+*.war+` file, and then deploy the `+*.war+` file into a standalone Servlet Container that you have previously installed.
|
||||||
|
|
||||||
|
The Eclipse Jetty server libraries allow you to write web applications components using either the Jetty APIs (by writing <<handler,Jetty ``Handler``s>>) or using the standard <<handler-use-servlet,Servlet APIs>> (by writing ``Servlet``s and Servlet ``Filter``s).
|
||||||
|
These components can then be programmatically assembled together, without the need of creating a `+*.war+` file, added to a Jetty ``Server`` instance that is then started.
|
||||||
|
This result in your web applications to be available to HTTP clients as if you deployed your `+*.war+` files in a standalone Jetty server.
|
||||||
|
|
||||||
|
The Maven artifact coordinates are:
|
||||||
|
|
||||||
|
[,xml,subs=normal]
|
||||||
|
----
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-server</artifactId>
|
||||||
|
<version>{version}</version>
|
||||||
|
</dependency>
|
||||||
|
----
|
||||||
|
|
||||||
|
An `org.eclipse.jetty.server.Server` instance is the central component that links together a collection of ``Connector``s and a collection of ``Handler``s, with threads from a `ThreadPool` doing the work.
|
||||||
|
|
||||||
|
[plantuml]
|
||||||
|
----
|
||||||
|
skinparam backgroundColor transparent
|
||||||
|
skinparam monochrome true
|
||||||
|
skinparam shadowing false
|
||||||
|
skinparam padding 5
|
||||||
|
|
||||||
|
scale 1.5
|
||||||
|
|
||||||
|
hide members
|
||||||
|
hide circle
|
||||||
|
|
||||||
|
Server - ThreadPool
|
||||||
|
Connectors - Server
|
||||||
|
Server -- Handlers
|
||||||
|
----
|
||||||
|
|
||||||
|
The components that accept connections from clients are `org.eclipse.jetty.server.Connector` implementations.
|
||||||
|
|
||||||
|
When a Jetty server interprets the HTTP protocol (HTTP/1.1, HTTP/2 or HTTP/3), it uses `org.eclipse.jetty.server.Handler` instances to process incoming requests and eventually produce responses.
|
||||||
|
|
||||||
|
A `Server` must be created, configured and started:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=simple]
|
||||||
|
----
|
||||||
|
|
||||||
|
The example above shows the simplest HTTP/1.1 server; it has no support for HTTP sessions, for HTTP authentication, or for any of the features required by the Servlet specification.
|
||||||
|
|
||||||
|
All these features are provided by the Jetty Server Libraries, and server applications only need to put the required components together to provide all the required features.
|
||||||
|
|
||||||
|
The ``Handler``s provided by the Jetty Server Libraries allow writing server applications that have functionalities similar to Apache HTTPD or Nginx (for example: URL redirection, URL rewriting, serving static content, reverse proxying, etc.), as well as generating content dynamically by processing incoming requests.
|
||||||
|
Read <<handler,this section>> for further details about ``Handler``s.
|
||||||
|
|
||||||
|
If you are interested in writing your server application based on the Servlet APIs, jump to <<handler-use-servlet,this section>>.
|
||||||
|
|
||||||
|
[[request-processing]]
|
||||||
|
== Request Processing
|
||||||
|
|
||||||
|
The Jetty HTTP request processing is outlined below in the diagram below.
|
||||||
|
You may want to refer to the xref:arch/io.adoc[Jetty I/O architecture] for additional information about the classes mentioned below.
|
||||||
|
|
||||||
|
Request handing is slightly different for each protocol; in HTTP/2 Jetty takes into account multiplexing, something that is not present in HTTP/1.1.
|
||||||
|
|
||||||
|
However, the diagram below captures the essence of request handling that is common among all protocols that carry HTTP requests.
|
||||||
|
|
||||||
|
[plantuml]
|
||||||
|
----
|
||||||
|
skinparam backgroundColor transparent
|
||||||
|
skinparam monochrome true
|
||||||
|
skinparam shadowing false
|
||||||
|
|
||||||
|
participant ManagedSelector
|
||||||
|
participant EndPoint
|
||||||
|
participant Connection
|
||||||
|
participant Parser
|
||||||
|
participant HttpChannel
|
||||||
|
participant Server
|
||||||
|
participant Handlers
|
||||||
|
|
||||||
|
ManagedSelector -> EndPoint : read ready
|
||||||
|
EndPoint -> Connection : onFillable()
|
||||||
|
Connection -> EndPoint : fill()
|
||||||
|
EndPoint --> Connection
|
||||||
|
Connection -> Parser : parse()
|
||||||
|
Parser -> HttpChannel : events
|
||||||
|
Connection -> HttpChannel : handle()
|
||||||
|
HttpChannel -> Server : handle()
|
||||||
|
Server -> Handlers : handle()
|
||||||
|
----
|
||||||
|
|
||||||
|
First, the Jetty I/O layer emits an event that a socket has data to read.
|
||||||
|
This event is converted to a call to `AbstractConnection.onFillable()`, where the `Connection` first reads from the `EndPoint` into a `ByteBuffer`, and then calls a protocol specific parser to parse the bytes in the `ByteBuffer`.
|
||||||
|
|
||||||
|
The parser emit events that are protocol specific; the HTTP/2 parser, for example, emits events for each HTTP/2 frame that has been parsed, and similarly does the HTTP/3 parser.
|
||||||
|
The parser events are then converted to protocol independent events such as _"request start"_, _"request headers"_, _"request content chunk"_, etc.
|
||||||
|
that in turn are converted into method calls to `HttpChannel`.
|
||||||
|
|
||||||
|
When enough of the HTTP request is arrived, the `Connection` calls `HttpChannel.handle()` that calls the `Handler` chain, that eventually calls the server application code.
|
||||||
|
|
||||||
|
[[channel-events]]
|
||||||
|
=== HttpChannel Events
|
||||||
|
|
||||||
|
The central component processing HTTP requests is `HttpChannel`.
|
||||||
|
There is a 1-to-1 relationship between an HTTP request/response and an `HttpChannel`, no matter what is the specific protocol that carries the HTTP request over the network (HTTP/1.1, HTTP/2, HTTP/3 or FastCGI).
|
||||||
|
|
||||||
|
Advanced server applications may be interested in the progress of the processing of an HTTP request/response by `HttpChannel`.
|
||||||
|
A typical case is to know exactly _when_ the HTTP request/response processing is complete, for example to monitor processing times.
|
||||||
|
|
||||||
|
NOTE: A `Handler` or a Servlet `Filter` may not report precisely when an HTTP request/response processing is finished.
|
||||||
|
A server application may write a small enough content that is aggregated by Jetty for efficiency reasons; the write returns immediately, but nothing has been written to the network yet.
|
||||||
|
|
||||||
|
`HttpChannel` notifies ``HttpChannel.Listener``s of the progress of the HTTP request/response handling.
|
||||||
|
Currently, the following events are available:
|
||||||
|
|
||||||
|
* `requestBegin`
|
||||||
|
* `beforeDispatch`
|
||||||
|
* `dispatchFailure`
|
||||||
|
* `afterDispatch`
|
||||||
|
* `requestContent`
|
||||||
|
* `requestContentEnd`
|
||||||
|
* `requestTrailers`
|
||||||
|
* `requestEnd`
|
||||||
|
* `responseBegin`
|
||||||
|
* `responseCommit`
|
||||||
|
* `responseContent`
|
||||||
|
* `responseFailure`
|
||||||
|
* `responseEnd`
|
||||||
|
* `complete`
|
||||||
|
|
||||||
|
Please refer to the `HttpChannel.Listener` link:{javadoc-url}/org/eclipse/jetty/server/HttpChannel.Listener.html[javadocs] for the complete list of events.
|
||||||
|
|
||||||
|
Server applications can register `HttpChannel.Listener` by adding them as xref:arch/bean.adoc[beans] to the `Connector`:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=httpChannelListener]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[request-logging]]
|
||||||
|
== Request Logging
|
||||||
|
|
||||||
|
HTTP requests and responses can be logged to provide data that can be later analyzed with other tools.
|
||||||
|
These tools can provide information such as the most frequently accessed request URIs, the response status codes, the request/response content lengths, geographical information about the clients, etc.
|
||||||
|
|
||||||
|
The default request/response log line format is the https://en.wikipedia.org/wiki/Common_Log_Format[NCSA Format] extended with referrer data and user-agent data.
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
Typically, the extended NCSA format is the is enough and it's the standard used and understood by most log parsing tools and monitoring tools.
|
||||||
|
|
||||||
|
To customize the request/response log line format see the link:{javadoc-url}/org/eclipse/jetty/server/CustomRequestLog.html[`CustomRequestLog` javadocs].
|
||||||
|
====
|
||||||
|
|
||||||
|
Request logging can be enabled at the server level, or at the web application context level.
|
||||||
|
|
||||||
|
The request logging output can be directed to an SLF4J logger named `"org.eclipse.jetty.server.RequestLog"` at `INFO` level, and therefore to any logging library implementation of your choice (see also xref:troubleshooting/logging.adoc[this section] about logging).
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=serverRequestLogSLF4J]
|
||||||
|
----
|
||||||
|
|
||||||
|
Alternatively, the request logging output can be directed to a daily rolling file of your choice, and the file name must contain `yyyy_MM_dd` so that rolled over files retain their date:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=serverRequestLogFile]
|
||||||
|
----
|
||||||
|
|
||||||
|
For maximum flexibility, you can log to multiple ``RequestLog``s using class `RequestLog.Collection`, for example by logging with different formats or to different outputs.
|
||||||
|
|
||||||
|
You can use `CustomRequestLog` with a custom `RequestLog.Writer` to direct the request logging output to your custom targets (for example, an RDBMS).
|
||||||
|
You can implement your own `RequestLog` if you want to have functionalities that are not implemented by `CustomRequestLog`.
|
||||||
|
|
||||||
|
Request logging can also be enabled at the web application context level, using `RequestLogHandler` (see <<handler,this section>> about how to organize Jetty ``Handler``s) to wrap a web application `Handler`:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=contextRequestLog]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[connector]]
|
||||||
|
== Server Connectors
|
||||||
|
|
||||||
|
A `Connector` is the component that handles incoming requests from clients, and works in conjunction with `ConnectionFactory` instances.
|
||||||
|
|
||||||
|
The available implementations are:
|
||||||
|
|
||||||
|
* `org.eclipse.jetty.server.ServerConnector`, for TCP/IP sockets.
|
||||||
|
* `org.eclipse.jetty.unixdomain.server.UnixDomainServerConnector` for Unix-Domain sockets (requires Java 16 or later).
|
||||||
|
|
||||||
|
Both use a `java.nio.channels.ServerSocketChannel` to listen to a socket address and to accept socket connections.
|
||||||
|
|
||||||
|
Since `ServerConnector` wraps a `ServerSocketChannel`, it can be configured in a similar way, for example the IP port to listen to, the IP address to bind to, etc.:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=configureConnector]
|
||||||
|
----
|
||||||
|
|
||||||
|
Likewise, `UnixDomainServerConnector` also wraps a `ServerSocketChannel` and can be configured with the Unix-Domain path to listen to:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=configureConnectorUnix]
|
||||||
|
----
|
||||||
|
|
||||||
|
[IMPORTANT]
|
||||||
|
====
|
||||||
|
You can use Unix-Domain sockets support only when you run your server with Java 16 or later.
|
||||||
|
====
|
||||||
|
|
||||||
|
The _acceptors_ are threads (typically only one) that compete to accept socket connections.
|
||||||
|
When a connection is accepted, `ServerConnector` wraps the accepted `SocketChannel` and passes it to the xref:arch/io.adoc#selector-manager[`SelectorManager`].
|
||||||
|
Therefore, there is a little moment where the acceptor thread is not accepting new connections because it is busy wrapping the just accepted connection to pass it to the `SelectorManager`.
|
||||||
|
Connections that are ready to be accepted but are not accepted yet are queued in a bounded queue (at the OS level) whose capacity can be configured with the `acceptQueueSize` parameter.
|
||||||
|
|
||||||
|
If your application must withstand a very high rate of connections opened, configuring more than one acceptor thread may be beneficial: when one acceptor thread accepts one connection, another acceptor thread can take over accepting connections.
|
||||||
|
|
||||||
|
The _selectors_ are components that manage a set of connected sockets, implemented by xref:arch/io.adoc#selector-manager[`ManagedSelector`].
|
||||||
|
Each selector requires one thread and uses the Java NIO mechanism to efficiently handle a set of connected sockets.
|
||||||
|
As a rule of thumb, a single selector can easily manage up to 1000-5000 sockets, although the number may vary greatly depending on the application.
|
||||||
|
|
||||||
|
For example, web site applications tend to use sockets for one or more HTTP requests to retrieve resources and then the socket is idle for most of the time.
|
||||||
|
In this case a single selector may be able to manage many sockets because chances are that they will be idle most of the time.
|
||||||
|
On the contrary, web messaging applications tend to send many small messages at a very high frequency so that sockets are rarely idle.
|
||||||
|
In this case a single selector may be able to manage less sockets because chances are that many of them will be active at the same time.
|
||||||
|
|
||||||
|
It is possible to configure more than one `ServerConnector` (each listening on a different port), or more than one `UnixDomainServerConnector` (each listening on a different path), or ``ServerConnector``s and ``UnixDomainServerConnector``s, for example:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=configureConnectors]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[connector-protocol]]
|
||||||
|
=== Configuring Protocols
|
||||||
|
|
||||||
|
For each accepted socket connection, the server `Connector` asks a `ConnectionFactory` to create a `Connection` object that handles the traffic on that socket connection, parsing and generating bytes for a specific protocol (see xref:arch/io.adoc[this section] for more details about `Connection` objects).
|
||||||
|
|
||||||
|
A server `Connector` can be configured with one or more ``ConnectionFactory``s.
|
||||||
|
If no `ConnectionFactory` is specified then `HttpConnectionFactory` is implicitly configured.
|
||||||
|
|
||||||
|
[[connector-protocol-http11]]
|
||||||
|
==== Clear-Text HTTP/1.1
|
||||||
|
|
||||||
|
`HttpConnectionFactory` creates `HttpConnection` objects that parse bytes and generate bytes for the HTTP/1.1 protocol.
|
||||||
|
|
||||||
|
This is how you configure Jetty to support clear-text HTTP/1.1:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=http11]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[connector-protocol-http11-tls]]
|
||||||
|
==== Encrypted HTTP/1.1 (https)
|
||||||
|
|
||||||
|
Supporting encrypted HTTP/1.1 (that is, requests with the `https` scheme) is supported by configuring an `SslContextFactory` that has access to the KeyStore containing the private server key and public server certificate, in this way:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=tlsHttp11]
|
||||||
|
----
|
||||||
|
|
||||||
|
You can customize the SSL/TLS provider as explained in <<connector-protocol-tls-conscrypt,this section>>.
|
||||||
|
|
||||||
|
[[connector-protocol-http2]]
|
||||||
|
==== Clear-Text HTTP/2
|
||||||
|
|
||||||
|
It is well known that the HTTP ports are `80` (for clear-text HTTP) and `443` for encrypted HTTP.
|
||||||
|
By using those ports, a client had _prior knowledge_ that the server would speak, respectively, the HTTP/1.x protocol and the TLS protocol (and, after decryption, the HTTP/1.x protocol).
|
||||||
|
|
||||||
|
HTTP/2 was designed to be a smooth transition from HTTP/1.1 for users and as such the HTTP ports were not changed.
|
||||||
|
However the HTTP/2 protocol is, on the wire, a binary protocol, completely different from HTTP/1.1.
|
||||||
|
Therefore, with HTTP/2, clients that connect to port `80` (or to a specific Unix-Domain path) may speak either HTTP/1.1 or HTTP/2, and the server must figure out which version of the HTTP protocol the client is speaking.
|
||||||
|
|
||||||
|
Jetty can support both HTTP/1.1 and HTTP/2 on the same clear-text port by configuring both the HTTP/1.1 and the HTTP/2 ``ConnectionFactory``s:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=http11H2C]
|
||||||
|
----
|
||||||
|
|
||||||
|
Note how the ``ConnectionFactory``s passed to `ServerConnector` are in order: first HTTP/1.1, then HTTP/2.
|
||||||
|
This is necessary to support both protocols on the same port: Jetty will start parsing the incoming bytes as HTTP/1.1, but then realize that they are HTTP/2 bytes and will therefore _upgrade_ from HTTP/1.1 to HTTP/2.
|
||||||
|
|
||||||
|
This configuration is also typical when Jetty is installed in backend servers behind a load balancer that also takes care of offloading TLS.
|
||||||
|
When Jetty is behind a load balancer, you can always prepend the PROXY protocol as described in <<connector-protocol-proxy-http11,this section>>.
|
||||||
|
|
||||||
|
[[connector-protocol-http2-tls]]
|
||||||
|
==== Encrypted HTTP/2
|
||||||
|
|
||||||
|
When using encrypted HTTP/2, the unencrypted protocol is negotiated by client and server using an extension to the TLS protocol called ALPN.
|
||||||
|
|
||||||
|
Jetty supports ALPN and encrypted HTTP/2 with this configuration:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=tlsALPNHTTP]
|
||||||
|
----
|
||||||
|
|
||||||
|
Note how the ``ConnectionFactory``s passed to `ServerConnector` are in order: TLS, ALPN, HTTP/2, HTTP/1.1.
|
||||||
|
|
||||||
|
Jetty starts parsing TLS bytes so that it can obtain the ALPN extension.
|
||||||
|
With the ALPN extension information, Jetty can negotiate a protocol and pick, among the ``ConnectionFactory``s supported by the `ServerConnector`, the `ConnectionFactory` correspondent to the negotiated protocol.
|
||||||
|
|
||||||
|
The fact that the HTTP/2 protocol comes before the HTTP/1.1 protocol indicates that HTTP/2 is the preferred protocol for the server.
|
||||||
|
|
||||||
|
Note also that the default protocol set in the ALPN ``ConnectionFactory``, which is used in case ALPN is not supported by the client, is HTTP/1.1 -- if the client does not support ALPN is probably an old client so HTTP/1.1 is the safest choice.
|
||||||
|
|
||||||
|
You can customize the SSL/TLS provider as explained in <<connector-protocol-tls-conscrypt,this section>>.
|
||||||
|
|
||||||
|
[[connector-protocol-http3]]
|
||||||
|
==== HTTP/3
|
||||||
|
|
||||||
|
HTTP/3 is based on UDP, differently from HTTP/1 and HTTP/2 that are based on TCP.
|
||||||
|
|
||||||
|
An HTTP/3 client may initiate a connection (using the QUIC protocol via UDP) on the canonical HTTP secure port `443`, but chances are that the connection may not succeed (for example, the server does not listen for UDP on port `443`, only listens for TCP).
|
||||||
|
|
||||||
|
For this reason, HTTP servers typically listen on the canonical HTTP secure port `443` for HTTP/1 and HTTP/2, and advertise the availability HTTP/3 as an https://datatracker.ietf.org/doc/html/rfc7838[_HTTP alternate service_] on a different port (and possibly a different host).
|
||||||
|
|
||||||
|
For example, an HTTP/2 response may include the following header:
|
||||||
|
|
||||||
|
[source]
|
||||||
|
----
|
||||||
|
Alt-Svc: h3=":843"
|
||||||
|
----
|
||||||
|
|
||||||
|
The presence of this header indicates that protocol `h3` is available on the same host (since no host is defined before the port), but on port `843`.
|
||||||
|
The HTTP/3 client may now initiate a QUIC connection on port `843` and make HTTP/3 requests.
|
||||||
|
|
||||||
|
[plantuml]
|
||||||
|
----
|
||||||
|
skinparam backgroundColor transparent
|
||||||
|
skinparam monochrome true
|
||||||
|
skinparam shadowing false
|
||||||
|
|
||||||
|
participant client
|
||||||
|
participant "server:443" as h2server
|
||||||
|
participant "server:843" as h3server
|
||||||
|
|
||||||
|
client -> h2server : HTTP/2 request
|
||||||
|
h2server -> client : HTTP/2 response\nAlt-Svc: h3=":843"
|
||||||
|
client -> h3server : HTTP/3 requests
|
||||||
|
h3server -> client : HTTP/3 responses
|
||||||
|
...
|
||||||
|
----
|
||||||
|
|
||||||
|
The code necessary to configure HTTP/2 is described in <<connector-protocol-http2-tls,this section>>.
|
||||||
|
|
||||||
|
To setup HTTP/3, for example on port `843`, you need the following code (some of which could be shared with other connectors such as HTTP/2's):
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=h3]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[connector-protocol-tls-conscrypt]]
|
||||||
|
==== Using Conscrypt as SSL/TLS Provider
|
||||||
|
|
||||||
|
If not explicitly configured, the TLS implementation is provided by the JDK you are using at runtime.
|
||||||
|
|
||||||
|
OpenJDK's vendors may replace the default TLS provider with their own, but you can also explicitly configure an alternative TLS provider.
|
||||||
|
|
||||||
|
The standard TLS provider from OpenJDK is implemented in Java (no native code), and its performance is not optimal, both in CPU usage and memory usage.
|
||||||
|
|
||||||
|
A faster alternative, implemented natively, is Google's https://github.com/google/conscrypt/[Conscrypt], which is built on https://boringssl.googlesource.com/boringssl/[BoringSSL], which is Google's fork of https://www.openssl.org/[OpenSSL].
|
||||||
|
|
||||||
|
CAUTION: As Conscrypt eventually binds to a native library, there is a higher risk that a bug in Conscrypt or in the native library causes a JVM crash, while the Java implementation will not cause a JVM crash.
|
||||||
|
|
||||||
|
To use Conscrypt as TLS provider, you must have the Conscrypt jar and the Jetty dependency `jetty-alpn-conscrypt-server-{version}.jar` in the class-path or module-path.
|
||||||
|
|
||||||
|
Then, you must configure the JDK with the Conscrypt provider, and configure Jetty to use the Conscrypt provider, in this way:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=conscrypt]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[connector-protocol-proxy-http11]]
|
||||||
|
==== Jetty Behind a Load Balancer
|
||||||
|
|
||||||
|
It is often the case that Jetty receives connections from a load balancer configured to distribute the load among many Jetty backend servers.
|
||||||
|
|
||||||
|
From the Jetty point of view, all the connections arrive from the load balancer, rather than the real clients, but is possible to configure the load balancer to forward the real client IP address and IP port to the backend Jetty server using the https://www.haproxy.org/download/2.1/doc/proxy-protocol.txt[PROXY protocol].
|
||||||
|
|
||||||
|
NOTE: The PROXY protocol is widely supported by load balancers such as http://cbonte.github.io/haproxy-dconv/2.2/configuration.html#5.2-send-proxy[HAProxy] (via its `send-proxy` directive), https://docs.nginx.com/nginx/admin-guide/load-balancer/using-proxy-protocol[Nginx](via its `proxy_protocol on` directive) and others.
|
||||||
|
|
||||||
|
To support this case, Jetty can be configured in this way:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=proxyHTTP]
|
||||||
|
----
|
||||||
|
|
||||||
|
Note how the ``ConnectionFactory``s passed to `ServerConnector` are in order: first PROXY, then HTTP/1.1.
|
||||||
|
Note also how the PROXY `ConnectionFactory` needs to know its _next_ protocol (in this example, HTTP/1.1).
|
||||||
|
|
||||||
|
Each `ConnectionFactory` is asked to create a `Connection` object for each accepted TCP connection; the `Connection` objects will be chained together to handle the bytes, each for its own protocol.
|
||||||
|
Therefore the `ProxyConnection` will handle the PROXY protocol bytes and `HttpConnection` will handle the HTTP/1.1 bytes producing a request object and response object that will be processed by ``Handler``s.
|
||||||
|
|
||||||
|
The load balancer may be configured to communicate with Jetty backend servers via Unix-Domain sockets (requires Java 16 or later).
|
||||||
|
For example:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=proxyHTTPUnix]
|
||||||
|
----
|
||||||
|
|
||||||
|
Note that the only difference when using Unix-Domain sockets is instantiating `UnixDomainServerConnector` instead of `ServerConnector` and configuring the Unix-Domain path instead of the IP port.
|
||||||
|
|
||||||
|
[[handler]]
|
||||||
|
== Server Handlers
|
||||||
|
|
||||||
|
An `org.eclipse.jetty.server.Handler` is the component that processes incoming HTTP requests and eventually produces HTTP responses.
|
||||||
|
|
||||||
|
``Handler``s can be organized in different ways:
|
||||||
|
|
||||||
|
* in a sequence, where ``Handler``s are invoked one after the other
|
||||||
|
** `HandlerCollection` invokes _all_ ``Handler``s one after the other
|
||||||
|
** `HandlerList` invokes ``Handlers``s until one calls `Request.setHandled(true)` to indicate that the request has been handled and no further `Handler` should be invoked
|
||||||
|
* nested, where one `Handler` invokes the next, nested, `Handler`
|
||||||
|
** `HandlerWrapper` implements this behavior
|
||||||
|
|
||||||
|
The `HandlerCollection` behavior (invoking _all_ handlers) is useful when for example the last `Handler` is a logging `Handler` that logs the request (that may have been modified by previous handlers).
|
||||||
|
|
||||||
|
The `HandlerList` behavior (invoking handlers up to the first that calls `Request.setHandled(true)`) is useful when each handler processes a different URIs or a different virtual hosts: ``Handler``s are invoked one after the other until one matches the URI or virtual host.
|
||||||
|
|
||||||
|
The nested behavior is useful to enrich the request with additional services such as HTTP session support (`SessionHandler`), or with specific behaviors dictated by the Servlet specification (`ServletHandler`).
|
||||||
|
|
||||||
|
``Handler``s can be organized in a tree by composing them together:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=handlerTree]
|
||||||
|
----
|
||||||
|
|
||||||
|
The corresponding `Handler` tree structure looks like the following:
|
||||||
|
|
||||||
|
[,screen]
|
||||||
|
----
|
||||||
|
HandlerCollection
|
||||||
|
├── HandlerList
|
||||||
|
│ ├── App1Handler
|
||||||
|
│ └── HandlerWrapper
|
||||||
|
│ └── App2Handler
|
||||||
|
└── LoggingHandler
|
||||||
|
----
|
||||||
|
|
||||||
|
Server applications should rarely write custom ``Handler``s, preferring instead to use existing ``Handler``s provided by the Jetty Server Libraries for managing web application contexts, security, HTTP sessions and Servlet support.
|
||||||
|
Refer to <<handler-use,this section>> for more information about how to use the ``Handler``s provided by the Jetty Server Libraries.
|
||||||
|
|
||||||
|
However, in some cases the additional features are not required, or additional constraints on memory footprint, or performance, or just simplicity must be met.
|
||||||
|
In these cases, implementing your own `Handler` may be a better solution.
|
||||||
|
Refer to <<handler-implement,this section>> for more information about how to write your own ``Handler``s.
|
||||||
|
|
||||||
|
[[handler-use]]
|
||||||
|
=== Jetty Handlers
|
||||||
|
|
||||||
|
Web applications are the unit of deployment in an HTTP server or Servlet container such as Jetty.
|
||||||
|
|
||||||
|
Two different web applications are typically deployed on different __context path__s, where a _context path_ is the initial segment of the URI path.
|
||||||
|
For example, web application `webappA` that implements a web user interface for an e-commerce site may be deployed to context path `/shop`, while web application `webappB` that implements a REST API for the e-commerce business may be deployed to `/api`.
|
||||||
|
|
||||||
|
A client making a request to URI `/shop/cart` is directed by Jetty to `webappA`, while a request to URI `/api/products` is directed to `webappB`.
|
||||||
|
|
||||||
|
An alternative way to deploy the two web applications of the example above is to use _virtual hosts_.
|
||||||
|
A _virtual host_ is a subdomain of the primary domain that shares the same IP address with the primary domain.
|
||||||
|
If the e-commerce business primary domain is `domain.com`, then a virtual host for `webappA` could be `shop.domain.com`, while a virtual host for `webappB` could be `api.domain.com`.
|
||||||
|
|
||||||
|
Web application `webappA` can now be deployed to virtual host `shop.domain.com` and context path `/`, while web application `webappB` can be deployed to virtual host `api.domain.com` and context path `/`.
|
||||||
|
Both applications have the same context path `/`, but they can be distinguished by the subdomain.
|
||||||
|
|
||||||
|
A client making a request to `+https://shop.domain.com/cart+` is directed by Jetty to `webappA`, while a request to `+https://api.domain.com/products+` is directed to `webappB`.
|
||||||
|
|
||||||
|
Therefore, in general, a web application is deployed to a _context_ which can be seen as the pair `(virtual_host, context_path)`.
|
||||||
|
In the first case the contexts were `(domain.com, /shop)` and `(domain.com, /api)`, while in the second case the contexts were `(shop.domain.com, /)` and `(api.domain.com, /)`.
|
||||||
|
Server applications using the Jetty Server Libraries create and configure a _context_ for each web application.
|
||||||
|
Many __context__s can be deployed together to enrich the web application offering -- for example a catalog context, a shop context, an API context, an administration context, etc.
|
||||||
|
|
||||||
|
Web applications can be written using exclusively the Servlet APIs, since developers know well the Servlet API and because they guarantee better portability across Servlet container implementations.
|
||||||
|
|
||||||
|
Embedded web applications based on the Servlet APIs are described in <<handler-use-servlet,this section>>.
|
||||||
|
|
||||||
|
Embedded web applications may also require additional features such as access to Jetty specific APIs, or utility features such as redirection from HTTP to HTTPS, support for `gzip` content compression, etc.
|
||||||
|
The Jetty Server Libraries provides a number of out-of-the-box __Handler__s that implement the most common functionalities and are described in <<handler-use,this section>>.
|
||||||
|
|
||||||
|
[[handler-use-util-context]]
|
||||||
|
==== ContextHandler
|
||||||
|
|
||||||
|
`ContextHandler` is a `Handler` that represents a _context_ for a web application.
|
||||||
|
It is a `HandlerWrapper` that performs some action before and after delegating to the nested `Handler`.
|
||||||
|
// TODO: expand on what the ContextHandler does, e.g. ServletContext.
|
||||||
|
|
||||||
|
The simplest use of `ContextHandler` is the following:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=contextHandler]
|
||||||
|
----
|
||||||
|
|
||||||
|
The `Handler` tree structure looks like the following:
|
||||||
|
|
||||||
|
[,screen]
|
||||||
|
----
|
||||||
|
Server
|
||||||
|
└── ContextHandler /shop
|
||||||
|
└── ShopHandler
|
||||||
|
----
|
||||||
|
|
||||||
|
[[handler-use-util-context-collection]]
|
||||||
|
==== ContextHandlerCollection
|
||||||
|
|
||||||
|
Server applications may need to deploy to Jetty more than one web application.
|
||||||
|
|
||||||
|
Recall from the <<handler,introduction>> that Jetty offers `HandlerCollection` and `HandlerList` that may contain a sequence of children ``Handler``s.
|
||||||
|
However, both of these have no knowledge of the concept of _context_ and just iterate through the sequence of ``Handler``s.
|
||||||
|
|
||||||
|
A better choice for multiple web application is `ContextHandlerCollection`, that matches a _context_ from either its _context path_ or _virtual host_, without iterating through the ``Handler``s.
|
||||||
|
|
||||||
|
If `ContextHandlerCollection` does not find a match, it just returns.
|
||||||
|
What happens next depends on the `Handler` tree structure: other ``Handler``s may be invoked after `ContextHandlerCollection`, for example `DefaultHandler` (see <<handler-use-util-default-handler,this section>>).
|
||||||
|
Eventually, if `Request.setHandled(true)` is not called, Jetty returns an HTTP `404` response to the client.
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=contextHandlerCollection]
|
||||||
|
----
|
||||||
|
|
||||||
|
The `Handler` tree structure looks like the following:
|
||||||
|
|
||||||
|
[,screen]
|
||||||
|
----
|
||||||
|
Server
|
||||||
|
└── ContextHandlerCollection
|
||||||
|
├── ContextHandler /shop
|
||||||
|
│ └── ShopHandler
|
||||||
|
└── ContextHandler /api
|
||||||
|
└── RESTHandler
|
||||||
|
----
|
||||||
|
|
||||||
|
[[handler-use-util-resource-handler]]
|
||||||
|
==== ResourceHandler -- Static Content
|
||||||
|
|
||||||
|
Static content such as images or files (HTML, JavaScript, CSS) can be sent by Jetty very efficiently because Jetty can write the content asynchronously, using direct ``ByteBuffer``s to minimize data copy, and using a memory cache for faster access to the data to send.
|
||||||
|
|
||||||
|
Being able to write content asynchronously means that if the network gets congested (for example, the client reads the content very slowly) and the server stalls the send of the requested data, then Jetty will wait to resume the send _without_ blocking a thread to finish the send.
|
||||||
|
|
||||||
|
`ResourceHandler` supports the following features:
|
||||||
|
|
||||||
|
* Welcome files, for example serving `/index.html` for request URI `/`
|
||||||
|
* Precompressed resources, serving a precompressed `/document.txt.gz` for request URI `/document.txt`
|
||||||
|
* https://tools.ietf.org/html/rfc7233[Range requests], for requests containing the `Range` header, which allows clients to pause and resume downloads of large files
|
||||||
|
* Directory listing, serving a HTML page with the file list of the requested directory
|
||||||
|
* Conditional headers, for requests containing the `If-Match`, `If-None-Match`, `If-Modified-Since`, `If-Unmodified-Since` headers.
|
||||||
|
|
||||||
|
The number of features supported and the efficiency in sending static content are on the same level as those of common front-end servers used to serve static content such as Nginx or Apache.
|
||||||
|
Therefore, the traditional architecture where Nginx/Apache was the front-end server used only to send static content and Jetty was the back-end server used only to send dynamic content is somehow obsolete as Jetty can perform efficiently both tasks.
|
||||||
|
This leads to simpler systems (less components to configure and manage) and more performance (no need to proxy dynamic requests from front-end servers to back-end servers).
|
||||||
|
|
||||||
|
NOTE: It is common to use Nginx/Apache as load balancers, or as rewrite/redirect servers.
|
||||||
|
We typically recommend https://haproxy.org[HAProxy] as load balancer, and Jetty has <<handler-use-util-rewrite-handler,rewrite/redirect features>> as well.
|
||||||
|
|
||||||
|
This is how you configure a `ResourceHandler` to create a simple file server:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=resourceHandler]
|
||||||
|
----
|
||||||
|
|
||||||
|
If you need to serve static resources from multiple directories:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=multipleResourcesHandler]
|
||||||
|
----
|
||||||
|
|
||||||
|
If the resource is not found, `ResourceHandler` will not call `Request.setHandled(true)` so what happens next depends on the `Handler` tree structure.
|
||||||
|
See also <<handler-use-util-default-handler,how to use>> `DefaultHandler`.
|
||||||
|
|
||||||
|
[[handler-use-util-gzip-handler]]
|
||||||
|
==== GzipHandler
|
||||||
|
|
||||||
|
`GzipHandler` provides supports for automatic decompression of compressed request content and automatic compression of response content.
|
||||||
|
|
||||||
|
`GzipHandler` is a `HandlerWrapper` that inspects the request and, if the request matches the `GzipHandler` configuration, just installs the required components to eventually perform decompression of the request content or compression of the response content.
|
||||||
|
The decompression/compression is not performed until the web application reads request content or writes response content.
|
||||||
|
|
||||||
|
`GzipHandler` can be configured at the server level in this way:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=serverGzipHandler]
|
||||||
|
----
|
||||||
|
|
||||||
|
The `Handler` tree structure looks like the following:
|
||||||
|
|
||||||
|
[,screen]
|
||||||
|
----
|
||||||
|
Server
|
||||||
|
└── GzipHandler
|
||||||
|
└── ContextHandlerCollection
|
||||||
|
├── ContextHandler 1
|
||||||
|
:── ...
|
||||||
|
└── ContextHandler N
|
||||||
|
----
|
||||||
|
|
||||||
|
However, in less common cases, you can configure `GzipHandler` on a per-context basis, for example because you want to configure `GzipHandler` with different parameters for each context, or because you want only some contexts to have compression support:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=contextGzipHandler]
|
||||||
|
----
|
||||||
|
|
||||||
|
The `Handler` tree structure looks like the following:
|
||||||
|
|
||||||
|
[,screen]
|
||||||
|
----
|
||||||
|
Server
|
||||||
|
└── ContextHandlerCollection
|
||||||
|
└── ContextHandlerCollection
|
||||||
|
├── GzipHandler
|
||||||
|
│ └── ContextHandler /shop
|
||||||
|
│ └── ShopHandler
|
||||||
|
└── ContextHandler /api
|
||||||
|
└── RESTHandler
|
||||||
|
----
|
||||||
|
|
||||||
|
// TODO: does ServletContextHandler really need a special configuration?
|
||||||
|
|
||||||
|
[[handler-use-util-rewrite-handler]]
|
||||||
|
==== RewriteHandler
|
||||||
|
|
||||||
|
`RewriteHandler` provides support for URL rewriting, very similarly to https://httpd.apache.org/docs/current/mod/mod_rewrite.html[Apache's mod_rewrite] or https://nginx.org/en/docs/http/ngx_http_rewrite_module.html[Nginx rewrite module].
|
||||||
|
|
||||||
|
The Maven artifact coordinates are:
|
||||||
|
|
||||||
|
[,xml,subs=normal]
|
||||||
|
----
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-rewrite</artifactId>
|
||||||
|
<version>{version}</version>
|
||||||
|
</dependency>
|
||||||
|
----
|
||||||
|
|
||||||
|
`RewriteHandler` can be configured with a set of __rule__s; a _rule_ inspects the request and when it matches it performs some change to the request (for example, changes the URI path, adds/removes headers, etc.).
|
||||||
|
|
||||||
|
The Jetty Server Libraries provide rules for the most common usages, but you can write your own rules by extending the `org.eclipse.jetty.rewrite.handler.Rule` class.
|
||||||
|
|
||||||
|
Please refer to the `jetty-rewrite` module link:{javadoc-url}/org/eclipse/jetty/rewrite/handler/package-summary.html[javadocs] for the complete list of available rules.
|
||||||
|
|
||||||
|
You typically want to configure `RewriteHandler` at the server level, although it is possible to configure it on a per-context basis.
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=rewriteHandler]
|
||||||
|
----
|
||||||
|
|
||||||
|
The `Handler` tree structure looks like the following:
|
||||||
|
|
||||||
|
[,screen]
|
||||||
|
----
|
||||||
|
Server
|
||||||
|
└── RewriteHandler
|
||||||
|
└── ContextHandlerCollection
|
||||||
|
├── ContextHandler 1
|
||||||
|
:── ...
|
||||||
|
└── ContextHandler N
|
||||||
|
----
|
||||||
|
|
||||||
|
[[handler-use-util-stats-handler]]
|
||||||
|
==== StatisticsHandler
|
||||||
|
|
||||||
|
`StatisticsHandler` gathers and exposes a number of statistic values related to request processing such as:
|
||||||
|
|
||||||
|
* Total number of requests
|
||||||
|
* Current number of concurrent requests
|
||||||
|
* Minimum, maximum, average and standard deviation of request processing times
|
||||||
|
* Number of responses grouped by HTTP code (i.e. how many `2xx` responses, how many `3xx` responses, etc.)
|
||||||
|
* Total response content bytes
|
||||||
|
|
||||||
|
Server applications can read these values and use them internally, or expose them via some service, or xref:arch/jmx.adoc[export them to JMX].
|
||||||
|
|
||||||
|
`StatisticsHandler` can be configured at the server level or at the context level.
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=statsHandler]
|
||||||
|
----
|
||||||
|
|
||||||
|
The `Handler` tree structure looks like the following:
|
||||||
|
|
||||||
|
[,screen]
|
||||||
|
----
|
||||||
|
Server
|
||||||
|
└── StatisticsHandler
|
||||||
|
└── ContextHandlerCollection
|
||||||
|
├── ContextHandler 1
|
||||||
|
:── ...
|
||||||
|
└── ContextHandler N
|
||||||
|
----
|
||||||
|
|
||||||
|
[[handler-use-util-secure-handler]]
|
||||||
|
==== SecuredRedirectHandler -- Redirect from HTTP to HTTPS
|
||||||
|
|
||||||
|
`SecuredRedirectHandler` allows to redirect requests made with the `http` scheme (and therefore to the clear-text port) to the `https` scheme (and therefore to the encrypted port).
|
||||||
|
|
||||||
|
For example a request to `+http://domain.com:8080/path?param=value+` is redirected to `+https://domain.com:8443/path?param=value+`.
|
||||||
|
|
||||||
|
Server applications must configure a `HttpConfiguration` object with the secure scheme and secure port so that `SecuredRedirectHandler` can build the redirect URI.
|
||||||
|
|
||||||
|
`SecuredRedirectHandler` is typically configured at the server level, although it can be configured on a per-context basis.
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=securedHandler]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[handler-use-util-default-handler]]
|
||||||
|
==== DefaultHandler
|
||||||
|
|
||||||
|
`DefaultHandler` is a terminal `Handler` that always calls `Request.setHandled(true)` and performs the following:
|
||||||
|
|
||||||
|
* Serves the `favicon.ico` Jetty icon when it is requested
|
||||||
|
* Sends a HTTP `404` response for any other request
|
||||||
|
* The HTTP `404` response content nicely shows a HTML table with all the contexts deployed on the `Server` instance
|
||||||
|
|
||||||
|
`DefaultHandler` is best used as the last `Handler` of a `HandlerList`, for example:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=defaultHandler]
|
||||||
|
----
|
||||||
|
|
||||||
|
The `Handler` tree structure looks like the following:
|
||||||
|
|
||||||
|
[,screen]
|
||||||
|
----
|
||||||
|
Server
|
||||||
|
└── HandlerList
|
||||||
|
├── ContextHandlerCollection
|
||||||
|
│ ├── ContextHandler 1
|
||||||
|
│ :── ...
|
||||||
|
│ └── ContextHandler N
|
||||||
|
└── DefaultHandler
|
||||||
|
----
|
||||||
|
|
||||||
|
In the example above, `ContextHandlerCollection` will try to match a request to one of the contexts; if the match fails, `HandlerList` will call the next `Handler` which is `DefaultHandler` that will return a HTTP `404` with an HTML page showing the existing contexts deployed on the `Server`.
|
||||||
|
|
||||||
|
NOTE: `DefaultHandler` just sends a nicer HTTP `404` response in case of wrong requests from clients.
|
||||||
|
Jetty will send an HTTP `404` response anyway if `DefaultHandler` is not used.
|
||||||
|
|
||||||
|
[[handler-use-servlet]]
|
||||||
|
=== Servlet API Handlers
|
||||||
|
|
||||||
|
[[handler-use-servlet-context]]
|
||||||
|
==== ServletContextHandler
|
||||||
|
|
||||||
|
``Handler``s are easy to write, but often web applications have already been written using the Servlet APIs, using ``Servlet``s and ``Filter``s.
|
||||||
|
|
||||||
|
`ServletContextHandler` is a `ContextHandler` that provides support for the Servlet APIs and implements the behaviors required by the Servlet specification.
|
||||||
|
|
||||||
|
The Maven artifact coordinates are:
|
||||||
|
|
||||||
|
[,xml,subs=normal]
|
||||||
|
----
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-servlet</artifactId>
|
||||||
|
<version>{version}</version>
|
||||||
|
</dependency>
|
||||||
|
----
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=servletContextHandler]
|
||||||
|
----
|
||||||
|
|
||||||
|
The `Handler` and Servlet components tree structure looks like the following:
|
||||||
|
|
||||||
|
[,screen,subs=normal]
|
||||||
|
----
|
||||||
|
Server
|
||||||
|
└── ServletContextHandler /shop
|
||||||
|
├── _ShopCartServlet /cart/*_
|
||||||
|
└── _CrossOriginFilter /*_
|
||||||
|
----
|
||||||
|
|
||||||
|
Note how the Servlet components (they are not ``Handler``s) are represented in _italic_.
|
||||||
|
|
||||||
|
Note also how adding a `Servlet` or a `Filter` returns a _holder_ object that can be used to specify additional configuration for that particular `Servlet` or `Filter`.
|
||||||
|
|
||||||
|
When a request arrives to `ServletContextHandler` the request URI will be matched against the ``Filter``s and ``Servlet`` mappings and only those that match will process the request, as dictated by the Servlet specification.
|
||||||
|
|
||||||
|
IMPORTANT: `ServletContextHandler` is a terminal `Handler`, that is it always calls `Request.setHandled(true)` when invoked.
|
||||||
|
Server applications must be careful when creating the `Handler` tree to put ``ServletContextHandler``s as last ``Handler``s in a `HandlerList` or as children of `ContextHandlerCollection`.
|
||||||
|
|
||||||
|
// TODO: revise what above, as ServletContextHandler is not a terminal handler.
|
||||||
|
// TODO: introduce the fact that ServletContextHandler can have a class loader that may be used to "isolate" web application classes.
|
||||||
|
|
||||||
|
[[handler-use-webapp-context]]
|
||||||
|
==== WebAppContext
|
||||||
|
|
||||||
|
`WebAppContext` is a `ServletContextHandler` that auto configures itself by reading a `web.xml` Servlet configuration file.
|
||||||
|
|
||||||
|
Server applications can specify a `+*.war+` file or a directory with the structure of a `+*.war+` file to `WebAppContext` to deploy a standard Servlet web application packaged as a `war` (as defined by the Servlet specification).
|
||||||
|
|
||||||
|
Where server applications using `ServletContextHandler` must manually invoke methods to add ``Servlet``s and ``Filter``s, `WebAppContext` reads `WEB-INF/web.xml` to add ``Servlet``s and ``Filter``s, and also enforces a number of restrictions defined by the Servlet specification, in particular related to class loading.
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=webAppContextHandler]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[handler-use-webapp-context-class-loading]]
|
||||||
|
.WebAppContext Class Loading
|
||||||
|
|
||||||
|
The Servlet specification requires that a web application class loader must load the web application classes from `WEB-INF/classes` and `WEB_INF/lib`.
|
||||||
|
The web application class loader is special because it behaves differently from typical class loaders: where typical class loaders first delegate to their parent class loader and then try to find the class locally, the web application class loader first tries to find the class locally and then delegates to the parent class loader.
|
||||||
|
The typical class loading model, parent-first, is _inverted_ for web application class loaders, as they use a child-first model.
|
||||||
|
|
||||||
|
Furthermore, the Servlet specification requires that web applications cannot load or otherwise access the Servlet container implementation classes, also called _server classes_.
|
||||||
|
In the Jetty case, the Servlet specification class `javax.servlet.http.HttpServletRequest` is implemented by `org.eclipse.jetty.server.Request`.
|
||||||
|
Web applications cannot downcast Servlet's `HttpServletRequest` to Jetty's `Request` to access Jetty specific features -- this ensures maximum web application portability across Servlet container implementations.
|
||||||
|
|
||||||
|
Lastly, the Servlet specification requires that other classes, also called _system classes_, such as `javax.servlet.http.HttpServletRequest` or JDK classes such as `java.lang.String` or `java.sql.Connection` cannot be modified by web applications by putting, for example, modified versions of those classes in `WEB-INF/classes` so that they are loaded first by the web application class loader (instead of the class-path class loader where they are normally loaded from).
|
||||||
|
|
||||||
|
`WebAppContext` implements this class loader logic using a single class loader, `org.eclipse.jetty.webapp.WebAppClassLoader`, with filtering capabilities: when it loads a class, it checks whether the class is a _system class_ or a _server class_ and acts according to the Servlet specification.
|
||||||
|
|
||||||
|
When `WebAppClassLoader` is asked to load a class, it first tries to find the class locally (since it must use the inverted child-first model); if the class is found, and it is not a _system class_, the class is loaded; otherwise the class is not found locally.
|
||||||
|
If the class is not found locally, the parent class loader is asked to load the class; the parent class loader uses the standard parent-first model, so it delegates the class loading to its parent, and so on.
|
||||||
|
If the class is found, and it is not a _server class_, the class is loaded; otherwise the class is not found and a `ClassNotFoundException` is thrown.
|
||||||
|
|
||||||
|
Unfortunately, the Servlet specification does not define exactly which classes are _system classes_ and which classes are _server classes_.
|
||||||
|
However, Jetty picks good defaults and allows server applications to customize _system classes_ and _server classes_ in `WebAppContext`.
|
||||||
|
|
||||||
|
// TODO: add a section on parentLoaderPriority.
|
||||||
|
// TODO: add a code example about how to set system/server classes.
|
||||||
|
// TODO: add a code example about how to configure extra classpath
|
||||||
|
// TODO: add a section on ClassLoading (see old docs)
|
||||||
|
|
||||||
|
// TODO: add a section on Configuration (system/server classes)
|
||||||
|
// TODO: add a section about how to setup JSP support
|
||||||
|
|
||||||
|
[[handler-use-default-servlet]]
|
||||||
|
==== DefaultServlet -- Static Content for Servlets
|
||||||
|
|
||||||
|
If you have a <<handler-use-servlet-context,Servlet web application>>, you may want to use a `DefaultServlet` instead of `ResourceHandler`.
|
||||||
|
The features are similar, but `DefaultServlet` is more commonly used to serve static files for Servlet web applications.
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=defaultServlet]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[handler-implement]]
|
||||||
|
=== Implementing Handler
|
||||||
|
|
||||||
|
The `Handler` API consist fundamentally of just one method:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=handlerAPI]
|
||||||
|
----
|
||||||
|
|
||||||
|
The `target` parameter is an identifier for the resource.
|
||||||
|
This is normally the URI that is parsed from an HTTP request.
|
||||||
|
However, a request could be forwarded to either a named resource, in which case `target` will be the name of the resource, or to a different URI, in which case `target` will be the new URI.
|
||||||
|
|
||||||
|
Applications may wrap the request or response (or both) and forward the wrapped request or response to a different URI (which may be possibly handled by a different `Handler`).
|
||||||
|
This is the reason why there are two request parameters in the `Handler` APIs: the first is the unwrapped, original, request that also gives access to Jetty-specific APIs, while the second is the application-wrapped Servlet request.
|
||||||
|
|
||||||
|
[[handler-impl-hello]]
|
||||||
|
==== Hello World Handler
|
||||||
|
|
||||||
|
A simple "Hello World" `Handler` is the following:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=handlerHello]
|
||||||
|
----
|
||||||
|
|
||||||
|
Such a simple `Handler` extends from `AbstractHandler` and can access the request and response main features, such as reading request headers and content, or writing response headers and content.
|
||||||
|
|
||||||
|
[[handler-impl-filter]]
|
||||||
|
==== Filtering Handler
|
||||||
|
|
||||||
|
A filtering `Handler` is a handler that perform some modification to the request or response, and then either forwards the request to another `Handler` or produces an error response:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=handlerFilter]
|
||||||
|
----
|
||||||
|
|
||||||
|
Note how a filtering `Handler` extends from `HandlerWrapper` and as such needs another handler to forward the request processing to, and how the two ``Handler``s needs to be linked together to work properly.
|
||||||
|
|
||||||
|
[[security]]
|
||||||
|
=== Securing HTTP Server Applications
|
||||||
|
|
||||||
|
// TODO: ConstraintSecurityHandler and Authenticators and LoginServices
|
||||||
|
TODO
|
||||||
|
|
||||||
|
[[application]]
|
||||||
|
=== Writing HTTP Server Applications
|
||||||
|
|
||||||
|
Writing HTTP applications is typically simple, especially when using blocking APIs.
|
||||||
|
However, there are subtle cases where it is worth clarifying what a server application should do to obtain the desired results when run by Jetty.
|
||||||
|
|
||||||
|
[[application-1xx]]
|
||||||
|
==== Sending 1xx Responses
|
||||||
|
|
||||||
|
The https://tools.ietf.org/html/rfc7231#section-5.1.1[HTTP/1.1 RFC] allows for `1xx` informational responses to be sent before a real content response.
|
||||||
|
Unfortunately the servlet specification does not provide a way for these to be sent, so Jetty has had to provide non-standard handling of these headers.
|
||||||
|
|
||||||
|
[[application-100]]
|
||||||
|
==== 100 Continue
|
||||||
|
|
||||||
|
The `100 Continue` response should be sent by the server when a client sends a request with an `Expect: 100-continue` header, as the client will not send the body of the request until the `100 Continue` response has been sent.
|
||||||
|
|
||||||
|
The intent of this feature is to allow a server to inspect the headers and to tell the client to not send a request body that might be too large or insufficiently private or otherwise unable to be handled.
|
||||||
|
|
||||||
|
Jetty achieves this by waiting until the input stream or reader is obtained by the filter/servlet, before sending the `100 Continue` response.
|
||||||
|
Thus a filter/servlet may inspect the headers of a request before getting the input stream and send an error response (or redirect etc.) rather than the 100 continues.
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=continue100]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[jetty-102-processing]]
|
||||||
|
==== 102 Processing
|
||||||
|
|
||||||
|
https://tools.ietf.org/html/rfc2518[RFC 2518] defined the `102 Processing` status code that can be sent:
|
||||||
|
|
||||||
|
[quote,RFC 2518 section 10.1]
|
||||||
|
when the server has a reasonable expectation that the request will take significant time to complete.
|
||||||
|
As guidance, if a method is taking longer than 20 seconds (a reasonable, but arbitrary value) to process the server SHOULD return a `102 Processing` response.
|
||||||
|
|
||||||
|
However, a later update of RFC 2518, https://tools.ietf.org/html/rfc4918[RFC 4918], removed the `102 Processing` status code for https://tools.ietf.org/html/rfc4918#appendix-F.3["lack of implementation"].
|
||||||
|
|
||||||
|
Jetty supports the `102 Processing` status code.
|
||||||
|
If a request is received with the `Expect: 102-processing` header, then a filter/servlet may send a `102 Processing` response (without terminating further processing) by calling `response.sendError(102)`.
|
|
@ -0,0 +1,185 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= HTTP/2 Server Library
|
||||||
|
|
||||||
|
In the vast majority of cases, server applications should use the generic, high-level, xref:server/http.adoc[HTTP server library] that also provides HTTP/2 support via the HTTP/2 ``ConnectionFactory``s as described in details xref:server/http.adoc#connector-protocol-http2[here].
|
||||||
|
|
||||||
|
The low-level HTTP/2 server library has been designed for those applications that need low-level access to HTTP/2 features such as _sessions_, _streams_ and _frames_, and this is quite a rare use case.
|
||||||
|
|
||||||
|
See also the correspondent xref:client/http2.adoc[HTTP/2 client library].
|
||||||
|
|
||||||
|
[[intro]]
|
||||||
|
== Introduction
|
||||||
|
|
||||||
|
The Maven artifact coordinates for the HTTP/2 client library are the following:
|
||||||
|
|
||||||
|
[,xml,subs=normal]
|
||||||
|
----
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty.http2</groupId>
|
||||||
|
<artifactId>http2-server</artifactId>
|
||||||
|
<version>{version}</version>
|
||||||
|
</dependency>
|
||||||
|
----
|
||||||
|
|
||||||
|
HTTP/2 is a multiplexed protocol: it allows multiple HTTP/2 requests to be sent on the same TCP connection, or _session_.
|
||||||
|
Each request/response cycle is represented by a _stream_.
|
||||||
|
Therefore, a single _session_ manages multiple concurrent _streams_.
|
||||||
|
A _stream_ has typically a very short life compared to the _session_: a _stream_ only exists for the duration of the request/response cycle and then disappears.
|
||||||
|
|
||||||
|
[[flow-control]]
|
||||||
|
== HTTP/2 Flow Control
|
||||||
|
|
||||||
|
The HTTP/2 protocol is _flow controlled_ (see https://tools.ietf.org/html/rfc7540#section-5.2[the specification]).
|
||||||
|
This means that a sender and a receiver maintain a _flow control window_ that tracks the number of data bytes sent and received, respectively.
|
||||||
|
When a sender sends data bytes, it reduces its flow control window.
|
||||||
|
When a receiver receives data bytes, it also reduces its flow control window, and then passes the received data bytes to the application.
|
||||||
|
The application consumes the data bytes and tells back the receiver that it has consumed the data bytes.
|
||||||
|
The receiver then enlarges the flow control window, and arranges to send a message to the sender with the number of bytes consumed, so that the sender can enlarge its flow control window.
|
||||||
|
|
||||||
|
A sender can send data bytes up to its whole flow control window, then it must stop sending until it receives a message from the receiver that the data bytes have been consumed, which enlarges the flow control window, which allows the sender to send more data bytes.
|
||||||
|
|
||||||
|
HTTP/2 defines _two_ flow control windows: one for each _session_, and one for each _stream_.
|
||||||
|
Let's see with an example how they interact, assuming that in this example the session flow control window is 120 bytes and the stream flow control window is 100 bytes.
|
||||||
|
|
||||||
|
The sender opens a session, and then opens `stream_1` on that session, and sends `80` data bytes.
|
||||||
|
At this point the session flow control window is `40` bytes (`120 - 80`), and ``stream_1``'s flow control window is `20` bytes (`100 - 80`).
|
||||||
|
The sender now opens `stream_2` on the same session and sends `40` data bytes.
|
||||||
|
At this point, the session flow control window is `0` bytes (`40 - 40`), while ``stream_2``'s flow control window is `60` (`100 - 40`).
|
||||||
|
Since now the session flow control window is `0`, the sender cannot send more data bytes, neither on `stream_1` nor on `stream_2` despite both have their stream flow control windows greater than `0`.
|
||||||
|
|
||||||
|
The receiver consumes ``stream_2``'s `40` data bytes and sends a message to the sender with this information.
|
||||||
|
At this point, the session flow control window is `40` (`0 40`), ``stream_1``'s flow control window is still `20` and ``stream_2``'s flow control window is `100` (`60 40`).
|
||||||
|
If the sender opens `stream_3` and would like to send 50 data bytes, it would only be able to send `40` because that is the maximum allowed by the session flow control window at this point.
|
||||||
|
|
||||||
|
It is therefore very important that applications notify the fact that they have consumed data bytes as soon as possible, so that the implementation (the receiver) can send a message to the sender (in the form of a `WINDOW_UPDATE` frame) with the information to enlarge the flow control window, therefore reducing the possibility that sender stalls due to the flow control windows being reduced to `0`.
|
||||||
|
|
||||||
|
How a server application should handle HTTP/2 flow control is discussed in details in <<request,this section>>.
|
||||||
|
|
||||||
|
[[setup]]
|
||||||
|
== Server Setup
|
||||||
|
|
||||||
|
The low-level HTTP/2 support is provided by `org.eclipse.jetty.http2.server.RawHTTP2ServerConnectionFactory` and `org.eclipse.jetty.http2.api.server.ServerSessionListener`:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http2/HTTP2ServerDocs.java[tags=setup]
|
||||||
|
----
|
||||||
|
|
||||||
|
Where server applications using the xref:server/http.adoc[high-level server library] deal with HTTP requests and responses in ``Handler``s, server applications using the low-level HTTP/2 server library deal directly with HTTP/2 __session__s, __stream__s and __frame__s in a `ServerSessionListener` implementation.
|
||||||
|
|
||||||
|
The `ServerSessionListener` interface defines a number of methods that are invoked by the implementation upon the occurrence of HTTP/2 events, and that server applications can override to react to those events.
|
||||||
|
|
||||||
|
Please refer to the `ServerSessionListener` link:{javadoc-url}/org/eclipse/jetty/http2/api/server/ServerSessionListener.html[javadocs] for the complete list of events.
|
||||||
|
|
||||||
|
The first event is the _accept_ event and happens when a client opens a new TCP connection to the server and the server accepts the connection.
|
||||||
|
This is the first occasion where server applications have access to the HTTP/2 `Session` object:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http2/HTTP2ServerDocs.java[tags=accept]
|
||||||
|
----
|
||||||
|
|
||||||
|
After connecting to the server, a compliant HTTP/2 client must send the https://tools.ietf.org/html/rfc7540#section-3.5[HTTP/2 client preface], and when the server receives it, it generates the _preface_ event on the server.
|
||||||
|
This is where server applications can customize the connection settings by returning a map of settings that the implementation will send to the client:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http2/HTTP2ServerDocs.java[tags=preface]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[request]]
|
||||||
|
== Receiving a Request
|
||||||
|
|
||||||
|
Receiving an HTTP request from the client, and sending a response, creates a _stream_ that encapsulates the exchange of HTTP/2 frames that compose the request and the response.
|
||||||
|
|
||||||
|
An HTTP request is made of a `HEADERS` frame, that carries the request method, the request URI and the request headers, and optional `DATA` frames that carry the request content.
|
||||||
|
|
||||||
|
Receiving the `HEADERS` frame opens the `Stream`:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http2/HTTP2ServerDocs.java[tags=request]
|
||||||
|
----
|
||||||
|
|
||||||
|
Server applications should return a `Stream.Listener` implementation from `onNewStream(...)` to be notified of events generated by the client, such as `DATA` frames carrying request content, or a `RST_STREAM` frame indicating that the client wants to _reset_ the request, or an idle timeout event indicating that the client was supposed to send more frames but it did not.
|
||||||
|
|
||||||
|
The example below shows how to receive request content:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http2/HTTP2ServerDocs.java[tags=requestContent]
|
||||||
|
----
|
||||||
|
|
||||||
|
NOTE: Returning from the `onData(...)` method implicitly demands for more `DATA` frames (unless the one just delivered was the last).
|
||||||
|
Additional `DATA` frames may be delivered immediately if they are available or later, asynchronously, when they arrive.
|
||||||
|
|
||||||
|
Applications that consume the content buffer within `onData(...)` (for example, writing it to a file, or copying the bytes to another storage) should succeed the callback as soon as they have consumed the content buffer.
|
||||||
|
This allows the implementation to reuse the buffer, reducing the memory requirements needed to handle the content buffers.
|
||||||
|
|
||||||
|
Alternatively, a client application may store away _both_ the buffer and the callback to consume the buffer bytes later, or pass _both_ the buffer and the callback to another asynchronous API (this is typical in proxy applications).
|
||||||
|
|
||||||
|
IMPORTANT: Completing the `Callback` is very important not only to allow the implementation to reuse the buffer, but also tells the implementation to enlarge the stream and session flow control windows so that the sender will be able to send more `DATA` frames without stalling.
|
||||||
|
|
||||||
|
Applications can also precisely control _when_ to demand more `DATA` frames, by implementing the `onDataDemanded(...)` method instead of `onData(...)`:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/HTTP2Docs.java[tags=dataDemanded]
|
||||||
|
----
|
||||||
|
|
||||||
|
IMPORTANT: Applications that implement `onDataDemanded(...)` must remember to call `Stream.demand(...)`.
|
||||||
|
If they don't, the implementation will not deliver `DATA` frames and the application will stall threadlessly until an idle timeout fires to close the stream or the session.
|
||||||
|
|
||||||
|
[[response]]
|
||||||
|
== Sending a Response
|
||||||
|
|
||||||
|
After receiving an HTTP request, a server application must send an HTTP response.
|
||||||
|
|
||||||
|
An HTTP response is typically composed of a `HEADERS` frame containing the HTTP status code and the response headers, and optionally one or more `DATA` frames containing the response content bytes.
|
||||||
|
|
||||||
|
The HTTP/2 protocol also supports response trailers (that is, headers that are sent after the response content) that also are sent using a `HEADERS` frame.
|
||||||
|
|
||||||
|
A server application can send a response in this way:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http2/HTTP2ServerDocs.java[tags=response;!exclude]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[reset]]
|
||||||
|
== Resetting a Request
|
||||||
|
|
||||||
|
A server application may decide that it does not want to accept the request.
|
||||||
|
For example, it may throttle the client because it sent too many requests in a time window, or the request is invalid (and does not deserve a proper HTTP response), etc.
|
||||||
|
|
||||||
|
A request can be reset in this way:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http2/HTTP2ServerDocs.java[tags=reset;!exclude]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[push]]
|
||||||
|
== HTTP/2 Push of Resources
|
||||||
|
|
||||||
|
A server application may _push_ secondary resources related to a primary resource.
|
||||||
|
|
||||||
|
A client may inform the server that it does not accept pushed resources(see https://tools.ietf.org/html/rfc7540#section-8.2[this section] of the specification) via a `SETTINGS` frame.
|
||||||
|
Server applications must track `SETTINGS` frames and verify whether the client supports HTTP/2 push, and only push if the client supports it:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http2/HTTP2ServerDocs.java[tags=push]
|
||||||
|
----
|
|
@ -0,0 +1,135 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= HTTP/3 Server Library
|
||||||
|
|
||||||
|
In the vast majority of cases, server applications should use the generic, high-level, xref:server/http.adoc[HTTP server library] that also provides HTTP/3 support via the HTTP/3 connector and ``ConnectionFactory``s as described in details xref:server/http.adoc#connector-protocol-http3[here].
|
||||||
|
|
||||||
|
The low-level HTTP/3 server library has been designed for those applications that need low-level access to HTTP/3 features such as _sessions_, _streams_ and _frames_, and this is quite a rare use case.
|
||||||
|
|
||||||
|
See also the correspondent xref:client/http3.adoc[HTTP/3 client library].
|
||||||
|
|
||||||
|
[[intro]]
|
||||||
|
== Introduction
|
||||||
|
|
||||||
|
The Maven artifact coordinates for the HTTP/3 client library are the following:
|
||||||
|
|
||||||
|
[,xml,subs=normal]
|
||||||
|
----
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty.http3</groupId>
|
||||||
|
<artifactId>http3-server</artifactId>
|
||||||
|
<version>{version}</version>
|
||||||
|
</dependency>
|
||||||
|
----
|
||||||
|
|
||||||
|
HTTP/3 is a multiplexed protocol because it relies on the multiplexing capabilities of QUIC, the protocol based on UDP that transports HTTP/3 frames.
|
||||||
|
Thanks to multiplexing, multiple HTTP/3 requests are sent on the same QUIC connection, or _session_.
|
||||||
|
Each request/response cycle is represented by a _stream_.
|
||||||
|
Therefore, a single _session_ manages multiple concurrent _streams_.
|
||||||
|
A _stream_ has typically a very short life compared to the _session_: a _stream_ only exists for the duration of the request/response cycle and then disappears.
|
||||||
|
|
||||||
|
// TODO: flow control?
|
||||||
|
//[[pg-server-http3-flow-control]]
|
||||||
|
//==== HTTP/3 Flow Control
|
||||||
|
//
|
||||||
|
//include::../../http3.adoc[tag=flowControl]
|
||||||
|
//
|
||||||
|
//How a server application should handle HTTP/3 flow control is discussed in details in <<request,this section>>.
|
||||||
|
|
||||||
|
[[setup]]
|
||||||
|
== Server Setup
|
||||||
|
|
||||||
|
The low-level HTTP/3 support is provided by `org.eclipse.jetty.http3.server.RawHTTP3ServerConnectionFactory` and `org.eclipse.jetty.http3.api.Session.Server.Listener`:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http3/HTTP3ServerDocs.java[tags=setup]
|
||||||
|
----
|
||||||
|
|
||||||
|
Where server applications using the xref:server/http.adoc[high-level server library] deal with HTTP requests and responses in ``Handler``s, server applications using the low-level HTTP/3 server library deal directly with HTTP/3 __session__s, __stream__s and __frame__s in a `Session.Server.Listener` implementation.
|
||||||
|
|
||||||
|
The `Session.Server.Listener` interface defines a number of methods that are invoked by the implementation upon the occurrence of HTTP/3 events, and that server applications can override to react to those events.
|
||||||
|
|
||||||
|
Please refer to the `Session.Server.Listener` link:{javadoc-url}/org/eclipse/jetty/http3/api/Session.Server.Listener.html[javadocs] for the complete list of events.
|
||||||
|
|
||||||
|
The first event is the _accept_ event and happens when a client opens a new QUIC connection to the server and the server accepts the connection.
|
||||||
|
This is the first occasion where server applications have access to the HTTP/3 `Session` object:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http3/HTTP3ServerDocs.java[tags=accept]
|
||||||
|
----
|
||||||
|
|
||||||
|
After the QUIC connection has been established, both client and server send an HTTP/3 `SETTINGS` frame to exchange their HTTP/3 configuration.
|
||||||
|
This generates the _preface_ event, where applications can customize the HTTP/3 settings by returning a map of settings that the implementation will send to the other peer:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http3/HTTP3ServerDocs.java[tags=preface]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[request]]
|
||||||
|
== Receiving a Request
|
||||||
|
|
||||||
|
Receiving an HTTP request from the client, and sending a response, creates a _stream_ that encapsulates the exchange of HTTP/3 frames that compose the request and the response.
|
||||||
|
|
||||||
|
An HTTP request is made of a `HEADERS` frame, that carries the request method, the request URI and the request headers, and optional `DATA` frames that carry the request content.
|
||||||
|
|
||||||
|
Receiving the `HEADERS` frame opens the `Stream`:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http3/HTTP3ServerDocs.java[tags=request]
|
||||||
|
----
|
||||||
|
|
||||||
|
Server applications should return a `Stream.Server.Listener` implementation from `onRequest(...)` to be notified of events generated by the client, such as `DATA` frames carrying request content, or a reset event indicating that the client wants to _reset_ the request, or an idle timeout event indicating that the client was supposed to send more frames but it did not.
|
||||||
|
|
||||||
|
The example below shows how to receive request content:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http3/HTTP3ServerDocs.java[tags=requestContent]
|
||||||
|
----
|
||||||
|
|
||||||
|
// TODO: flow control?
|
||||||
|
//include::../../http3.adoc[tag=apiFlowControl]
|
||||||
|
|
||||||
|
[[response]]
|
||||||
|
== Sending a Response
|
||||||
|
|
||||||
|
After receiving an HTTP request, a server application must send an HTTP response.
|
||||||
|
|
||||||
|
An HTTP response is typically composed of a `HEADERS` frame containing the HTTP status code and the response headers, and optionally one or more `DATA` frames containing the response content bytes.
|
||||||
|
|
||||||
|
The HTTP/3 protocol also supports response trailers (that is, headers that are sent after the response content) that also are sent using a `HEADERS` frame.
|
||||||
|
|
||||||
|
A server application can send a response in this way:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http3/HTTP3ServerDocs.java[tags=response;!exclude]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[reset]]
|
||||||
|
== Resetting a Request
|
||||||
|
|
||||||
|
A server application may decide that it does not want to accept the request.
|
||||||
|
For example, it may throttle the client because it sent too many requests in a time window, or the request is invalid (and does not deserve a proper HTTP response), etc.
|
||||||
|
|
||||||
|
A request can be reset in this way:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http3/HTTP3ServerDocs.java[tags=reset;!exclude]
|
||||||
|
----
|
|
@ -0,0 +1,29 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Server Libraries
|
||||||
|
|
||||||
|
The Eclipse Jetty Project provides server-side libraries that allow you to configure and start programmatically an HTTP or WebSocket server from a main class, or embed it in your existing application.
|
||||||
|
A typical example is a HTTP server that needs to expose a REST endpoint.
|
||||||
|
Another example is a proxy application that receives HTTP requests, processes them, and then forwards them to third party services, for example using the Jetty xref:client/index.adoc[client libraries].
|
||||||
|
|
||||||
|
While historically Jetty is an HTTP server, it is possible to use the Jetty server-side libraries to write a generic network server that interprets any network protocol (not only HTTP).
|
||||||
|
If you are interested in the low-level details of how the Eclipse Jetty server libraries work, or are interested in writing a custom protocol, look at the xref:server/io-arch.adoc[Server I/O Architecture].
|
||||||
|
|
||||||
|
The Jetty server-side libraries provide:
|
||||||
|
|
||||||
|
* HTTP support for HTTP/1.0, HTTP/1.1, HTTP/2, clear-text or encrypted, HTTP/3, for applications that want to embed Jetty as a generic HTTP server or proxy, via the xref:server/http.adoc[HTTP libraries]
|
||||||
|
* HTTP/2 low-level support, for applications that want to explicitly handle low-level HTTP/2 _sessions_, _streams_ and _frames_, via the xref:server/http2.adoc[HTTP/2 libraries]
|
||||||
|
* HTTP/3 low-level support, for applications that want to explicitly handle low-level HTTP/3 _sessions_, _streams_ and _frames_, via the xref:server/http3.adoc[HTTP/3 libraries]
|
||||||
|
* WebSocket support, for applications that want to embed a WebSocket server, via the xref:server/websocket.adoc[WebSocket libraries]
|
||||||
|
* FCGI support, to delegate requests to python or similar scripting languages.
|
|
@ -0,0 +1,223 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Server I/O Architecture
|
||||||
|
|
||||||
|
The Jetty server libraries provide the basic components and APIs to implement a network server.
|
||||||
|
|
||||||
|
They build on the common xref:arch/io.adoc[Jetty I/O Architecture] and provide server specific concepts.
|
||||||
|
|
||||||
|
The Jetty server libraries provide I/O support for TCP/IP sockets (for both IPv4 and IPv6) and, when using Java 16 or later, for Unix-Domain sockets.
|
||||||
|
|
||||||
|
Support for Unix-Domain sockets is interesting when Jetty is deployed behind a proxy or a load-balancer: it is possible to configure the proxy or load balancer to communicate with Jetty via Unix-Domain sockets, rather than via the loopback network interface.
|
||||||
|
|
||||||
|
The central I/O server-side component are `org.eclipse.jetty.server.ServerConnector`, that handles the TCP/IP socket traffic, and `org.eclipse.jetty.unixdomain.server.UnixDomainServerConnector`, that handles the Unix-Domain socket traffic.
|
||||||
|
|
||||||
|
`ServerConnector` and `UnixDomainServerConnector` are very similar, and while in the following sections `ServerConnector` is used, the same concepts apply to `UnixDomainServerConnector`, unless otherwise noted.
|
||||||
|
|
||||||
|
A `ServerConnector` manages a list of ``ConnectionFactory``s, that indicate what protocols the connector is able to speak.
|
||||||
|
|
||||||
|
[[connection-factory]]
|
||||||
|
== Creating Connections with `ConnectionFactory`
|
||||||
|
|
||||||
|
Recall from the xref:arch/io.adoc#connection[`Connection` section] of the Jetty I/O architecture that `Connection` instances are responsible for parsing bytes read from a socket and generating bytes to write to that socket.
|
||||||
|
|
||||||
|
On the server-side, a `ConnectionFactory` creates `Connection` instances that know how to parse and generate bytes for the specific protocol they support -- it can be either HTTP/1.1, or TLS, or FastCGI, or the https://www.haproxy.org/download/2.1/doc/proxy-protocol.txt[PROXY protocol].
|
||||||
|
|
||||||
|
For example, this is how clear-text HTTP/1.1 is configured for TCP/IP sockets:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=http]
|
||||||
|
----
|
||||||
|
|
||||||
|
With this configuration, the `ServerConnector` will listen on port `8080`.
|
||||||
|
|
||||||
|
Similarly, this is how clear-text HTTP/1.1 is configured for Unix-Domain sockets:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=httpUnix]
|
||||||
|
----
|
||||||
|
|
||||||
|
With this configuration, the `UnixDomainServerConnector` will listen on file `/tmp/jetty.sock`.
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
`ServerConnector` and `UnixDomainServerConnector` only differ by how they are configured -- for `ServerConnector` you specify the IP port it listens to, for `UnixDomainServerConnector` you specify the Unix-Domain path it listens to.
|
||||||
|
|
||||||
|
Both configure ``ConnectionFactory``s in exactly the same way.
|
||||||
|
====
|
||||||
|
|
||||||
|
When a new socket connection is established, `ServerConnector` delegates to the `ConnectionFactory` the creation of the `Connection` instance for that socket connection, that is linked to the corresponding `EndPoint`:
|
||||||
|
|
||||||
|
[plantuml]
|
||||||
|
----
|
||||||
|
skinparam backgroundColor transparent
|
||||||
|
skinparam monochrome true
|
||||||
|
skinparam shadowing false
|
||||||
|
skinparam padding 5
|
||||||
|
|
||||||
|
hide members
|
||||||
|
hide circle
|
||||||
|
|
||||||
|
scale 1.5
|
||||||
|
|
||||||
|
circle network
|
||||||
|
circle application
|
||||||
|
|
||||||
|
network - SocketChannelEndPoint
|
||||||
|
SocketChannelEndPoint - HttpConnection
|
||||||
|
HttpConnection - application
|
||||||
|
----
|
||||||
|
|
||||||
|
For every socket connection there will be an `EndPoint` + `Connection` pair.
|
||||||
|
|
||||||
|
[[connection-factory-wrapping]]
|
||||||
|
== Wrapping a `ConnectionFactory`
|
||||||
|
|
||||||
|
A `ConnectionFactory` may wrap another `ConnectionFactory`; for example, the TLS protocol provides encryption for any other protocol.
|
||||||
|
Therefore, to support encrypted HTTP/1.1 (also known as `https`), you need to configure the `ServerConnector` with two ``ConnectionFactory``s -- one for the TLS protocol and one for the HTTP/1.1 protocol, like in the example below:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=tlsHttp]
|
||||||
|
----
|
||||||
|
|
||||||
|
With this configuration, the `ServerConnector` will listen on port `8443`.
|
||||||
|
When a new socket connection is established, the first `ConnectionFactory` configured in `ServerConnector` is invoked to create a `Connection`.
|
||||||
|
In the example above, `SslConnectionFactory` creates a `SslConnection` and then asks to its wrapped `ConnectionFactory` (in the example, `HttpConnectionFactory`) to create the wrapped `Connection` (an `HttpConnection`) and will then link the two ``Connection``s together, in this way:
|
||||||
|
|
||||||
|
[plantuml]
|
||||||
|
----
|
||||||
|
skinparam backgroundColor transparent
|
||||||
|
skinparam monochrome true
|
||||||
|
skinparam shadowing false
|
||||||
|
skinparam padding 5
|
||||||
|
|
||||||
|
hide members
|
||||||
|
hide circle
|
||||||
|
|
||||||
|
scale 1.5
|
||||||
|
|
||||||
|
circle network
|
||||||
|
circle application
|
||||||
|
|
||||||
|
network - SocketChannelEndPoint
|
||||||
|
SocketChannelEndPoint - SslConnection
|
||||||
|
SslConnection -- DecryptedEndPoint
|
||||||
|
DecryptedEndPoint - HttpConnection
|
||||||
|
HttpConnection - application
|
||||||
|
----
|
||||||
|
|
||||||
|
Bytes read by the `SocketChannelEndPoint` will be interpreted as TLS bytes by the `SslConnection`, then decrypted and made available to the `DecryptedEndPoint` (a component part of `SslConnection`), which will then provide them to `HttpConnection`.
|
||||||
|
|
||||||
|
The application writes bytes through the `HttpConnection` to the `DecryptedEndPoint`, which will encrypt them through the `SslConnection` and write the encrypted bytes to the `SocketChannelEndPoint`.
|
||||||
|
|
||||||
|
[[connection-factory-detecting]]
|
||||||
|
== Choosing `ConnectionFactory` via Bytes Detection
|
||||||
|
|
||||||
|
Typically, a network port is associated with a specific protocol.
|
||||||
|
For example, port `80` is associated with clear-text HTTP, while port `443` is associated with encrypted HTTP (that is, the TLS protocol wrapping the HTTP protocol, also known as `https`).
|
||||||
|
|
||||||
|
In certain cases, applications need to listen to the same port for two or more protocols, or for different but incompatible versions of the same protocol, which can only be distinguished by reading the initial bytes and figuring out to what protocol they belong to.
|
||||||
|
|
||||||
|
The Jetty server libraries support this case by placing a `DetectorConnectionFactory` in front of other ``ConnectionFactory``s.
|
||||||
|
`DetectorConnectionFactory` accepts a list of ``ConnectionFactory``s that implement `ConnectionFactory.Detecting`, which will be called to see if one of them recognizes the initial bytes.
|
||||||
|
|
||||||
|
In the example below you can see how to support both clear-text and encrypted HTTP/1.1 (i.e. both `http` and `https`) _on the same network port_:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=detector]
|
||||||
|
----
|
||||||
|
|
||||||
|
<1> Creates the `DetectorConnectionFactory` with the `SslConnectionFactory` as the only detecting `ConnectionFactory`.
|
||||||
|
With this configuration, the detector will delegate to `SslConnectionFactory` to recognize the initial bytes, which will detect whether the bytes are TLS protocol bytes.
|
||||||
|
<2> Creates the `ServerConnector` with `DetectorConnectionFactory` as the first `ConnectionFactory`, and `HttpConnectionFactory` as the next `ConnectionFactory` to invoke if the detection fails.
|
||||||
|
|
||||||
|
In the example above `ServerConnector` will listen on port 8181.
|
||||||
|
When a new socket connection is established, `DetectorConnectionFactory` is invoked to create a `Connection`, because it is the first `ConnectionFactory` specified in the `ServerConnector` list.
|
||||||
|
`DetectorConnectionFactory` reads the initial bytes and asks to its detecting ``ConnectionFactory``s if they recognize the bytes.
|
||||||
|
In the example above, the detecting ``ConnectionFactory`` is `SslConnectionFactory` which will therefore detect whether the initial bytes are TLS bytes.
|
||||||
|
If one of the detecting ``ConnectionFactory``s recognizes the bytes, it creates a `Connection`; otherwise `DetectorConnectionFactory` will try the next `ConnectionFactory` after itself in the `ServerConnector` list.
|
||||||
|
In the example above, the next `ConnectionFactory` after `DetectorConnectionFactory` is `HttpConnectionFactory`.
|
||||||
|
|
||||||
|
The final result is that when new socket connection is established, the initial bytes are examined: if they are TLS bytes, a `SslConnectionFactory` will create a `SslConnection` that wraps an `HttpConnection` as explained <<connection-factory-wrapping,here>>, therefore supporting `https`; otherwise they are not TLS bytes and an `HttpConnection` is created, therefore supporting `http`.
|
||||||
|
|
||||||
|
[[connection-factory-custom]]
|
||||||
|
== Writing a Custom `ConnectionFactory`
|
||||||
|
|
||||||
|
This section explains how to use the Jetty server-side libraries to write a generic network server able to parse and generate any protocol..
|
||||||
|
|
||||||
|
Let's suppose that we want to write a custom protocol that is based on JSON but has the same semantic as HTTP; let's call this custom protocol `JSONHTTP`, so that a request would look like this:
|
||||||
|
|
||||||
|
[,json]
|
||||||
|
----
|
||||||
|
{
|
||||||
|
"type": "request",
|
||||||
|
"method": "GET",
|
||||||
|
"version": "HTTP/1.1",
|
||||||
|
"uri": "http://localhost/path",
|
||||||
|
"fields": {
|
||||||
|
"content-type": "text/plain;charset=ASCII"
|
||||||
|
},
|
||||||
|
"content": "HELLO"
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|
||||||
|
In order to implement this custom protocol, we need to:
|
||||||
|
|
||||||
|
* implement a `JSONHTTPConnectionFactory`
|
||||||
|
* implement a `JSONHTTPConnection`
|
||||||
|
* parse bytes and generate bytes in the `JSONHTTP` format
|
||||||
|
* design an easy to use API that applications use to process requests and respond
|
||||||
|
|
||||||
|
First, the `JSONHTTPConnectionFactory`:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=jsonHttpConnectionFactory]
|
||||||
|
----
|
||||||
|
|
||||||
|
Note how `JSONHTTPConnectionFactory` extends `AbstractConnectionFactory` to inherit facilities common to all `ConnectionFactory` implementations.
|
||||||
|
|
||||||
|
Second, the `JSONHTTPConnection`.
|
||||||
|
Recall from the xref:arch/io.adoc#echo[echo `Connection` example] that you need to override `onOpen()` to call `fillInterested()` so that the Jetty I/O system will notify your `Connection` implementation when there are bytes to read by calling `onFillable()`.
|
||||||
|
Furthermore, because the Jetty libraries are non-blocking and asynchronous, you need to use `IteratingCallback` to implement `onFillable()`:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=jsonHttpConnection]
|
||||||
|
----
|
||||||
|
|
||||||
|
Again, note how `JSONHTTPConnection` extends `AbstractConnection` to inherit facilities that you would otherwise need to re-implement from scratch.
|
||||||
|
|
||||||
|
When `JSONHTTPConnection` receives a full JSON object it calls `invokeApplication(...)` to allow the application to process the incoming request and produce a response.
|
||||||
|
|
||||||
|
At this point you need to design a non-blocking asynchronous API that takes a `Callback` parameter so that applications can signal to the implementation when the request processing is complete (either successfully or with a failure).
|
||||||
|
|
||||||
|
A simple example of this API design could be the following:
|
||||||
|
|
||||||
|
* Wrap the JSON `Map` into a `JSONHTTPRequest` parameter so that applications may use more specific HTTP APIs such as `JSONHTTPRequest.getMethod()` rather than a generic `Map.get("method")`
|
||||||
|
* Provide an equivalent `JSONHTTPResponse` parameter so that applications may use more specific APIs such as `JSONHTTPResponse.setStatus(int)` rather than a generic `Map.put("status", 200)`
|
||||||
|
* Provide a `Callback` (or a `CompletableFuture`) parameter so that applications may indicate when the request processing is complete
|
||||||
|
|
||||||
|
This results in the following API:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ServerDocs.java[tags=jsonHttpAPI]
|
||||||
|
----
|
||||||
|
|
||||||
|
The important part of this simple API example is the `Callback` parameter that makes the API non-blocking and asynchronous.
|
|
@ -0,0 +1,842 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= HTTP Session Management
|
||||||
|
|
||||||
|
Sessions are a concept within the Servlet API which allow requests to store and retrieve information across the time a user spends in an application.
|
||||||
|
Jetty provides a number of pluggable options for managing sessions.
|
||||||
|
In this section we'll look at the architecture of session support in Jetty, review the various pluggable options available and indicate what and how to customize should none of the existing options suit your usecase.
|
||||||
|
|
||||||
|
[[architecture]]
|
||||||
|
== Session Architecture
|
||||||
|
|
||||||
|
Terminology::
|
||||||
|
SessionIdManager::: is responsible for allocation of session ids
|
||||||
|
HouseKeeper::: is responsible for orchestrating the detection and removal of expired sessions
|
||||||
|
SessionHandler::: is responsible for managing the lifecycle of sessions within its associated context
|
||||||
|
SessionCache::: is an L1 cache of in-use `Session` objects
|
||||||
|
Session::: is a stateful object representing a `HttpSession`
|
||||||
|
SessionData::: encapsulates the attributes and metadata associated with a `Session`
|
||||||
|
SessionDataStore::: is responsible for creating, storing and reading `SessionData`
|
||||||
|
CachingSessionDataStore::: is an L2 cache of `SessionData`
|
||||||
|
|
||||||
|
The session architecture can be represented like so:
|
||||||
|
|
||||||
|
[plantuml]
|
||||||
|
----
|
||||||
|
title Session Composition Diagram
|
||||||
|
class Server
|
||||||
|
|
||||||
|
interface SessionIdManager
|
||||||
|
|
||||||
|
class HouseKeeper
|
||||||
|
|
||||||
|
class SessionHandler
|
||||||
|
|
||||||
|
interface SessionCache
|
||||||
|
|
||||||
|
interface SessionDataStore
|
||||||
|
|
||||||
|
class CachingSessionDataStore
|
||||||
|
|
||||||
|
class Session
|
||||||
|
|
||||||
|
class SessionData
|
||||||
|
|
||||||
|
Server "1" *-down- "1" SessionIdManager
|
||||||
|
SessionIdManager "1" *-left- "1" HouseKeeper
|
||||||
|
Server "1" *-down- "n" SessionHandler
|
||||||
|
SessionHandler "1" *-down- "1" SessionCache
|
||||||
|
SessionCache "1" *-down- "1" SessionDataStore
|
||||||
|
SessionCache o-down- Session
|
||||||
|
Session "1" *-- "1" SessionData
|
||||||
|
SessionDataStore --> SessionData: CRUD
|
||||||
|
SessionDataStore <|-- CachingSessionDataStore
|
||||||
|
CachingSessionDataStore o- SessionData
|
||||||
|
----
|
||||||
|
|
||||||
|
[[idmgr]]
|
||||||
|
== The SessionIdManager
|
||||||
|
|
||||||
|
There is a maximum of one `SessionIdManager` per `Server` instance.
|
||||||
|
Its purpose is to generate fresh, unique session ids and to coordinate the re-use of session ids amongst co-operating contexts.
|
||||||
|
|
||||||
|
The `SessionIdManager` is agnostic with respect to the type of clustering technology chosen.
|
||||||
|
|
||||||
|
Jetty provides a default implementation - the link:{javadoc-url}/org/eclipse/jetty/server/session/DefaultSessionIdManager.html[DefaultSessionIdManager] - which should meet the needs of most users.
|
||||||
|
|
||||||
|
NOTE: If you do not explicitly configure a `SessionIdManager`, then when the `SessionHandler` starts, it will use an instance of the `DefaultSessionIdManager`.
|
||||||
|
|
||||||
|
[[defaultidmgr]]
|
||||||
|
=== The DefaultSessionIdManager
|
||||||
|
|
||||||
|
At startup, if no instance of the `HouseKeeper` has been explicitly set, the `DefaultSessionIdManager` will create one.
|
||||||
|
|
||||||
|
[[workername]]
|
||||||
|
Also at startup, the `workerName` is determined.
|
||||||
|
The `workerName` must be unique per `Server`, and identifies the server in a cluster.
|
||||||
|
If a `workerName` has not been explicitly set, then the value is derived as follows:
|
||||||
|
|
||||||
|
+node[JETTY_WORKER_NAME]+
|
||||||
|
|
||||||
|
where `JETTY_WORKER_NAME` is an environment variable whose value can be an integer or string.
|
||||||
|
If the environment variable is not set, then it defaults to `0`, yielding the default `workerName` of `"node0"`.
|
||||||
|
|
||||||
|
The `DefaultSessionIdManager` uses `SecureRandom` to generate unique session ids.
|
||||||
|
|
||||||
|
The `SessionHandler` class, which is used by both the `ServletContextHandler` and the `WebAppContext` classes, will instantiate a `DefaultSessionIdManager` on startup if it does not detect one already present for the `Server`.
|
||||||
|
|
||||||
|
Here is an example of explicitly setting up a `DefaultSessionIdManager` with a `workerName` of `server3` in code:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/session/SessionDocs.java[tags=default]
|
||||||
|
----
|
||||||
|
|
||||||
|
== Implementing a Custom SessionIdManager
|
||||||
|
|
||||||
|
If the `DefaultSessionIdManager` does not meet your needs, you can extend it, or implement the `SessionIdManager` interface directly.
|
||||||
|
|
||||||
|
When implementing a `SessionIdManager` pay particular attention to the following:
|
||||||
|
|
||||||
|
* the `getWorkerName()` method _must_ return a name that is unique to the `Server` instance.
|
||||||
|
The `workerName` becomes important in clustering scenarios because sessions can migrate from node to node: the `workerName` identifies which node was last managing a `Session`.
|
||||||
|
* the contract of the `isIdInUse(String id)` method is very specific: a session id may _only_ be reused _iff_ it is already in use by another context.
|
||||||
|
This restriction is important to support cross-context dispatch.
|
||||||
|
* you should be _very_ careful to ensure that the `newSessionId(HttpServletRequest request, long created)` method does not return duplicate or predictable session ids.
|
||||||
|
|
||||||
|
[[housekeeper]]
|
||||||
|
=== The HouseKeeper
|
||||||
|
|
||||||
|
There is a maximum of one link:{javadoc-url}/org/eclipse/jetty/server/session/HouseKeeper.html[HouseKeeper] per `SessionIdManager`.
|
||||||
|
Its purpose is to periodically poll the link:{javadoc-url}/org/eclipse/jetty/server/session/SessionHandler.html[SessionHandlers] to clean out expired sessions.
|
||||||
|
This operation is usually referred to as "scavenging" expired sessions.
|
||||||
|
The scavenging interval is configured by the `setIntervalSec(long)` method.
|
||||||
|
The default value is ``600``sec, ie ``10``mins.
|
||||||
|
|
||||||
|
[IMPORTANT]
|
||||||
|
====
|
||||||
|
The HouseKeeper semi-randomly adds an additional `10%` of the configured `intervalSec`.
|
||||||
|
This is to help prevent sync-ing up of servers in a cluster that are all restarted at once, and slightly stagger their scavenge cycles to ensure any load on the persistent storage mechanism is spread out.
|
||||||
|
====
|
||||||
|
|
||||||
|
This code example shows how to configure a `HouseKeeper`, along with a `DefaultSessionIdManager`:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/session/SessionDocs.java[tags=housekeeper]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[handler]]
|
||||||
|
== The SessionHandler
|
||||||
|
|
||||||
|
Each context can have a single `SessionHandler`.
|
||||||
|
The purpose of the `SessionHandler` is to interact with the `Request` and `Response` to create, maintain and propagate sessions.
|
||||||
|
It also calls the context-level session listeners at appropriate points in the session lifecycle.
|
||||||
|
|
||||||
|
=== Configuration
|
||||||
|
|
||||||
|
The majority of configuration for the link:{javadoc-url}/org/eclipse/jetty/server/session/SessionHandler.html[SessionHandler] can be done via `web.xml` `<session-config>` declarations, or the `javax.servlet.SessionCookieConfig` api.
|
||||||
|
There are also a few jetty-specific configuration options that we will cover here:
|
||||||
|
|
||||||
|
checkingRemoteSessionIdEncoding::
|
||||||
|
Boolean, default `false`.
|
||||||
|
This controls whether or not the `javax.servlet.http.Response.encodeURL(String)` method will include the session id as a path parameter when the URL is destined for a remote node.
|
||||||
|
This can also be configured by:
|
||||||
|
* setting the `org.eclipse.jetty.servlet.CheckingRemoteSessionIdEncoding` context init paramter
|
||||||
|
|
||||||
|
setMaxInactiveInterval::
|
||||||
|
Integer, seconds.
|
||||||
|
This is the amount of time after which an unused session may be scavenged.
|
||||||
|
This can also be configured by:
|
||||||
|
|
||||||
|
* defining the `<session-config><session-timeout/></session-config>` element in `web.xml`, although take note that this element is specified in _minutes_ but this method uses _seconds_.
|
||||||
|
* calling the `javax.servlet.ServletContext.setSessionTimeout(int)` method, where the timeout is configured in _minutes_.
|
||||||
|
|
||||||
|
setHttpOnly::
|
||||||
|
Boolean, default `false`.
|
||||||
|
If `true`, the session cookie will not be exposed to client-side scripting code.
|
||||||
|
This can also be configured by:
|
||||||
|
|
||||||
|
* using `javax.servlet.SessionCookieConfig.setHttpOnly(boolean)` method
|
||||||
|
* defining the `<session-config><cookie-config><http-only/></cookie-config></session-config>` element in `web.xml`
|
||||||
|
|
||||||
|
[[handler-refreshcookie]]
|
||||||
|
refreshCookieAge::
|
||||||
|
Integer, seconds, default is `-1`.
|
||||||
|
This controls resetting the session cookie when `SessionCookieConfig.setMaxAge(int)` is non-zero.
|
||||||
|
See also <<handler-maxAge,setting the max session cookie age with an init parameter>>.
|
||||||
|
If the amount of time since the session cookie was last set exceeds this time, the session cookie is regenerated to keep the session cookie valid.
|
||||||
|
|
||||||
|
sameSite::
|
||||||
|
`HttpCookie.SameSite`, default `null`.
|
||||||
|
The values are `HttpCookie.SameSite.NONE`, `HttpCookie.SameSite.STRICT`, `HttpCookie.SameSite.LAX`.
|
||||||
|
|
||||||
|
secureRequestOnly::
|
||||||
|
Boolean, default `true`.
|
||||||
|
If `true` and the request is HTTPS, the set session cookie will be marked as `secure`, meaning the client will only send the session cookie to the server on subsequent requests over HTTPS.
|
||||||
|
This can also be configured by:
|
||||||
|
|
||||||
|
* using the `javax.servlet.SessionCookieConfig.setSecure(true)` method, in which case the set session cookie will _always_ be marked as `secure`, even if the request triggering the creation of the cookie was not over HTTPS.
|
||||||
|
|
||||||
|
sessionCookie::
|
||||||
|
String, default is `JSESSIONID`.
|
||||||
|
This is the name of the session cookie.
|
||||||
|
It can alternatively be configured by:
|
||||||
|
|
||||||
|
* using `javax.servlet.SessionCookieConfig.setName(String)` method
|
||||||
|
* setting the `org.eclipse.jetty.servlet.SessionCookie` context init parameter.
|
||||||
|
|
||||||
|
sessionIdPathParameterName::
|
||||||
|
String, default is `jsessionid`.
|
||||||
|
This is the name of the path parameter used to transmit the session id on request URLs, and on encoded URLS in responses.
|
||||||
|
It can alternatively be configured by:
|
||||||
|
|
||||||
|
* setting the `org.eclipse.jetty.servlet.SessionIdPathParameterName` context init parameter
|
||||||
|
|
||||||
|
sessionTrackingModes::
|
||||||
|
`Set<javax.servlet.SessionTrackingMode>`.
|
||||||
|
Default is `SessionTrackingMode.COOKIE`, `SessionTrackingMode.URL`.
|
||||||
|
This can also be configured by:
|
||||||
|
|
||||||
|
* using the `setSessionTrackingModes(Set<javax.servlet.SessionTrackingMode>)` method
|
||||||
|
* using the `javax.servlet.ServletContext.setSessionTrackingModes<Set<javax.servlet.SessionTrackingMode>)` method
|
||||||
|
* defining up to three ``<tracking-mode>``s for the `<session-config>` element in `web.xml`
|
||||||
|
|
||||||
|
usingCookies::
|
||||||
|
Boolean, default `true`.
|
||||||
|
Determines whether or not the `SessionHandler` will look for session cookies on requests, and will set session cookies on responses.
|
||||||
|
If `false` session ids must be transmitted as path params on URLs.
|
||||||
|
This can also be configured by:
|
||||||
|
|
||||||
|
* using the `setSessionTrackingModes(Set<javax.servlet.SessionTrackingMode>)` method
|
||||||
|
* using the `javax.servlet.ServletContext.setSessionTrackingModes<Set<javax.servlet.SessionTrackingMode>)` method
|
||||||
|
|
||||||
|
There are also a few session settings that do not have SessionHandler setters, but can be configured with context init parameters:
|
||||||
|
|
||||||
|
[[handler-maxAge]]
|
||||||
|
org.eclipse.jetty.servlet.MaxAge::
|
||||||
|
This is the maximum number of seconds that the session cookie will be considered to be valid.
|
||||||
|
By default, the cookie has no maximum validity time.
|
||||||
|
See also <<handler-refreshcookie,refreshing the session cookie>>.
|
||||||
|
The value can also be configured by:
|
||||||
|
|
||||||
|
* calling the `SessionCookieConfig.setMaxAge(int)` method.
|
||||||
|
|
||||||
|
org.eclipse.jetty.servlet.SessionDomain::
|
||||||
|
String, default `null`.
|
||||||
|
This is the domain of the session cookie.
|
||||||
|
This can also be configured by:
|
||||||
|
|
||||||
|
* using the `javax.servlet.SessionCookieConfig.setDomain(String)` method
|
||||||
|
* defining the `<session-config><cookie-config><domain/></cookie-config></session-config>` element in `web.xml`
|
||||||
|
|
||||||
|
org.eclipse.jetty.servlet.SessionPath::
|
||||||
|
String, default `null`.
|
||||||
|
This is used when creating a new session cookie.
|
||||||
|
If nothing is configured, the context path is used instead, defaulting to `/`.
|
||||||
|
This can also be configured by:
|
||||||
|
|
||||||
|
* using the `javax.servlet.SessionCookieConfig.setPath(String)` method
|
||||||
|
* defining the `<session-config><cookie-config><path/></cookie-config></session-config>` element in `web.xml`
|
||||||
|
|
||||||
|
=== Statistics
|
||||||
|
|
||||||
|
Some statistics about the sessions for a context can be obtained from the `SessionHandler`, either by calling the methods directly or via `jmx`:
|
||||||
|
|
||||||
|
sessionsCreated::
|
||||||
|
This is the total number of sessions that have been created for this context since Jetty started.
|
||||||
|
|
||||||
|
sessionTimeMax::
|
||||||
|
The longest period of time a session was valid in this context before being invalidated.
|
||||||
|
|
||||||
|
sessionTimeMean::
|
||||||
|
The average period of time a session in this context was valid.
|
||||||
|
|
||||||
|
sessionTimeStdDev::
|
||||||
|
The standard deviation of the session validity times for this context.
|
||||||
|
|
||||||
|
sessionTimeTotal::
|
||||||
|
The total time that all sessions in this context have remained valid.
|
||||||
|
|
||||||
|
You can reset the statistics counters by either calling the following method directly on the the `SessionHandler`, or using `jmx`:
|
||||||
|
|
||||||
|
statsReset::
|
||||||
|
Resets the `SessionHandler` statistics counters.
|
||||||
|
|
||||||
|
[[cache]]
|
||||||
|
== The SessionCache
|
||||||
|
|
||||||
|
There is one `SessionCache` per `SessionHandler`, and thus one per context.
|
||||||
|
Its purpose is to provide an L1 cache of `Session` objects.
|
||||||
|
Having a working set of `Session` objects in memory allows multiple simultaneous requests for the same session to share the same `Session` object.
|
||||||
|
A `SessionCache` uses a `SessionDataStore` to create, read, store and delete the `SessionData` associated with the `Session`.
|
||||||
|
|
||||||
|
There are two ways to create a `SessionCache` for a `SessionHandler`:
|
||||||
|
|
||||||
|
. allow the `SessionHandler` to create one lazily at startup.
|
||||||
|
The `SessionHandler` looks for a `SessionCacheFactory` bean on the server to produce the `SessionCache` instance.
|
||||||
|
It then looks for a `SessionDataStoreFactory` bean on the server to produce a `SessionDataStore` instance to use with the `SessionCache`.
|
||||||
|
|
||||||
|
. pass a fully configured `SessionCache` instance to the `SessionHandler`.
|
||||||
|
You are responsible for configuring both the `SessionCache` instance and its `SessionDataStore`
|
||||||
|
|
||||||
|
More on ``SessionDataStore``s <<datastore,later>>, in this section we will concentrate on the `SessionCache` and `SessionCacheFactory`.
|
||||||
|
|
||||||
|
|
||||||
|
The link:{javadoc-url}/org/eclipse/jetty/server/session/AbstractSessionCache.html[AbstractSessionCache] provides most of the behaviour of ``SessionCache``s.
|
||||||
|
If you are implementing a custom `SessionCache` we strongly recommend you extend this base class, as the Servlet Specification has many subtleties and extending the base class ensures that your implementation will take account of them.
|
||||||
|
|
||||||
|
Some of the important behaviours of ``SessionCache``s are:
|
||||||
|
|
||||||
|
eviction::
|
||||||
|
By default, sessions remain in a cache until they are expired or invalidated.
|
||||||
|
If you have many or large sessions that are infrequently referenced you can use eviction to reduce the memory consumed by the cache.
|
||||||
|
When a session is evicted, it is removed from the cache but it is _not_ invalidated.
|
||||||
|
If you have configured a `SessionDataStore` that persists or distributes the session in some way, it will continue to exist, and can be read back in when it needs to be referenced again.
|
||||||
|
The eviction strategies are:
|
||||||
|
NEVER_EVICT:::
|
||||||
|
This is the default, sessions remain in the cache until expired or invalidated.
|
||||||
|
EVICT_ON_SESSION_EXIT:::
|
||||||
|
When the last simultaneous request for a session finishes, the session will be evicted from the cache.
|
||||||
|
EVICT_ON_INACTIVITY:::
|
||||||
|
If a session has not been referenced for a configurable number of seconds, then it will be evicted from the cache.
|
||||||
|
|
||||||
|
saveOnInactiveEviction::
|
||||||
|
This controls whether a session will be persisted to the `SessionDataStore` if it is being evicted due to the EVICT_ON_INACTIVITY policy.
|
||||||
|
Usually sessions are written to the `SessionDataStore` whenever the last simultaneous request exits the session.
|
||||||
|
However, as `SessionDataStores` can be configured to <<datastore-skip,skip some writes>>, this option ensures that the session will be written out.
|
||||||
|
|
||||||
|
saveOnCreate::
|
||||||
|
Usually a session will be written through to the configured `SessionDataStore` when the last request for it finishes.
|
||||||
|
In the case of a freshly created session, this means that it will not be persisted until the request is fully finished.
|
||||||
|
If your application uses context forwarding or including, the newly created session id will not be available in the subsequent contexts.
|
||||||
|
You can enable this feature to ensure that a freshly created session is immediately persisted after creation: in this way the session id will be available for use in other contexts accessed during the same request.
|
||||||
|
|
||||||
|
removeUnloadableSessions::
|
||||||
|
If a session becomes corrupted in the persistent store, it cannot be re-loaded into the `SessionCache`.
|
||||||
|
This can cause noisy log output during scavenge cycles, when the same corrupted session fails to load over and over again.
|
||||||
|
To prevent his, enable this feature and the `SessionCache` will ensure that if a session fails to be loaded, it will be deleted.
|
||||||
|
|
||||||
|
invalidateOnShutdown::
|
||||||
|
Some applications want to ensure that all cached sessions are removed when the server shuts down.
|
||||||
|
This option will ensure that all cached sessions are invalidated.
|
||||||
|
The `AbstractSessionCache` does not implement this behaviour, a subclass must implement the link:{javadoc-url}/org/eclipse/jetty/server/session/SessionCache.html#shutdown()[SessionCache.shutdown()] method.
|
||||||
|
|
||||||
|
flushOnResponseCommit::
|
||||||
|
This forces a "dirty" session to be written to the `SessionDataStore` just before a response is returned to the client, rather than waiting until the request is finished.
|
||||||
|
A "dirty" session is one whose attributes have changed, or it has been freshly created.
|
||||||
|
Using this option ensures that all subsequent requests - either to the same or a different node - will see the latest changes to the session.
|
||||||
|
|
||||||
|
Jetty provides two `SessionCache` implementations: the link:{javadoc-url}/org/eclipse/jetty/server/session/DefaultSessionCache.html[DefaultSessionCache] and the link:{javadoc-url}/org/eclipse/jetty/server/session/NullSessionCache.html[NullSessionCache].
|
||||||
|
|
||||||
|
[[hash]]
|
||||||
|
=== The DefaultSessionCache
|
||||||
|
|
||||||
|
The link:{javadoc-url}/org/eclipse/jetty/server/session/DefaultSessionCache.html[DefaultSessionCache] retains `Session` objects in memory in a `ConcurrentHashMap`.
|
||||||
|
It is suitable for non-clustered and clustered deployments.
|
||||||
|
For clustered deployments, a sticky load balancer is *strongly* recommended, otherwise you risk indeterminate session state as the session bounces around multiple nodes.
|
||||||
|
|
||||||
|
It implements the link:{javadoc-url}/org/eclipse/jetty/server/session/SessionCache.html#shutdown()[SessionCache.shutdown()] method.
|
||||||
|
|
||||||
|
It also provides some statistics on sessions, which are convenient to access either directly in code or remotely via jmx:
|
||||||
|
|
||||||
|
current sessions::
|
||||||
|
The link:{javadoc-url}/org/eclipse/jetty/server/session/DefaultSessionCache.html#getSessionsCurrent()[DefaultSessionCache.getSessionsCurrent()] reports the number of sessions in the cache at the time of the method call.
|
||||||
|
|
||||||
|
max sessions::
|
||||||
|
The link:{javadoc-url}/org/eclipse/jetty/server/session/DefaultSessionCache.html#getSessionsCurrent()[DefaultSessionCache.getSessionsMax()] reports the highest number of sessions in the cache at the time of the method call.
|
||||||
|
|
||||||
|
|
||||||
|
total sessions::
|
||||||
|
The link:{javadoc-url}/org/eclipse/jetty/server/session/DefaultSessionCache.html#getSessionsTotal()[DefaultSessionCache.getSessionsTotal()] reports the cumulative total of the number of sessions in the cache at the time of the method call.
|
||||||
|
|
||||||
|
reset::
|
||||||
|
The link:{javadoc-url}/org/eclipse/jetty/server/session/DefaultSessionCache.html#resetStats()[DefaultSessionCache.resetStats()] zeros out the statistics counters.
|
||||||
|
|
||||||
|
If you create a link:{javadoc-url}/org/eclipse/jetty/server/session/DefaultSessionCacheFactory.html[DefaultSessionFactory] and register it as `Server` bean, a `SessionHandler` will be able to lazily create a `DefaultSessionCache`.
|
||||||
|
The `DefaultSessionCacheFactory` has all of the same configuration setters as a `DefaultSessionCache`.
|
||||||
|
Alternatively, if you only have a single `SessionHandler`, or you need to configure a `DefaultSessionCache` differently for every `SessionHandler`, then you could dispense with the `DefaultSessionCacheFactory` and simply instantiate, configure and pass in the `DefaultSessionCache` yourself.
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/session/SessionDocs.java[tags=defaultsessioncache]
|
||||||
|
----
|
||||||
|
|
||||||
|
NOTE: If you don't configure any `SessionCache` or `SessionCacheFactory`, the `SessionHandler` will automatically create a `DefaultSessionCache`.
|
||||||
|
|
||||||
|
[[null]]
|
||||||
|
=== The NullSessionCache
|
||||||
|
|
||||||
|
The link:{javadoc-url}/org/eclipse/jetty/server/session/NullSessionCache.html[NullSessionCache] does not actually cache any objects: each request uses a fresh `Session` object.
|
||||||
|
It is suitable for clustered deployments without a sticky load balancer and non-clustered deployments when purely minimal support for sessions is needed.
|
||||||
|
|
||||||
|
As no sessions are actually cached, of course functions like `invalidateOnShutdown` and all of the eviction strategies have no meaning for the `NullSessionCache`.
|
||||||
|
|
||||||
|
There is a link:{javadoc-url}/org/eclipse/jetty/server/session/NullSessionCacheFactory.html[NullSessionCacheFactory] which you can instantiate, configure and set as a `Server` bean to enable the `SessionHandler` to automatically create new ``NullCache``s as needed.
|
||||||
|
All of the same configuration options are available on the `NullSessionCacheFactory` as the `NullSessionCache` itself.
|
||||||
|
Alternatively, if you only have a single `SessionHandler`, or you need to configure a `NullSessionCache` differently for every `SessionHandler`, then you could dispense with the `NullSessionCacheFactory` and simply instantiate, configure and pass in the `NullSessionCache` yourself.
|
||||||
|
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/session/SessionDocs.java[tags=nullsessioncache]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[customcache]]
|
||||||
|
=== Implementing a Custom SessionCache
|
||||||
|
|
||||||
|
As previously mentioned, we highly recommend that you extend the link:{javadoc-url}/org/eclipse/jetty/server/session/AbstractSessionCache.html[AbstractSessionCache].
|
||||||
|
|
||||||
|
=== Heterogeneous Caching
|
||||||
|
|
||||||
|
Using one of the ``SessionCacheFactory``s will ensure that every time a `SessionHandler` starts it will create a new instance of the corresponding type of `SessionCache`.
|
||||||
|
|
||||||
|
But, what if you deploy multiple webapps, and for one of them, you don't want to use sessions?
|
||||||
|
Or alternatively, you don't want to use sessions, but you have one webapp that now needs them?
|
||||||
|
In that case, you can configure the `SessionCacheFactory` appropriate to the majority, and then specifically create the right type of `SessionCache` for the others.
|
||||||
|
Here's an example where we configure the `DefaultSessionCacheFactory` to handle most webapps, but then specifically use a `NullSessionCache` for another:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/session/SessionDocs.java[tags=mixedsessioncache]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[datastore]]
|
||||||
|
== The SessionDataStore
|
||||||
|
|
||||||
|
A link:{javadoc-url}/org/eclipse/jetty/server/session/SessionDataStore.html[SessionDataStore] mediates the storage, retrieval and deletion of `SessionData`.
|
||||||
|
There is one `SessionDataStore` per `SessionCache`.
|
||||||
|
The server libraries provide a number of alternative `SessionDataStore` implementations.
|
||||||
|
|
||||||
|
[plantuml]
|
||||||
|
----
|
||||||
|
title SessionDataStores
|
||||||
|
|
||||||
|
interface SessionDataStore
|
||||||
|
class AbstractSessionDataStore
|
||||||
|
class FileSessionDataStore
|
||||||
|
class GCloudSessionDataStore
|
||||||
|
class HazelcastSessionDataStore
|
||||||
|
class InfinispanSessionDataStore
|
||||||
|
class JDBCSessionDataStore
|
||||||
|
class MongoSessionDataStore
|
||||||
|
class CachingSessionDataStore
|
||||||
|
|
||||||
|
|
||||||
|
SessionDataStore <|-- AbstractSessionDataStore
|
||||||
|
AbstractSessionDataStore <|-- FileSessionDataStore
|
||||||
|
AbstractSessionDataStore <|-- GCloudSessionDataStore
|
||||||
|
AbstractSessionDataStore <|-- HazelcastSessionDataStore
|
||||||
|
AbstractSessionDataStore <|-- InfinispanSessionDataStore
|
||||||
|
AbstractSessionDataStore <|-- JDBCSessionDataStore
|
||||||
|
AbstractSessionDataStore <|-- MongoSessionDataStore
|
||||||
|
SessionDataStore <|-- CachingSessionDataStore
|
||||||
|
----
|
||||||
|
|
||||||
|
The link:{javadoc-url}/org/eclipse/jetty/server/session/AbstractSessionDataStore.html[AbstractSessionDataStore] provides most of the behaviour common to ``SessionDataStore``s:
|
||||||
|
|
||||||
|
passivation::
|
||||||
|
Supporting passivation means that session data is serialized.
|
||||||
|
Some persistence mechanisms serialize, such as JDBC, GCloud Datastore etc.
|
||||||
|
Others store an object in shared memory, e.g. Infinispan and thus don't serialize session data.
|
||||||
|
Whether or not a persistence technology entails passivation controls whether or not ``HttpSessionActivationListener``s will be called.
|
||||||
|
When implementing a custom `SessionDataStore` you need to decide whether or not passivation will be supported.
|
||||||
|
|
||||||
|
[[datastore-skip]]
|
||||||
|
//tag::common-datastore-config[]
|
||||||
|
savePeriod::
|
||||||
|
This is an interval defined in seconds.
|
||||||
|
It is used to reduce the frequency with which `SessionData` is written.
|
||||||
|
Normally, whenever the last concurrent request leaves a `Session`, the `SessionData` for that `Session` is always persisted, even if the only thing that changed is the `lastAccessTime`.
|
||||||
|
If the `savePeriod` is non-zero, the `SessionData` will not be persisted if no session attributes changed, _unless_ the time since the last save exceeds the `savePeriod`.
|
||||||
|
Setting a non-zero value can reduce the load on the persistence mechanism, but in a clustered environment runs the risk that other nodes will see the session as expired because it has not been persisted sufficiently recently.
|
||||||
|
|
||||||
|
gracePeriod::
|
||||||
|
The `gracePeriod` is an interval defined in seconds.
|
||||||
|
It is an attempt to deal with the non-transactional nature of sessions with regard to finding sessions that have expired.
|
||||||
|
In a clustered configuration - even with a sticky load balancer - it is always possible that a session is "live" on a node but not yet updated in the persistent store.
|
||||||
|
This means that it can be hard to determine at any given moment whether a clustered session has truly expired.
|
||||||
|
Thus, we use the `gracePeriod` to provide a bit of leeway around the moment of expiry during <<housekeeper,scavenge>>:
|
||||||
|
|
||||||
|
* on every <<housekeeper,scavenge>> cycle an `AbstractSessionDataStore` searches for sessions that belong to the context that expired at least one `gracePeriod` ago
|
||||||
|
* infrequently the `AbstractSessionDataStore` searches for and summarily deletes sessions - from any context - that expired at least 10 ``gracePeriod``s ago
|
||||||
|
//end::common-datastore-config[]
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
The trivial link:{javadoc-url}/org/eclipse/jetty/server/session/NullSessionDataStore.html[NullSessionDataStore] - which does not persist sessions - is the default used by the `SessionHandler`.
|
||||||
|
====
|
||||||
|
|
||||||
|
[[datastore-file]]
|
||||||
|
=== The FileSessionDataStore
|
||||||
|
|
||||||
|
The `FileSessionDataStore` supports persistent storage of session data in a filesystem.
|
||||||
|
|
||||||
|
IMPORTANT: Persisting sessions to the local file system should *never* be used in a clustered environment.
|
||||||
|
|
||||||
|
One file represents one session in one context.
|
||||||
|
|
||||||
|
File names follow this pattern:
|
||||||
|
|
||||||
|
+[expiry]_[contextpath]_[virtualhost]_[id]+
|
||||||
|
|
||||||
|
expiry::
|
||||||
|
This is the expiry time in milliseconds since the epoch.
|
||||||
|
|
||||||
|
contextpath::
|
||||||
|
This is the context path with any special characters, including `/`, replaced by the `_` underscore character.
|
||||||
|
For example, a context path of `/catalog` would become `_catalog`.
|
||||||
|
A context path of simply `/` becomes just `__`.
|
||||||
|
virtualhost::
|
||||||
|
This is the first virtual host associated with the context and has the form of 4 digits separated by `.` characters.
|
||||||
|
If there are no virtual hosts associated with a context, then `0.0.0.0` is used:
|
||||||
|
|
||||||
|
[digit].[digit].[digit].[digit]
|
||||||
|
|
||||||
|
id::
|
||||||
|
This is the unique id of the session.
|
||||||
|
|
||||||
|
Putting all of the above together as an example, a session with an id of `node0ek3vx7x2y1e7pmi3z00uqj1k0` for the context with path `/test` with no virtual hosts and an expiry of `1599558193150` would have a file name of:
|
||||||
|
|
||||||
|
`1599558193150__test_0.0.0.0_node0ek3vx7x2y1e7pmi3z00uqj1k0`
|
||||||
|
|
||||||
|
|
||||||
|
==== Configuration
|
||||||
|
|
||||||
|
You can configure either a link:{javadoc-url}/org/eclipse/jetty/server/session/FileSessionDataStore.html[FileSessionDataStore] individually, or a `FileSessionDataStoreFactory` if you want multiple ``SessionHandler``s to use ``FileSessionDataStore``s that are identically configured.
|
||||||
|
The configuration methods are:
|
||||||
|
|
||||||
|
storeDir::
|
||||||
|
This is a File that defines the location for storage of session files.
|
||||||
|
If the directory does not exist at startup, it will be created.
|
||||||
|
If you use the same `storeDir` for multiple `SessionHandlers`, then the sessions for all of those contexts are stored in the same directory.
|
||||||
|
This is not a problem, as the name of the file is unique because it contains the context information.
|
||||||
|
You _must_ supply a value for this, otherwise startup of the `FileSessionDataStore` will fail.
|
||||||
|
|
||||||
|
deleteUnrestorableFiles::
|
||||||
|
Boolean, default `false`.
|
||||||
|
If set to `true`, unreadable files will be deleted.
|
||||||
|
This is useful to prevent repeated logging of the same error when the <<housekeeper,scavenger>> periodically (re-)attempts to load the corrupted information for a session in order to expire it.
|
||||||
|
|
||||||
|
savePeriod::
|
||||||
|
This is an interval defined in seconds.
|
||||||
|
It is used to reduce the frequency with which `SessionData` is written.
|
||||||
|
Normally, whenever the last concurrent request leaves a `Session`, the `SessionData` for that `Session` is always persisted, even if the only thing that changed is the `lastAccessTime`.
|
||||||
|
If the `savePeriod` is non-zero, the `SessionData` will not be persisted if no session attributes changed, _unless_ the time since the last save exceeds the `savePeriod`.
|
||||||
|
Setting a non-zero value can reduce the load on the persistence mechanism, but in a clustered environment runs the risk that other nodes will see the session as expired because it has not been persisted sufficiently recently.
|
||||||
|
|
||||||
|
gracePeriod::
|
||||||
|
The `gracePeriod` is an interval defined in seconds.
|
||||||
|
It is an attempt to deal with the non-transactional nature of sessions with regard to finding sessions that have expired.
|
||||||
|
In a clustered configuration - even with a sticky load balancer - it is always possible that a session is "live" on a node but not yet updated in the persistent store.
|
||||||
|
This means that it can be hard to determine at any given moment whether a clustered session has truly expired.
|
||||||
|
Thus, we use the `gracePeriod` to provide a bit of leeway around the moment of expiry during <<housekeeper,scavenge>>:
|
||||||
|
|
||||||
|
* on every <<housekeeper,scavenge>> cycle an `AbstractSessionDataStore` searches for sessions that belong to the context that expired at least one `gracePeriod` ago
|
||||||
|
* infrequently the `AbstractSessionDataStore` searches for and summarily deletes sessions - from any context - that expired at least 10 ``gracePeriod``s ago
|
||||||
|
|
||||||
|
Let's look at an example of configuring a `FileSessionDataStoreFactory`:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/session/SessionDocs.java[tags=filesessiondatastorefactory]
|
||||||
|
----
|
||||||
|
|
||||||
|
Here's an alternate example, configuring a `FileSessionDataStore` directly:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/session/SessionDocs.java[tags=filesessiondatastore]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[datastore-jdbc]]
|
||||||
|
=== The JDBCSessionDataStore
|
||||||
|
|
||||||
|
The `JDBCSessionDataStore` supports persistent storage of session data in a relational database.
|
||||||
|
To do that, it requires a `DatabaseAdaptor` that handles the differences between databases (eg Oracle, Postgres etc), and a `SessionTableSchema` that allows for the customization of table and column names.
|
||||||
|
|
||||||
|
[plantuml]
|
||||||
|
----
|
||||||
|
class JDBCSessionDataStore
|
||||||
|
class DatabaseAdaptor
|
||||||
|
class SessionTableSchema
|
||||||
|
|
||||||
|
JDBCSessionDataStore "1" *-- "1" DatabaseAdaptor
|
||||||
|
JDBCSessionDataStore "1" *-- "1" SessionTableSchema
|
||||||
|
----
|
||||||
|
|
||||||
|
`SessionData` is stored in a table with one row per session.
|
||||||
|
This is the table, with the table name, column names and type keywords at their default settings:
|
||||||
|
|
||||||
|
|
||||||
|
[caption="Table:"]
|
||||||
|
.JettySessions
|
||||||
|
[frame=all]
|
||||||
|
[cols=12*,options="header"]
|
||||||
|
|===
|
||||||
|
|sessionId
|
||||||
|
|contextPath
|
||||||
|
|virtualHost
|
||||||
|
|lastNode
|
||||||
|
|accessTime
|
||||||
|
|lastAccessTime
|
||||||
|
|createTime
|
||||||
|
|cookieTime
|
||||||
|
|lastSavedTime
|
||||||
|
|expiryTime
|
||||||
|
|maxInterval
|
||||||
|
|map
|
||||||
|
|120 varchar|60 varchar|60 varchar|60 varchar|long|long|long|long|long|long|long|blob
|
||||||
|
|===
|
||||||
|
|
||||||
|
The name of the table and all columns can be configured using the `SessionTableSchema` class described below.
|
||||||
|
Many databases use different keywords for the `long`, `blob` and `varchar` types, so you can explicitly configure these if jetty cannot determine what they should be at runtime based on the metadata available from a db connection using the `DatabaseAdaptor` class described below.
|
||||||
|
|
||||||
|
==== Configuration
|
||||||
|
|
||||||
|
The link:{javadoc-url}/org/eclipse/jetty/server/session/JDBCSessionDataStore.html[JDBCSessionDataStore] and corresponding link:{javadoc-url}/org/eclipse/jetty/server/session/JDBCSessionDataStoreFactory.html[JDBCSessionDataStoreFactory] supports the following configuration:
|
||||||
|
|
||||||
|
savePeriod::
|
||||||
|
This is an interval defined in seconds.
|
||||||
|
It is used to reduce the frequency with which `SessionData` is written.
|
||||||
|
Normally, whenever the last concurrent request leaves a `Session`, the `SessionData` for that `Session` is always persisted, even if the only thing that changed is the `lastAccessTime`.
|
||||||
|
If the `savePeriod` is non-zero, the `SessionData` will not be persisted if no session attributes changed, _unless_ the time since the last save exceeds the `savePeriod`.
|
||||||
|
Setting a non-zero value can reduce the load on the persistence mechanism, but in a clustered environment runs the risk that other nodes will see the session as expired because it has not been persisted sufficiently recently.
|
||||||
|
|
||||||
|
gracePeriod::
|
||||||
|
The `gracePeriod` is an interval defined in seconds.
|
||||||
|
It is an attempt to deal with the non-transactional nature of sessions with regard to finding sessions that have expired.
|
||||||
|
In a clustered configuration - even with a sticky load balancer - it is always possible that a session is "live" on a node but not yet updated in the persistent store.
|
||||||
|
This means that it can be hard to determine at any given moment whether a clustered session has truly expired.
|
||||||
|
Thus, we use the `gracePeriod` to provide a bit of leeway around the moment of expiry during <<housekeeper,scavenge>>:
|
||||||
|
|
||||||
|
* on every <<housekeeper,scavenge>> cycle an `AbstractSessionDataStore` searches for sessions that belong to the context that expired at least one `gracePeriod` ago
|
||||||
|
* infrequently the `AbstractSessionDataStore` searches for and summarily deletes sessions - from any context - that expired at least 10 ``gracePeriod``s ago
|
||||||
|
|
||||||
|
DatabaseAdaptor::
|
||||||
|
The `DatabaseAdaptor` can connect to a database either via a `javax.sql.Datasource` or a `java.sql.Driver`.
|
||||||
|
Additionally, a database-specific keyword can be configured for the `blob`, `varchar` and `long` types.
|
||||||
|
Note that the `DatabaseAdaptor` tries to automatically detect the type of the database from the first connection and select the appropriate type keywords, however you may need to explicitly configure them if you're not using `Postgres` or `Oracle`.
|
||||||
|
|
||||||
|
datasource:::
|
||||||
|
This can either be a `Datasource` instance or the jndi name of a `Datasource` to look up.
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/session/SessionDocs.java[tags=dbaDatasource]
|
||||||
|
----
|
||||||
|
|
||||||
|
driverInfo:::
|
||||||
|
This is the name or instance of a jdbc `Driver` class and a connection url.
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/session/SessionDocs.java[tags=dbaDriver]
|
||||||
|
----
|
||||||
|
|
||||||
|
blobType:::
|
||||||
|
Default `blob` or `bytea` for Postgres.
|
||||||
|
|
||||||
|
longType:::
|
||||||
|
Default `bigint` or `number(20)` for Oracle.
|
||||||
|
|
||||||
|
stringType:::
|
||||||
|
Default `varchar`.
|
||||||
|
|
||||||
|
|
||||||
|
SessionTableSchema::
|
||||||
|
schemaName:::
|
||||||
|
catalogName:::
|
||||||
|
The exact meaning of these two are dependent on your database vendor, but can broadly be described as further scoping for the session table name.
|
||||||
|
See https://en.wikipedia.org/wiki/Database_schema and https://en.wikipedia.org/wiki/Database_catalog.
|
||||||
|
These extra scoping names can come into play at startup time when Jetty determines if the session table already exists, or otherwise creates it on-the-fly.
|
||||||
|
If you have employed either of these concepts when you pre-created the session table, or you want to ensure that Jetty uses them when it auto-creates the session table, then you have two options: either set them explicitly, or let Jetty infer them from a database connection.
|
||||||
|
If you leave them unset, then no scoping will be done.
|
||||||
|
If you use the special value `INFERRED`, Jetty will determine them from a database connection.
|
||||||
|
|
||||||
|
tableName:::
|
||||||
|
Default `JettySessions`.
|
||||||
|
This is the name of the table in which session data is stored.
|
||||||
|
|
||||||
|
accessTimeColumn:::
|
||||||
|
Default `accessTime`.
|
||||||
|
This is the name of the column that stores the time - in ms since the epoch - at which a session was last accessed
|
||||||
|
|
||||||
|
contextPathColumn:::
|
||||||
|
Default `contextPath`.
|
||||||
|
This is the name of the column that stores the `contextPath` of a session.
|
||||||
|
|
||||||
|
cookieTimeColumn:::
|
||||||
|
Default `cookieTime`.
|
||||||
|
This is the name of the column that stores the time - in ms since the epoch - that the cookie was last set for a session.
|
||||||
|
|
||||||
|
createTimeColumn:::
|
||||||
|
Default `createTime`.
|
||||||
|
This is the name of the column that stores the time - in ms since the epoch - at which a session was created.
|
||||||
|
|
||||||
|
expiryTimeColumn:::
|
||||||
|
Default `expiryTime`.
|
||||||
|
This is name of the column that stores - in ms since the epoch - the time at which a session will expire.
|
||||||
|
|
||||||
|
lastAccessTimeColumn:::
|
||||||
|
Default `lastAccessTime`.
|
||||||
|
This is the name of the column that stores the time - in ms since the epoch - that a session was previously accessed.
|
||||||
|
|
||||||
|
lastSavedTimeColumn:::
|
||||||
|
Default `lastSavedTime`.
|
||||||
|
This is the name of the column that stores the time - in ms since the epoch - at which a session was last written.
|
||||||
|
|
||||||
|
idColumn:::
|
||||||
|
Default `sessionId`.
|
||||||
|
This is the name of the column that stores the id of a session.
|
||||||
|
|
||||||
|
lastNodeColumn:::
|
||||||
|
Default `lastNode`.
|
||||||
|
This is the name of the column that stores the `workerName` of the last node to write a session.
|
||||||
|
|
||||||
|
virtualHostColumn:::
|
||||||
|
Default `virtualHost`.
|
||||||
|
This is the name of the column that stores the first virtual host of the context of a session.
|
||||||
|
|
||||||
|
maxIntervalColumn:::
|
||||||
|
Default `maxInterval`.
|
||||||
|
This is the name of the column that stores the interval - in ms - during which a session can be idle before being considered expired.
|
||||||
|
|
||||||
|
mapColumn:::
|
||||||
|
Default `map`.
|
||||||
|
This is the name of the column that stores the serialized attributes of a session.
|
||||||
|
|
||||||
|
[[datastore-mongo]]
|
||||||
|
=== The MongoSessionDataStore
|
||||||
|
|
||||||
|
The `MongoSessionDataStore` supports persistence of `SessionData` in a nosql database.
|
||||||
|
|
||||||
|
The best description for the document model for session information is found in the javadoc for the link:{javadoc-url}/org/eclipse/jetty/nosql/mongodb/MongoSessionDataStore.html[MongoSessionDataStore].
|
||||||
|
In overview, it can be represented thus:
|
||||||
|
|
||||||
|
[plantuml]
|
||||||
|
----
|
||||||
|
database HttpSessions {
|
||||||
|
folder jettySessions {
|
||||||
|
file session {
|
||||||
|
file "context" {
|
||||||
|
rectangle attributes
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|
||||||
|
The database contains a document collection for the sessions.
|
||||||
|
Each document represents a session id, and contains one nested document per context in which that session id is used.
|
||||||
|
For example, the session id `abcd12345` might be used by two contexts, one with path `/contextA` and one with path `/contextB`.
|
||||||
|
In that case, the outermost document would refer to `abcd12345` and it would have a nested document for `/contextA` containing the session attributes for that context, and another nested document for `/contextB` containing the session attributes for that context.
|
||||||
|
Remember, according to the Servlet Specification, a session id can be shared by many contexts, but the attributes must be unique per context.
|
||||||
|
|
||||||
|
The outermost document contains these fields:
|
||||||
|
|
||||||
|
id::
|
||||||
|
The session id.
|
||||||
|
created::
|
||||||
|
The time (in ms since the epoch) at which the session was first created in any context.
|
||||||
|
maxIdle::
|
||||||
|
The time (in ms) for which an idle session is regarded as valid.
|
||||||
|
As maxIdle times can be different for ``Session``s from different contexts, this is the _shortest_ maxIdle time.
|
||||||
|
expiry::
|
||||||
|
The time (in ms since the epoch) at which the session will expire.
|
||||||
|
As the expiry time can be different for ``Session``s from different contexts, this is the _shortest_ expiry time.
|
||||||
|
|
||||||
|
Each nested context-specific document contains:
|
||||||
|
|
||||||
|
attributes::
|
||||||
|
The session attributes as a serialized map.
|
||||||
|
lastSaved::
|
||||||
|
The time (in ms since the epoch) at which the session in this context was saved.
|
||||||
|
lastAccessed::
|
||||||
|
The time (in ms since the epoch) at which the session in this context was previously accessed.
|
||||||
|
accessed::
|
||||||
|
The time (in ms since the epoch) at which this session was most recently accessed.
|
||||||
|
lastNode::
|
||||||
|
The <<workername,workerName>> of the last server that saved the session data.
|
||||||
|
version::
|
||||||
|
An object that is updated every time a session is written out for a context.
|
||||||
|
|
||||||
|
==== Configuration
|
||||||
|
|
||||||
|
You can configure either a link:{javadoc-url}/org/eclipse/jetty/nosql/mongodb/MongoSessionDataStore.html[MongoSessionDataStore] individually, or a link:{javadoc-url}/org/eclipse/jetty/nosql/mongodb/MongoSessionDataStore.html[MongoSessionDataStoreFactory] if you want multiple ``SessionHandler``s to use ``MongoSessionDataStore``s that are identically configured.
|
||||||
|
The configuration methods for the `MongoSessionDataStoreFactory` are:
|
||||||
|
|
||||||
|
savePeriod::
|
||||||
|
This is an interval defined in seconds.
|
||||||
|
It is used to reduce the frequency with which `SessionData` is written.
|
||||||
|
Normally, whenever the last concurrent request leaves a `Session`, the `SessionData` for that `Session` is always persisted, even if the only thing that changed is the `lastAccessTime`.
|
||||||
|
If the `savePeriod` is non-zero, the `SessionData` will not be persisted if no session attributes changed, _unless_ the time since the last save exceeds the `savePeriod`.
|
||||||
|
Setting a non-zero value can reduce the load on the persistence mechanism, but in a clustered environment runs the risk that other nodes will see the session as expired because it has not been persisted sufficiently recently.
|
||||||
|
|
||||||
|
gracePeriod::
|
||||||
|
The `gracePeriod` is an interval defined in seconds.
|
||||||
|
It is an attempt to deal with the non-transactional nature of sessions with regard to finding sessions that have expired.
|
||||||
|
In a clustered configuration - even with a sticky load balancer - it is always possible that a session is "live" on a node but not yet updated in the persistent store.
|
||||||
|
This means that it can be hard to determine at any given moment whether a clustered session has truly expired.
|
||||||
|
Thus, we use the `gracePeriod` to provide a bit of leeway around the moment of expiry during <<housekeeper,scavenge>>:
|
||||||
|
|
||||||
|
* on every <<housekeeper,scavenge>> cycle an `AbstractSessionDataStore` searches for sessions that belong to the context that expired at least one `gracePeriod` ago
|
||||||
|
* infrequently the `AbstractSessionDataStore` searches for and summarily deletes sessions - from any context - that expired at least 10 ``gracePeriod``s ago
|
||||||
|
|
||||||
|
dbName::
|
||||||
|
This is the name of the database.
|
||||||
|
collectionName::
|
||||||
|
The name of the document collection.
|
||||||
|
|
||||||
|
There are two alternative ways to specify the connection to mongodb:
|
||||||
|
|
||||||
|
connectionString::
|
||||||
|
This is a mongodb url, eg `mongodb://localhost`
|
||||||
|
host::
|
||||||
|
port::
|
||||||
|
This is the hostname and port number of the mongodb instance to contact.
|
||||||
|
|
||||||
|
Let's look at an example of configuring a `MongoSessionDataStoreFactory`:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/session/SessionDocs.java[tags=mongosdfactory]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[cachingsessiondatastore]]
|
||||||
|
=== The CachingSessionDataStore
|
||||||
|
|
||||||
|
[plantuml]
|
||||||
|
----
|
||||||
|
interface SessionDataMap
|
||||||
|
class CachingSessionDataStore
|
||||||
|
interface SessionDataStore
|
||||||
|
|
||||||
|
CachingSessionDataStore "1" *-down- "1" SessionDataMap
|
||||||
|
CachingSessionDataStore "1" *-down- "1" SessionDataStore
|
||||||
|
SessionDataMap <|-- MemcachedSessionDataMap
|
||||||
|
----
|
||||||
|
|
||||||
|
The link:{javadoc-url}/org/eclipse/jetty/server/session/CachingSessionDataStore.html[CachingSessionDataStore] is a special type of `SessionDataStore` that checks an L2 cache for `SessionData` before checking a delegate `SessionDataStore`.
|
||||||
|
This can improve the performance of slow stores.
|
||||||
|
|
||||||
|
The L2 cache is an instance of a link:{javadoc-url}/org/eclipse/jetty/server/session/SessionDataMap.html[SessionDataMap].
|
||||||
|
Jetty provides one implementation of this L2 cache based on `memcached`, link:{javadoc-url}/org/eclipse/jetty/memcached/session/MemcachedSessionDataMap.html[MemcachedSessionDataMap].
|
||||||
|
|
||||||
|
==== Configuration
|
||||||
|
|
||||||
|
Here's an example of how to programmatically configure ``CachingSessionDataStore``s, using a <<datastore-file,FileSessionDataStore>> as a delegate, and `memcached` as the L2 cache:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/session/SessionDocs.java[tags=cachingsds]
|
||||||
|
----
|
|
@ -0,0 +1,422 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= WebSocket Server
|
||||||
|
|
||||||
|
Jetty provides two API implementations of the WebSocket protocol:
|
||||||
|
|
||||||
|
* An implementation for the standard `javax.websocket` APIs provided by https://www.jcp.org/en/jsr/detail?id=356[JSR 356], described in <<standard,this section>>.
|
||||||
|
* An implementation for Jetty-specific WebSocket APIs, described in <<jetty,this section>>.
|
||||||
|
|
||||||
|
Using the standard `javax.websocket` APIs allows your applications to depend only on standard APIs, and your applications may be deployed in any compliant WebSocket Container that supports JSR 356.
|
||||||
|
|
||||||
|
The standard APIs provide these features that are not present in the Jetty WebSocket APIs:
|
||||||
|
|
||||||
|
* Encoders and Decoders for automatic conversion of text or binary messages to objects.
|
||||||
|
|
||||||
|
On the other hand, the Jetty WebSocket APIs are more efficient and offer greater and more fine-grained control, and provide these features that are not present in the standard APIs:
|
||||||
|
|
||||||
|
* Suspend/resume to control backpressure.
|
||||||
|
* Remote socket address (IP address and port) information.
|
||||||
|
* WebSocket upgrade handling via Filter or Servlet.
|
||||||
|
* Advanced URI matching with Servlet WebSocket upgrade.
|
||||||
|
* Configuration of the network buffer capacity.
|
||||||
|
|
||||||
|
If your application needs specific features that are not provided by the standard APIs, the Jetty WebSocket APIs may provide such features -- and if they do not, you may ask for these features by submitting an issue to the Jetty Project without waiting for the standard process to approve them.
|
||||||
|
|
||||||
|
[[standard]]
|
||||||
|
== Standard APIs Implementation
|
||||||
|
|
||||||
|
When you write a WebSocket application using the standard `javax.websocket` APIs, your code typically need to depend on just the APIs to compile your application.
|
||||||
|
However, at runtime you need to have an implementation of the standard APIs in your class-path (or module-path).
|
||||||
|
|
||||||
|
The standard `javax.websocket` APIs are provided by the following Maven artifact:
|
||||||
|
|
||||||
|
[,xml,subs=normal]
|
||||||
|
----
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.websocket</groupId>
|
||||||
|
<artifactId>javax.websocket-api</artifactId>
|
||||||
|
<version>1.1</version>
|
||||||
|
</dependency>
|
||||||
|
----
|
||||||
|
|
||||||
|
However, the artifact above lacks a proper JPMS `module-info.class` file, and therefore it is a little more difficult to use if you want to use of JPMS for your application.
|
||||||
|
|
||||||
|
If you want to use JPMS for your application, you can use this Maven artifact instead:
|
||||||
|
|
||||||
|
[,xml,subs=normal]
|
||||||
|
----
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty.toolchain</groupId>
|
||||||
|
<artifactId>jetty-javax-websocket-api</artifactId>
|
||||||
|
<version>1.1.2</version>
|
||||||
|
</dependency>
|
||||||
|
----
|
||||||
|
|
||||||
|
This artifact is nothing more than the `javax.websocket:javax.websocket-api:1.1` artifact repackaged with a proper `module-info.class` file.
|
||||||
|
|
||||||
|
At runtime, you also need an implementation of the standard `javax.websocket` APIs.
|
||||||
|
|
||||||
|
Jetty's implementation of the standard `javax.websocket` APIs is provided by the following Maven artifact (and its transitive dependencies):
|
||||||
|
|
||||||
|
[,xml,subs=normal]
|
||||||
|
----
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty.websocket</groupId>
|
||||||
|
<artifactId>websocket-javax-server</artifactId>
|
||||||
|
<version>{version}</version>
|
||||||
|
</dependency>
|
||||||
|
----
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
The `javax.websocket-api` artifact and the `websocket-javax-server` artifact (and its transitive dependencies) should be present in the server class-path (or module-path), and never in the web application's `/WEB-INF/lib` directory.
|
||||||
|
====
|
||||||
|
|
||||||
|
To configure correctly your WebSocket application based on the standard `javax.websocket` APIs, you need two steps:
|
||||||
|
|
||||||
|
. Make sure that Jetty <<standard-container,sets up>> an instance of `javax.websocket.server.ServerContainer`.
|
||||||
|
. <<standard-endpoints,Configure>> the WebSocket endpoints that implement your application logic, either by annotating their classes with the standard `javax.websocket` annotations, or by using the `ServerContainer` APIs to register them in your code.
|
||||||
|
|
||||||
|
[[standard-container]]
|
||||||
|
=== Setting Up `ServerContainer`
|
||||||
|
|
||||||
|
Jetty sets up a `ServerContainer` instance using `JavaxWebSocketServletContainerInitializer`.
|
||||||
|
|
||||||
|
When you deploy web applications using xref:server/http.adoc#handler-use-webapp-context[`WebAppContext`], then `JavaxWebSocketServletContainerInitializer` is automatically discovered and initialized by Jetty when the web application starts, so that it sets up the `ServerContainer`.
|
||||||
|
In this way, you do not need to write any additional code:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=standardContainerWebAppContext]
|
||||||
|
----
|
||||||
|
|
||||||
|
On the other hand, when you deploy web applications using xref:server/http.adoc#handler-use-servlet-context[`ServletContextHandler`], you have to write the code to ensure that the `JavaxWebSocketServletContainerInitializer` is initialized, so that it sets up the `ServerContainer`:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=standardContainerServletContextHandler]
|
||||||
|
----
|
||||||
|
|
||||||
|
Calling `JavaxWebSocketServletContainerInitializer.configure(\...)` must be done _before_ the `ServletContextHandler` is started, and configures the `javax.websocket` implementation for that web application context.
|
||||||
|
|
||||||
|
[[standard-endpoints]]
|
||||||
|
=== Configuring Endpoints
|
||||||
|
|
||||||
|
Once you have <<standard-container,setup>> the `ServerContainer`, you can configure your WebSocket endpoints.
|
||||||
|
|
||||||
|
The WebSocket endpoints classes may be either annotated with the standard `javax.websocket` annotations, extend the `javax.websocket.Endpoint` abstract class, or implement the `javax.websocket.server.ServerApplicationConfig` interface.
|
||||||
|
|
||||||
|
When you deploy web applications using xref:server/http.adoc#handler-use-webapp-context[`WebAppContext`], then annotated WebSocket endpoint classes are automatically discovered and registered.
|
||||||
|
In this way, you do not need to write any additional code; you just need to ensure that your WebSocket endpoint classes are present in the web application's `/WEB-INF/classes` directory, or in a `*.jar` file in `/WEB-INF/lib`.
|
||||||
|
|
||||||
|
On the other hand, when you deploy web applications using xref:server/http.adoc#handler-use-webapp-context[`WebAppContext`] but you need to perform more advanced configuration of the `ServerContainer` or of the WebSocket endpoints, or when you deploy web applications using xref:server/http.adoc#handler-use-servlet-context[`ServletContextHandler`], you need to access the `ServerContainer` APIs.
|
||||||
|
|
||||||
|
The `ServerContainer` instance is stored as a `ServletContext` attribute, so it can be retrieved when the `ServletContext` is initialized, either from a `ServletContextListener` or from a `HttpServlet`:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=standardEndpointsInitialization]
|
||||||
|
----
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=standardWebSocketInitializerServlet]
|
||||||
|
----
|
||||||
|
|
||||||
|
When you deploy web applications using xref:server/http.adoc#handler-use-servlet-context[`ServletContextHandler`], you can also use this variant to set up the `ServerContainer` and configure the WebSocket endpoints in one step:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=standardContainerAndEndpoints]
|
||||||
|
----
|
||||||
|
|
||||||
|
When the `ServletContextHandler` is started, the `Configurator` lambda (the second parameter passed to `JavaxWebSocketServletContainerInitializer.configure(\...)`) is invoked and allows you to explicitly configure the WebSocket endpoints using the standard APIs provided by `ServerContainer`.
|
||||||
|
|
||||||
|
[[standard-upgrade]]
|
||||||
|
==== Upgrade to WebSocket
|
||||||
|
|
||||||
|
Under the hood, `JavaxWebSocketServletContainerInitializer` installs the `org.eclipse.jetty.websocket.servlet.WebSocketUpgradeFilter`, which is the component that intercepts HTTP requests to upgrade to WebSocket, and performs the upgrade from the HTTP protocol to the WebSocket protocol.
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
The `WebSocketUpgradeFilter` is installed under the filter name corresponding to its class name (that is, the string `"org.eclipse.jetty.websocket.servlet.WebSocketUpgradeFilter"`) and with a filter mapping of `/*`.
|
||||||
|
|
||||||
|
Refer to the <<configure-filter,advanced `WebSocketUpgradeFilter` configuration section>> for more information.
|
||||||
|
====
|
||||||
|
|
||||||
|
With the default configuration, every HTTP request flows first through the `WebSocketUpgradeFilter`.
|
||||||
|
|
||||||
|
If the HTTP request is a valid upgrade to WebSocket, then `WebSocketUpgradeFilter` tries to find a matching WebSocket endpoint for the request URI path; if the match is found, `WebSocketUpgradeFilter` performs the upgrade and does not invoke any other Filter or Servlet.
|
||||||
|
From this point on, the communication happens with the WebSocket protocol, and HTTP components such as Filters and Servlets are not relevant anymore.
|
||||||
|
|
||||||
|
If the HTTP request is not an upgrade to WebSocket, or `WebSocketUpgradeFilter` did not find a matching WebSocket endpoint for the request URI path, then the request is passed to the Filter chain of your web application, and eventually the request arrives to a Servlet to be processed (otherwise a `404 Not Found` response is returned to client).
|
||||||
|
|
||||||
|
[[jetty]]
|
||||||
|
== Jetty APIs Implementation
|
||||||
|
|
||||||
|
When you write a WebSocket application using the Jetty WebSocket APIs, your code typically need to depend on just the Jetty WebSocket APIs to compile your application.
|
||||||
|
However, at runtime you need to have the _implementation_ of the Jetty WebSocket APIs in your class-path (or module-path).
|
||||||
|
|
||||||
|
Jetty's WebSocket APIs are provided by the following Maven artifact:
|
||||||
|
|
||||||
|
[,xml,subs=normal]
|
||||||
|
----
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty.websocket</groupId>
|
||||||
|
<artifactId>websocket-jetty-api</artifactId>
|
||||||
|
<version>{version}</version>
|
||||||
|
</dependency>
|
||||||
|
----
|
||||||
|
|
||||||
|
Jetty's implementation of the Jetty WebSocket APIs is provided by the following Maven artifact (and its transitive dependencies):
|
||||||
|
|
||||||
|
[,xml,subs=normal]
|
||||||
|
----
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty.websocket</groupId>
|
||||||
|
<artifactId>websocket-jetty-server</artifactId>
|
||||||
|
<version>{version}</version>
|
||||||
|
</dependency>
|
||||||
|
----
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
The `websocket-jetty-api` artifact and the `websocket-jetty-server` artifact (and its transitive dependencies) should be present in the server class-path (or module-path), and never in the web application's `/WEB-INF/lib` directory.
|
||||||
|
====
|
||||||
|
|
||||||
|
To configure correctly your WebSocket application based on the Jetty WebSocket APIs, you need two steps:
|
||||||
|
|
||||||
|
. Make sure that Jetty <<jetty-container,sets up>> an instance of `JettyWebSocketServerContainer`.
|
||||||
|
. Use the `JettyWebSocketServerContainer` APIs in your applications to <<jetty-endpoints,register your WebSocket endpoints>> that implement your application logic.
|
||||||
|
|
||||||
|
You can read more about the xref:client/websocket.adoc#architecture[Jetty WebSocket architecture], which is common to both client-side and server-side, to get familiar with the terminology used in the following sections.
|
||||||
|
|
||||||
|
[[jetty-container]]
|
||||||
|
=== Setting up `JettyWebSocketServerContainer`
|
||||||
|
|
||||||
|
Jetty sets up a `JettyWebSocketServerContainer` instance using `JettyWebSocketServletContainerInitializer`.
|
||||||
|
|
||||||
|
When you deploy web applications using xref:server/http.adoc#handler-use-webapp-context[`WebAppContext`], then `JettyWebSocketServletContainerInitializer` is automatically discovered and initialized by Jetty when the web application starts, so that it sets up the `JettyWebSocketServerContainer`.
|
||||||
|
In this way, you do not need to write any additional code:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=standardContainerWebAppContext]
|
||||||
|
----
|
||||||
|
|
||||||
|
On the other hand, when you deploy web applications using xref:server/http.adoc#handler-use-servlet-context[`ServletContextHandler`], you have to write the code to ensure that the `JettyWebSocketServletContainerInitializer` is initialized, so that it sets up the `JettyWebSocketServerContainer`:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=jettyContainerServletContextHandler]
|
||||||
|
----
|
||||||
|
|
||||||
|
Calling `JettyWebSocketServletContainerInitializer.configure(\...)` must be done _before_ the `ServletContextHandler` is started, and configures the Jetty WebSocket implementation for that web application context.
|
||||||
|
|
||||||
|
[[jetty-endpoints]]
|
||||||
|
=== Configuring Endpoints
|
||||||
|
|
||||||
|
Once you have <<jetty-container,setup>> the `JettyWebSocketServerContainer`, you can configure your xref:client/websocket.adoc#endpoints[WebSocket endpoints].
|
||||||
|
|
||||||
|
Differently from the <<standard-endpoints,configuration of standard WebSocket endpoints>>, WebSocket endpoint classes may be annotated with Jetty WebSocket API annotations, or extend the `org.eclipse.jetty.websocket.api.WebSocketListener` interface, but they are not automatically discovered, not even when deploying web applications using xref:server/http.adoc#handler-use-webapp-context[`WebAppContext`].
|
||||||
|
|
||||||
|
[IMPORTANT]
|
||||||
|
====
|
||||||
|
When using the Jetty WebSocket APIs, WebSocket endpoints must always be explicitly configured.
|
||||||
|
====
|
||||||
|
|
||||||
|
There are two ways of configuring WebSocket endpoints when using the Jetty WebSocket APIs:
|
||||||
|
|
||||||
|
* <<jetty-endpoints-container,Using `JettyWebSocketServerContainer`>>, which is very similar to how WebSocket endpoints are configured when using the <<standard-endpoints,standard `javax.websocket` APIs>>, but also provides APIs to perform a direct, programmatic, WebSocket upgrade.
|
||||||
|
* <<jetty-endpoints-servlet,Using `JettyWebSocketServlet`>>, which may configured in `web.xml`, rather than in Java code.
|
||||||
|
|
||||||
|
[[jetty-endpoints-container]]
|
||||||
|
==== Using `JettyWebSocketServerContainer`
|
||||||
|
|
||||||
|
To register WebSocket endpoints using the Jetty WebSocket APIs you need to access the `JettyWebSocketServerContainer` APIs.
|
||||||
|
|
||||||
|
The `JettyWebSocketServerContainer` instance is stored in the `ServletContext`, so it can be retrieved when the `ServletContext` is initialized, either from a `ServletContextListener` or from a `HttpServlet`:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=jettyEndpointsInitialization]
|
||||||
|
----
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=jettyWebSocketInitializerServlet]
|
||||||
|
----
|
||||||
|
|
||||||
|
You can also use this variant to set up the `JettyWebSocketServerContainer` and configure the WebSocket endpoints in one step:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=jettyContainerAndEndpoints]
|
||||||
|
----
|
||||||
|
|
||||||
|
In the call to `JettyWebSocketServerContainer.addMapping(\...)`, you can specify a _path spec_ (the first parameter) that can be configured as specified in <<jetty-pathspec,this section>>.
|
||||||
|
|
||||||
|
When the `ServletContextHandler` is started, the `Configurator` lambda (the second parameter passed to `JettyWebSocketServletContainerInitializer.configure(\...)`) is invoked and allows you to explicitly configure the WebSocket endpoints using the Jetty WebSocket APIs provided by `JettyWebSocketServerContainer`.
|
||||||
|
|
||||||
|
Under the hood, the call to `JettyWebSocketServerContainer.addMapping(\...)` installs the `org.eclipse.jetty.websocket.servlet.WebSocketUpgradeFilter`, which is the component that intercepts HTTP requests to upgrade to WebSocket, described in <<standard-upgrade,this section>>.
|
||||||
|
For more information about the configuration of `WebSocketUpgradeFilter` see also <<configure-filter,this section>>.
|
||||||
|
|
||||||
|
One last alternative to register your WebSocket endpoints is to use a programmatic WebSocket upgrade via `JettyWebSocketServerContainer.upgrade(\...)`, which allows you to use a standard `HttpServlet` subclass (rather than a `JettyWebSocketServlet` as explained in <<jetty-endpoints-servlet,this section>>) to perform a direct WebSocket upgrade when your application logic demands so:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=jettyContainerServletContextHandler]
|
||||||
|
----
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=jettyContainerUpgrade]
|
||||||
|
----
|
||||||
|
|
||||||
|
When using `JettyWebSocketServerContainer.upgrade(\...)`, the `WebSocketUpgradeFilter` is not installed, since the WebSocket upgrade is performed programmatically.
|
||||||
|
|
||||||
|
[[jetty-endpoints-servlet]]
|
||||||
|
==== Using `JettyWebSocketServlet`
|
||||||
|
|
||||||
|
An alternative way to register WebSocket endpoints using the Jetty WebSocket APIs is to use a `JettyWebSocketServlet` subclass (or even many different `JettyWebSocketServlet` subclasses).
|
||||||
|
|
||||||
|
This method has the advantage that it does not install the `WebSocketUpgradeFilter` under the hood, because the WebSocket upgrade is handled directly by your `JettyWebSocketServlet` subclass.
|
||||||
|
This may also have a performance benefit for non-WebSocket HTTP requests (as they will not pass through the `WebSocketUpgradeFilter`).
|
||||||
|
|
||||||
|
Your `JettyWebSocketServlet` subclass may be declared and configured either in code or in `web.xml`.
|
||||||
|
Declaring your `JettyWebSocketServlet` subclass explicitly in code or in `web.xml` also simplifies the declaration and configuration of other web components such as other Servlets and/or Filters (for example, it is easier to configure the `CrossOriginFilter`, see also <<configure-filter,this section>> for more information).
|
||||||
|
|
||||||
|
For example, your `JettyWebSocketServlet` subclass may be declared in code in this way:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=jettyWebSocketServletMain]
|
||||||
|
----
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=jettyWebSocketServlet]
|
||||||
|
----
|
||||||
|
|
||||||
|
Note how in the call to `JettyWebSocketServletContainerInitializer.configure(\...)` the second parameter is `null`, because WebSocket endpoints are not created here, but instead by one (or more) `JettyWebSocketServlet` subclasses.
|
||||||
|
Yet the call is necessary to create other WebSocket implementation components that are necessary also when using `JettyWebSocketServlet` subclasses.
|
||||||
|
|
||||||
|
An HTTP upgrade request to WebSocket that matches your `JettyWebSocketServlet` subclass path mapping (specified above via `ServletContextHandler.addServlet(\...)`) arrives at the Servlet and is inspected to verify whether it is a valid upgrade to WebSocket.
|
||||||
|
|
||||||
|
If the HTTP request is a valid upgrade to WebSocket, `JettyWebSocketServlet` calls `configure(JettyWebSocketServletFactory factory)` that you have overridden in your subclass, so that your application can instantiate and return the WebSocket endpoint.
|
||||||
|
After having obtained the WebSocket endpoint, `JettyWebSocketServlet` performs the WebSocket upgrade.
|
||||||
|
From this point on, the communication happens with the WebSocket protocol, and HTTP components such as Filters and Servlets are not relevant anymore.
|
||||||
|
|
||||||
|
If the HTTP request is not an upgrade to WebSocket, `JettyWebSocketServlet` delegates the processing to the superclass, `javax.servlet.HttpServlet`, which in turn invokes methods such as `doGet(\...)` or `doPost(\...)` depending on the HTTP method.
|
||||||
|
If your `JettyWebSocketServlet` subclass did not override the `doXYZ(\...)` method corresponding to the HTTP request, a `405 Method Not Allowed` response is returned to the client, as per the standard `HttpServlet` class implementation.
|
||||||
|
|
||||||
|
[NOTE]
|
||||||
|
====
|
||||||
|
It is possible to use both `JettyWebSocketServerContainer` and `JettyWebSocketServlet`.
|
||||||
|
|
||||||
|
However, it is typically best to avoid mixing the use of `JettyWebSocketServerContainer` with the use of `JettyWebSocketServlet`, so that all your WebSocket endpoints are initialized by the same code in one place only.
|
||||||
|
====
|
||||||
|
|
||||||
|
Using `JettyWebSocketServerContainer.addMapping(\...)` will install the `WebSocketUpgradeFilter` under the hood, which by default will intercepts all HTTP requests to upgrade to WebSocket.
|
||||||
|
However, as explained in <<standard-upgrade,this section>>, if `WebSocketUpgradeFilter` does not find a matching WebSocket endpoint for the request URI path, then the HTTP request is passed to the Filter chain of your web application and may arrive to your `JettyWebSocketServlet` subclass, where it would be processed and possibly result in a WebSocket upgrade.
|
||||||
|
|
||||||
|
[[jetty-pathspec]]
|
||||||
|
==== Custom PathSpec Mappings
|
||||||
|
|
||||||
|
The `JettyWebSocketServerContainer.addMapping(\...)` API maps a _path spec_ to a `JettyWebSocketCreator` instance (typically a lambda expression).
|
||||||
|
The path spec is matched against the WebSocket upgrade request URI to select the correspondent `JettyWebSocketCreator` to invoke.
|
||||||
|
|
||||||
|
The path spec can have these forms:
|
||||||
|
|
||||||
|
* Servlet syntax, specified with `servlet|<path spec>`, where the `servlet|` prefix can be omitted if the path spec begins with `/` or `+*.+` (for example, `/ws`, `/ws/chat` or `+*.ws+`).
|
||||||
|
* Regex syntax, specified with `regex|<path spec>`, where the `regex|` prefix can be omitted if the path spec begins with `^` (for example, `+^/ws/[0-9]++`).
|
||||||
|
* URI template syntax, specified with `uri-template|<path spec>` (for example `+uri-template|/ws/chat/{room}+`).
|
||||||
|
|
||||||
|
Within the `JettyWebSocketCreator`, it is possible to access the path spec and, for example in case of URI templates, extract additional information in the following way:
|
||||||
|
|
||||||
|
[,java,indent=0]
|
||||||
|
----
|
||||||
|
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/websocket/WebSocketServerDocs.java[tags=uriTemplatePathSpec]
|
||||||
|
----
|
||||||
|
|
||||||
|
[[configure-filter]]
|
||||||
|
== Advanced `WebSocketUpgradeFilter` Configuration
|
||||||
|
|
||||||
|
The `WebSocketUpgradeFilter` that handles the HTTP requests that upgrade to WebSocket is installed in these cases:
|
||||||
|
|
||||||
|
* Either by the `JavaxWebSocketServletContainerInitializer`, as described in <<standard,this section>>.
|
||||||
|
* Or by a call to `JettyWebSocketServerContainer.addMapping(\...)`, as described in <<jetty,this section>>.
|
||||||
|
|
||||||
|
Typically, the `WebSocketUpgradeFilter` is not present in the `web.xml` configuration, and therefore the mechanisms above create a new `WebSocketUpgradeFilter` and install it _before_ any other Filter declared in `web.xml`, under the default name of `"org.eclipse.jetty.websocket.servlet.WebSocketUpgradeFilter"` and with path mapping `/*`.
|
||||||
|
|
||||||
|
However, if the `WebSocketUpgradeFilter` is already present in `web.xml` under the default name, then the ``ServletContainerInitializer``s will use that declared in `web.xml` instead of creating a new one.
|
||||||
|
|
||||||
|
This allows you to customize:
|
||||||
|
|
||||||
|
* The filter order; for example, by configuring the `CrossOriginFilter` (or other filters) for increased security or authentication _before_ the `WebSocketUpgradeFilter`.
|
||||||
|
* The `WebSocketUpgradeFilter` configuration via ``init-param``s, that affects all `Session` instances created by this filter.
|
||||||
|
* The `WebSocketUpgradeFilter` path mapping. Rather than the default mapping of `+/*+`, you can map the `WebSocketUpgradeFilter` to a more specific path such as `+/ws/*+`.
|
||||||
|
* The possibility to have multiple ``WebSocketUpgradeFilter``s, mapped to different paths, each with its own configuration.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
[,xml,subs=verbatim]
|
||||||
|
----
|
||||||
|
<?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_4_0.xsd"
|
||||||
|
version="4.0">
|
||||||
|
|
||||||
|
<display-name>My WebSocket WebApp</display-name>
|
||||||
|
|
||||||
|
<!-- The CrossOriginFilter *must* be the first --> <!--1-->
|
||||||
|
<filter>
|
||||||
|
<filter-name>cross-origin</filter-name>
|
||||||
|
<filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
|
||||||
|
<async-supported>true</async-supported>
|
||||||
|
</filter>
|
||||||
|
<filter-mapping>
|
||||||
|
<filter-name>cross-origin</filter-name>
|
||||||
|
<url-pattern>/*</url-pattern>
|
||||||
|
</filter-mapping>
|
||||||
|
|
||||||
|
<!-- Configure the default WebSocketUpgradeFilter --> <!--2-->
|
||||||
|
<filter>
|
||||||
|
<!-- The filter name must be the default WebSocketUpgradeFilter name -->
|
||||||
|
<filter-name>org.eclipse.jetty.websocket.servlet.WebSocketUpgradeFilter</filter-name> <!--3-->
|
||||||
|
<filter-class>org.eclipse.jetty.websocket.servlet.WebSocketUpgradeFilter</filter-class>
|
||||||
|
<!-- Configure at most 1 MiB text messages -->
|
||||||
|
<init-param> <!--4-->
|
||||||
|
<param-name>maxTextMessageSize</param-name>
|
||||||
|
<param-value>1048576</param-value>
|
||||||
|
</init-param>
|
||||||
|
<async-supported>true</async-supported>
|
||||||
|
</filter>
|
||||||
|
<filter-mapping>
|
||||||
|
<filter-name>org.eclipse.jetty.websocket.servlet.WebSocketUpgradeFilter</filter-name>
|
||||||
|
<!-- Use a more specific path mapping for WebSocket requests -->
|
||||||
|
<url-pattern>/ws/*</url-pattern> <!--5-->
|
||||||
|
</filter-mapping>
|
||||||
|
|
||||||
|
</web-app>
|
||||||
|
----
|
||||||
|
<1> The `CrossOriginFilter` is the first to protect against https://owasp.org/www-community/attacks/csrf[cross-site request forgery attacks].
|
||||||
|
<2> The configuration for the _default_ `WebSocketUpgradeFilter`.
|
||||||
|
<3> Note the use of the _default_ `WebSocketUpgradeFilter` name.
|
||||||
|
<4> Specific configuration for `WebSocketUpgradeFilter` parameters.
|
||||||
|
<5> Use a more specific path mapping for `WebSocketUpgradeFilter`.
|
||||||
|
|
||||||
|
Note that using a more specific path mapping for WebSocket requests is also beneficial to the performance of normal HTTP requests: they do not go through the `WebSocketUpgradeFilter` (as they will not match its path mapping), saving the cost of analyzing them to see whether they are WebSocket upgrade requests or not.
|
|
@ -0,0 +1,25 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Jetty Component Tree Dump
|
||||||
|
|
||||||
|
Jetty components are organized in a xref:arch/bean.adoc[component tree].
|
||||||
|
|
||||||
|
At the root of the component tree there is typically a `ContainerLifeCycle` instance -- typically a `Server` instance on the server and an `HttpClient` instance on the client.
|
||||||
|
|
||||||
|
`ContainerLifeCycle` has built-in _dump_ APIs that can be invoked either directly or xref:arch/jmx.adoc[via JMX].
|
||||||
|
|
||||||
|
// TODO: images from JMC?
|
||||||
|
// TODO: Command line JMX will be in JMX section.
|
||||||
|
|
||||||
|
TIP: You can get more details from a Jetty's `QueuedThreadPool` dump by enabling detailed dumps via `queuedThreadPool.setDetailedDump(true)`.
|
|
@ -0,0 +1,29 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Debugging
|
||||||
|
|
||||||
|
Sometimes, in order to figure out a problem, enabling xref:troubleshooting/logging.adoc[DEBUG logging] is not enough and you really need to debug the code with a debugger.
|
||||||
|
|
||||||
|
Debugging an embedded Jetty application is most easily done from your preferred IDE, so refer to your IDE instruction for how to debug Java applications.
|
||||||
|
|
||||||
|
Remote debugging can be enabled in a Jetty application via command line options:
|
||||||
|
|
||||||
|
[,screen]
|
||||||
|
----
|
||||||
|
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8000 --class-path ...
|
||||||
|
----
|
||||||
|
|
||||||
|
The example above enables remote debugging so that debuggers (for example, your preferred IDE) can connect to port `8000` on the host running the Jetty application to receive debugging events.
|
||||||
|
|
||||||
|
NOTE: More technically, remote debugging exchanges JVM Tools Interface (JVMTI) events and commands via the Java Debug Wire Protocol (JDWP).
|
|
@ -0,0 +1,21 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Troubleshooting Jetty
|
||||||
|
|
||||||
|
TODO: introduction
|
||||||
|
// TODO: explain the process to troubleshoot Jetty:
|
||||||
|
// TODO: #1 enable JMX
|
||||||
|
// TODO: #2 enable GC logs
|
||||||
|
// TODO: #3 take jvm/component dumps
|
||||||
|
// TODO: #4 enable debug logging if you can
|
|
@ -0,0 +1,57 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= Logging
|
||||||
|
|
||||||
|
The Jetty libraries (both client and server) use http://slf4j.org/[SLF4J] as logging APIs.
|
||||||
|
You can therefore plug in any SLF4J logging implementation, and configure the logging category `org.eclipse.jetty` at the desired level.
|
||||||
|
|
||||||
|
When you have problems with Jetty, the first thing that you want to do is to enable DEBUG logging.
|
||||||
|
This is helpful because by reading the DEBUG logs you get a better understanding of what is going on in the system (and that alone may give you the answers you need to fix the problem), and because Jetty developers will probably need the DEBUG logs to help you.
|
||||||
|
|
||||||
|
== Jetty SLF4J Binding
|
||||||
|
|
||||||
|
The Jetty artifact `jetty-slf4j-impl` is a SLF4J binding, that is the Jetty implementation of the SLF4J APIs, and provides a number of easy-to-use features to configure logging.
|
||||||
|
|
||||||
|
The Jetty SLF4J binding only provides an appender that writes to `System.err`.
|
||||||
|
For more advanced configurations (for example, logging to a file), use http://logback.qos.ch[LogBack], or https://logging.apache.org/log4j/2.x/[Log4j2], or your preferred SLF4J binding.
|
||||||
|
|
||||||
|
CAUTION: Only one binding can be present in the class-path or module-path. If you use the LogBack SLF4J binding or the Log4j2 SLF4J binding, remember to remove the Jetty SLF4J binding.
|
||||||
|
|
||||||
|
The Jetty SLF4J binding reads a file in the class-path (or module-path) called `jetty-logging.properties` that can be configured with the logging levels for various logger categories:
|
||||||
|
|
||||||
|
.jetty-logging.properties
|
||||||
|
[,screen]
|
||||||
|
----
|
||||||
|
# By default, log at INFO level all Jetty classes.
|
||||||
|
org.eclipse.jetty.LEVEL=INFO
|
||||||
|
|
||||||
|
# However, the Jetty client classes are logged at DEBUG level.
|
||||||
|
org.eclipse.jetty.client.LEVEL=DEBUG
|
||||||
|
----
|
||||||
|
|
||||||
|
Similarly to how you configure the `jetty-logging.properties` file, you can set the system property `org.eclipse.jetty[.<package_names>].LEVEL=DEBUG` to quickly change the logging level to DEBUG without editing any file.
|
||||||
|
The system property can be set on the command line, or in your IDE when you run your tests or your Jetty-based application and will override the `jetty-logging.properties` file configuration.
|
||||||
|
For example to enable DEBUG logging for all the Jetty classes (_very_ verbose):
|
||||||
|
|
||||||
|
[,screen]
|
||||||
|
----
|
||||||
|
java -Dorg.eclipse.jetty.LEVEL=DEBUG --class-path ...
|
||||||
|
----
|
||||||
|
|
||||||
|
If you want to enable DEBUG logging but only for the HTTP/2 classes:
|
||||||
|
|
||||||
|
[,screen]
|
||||||
|
----
|
||||||
|
java -Dorg.eclipse.jetty.http2.LEVEL=DEBUG --class-path ...
|
||||||
|
----
|
|
@ -0,0 +1,16 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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
|
||||||
|
// ========================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
= JVM Thread Dump
|
||||||
|
|
||||||
|
TODO
|
|
@ -0,0 +1,163 @@
|
||||||
|
<?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.documentation</groupId>
|
||||||
|
<artifactId>documentation-parent</artifactId>
|
||||||
|
<version>10.0.21-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<artifactId>jetty</artifactId>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
<name>Documentation :: Content Root</name>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<!-- build cache must be skipped for antora goal to work and for run.jetty.classpath value to be computed properly -->
|
||||||
|
<maven.build.cache.skipCache>true</maven.build.cache.skipCache>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.antora</groupId>
|
||||||
|
<artifactId>antora-maven-plugin</artifactId>
|
||||||
|
<version>1.0.0-alpha.4</version>
|
||||||
|
<extensions>true</extensions>
|
||||||
|
<configuration>
|
||||||
|
<options>
|
||||||
|
<option>attribute[] jetty-home=${project.build.directory}/jetty-home-${project.version}</option>
|
||||||
|
</options>
|
||||||
|
<playbookProvider>
|
||||||
|
<repository>jetty/jetty.website</repository>
|
||||||
|
<path>main:lib/playbook-templates/per-branch-antora-playbook.yml</path>
|
||||||
|
</playbookProvider>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<profiles>
|
||||||
|
<profile>
|
||||||
|
<id>collector</id>
|
||||||
|
<activation>
|
||||||
|
<property>
|
||||||
|
<name>collector</name>
|
||||||
|
</property>
|
||||||
|
</activation>
|
||||||
|
<properties>
|
||||||
|
<jetty.home>${project.build.directory}/jetty-home-${project.version}</jetty.home>
|
||||||
|
<jetty.home.output.directory>${project.build.directory}</jetty.home.output.directory>
|
||||||
|
<maven.test.skip>true</maven.test.skip>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-home</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
<type>zip</type>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty.tests</groupId>
|
||||||
|
<artifactId>jetty-home-tester</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty.toolchain</groupId>
|
||||||
|
<artifactId>jetty-test-helper</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-dependency-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>build-jetty-home</id>
|
||||||
|
<goals>
|
||||||
|
<goal>unpack</goal>
|
||||||
|
</goals>
|
||||||
|
<phase>generate-resources</phase>
|
||||||
|
<configuration>
|
||||||
|
<artifactItems>
|
||||||
|
<artifactItem>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-home</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
<type>zip</type>
|
||||||
|
</artifactItem>
|
||||||
|
</artifactItems>
|
||||||
|
<outputDirectory>${jetty.home.output.directory}</outputDirectory>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
<execution>
|
||||||
|
<id>capture-run-jetty-classpath</id>
|
||||||
|
<goals>
|
||||||
|
<goal>build-classpath</goal>
|
||||||
|
</goals>
|
||||||
|
<phase>generate-resources</phase>
|
||||||
|
<configuration>
|
||||||
|
<outputProperty>run.jetty.classpath</outputProperty>
|
||||||
|
<includeScope>runtime</includeScope>
|
||||||
|
<!-- when mvn is run from project root, this jar gets resolved to build dir instead of local Maven repo -->
|
||||||
|
<excludeArtifactIds>jetty-home-tester</excludeArtifactIds>
|
||||||
|
<silent>true</silent>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-resources-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>pass-jetty-home</id>
|
||||||
|
<goals>
|
||||||
|
<goal>copy-resources</goal>
|
||||||
|
</goals>
|
||||||
|
<phase>process-resources</phase>
|
||||||
|
<configuration>
|
||||||
|
<outputDirectory>${project.build.directory}/collector</outputDirectory>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>${basedir}</directory>
|
||||||
|
<includes>
|
||||||
|
<include>antora.yml</include>
|
||||||
|
</includes>
|
||||||
|
<filtering>true</filtering>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</profile>
|
||||||
|
<profile>
|
||||||
|
<id>collector-with-antora-cache-dir</id>
|
||||||
|
<activation>
|
||||||
|
<property>
|
||||||
|
<name>env.ANTORA_CACHE_DIR</name>
|
||||||
|
</property>
|
||||||
|
</activation>
|
||||||
|
<properties>
|
||||||
|
<jetty.home>${env.ANTORA_CACHE_DIR}/jetty/jetty-home-${project.version}</jetty.home>
|
||||||
|
<jetty.home.output.directory>${env.ANTORA_CACHE_DIR}/jetty</jetty.home.output.directory>
|
||||||
|
</properties>
|
||||||
|
</profile>
|
||||||
|
<profile>
|
||||||
|
<id>examples</id>
|
||||||
|
<activation>
|
||||||
|
<property>
|
||||||
|
<name>!collector</name>
|
||||||
|
</property>
|
||||||
|
</activation>
|
||||||
|
<modules>
|
||||||
|
<module>modules/code/examples</module>
|
||||||
|
</modules>
|
||||||
|
</profile>
|
||||||
|
</profiles>
|
||||||
|
</project>
|
|
@ -13,7 +13,6 @@
|
||||||
<name>Jetty :: Documentation :: Parent</name>
|
<name>Jetty :: Documentation :: Parent</name>
|
||||||
|
|
||||||
<modules>
|
<modules>
|
||||||
<module>jetty-asciidoctor-extensions</module>
|
<module>jetty</module>
|
||||||
<module>jetty-documentation</module>
|
|
||||||
</modules>
|
</modules>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -70,13 +70,6 @@
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.codehaus.mojo</groupId>
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
<artifactId>buildnumber-maven-plugin</artifactId>
|
<artifactId>buildnumber-maven-plugin</artifactId>
|
||||||
<dependencies>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.maven.scm</groupId>
|
|
||||||
<artifactId>maven-scm-provider-jgit</artifactId>
|
|
||||||
<version>2.0.1</version>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<id>create-buildnumber</id>
|
<id>create-buildnumber</id>
|
||||||
|
@ -88,7 +81,7 @@
|
||||||
<doUpdate>false</doUpdate>
|
<doUpdate>false</doUpdate>
|
||||||
<revisionOnScmFailure>${nonCanonicalRevision}</revisionOnScmFailure>
|
<revisionOnScmFailure>${nonCanonicalRevision}</revisionOnScmFailure>
|
||||||
<providerImplementations>
|
<providerImplementations>
|
||||||
<git>jgit</git>
|
<git>git</git>
|
||||||
</providerImplementations>
|
</providerImplementations>
|
||||||
<scmDirectory>../</scmDirectory>
|
<scmDirectory>../</scmDirectory>
|
||||||
</configuration>
|
</configuration>
|
||||||
|
|
|
@ -0,0 +1,262 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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 org.eclipse.jetty.tests.hometester;
|
||||||
|
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>A Java application that captures the output of starting the Jetty server with
|
||||||
|
* a given set of parameters, filters the output, and prints it back to STDOUT.</p>
|
||||||
|
* <p>This application is used in the Jetty documentation to show the output from starting
|
||||||
|
* the Jetty server.</p>
|
||||||
|
* <p>Example usage:</p>
|
||||||
|
* <pre>
|
||||||
|
* java -cp $RUN_JETTY_CLASSPATH org.eclipse.jetty.tests.hometester.RunJetty --jetty-home $JETTY_HOME
|
||||||
|
* </pre>
|
||||||
|
* <p>Available options are:</p>
|
||||||
|
* <dl>
|
||||||
|
* <dt>jetty-home</dt>
|
||||||
|
* <dd>Required, the path to where Jetty is installed.</dd>
|
||||||
|
* <dt>jetty-base</dt>
|
||||||
|
* <dd>Optional, the path to the Jetty base directory where customizations are located.
|
||||||
|
* If not provided, will be automatically created.</dd>
|
||||||
|
* <dt>setup-args</dt>
|
||||||
|
* <dd>Optional, specifies the arguments to use in a Jetty server <em>setup</em> run.
|
||||||
|
* If missing, no Jetty server <em>setup</em> run will be executed.
|
||||||
|
* The output produced by this run is ignored.</dd>
|
||||||
|
* <dt>args</dt>
|
||||||
|
* <dd>Optional, specifies the arguments to use in a Jetty server run.
|
||||||
|
* If missing, a Jetty server run will be executed with no arguments.
|
||||||
|
* The output produced by this run is included in the Asciidoc document.</dd>
|
||||||
|
* <dt>replace</dt>
|
||||||
|
* <dd>Optional, specifies a comma-separated pair where the first element is a regular
|
||||||
|
* expression and the second is the string replacement.</dd>
|
||||||
|
* <dt>delete</dt>
|
||||||
|
* <dd>Optional, specifies a regular expression that when matched deletes the line</dd>
|
||||||
|
* <dt>highlight</dt>
|
||||||
|
* <dd>Optional, specifies a regular expression that matches lines that should be highlighted.
|
||||||
|
* If missing, no line will be highlighted.
|
||||||
|
* If the regular expression contains capturing groups, only the text matching
|
||||||
|
* the groups is highlighted, not the whole line.
|
||||||
|
* </dd>
|
||||||
|
* <dt>callouts</dt>
|
||||||
|
* <dd>Optional, specifies a comma-separated pair where the first element is a callout
|
||||||
|
* pattern, and the second element is a comma-separated list of regular expressions,
|
||||||
|
* each matching a single line, that get a callout added at the end of the line.</dd>
|
||||||
|
* </dl>
|
||||||
|
*
|
||||||
|
* @see JettyHomeTester
|
||||||
|
*/
|
||||||
|
public class RunJetty
|
||||||
|
{
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
Map<String, String> config = new HashMap<String, String>();
|
||||||
|
String name = null;
|
||||||
|
for (String arg : args)
|
||||||
|
{
|
||||||
|
if (arg.startsWith("--") && arg.contains("="))
|
||||||
|
{
|
||||||
|
String[] nameAndValue = arg.split("=", 2);
|
||||||
|
config.put(nameAndValue[0].substring(2), nameAndValue[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!config.containsKey("jetty-home"))
|
||||||
|
{
|
||||||
|
throw new RuntimeException("--jetty-home argument is required");
|
||||||
|
}
|
||||||
|
new RunJetty().run(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
public RunJetty()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run(Map<String, String> config)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Path jettyHome = Path.of(config.get("jetty-home"));
|
||||||
|
Path jettyBase = Path.of(config.getOrDefault("jetty-base", jettyHome.toString() + "-base"));
|
||||||
|
|
||||||
|
JettyHomeTester jetty = JettyHomeTester.Builder.newInstance()
|
||||||
|
.jettyHome(jettyHome)
|
||||||
|
.jettyBase(jettyBase)
|
||||||
|
.mavenLocalRepository(config.get("maven-local-repo"))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
String setupArgs = config.get("setup-args");
|
||||||
|
if (setupArgs != null)
|
||||||
|
{
|
||||||
|
try (JettyHomeTester.Run setupRun = jetty.start(setupArgs.split(" ")))
|
||||||
|
{
|
||||||
|
setupRun.awaitFor(15, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String args = config.get("args");
|
||||||
|
args = args == null ? "" : args + " ";
|
||||||
|
args += jettyHome.resolve("etc/jetty-halt.xml");
|
||||||
|
try (JettyHomeTester.Run run = jetty.start(args.split(" ")))
|
||||||
|
{
|
||||||
|
run.awaitFor(15, TimeUnit.SECONDS);
|
||||||
|
System.out.println(captureOutput(config, run));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Throwable x)
|
||||||
|
{
|
||||||
|
throw new RuntimeException(x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String captureOutput(Map<String, String> config, JettyHomeTester.Run run)
|
||||||
|
{
|
||||||
|
final String actualVersion = (String)config.get("jetty-version");
|
||||||
|
final String stableVersion = actualVersion == null ? null : actualVersion.replace("-SNAPSHOT", "");
|
||||||
|
Stream<String> lines = run.getLogs().stream()
|
||||||
|
.map(line -> redact(line, System.getProperty("java.home"), "/path/to/java.home"))
|
||||||
|
.map(line -> redact(line, run.getConfig().getMavenLocalRepository(), "/path/to/maven.repository"))
|
||||||
|
.map(line -> redact(line, run.getConfig().getJettyHome().toString(), "/path/to/jetty.home"))
|
||||||
|
.map(line -> redact(line, run.getConfig().getJettyBase().toString(), "/path/to/jetty.base"))
|
||||||
|
.map(line -> redact(line, actualVersion, stableVersion))
|
||||||
|
.map(line -> regexpRedact(line, "(^| )[^ ]+/etc/jetty-halt\\.xml", ""));
|
||||||
|
lines = replace(lines, config.get("replace"));
|
||||||
|
lines = delete(lines, config.get("delete"));
|
||||||
|
lines = denoteLineStart(lines);
|
||||||
|
lines = highlight(lines, config.get("highlight"));
|
||||||
|
lines = callouts(lines, config.get("callouts"));
|
||||||
|
return lines.collect(Collectors.joining(System.lineSeparator()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String redact(String line, String target, String replacement)
|
||||||
|
{
|
||||||
|
if (target != null && replacement != null)
|
||||||
|
return line.replace(target, replacement);
|
||||||
|
return line;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String regexpRedact(String line, String regexp, String replacement)
|
||||||
|
{
|
||||||
|
if (regexp != null && replacement != null)
|
||||||
|
return line.replaceAll(regexp, replacement);
|
||||||
|
return line;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Stream<String> replace(Stream<String> lines, String replace)
|
||||||
|
{
|
||||||
|
if (replace == null)
|
||||||
|
return lines;
|
||||||
|
|
||||||
|
// Format is: (regexp,replacement).
|
||||||
|
String[] parts = replace.split(",");
|
||||||
|
String regExp = parts[0];
|
||||||
|
String replacement = parts[1].replace("\\n", "\n");
|
||||||
|
|
||||||
|
return lines.flatMap(line -> Stream.of(line.replaceAll(regExp, replacement).split("\n")));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Stream<String> delete(Stream<String> lines, String delete)
|
||||||
|
{
|
||||||
|
if (delete == null)
|
||||||
|
return lines;
|
||||||
|
Pattern regExp = Pattern.compile(delete);
|
||||||
|
return lines.filter(line -> !regExp.matcher(line).find());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Stream<String> denoteLineStart(Stream<String> lines)
|
||||||
|
{
|
||||||
|
// Matches lines that start with a date such as "2020-01-01 00:00:00.000:".
|
||||||
|
Pattern regExp = Pattern.compile("(^\\d{4}[^:]+:[^:]+:[^:]+:)");
|
||||||
|
return lines.map(line ->
|
||||||
|
{
|
||||||
|
Matcher matcher = regExp.matcher(line);
|
||||||
|
if (!matcher.find())
|
||||||
|
return line;
|
||||||
|
return "**" + matcher.group(1) + "**" + line.substring(matcher.end(1));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private Stream<String> highlight(Stream<String> lines, String highlight)
|
||||||
|
{
|
||||||
|
if (highlight == null)
|
||||||
|
return lines;
|
||||||
|
|
||||||
|
Pattern regExp = Pattern.compile(highlight);
|
||||||
|
return lines.map(line ->
|
||||||
|
{
|
||||||
|
Matcher matcher = regExp.matcher(line);
|
||||||
|
if (!matcher.find())
|
||||||
|
return line;
|
||||||
|
|
||||||
|
int groupCount = matcher.groupCount();
|
||||||
|
|
||||||
|
// No capturing groups, highlight the whole line.
|
||||||
|
if (groupCount == 0)
|
||||||
|
return "##" + line + "##";
|
||||||
|
|
||||||
|
// Highlight the capturing groups.
|
||||||
|
StringBuilder result = new StringBuilder(line.length() + 4 * groupCount);
|
||||||
|
int start = 0;
|
||||||
|
for (int groupIndex = 1; groupIndex <= groupCount; ++groupIndex)
|
||||||
|
{
|
||||||
|
int matchBegin = matcher.start(groupIndex);
|
||||||
|
result.append(line, start, matchBegin);
|
||||||
|
result.append("##");
|
||||||
|
int matchEnd = matcher.end(groupIndex);
|
||||||
|
result.append(line, matchBegin, matchEnd);
|
||||||
|
result.append("##");
|
||||||
|
start = matchEnd;
|
||||||
|
}
|
||||||
|
result.append(line, start, line.length());
|
||||||
|
return result.toString();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private Stream<String> callouts(Stream<String> lines, String callouts)
|
||||||
|
{
|
||||||
|
if (callouts == null)
|
||||||
|
return lines;
|
||||||
|
|
||||||
|
// Format is (prefix$Nsuffix,regExp...).
|
||||||
|
String[] parts = callouts.split(",");
|
||||||
|
String calloutPattern = parts[0];
|
||||||
|
List<Pattern> regExps = Stream.of(parts)
|
||||||
|
.skip(1)
|
||||||
|
.map(Pattern::compile)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
AtomicInteger index = new AtomicInteger();
|
||||||
|
|
||||||
|
return lines.map(line ->
|
||||||
|
{
|
||||||
|
int regExpIndex = index.get();
|
||||||
|
if (regExpIndex == regExps.size())
|
||||||
|
return line;
|
||||||
|
Pattern regExp = regExps.get(regExpIndex);
|
||||||
|
if (!regExp.matcher(line).find())
|
||||||
|
return line;
|
||||||
|
int calloutIndex = index.incrementAndGet();
|
||||||
|
return line + calloutPattern.replace("$N", String.valueOf(calloutIndex));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue