-
-
Notifications
You must be signed in to change notification settings - Fork 29
[#112] Give each Jetty connection its own endpoint and close cached principals #115
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
Merged
vharseko
merged 3 commits into
OpenIdentityPlatform:master
from
vharseko:issue-112-jetty-principal-lifecycle
Jul 21, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
229 changes: 229 additions & 0 deletions
229
...er-jetty/src/main/java/org/forgerock/openicf/framework/server/jetty/OpenICFWebSocket.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| /* | ||
| * The contents of this file are subject to the terms of the Common Development and | ||
| * Distribution License (the License). You may not use this file except in compliance with the | ||
| * License. | ||
| * | ||
| * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the | ||
| * specific language governing permission and limitations under the License. | ||
| * | ||
| * When distributing Covered Software, include this CDDL Header Notice in each file and include | ||
| * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL | ||
| * Header, with the fields enclosed by brackets [] replaced by your own identifying | ||
| * information: "Portions copyright [year] [name of copyright owner]". | ||
| * | ||
| * Copyright 2015-2016 ForgeRock AS. | ||
| * Portions Copyrighted 2026 3A Systems, LLC | ||
| */ | ||
| package org.forgerock.openicf.framework.server.jetty; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.ByteBuffer; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.Future; | ||
| import java.util.concurrent.RejectedExecutionException; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
|
|
||
| import org.eclipse.jetty.websocket.api.BatchMode; | ||
| import org.eclipse.jetty.websocket.api.Frame; | ||
| import org.eclipse.jetty.websocket.api.Session; | ||
| import org.eclipse.jetty.websocket.api.StatusCode; | ||
| import org.eclipse.jetty.websocket.api.WebSocketFrameListener; | ||
| import org.eclipse.jetty.websocket.api.WebSocketListener; | ||
| import org.eclipse.jetty.websocket.api.WebSocketPingPongListener; | ||
| import org.forgerock.openicf.common.protobuf.RPCMessages; | ||
| import org.forgerock.openicf.framework.remote.ConnectionPrincipal; | ||
| import org.forgerock.openicf.framework.remote.rpc.RemoteOperationContext; | ||
| import org.forgerock.openicf.framework.remote.rpc.WebSocketConnectionHolder; | ||
| import org.forgerock.util.Utils; | ||
| import org.forgerock.util.promise.Promises; | ||
| import org.identityconnectors.common.logging.Log; | ||
| import org.identityconnectors.framework.common.exceptions.ConnectorIOException; | ||
|
|
||
| /** | ||
| * The Jetty endpoint of a single WebSocket connection. | ||
| * <p> | ||
| * One instance is created per connection by {@link OpenICFWebSocketCreator}, | ||
| * while the {@link ConnectionPrincipal} it belongs to is cached per principal | ||
| * name and shared by every connection of that name. Keeping the session, the | ||
| * {@link WebSocketConnectionHolder} and the close state here (instead of on | ||
| * the shared principal) lets concurrent and successive connections of one | ||
| * principal coexist: each connection delivers its own close event and cleans | ||
| * up its own resources. | ||
| */ | ||
| public class OpenICFWebSocket implements | ||
| WebSocketPingPongListener, WebSocketListener, WebSocketFrameListener { | ||
|
|
||
| private static final Log logger = Log.getLog(OpenICFWebSocket.class); | ||
|
|
||
| private final ConnectionPrincipal<?> principal; | ||
|
|
||
| // Written on the Jetty connect thread, read on the send-executor thread | ||
| // and by isOperational() callers. | ||
| private volatile Session session; | ||
|
|
||
| private final AtomicBoolean closed = new AtomicBoolean(false); | ||
|
|
||
| // Written on the handshake-processing pool thread, read by other message | ||
| // threads via getRemoteConnectionContext()/isHandHooked(). | ||
| private volatile RemoteOperationContext context = null; | ||
|
|
||
| // Single send thread per connection: frames must leave in submission | ||
| // order (the peer drops e.g. an operation response that overtakes the | ||
| // handshake response). Jetty's RemoteEndpoint is thread-safe, but | ||
| // concurrent blocking sends may reach the wire in any order. The executor | ||
| // is shut down when this connection closes. | ||
| private final ExecutorService sendExecutor = Executors.newSingleThreadExecutor( | ||
| Utils.newThreadFactory(null, "OpenICF Jetty WebSocket Send %d", true)); | ||
|
|
||
| public OpenICFWebSocket(final ConnectionPrincipal<?> principal) { | ||
| this.principal = principal; | ||
| } | ||
|
|
||
| Session getSession() { | ||
| return session; | ||
| } | ||
|
|
||
| @Override | ||
| public void onWebSocketConnect(Session session) { | ||
| WebSocketPingPongListener.super.onWebSocketConnect(session); | ||
| this.session = session; | ||
| principal.getOperationMessageListener().onConnect(adapter); | ||
| } | ||
|
|
||
| @Override | ||
| public void onWebSocketClose(int statusCode, String reason) { | ||
| if (!closed.compareAndSet(false, true)) { | ||
| return; | ||
| } | ||
| try { | ||
| principal.getOperationMessageListener().onClose(adapter, statusCode, reason); | ||
| } finally { | ||
| // Notifies the holder's close listeners so the | ||
| // WebSocketConnectionGroup drops this connection. | ||
| adapter.close(); | ||
| sendExecutor.shutdown(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onWebSocketError(Throwable t) { | ||
| logger.warn(t, "onError"); | ||
| principal.getOperationMessageListener().onError(t); | ||
| } | ||
|
|
||
| @Override | ||
| public void onWebSocketPing(ByteBuffer buffer) { | ||
| byte[] b = new byte[buffer.remaining()]; | ||
| buffer.get(b); | ||
| principal.getOperationMessageListener().onPing(adapter, b); | ||
| } | ||
|
|
||
| @Override | ||
| public void onWebSocketPong(ByteBuffer buffer) { | ||
| byte[] b = new byte[buffer.remaining()]; | ||
| buffer.get(b); | ||
| principal.getOperationMessageListener().onPong(adapter, b); | ||
| } | ||
|
|
||
| @Override | ||
| public void onWebSocketBinary(byte[] payload, int offset, int len) { | ||
| logger.ok("onBinaryMessage({0})", null != payload ? payload.length : 0); | ||
| principal.getOperationMessageListener().onMessage(adapter, payload); | ||
| } | ||
|
|
||
| @Override | ||
| public void onWebSocketText(String message) { | ||
| logger.ok("onTextMessage({0})", message); | ||
| principal.getOperationMessageListener().onMessage(adapter, message); | ||
| } | ||
|
|
||
| @Override | ||
| public void onWebSocketFrame(Frame frame) { | ||
| logger.ok("onWebSocketFrame({0})", frame); | ||
| } | ||
|
|
||
| private final WebSocketConnectionHolder adapter = new WebSocketConnectionHolder() { | ||
|
|
||
| @Override | ||
| protected void handshake(RPCMessages.HandshakeMessage message) { | ||
| context = principal.handshake(this, message); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isOperational() { | ||
| return null != getSession() && getSession().isOpen(); | ||
| } | ||
|
|
||
| @Override | ||
| public RemoteOperationContext getRemoteConnectionContext() { | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| return context; | ||
| } | ||
|
|
||
| @Override | ||
| public Future<?> sendBytes(byte[] data) { | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| return submitSend(() -> getSession().getRemote().sendBytes(ByteBuffer.wrap(data))); | ||
| } | ||
|
|
||
| @Override | ||
| public Future<?> sendString(String data) { | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| return submitSend(() -> getSession().getRemote().sendString(data)); | ||
| } | ||
|
|
||
| private Future<?> submitSend(FrameWrite frame) { | ||
| if (isOperational()) { | ||
| try { | ||
| return sendExecutor.submit(() -> { | ||
| try { | ||
| frame.write(); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| }); | ||
| } catch (RejectedExecutionException e) { | ||
| // The connection was closed and the executor shut down. | ||
| } | ||
| } | ||
| return Promises.newExceptionPromise(new ConnectorIOException( | ||
| "Socket is not connected.")); | ||
| } | ||
|
|
||
| @Override | ||
| public void sendPing(byte[] applicationData) throws Exception { | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| if (isOperational()) { | ||
| getSession().getRemote().sendPing(ByteBuffer.wrap(applicationData)); | ||
| if (getSession().getRemote().getBatchMode() == BatchMode.ON) { | ||
| getSession().getRemote().flush(); | ||
| } | ||
| } else { | ||
| throw new ConnectorIOException("Socket is not connected."); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void sendPong(byte[] applicationData) throws Exception { | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| if (isOperational()) { | ||
| getSession().getRemote().sendPong(ByteBuffer.wrap(applicationData)); | ||
| if (getSession().getRemote().getBatchMode() == BatchMode.ON) { | ||
| getSession().getRemote().flush(); | ||
| } | ||
| } else { | ||
| throw new ConnectorIOException("Socket is not connected."); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected void tryClose() { | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| final Session current = getSession(); | ||
| if (null != current && current.isOpen()) { | ||
| current.close(StatusCode.NORMAL, "Shutdown"); | ||
| } | ||
| } | ||
|
|
||
| }; | ||
|
|
||
| @FunctionalInterface | ||
| private interface FrameWrite { | ||
| void write() throws IOException; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.