diff --git a/.github/renovate.json b/.github/renovate.json index 335bdae40ada..aab994ad8187 100644 --- a/.github/renovate.json +++ b/.github/renovate.json @@ -47,16 +47,7 @@ ], "enabled": false, "matchPackageNames": [ - "jakarta.servlet:jakarta.servlet-api" - ] - }, - { - "description": "Contains incompatible API changes and needs compatibility work", - "matchManagers": [ - "maven" - ], - "enabled": false, - "matchPackageNames": [ + "jakarta.servlet:jakarta.servlet-api", "jakarta.servlet.jsp.jstl:jakarta.servlet.jsp.jstl-api" ] }, diff --git a/.idea/encodings.xml b/.idea/encodings.xml index de5572116383..ca018ebc3ab9 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -32,6 +32,9 @@ + + + diff --git a/bom/pom.xml b/bom/pom.xml index 80dafa99ec43..cf265f1dada9 100644 --- a/bom/pom.xml +++ b/bom/pom.xml @@ -40,7 +40,7 @@ THE SOFTWARE. 2.0.0-M2 2.0.16 - 1892.v73465f3d074d + 1894.v82b_2fb_35519d 2.4.21 diff --git a/pom.xml b/pom.xml index 152051851abc..6783b6e2d09e 100644 --- a/pom.xml +++ b/pom.xml @@ -53,6 +53,7 @@ THE SOFTWARE. bom websocket/spi websocket/jetty10 + websocket/jetty12-ee8 core war test @@ -97,7 +98,7 @@ THE SOFTWARE. 1.29 false - 6.21 + 7.0 - manual + 0 ${host} ${port} @@ -645,7 +651,9 @@ THE SOFTWARE. default - ${basedir}/src/realm.properties + + ${basedir}/src/realm.properties + diff --git a/websocket/jetty10/pom.xml b/websocket/jetty10/pom.xml index c91e1583c039..5147b02b81d6 100644 --- a/websocket/jetty10/pom.xml +++ b/websocket/jetty10/pom.xml @@ -52,7 +52,7 @@ THE SOFTWARE. org.jenkins-ci winstone - ${winstone.version} + 6.21 true diff --git a/websocket/jetty12-ee8/pom.xml b/websocket/jetty12-ee8/pom.xml new file mode 100644 index 000000000000..9a7a9ca94545 --- /dev/null +++ b/websocket/jetty12-ee8/pom.xml @@ -0,0 +1,85 @@ + + + + 4.0.0 + + + org.jenkins-ci.main + jenkins-parent + ${revision}${changelist} + ../.. + + + websocket-jetty12-ee8 + Jetty 12 (EE 8) implementation for WebSocket + An implementation of the WebSocket handler that works with Jetty 12 (EE 8). + + + + + org.jenkins-ci.main + jenkins-bom + ${project.version} + pom + import + + + + + + + org.jenkins-ci + winstone + ${winstone.version} + true + + + org.jenkins-ci.main + websocket-spi + ${project.version} + + + org.kohsuke + access-modifier-annotation + + + org.kohsuke.metainf-services + metainf-services + true + + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + + true + + + + + diff --git a/websocket/jetty12-ee8/src/main/java/jenkins/websocket/Jetty12EE8Provider.java b/websocket/jetty12-ee8/src/main/java/jenkins/websocket/Jetty12EE8Provider.java new file mode 100644 index 000000000000..cc006b180f26 --- /dev/null +++ b/websocket/jetty12-ee8/src/main/java/jenkins/websocket/Jetty12EE8Provider.java @@ -0,0 +1,178 @@ +/* + * The MIT License + * + * Copyright 2022, 2024 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 Jetty12EE8Provider implements Provider { + + /** + * Number of seconds a WebsocketConnection may stay idle until it expires. + * Zero to disable. + * This value must be higher than the jenkins.websocket.pingInterval. + * Per Jetty 12 documentation + * 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 = Jetty12EE8Provider.class.getName() + ".listener"; + + private boolean initialized = false; + + public Jetty12EE8Provider() { + 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 10+ has no obvious equivalent to WebSocketServerFactory.isUpgradeRequest; RFC6455Negotiation? + 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(Jetty12EE8Provider::createWebSocket, req, rsp)) { + rsp.sendError(HttpServletResponse.SC_BAD_REQUEST, "did not manage to upgrade"); + return null; + } + return new Handler() { + @Override + public Future sendBinary(ByteBuffer data) throws IOException { + CompletableFuture 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 sendText(String text) throws IOException { + CompletableFuture f = new CompletableFuture<>(); + session().getRemote().sendString(text, new WriteCallbackImpl(f)); + return f; + } + + @Override + public Future sendPing(ByteBuffer applicationData) throws IOException { + CompletableFuture 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 f; + + WriteCallbackImpl(CompletableFuture 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); + } + }; + } +}