Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to winstone with Jetty 12 #8380

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ properties([

def axes = [
platforms: ['linux', 'windows'],
jdks: [11, 17, 21],
jdks: [17, 21],
]

stage('Record build') {
Expand Down
4 changes: 3 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ THE SOFTWARE.
<module>bom</module>
<module>websocket/spi</module>
<module>websocket/jetty10</module>
<module>websocket/jetty12-ee8</module>
<module>core</module>
<module>war</module>
<module>test</module>
Expand Down Expand Up @@ -99,7 +100,8 @@ THE SOFTWARE.
<bridge-method-injector.version>1.29</bridge-method-injector.version>
<spotless.check.skip>false</spotless.check.skip>
<!-- Make sure to keep the jetty-maven-plugin version in war/pom.xml in sync with the Jetty release in Winstone: -->
<winstone.version>6.19</winstone.version>
<winstone.version>7.0-rc912.f5b_c197b_06e7</winstone.version>
<maven.compiler.release>17</maven.compiler.release>
</properties>

<!--
Expand Down
15 changes: 11 additions & 4 deletions war/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ THE SOFTWARE.
<artifactId>websocket-jetty10</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>websocket-jetty12-ee8</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<!--
We bundle slf4j binding since we got some components (sshd for example)
Expand Down Expand Up @@ -576,9 +581,9 @@ THE SOFTWARE.
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>10.0.20</version>
<groupId>org.eclipse.jetty.ee8</groupId>
<artifactId>jetty-ee8-maven-plugin</artifactId>
<version>12.0.9</version>
<configuration>
<!--
Reload webapp when you hit ENTER. (See JETTY-282 for more)
Expand All @@ -591,7 +596,9 @@ THE SOFTWARE.
<loginServices>
<loginService implementation="org.eclipse.jetty.security.HashLoginService">
<name>default</name>
<config>${basedir}/src/realm.properties</config>
<config implementation="org.eclipse.jetty.maven.MavenResource">
<resourceAsString>${basedir}/src/realm.properties</resourceAsString>
</config>
</loginService>
</loginServices>
<systemProperties>
Expand Down
2 changes: 1 addition & 1 deletion websocket/jetty10/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ THE SOFTWARE.
<dependency>
<groupId>org.jenkins-ci</groupId>
<artifactId>winstone</artifactId>
<version>${winstone.version}</version>
<version>6.18</version>
<optional>true</optional>
</dependency>
<dependency>
Expand Down
85 changes: 85 additions & 0 deletions websocket/jetty12-ee8/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License

Copyright (c) 2019, CloudBees, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>jenkins-parent</artifactId>
<version>${revision}${changelist}</version>
<relativePath>../..</relativePath>
</parent>

<artifactId>websocket-jetty12-ee8</artifactId>
<name>Jetty 12 (EE 8) implementation for WebSocket</name>
<description>An implementation of the WebSocket handler that works with Jetty 12 (EE 8).</description>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>jenkins-bom</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.jenkins-ci</groupId>
<artifactId>winstone</artifactId>
<version>${winstone.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>websocket-spi</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.kohsuke</groupId>
<artifactId>access-modifier-annotation</artifactId>
</dependency>
<dependency>
<groupId>org.kohsuke.metainf-services</groupId>
<artifactId>metainf-services</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/*
* The MIT License
*
* Copyright 2022 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package jenkins.websocket;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.ee8.websocket.api.Session;
import org.eclipse.jetty.ee8.websocket.api.WebSocketListener;
import org.eclipse.jetty.ee8.websocket.api.WriteCallback;
import org.eclipse.jetty.ee8.websocket.server.JettyServerUpgradeRequest;
import org.eclipse.jetty.ee8.websocket.server.JettyServerUpgradeResponse;
import org.eclipse.jetty.ee8.websocket.server.JettyWebSocketServerContainer;
import org.kohsuke.MetaInfServices;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

@Restricted(NoExternalUse.class)
@MetaInfServices(Provider.class)
public class Jetty12E88Provider implements Provider {
olamy marked this conversation as resolved.
Show resolved Hide resolved

/**
* Number of seconds a WebsocketConnection may stay idle until it expires.
* Zero to disable.
* This value must be higher than the <code>jenkins.websocket.pingInterval</code>.
* Per <a href=https://www.eclipse.org/jetty/documentation/jetty-12/programming-guide/index.html#pg-websocket-session-ping>Jetty 12 documentation</a>
olamy marked this conversation as resolved.
Show resolved Hide resolved
* a ping mechanism should keep the websocket active. Therefore, the idle timeout must be higher than the ping
* interval to avoid timeout issues.
*/
private static long IDLE_TIMEOUT_SECONDS = Long.getLong("jenkins.websocket.idleTimeout", 60L);

private static final String ATTR_LISTENER = Jetty12E88Provider.class.getName() + ".listener";

private boolean initialized = false;

public Jetty12E88Provider() {
JettyWebSocketServerContainer.class.hashCode();
}

private void init(HttpServletRequest req) {
if (!initialized) {
JettyWebSocketServerContainer.getContainer(req.getServletContext()).setIdleTimeout(Duration.ofSeconds(IDLE_TIMEOUT_SECONDS));
initialized = true;
}
}

@Override
public Handler handle(HttpServletRequest req, HttpServletResponse rsp, Listener listener) throws Exception {
init(req);
req.setAttribute(ATTR_LISTENER, listener);
// TODO Jetty 12 has no obvious equivalent to WebSocketServerFactory.isUpgradeRequest; RFC6455Negotiation?
olamy marked this conversation as resolved.
Show resolved Hide resolved
if (!"websocket".equalsIgnoreCase(req.getHeader("Upgrade"))) {
rsp.sendError(HttpServletResponse.SC_BAD_REQUEST, "only WS connections accepted here");
return null;
}
if (!JettyWebSocketServerContainer.getContainer(req.getServletContext()).upgrade(Jetty12E88Provider::createWebSocket, req, rsp)) {
rsp.sendError(HttpServletResponse.SC_BAD_REQUEST, "did not manage to upgrade");
return null;
}
return new Handler() {
@Override
public Future<Void> sendBinary(ByteBuffer data) throws IOException {
CompletableFuture<Void> f = new CompletableFuture<>();
session().getRemote().sendBytes(data, new WriteCallbackImpl(f));
return f;
}

@Override
public void sendBinary(ByteBuffer partialByte, boolean isLast) throws IOException {
session().getRemote().sendPartialBytes(partialByte, isLast);
}

@Override
public Future<Void> sendText(String text) throws IOException {
CompletableFuture<Void> f = new CompletableFuture<>();
session().getRemote().sendString(text, new WriteCallbackImpl(f));
return f;
}

@Override
public Future<Void> sendPing(ByteBuffer applicationData) throws IOException {
CompletableFuture<Void> f = new CompletableFuture<>();
session().getRemote().sendPing(applicationData, new WriteCallbackImpl(f));
return f;
}

@Override
public void close() throws IOException {
session().close();
}

private Session session() {
Session session = (Session) listener.getProviderSession();
if (session == null) {
throw new IllegalStateException("missing session");
}
return session;
}
};
}

private static final class WriteCallbackImpl implements WriteCallback {
private final CompletableFuture<Void> f;

WriteCallbackImpl(CompletableFuture<Void> f) {
this.f = f;
}

@Override
public void writeSuccess() {
f.complete(null);
}

@Override
public void writeFailed(Throwable x) {
f.completeExceptionally(x);
}
}

private static Object createWebSocket(JettyServerUpgradeRequest req, JettyServerUpgradeResponse resp) {
Listener listener = (Listener) req.getHttpServletRequest().getAttribute(ATTR_LISTENER);
if (listener == null) {
throw new IllegalStateException("missing listener attribute");
}
return new WebSocketListener() {
@Override
public void onWebSocketBinary(byte[] payload, int offset, int length) {
listener.onWebSocketBinary(payload, offset, length);
}

@Override
public void onWebSocketText(String message) {
listener.onWebSocketText(message);
}

@Override
public void onWebSocketClose(int statusCode, String reason) {
listener.onWebSocketClose(statusCode, reason);
}

@Override
public void onWebSocketConnect(Session session) {
listener.onWebSocketConnect(session);
}

@Override
public void onWebSocketError(Throwable cause) {
listener.onWebSocketError(cause);
}
};
}

}
Loading