Skip to content

Commit

Permalink
Makes SocketContext available to a WsSession (helidon-io#8944)
Browse files Browse the repository at this point in the history
* Makes SocketContext available to a WsSession to access peer and socket information. See issue 8856.

Signed-off-by: Santiago Pericas-Geertsen <[email protected]>

* Fixes javadoc

---------

Signed-off-by: Santiago Pericas-Geertsen <[email protected]>
  • Loading branch information
spericas authored Jul 12, 2024
1 parent 89caae9 commit 5a54e24
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
* Copyright (c) 2022, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -25,6 +25,7 @@

import io.helidon.common.buffers.BufferData;
import io.helidon.common.buffers.DataReader;
import io.helidon.common.socket.SocketContext;
import io.helidon.http.DateTime;
import io.helidon.webserver.ConnectionContext;
import io.helidon.webserver.spi.ServerConnection;
Expand Down Expand Up @@ -130,6 +131,11 @@ public Optional<String> subProtocol() {
return Optional.empty();
}

@Override
public SocketContext socketContext() {
return ctx;
}

@Override
public Duration idleTime() {
return Duration.between(lastRequestTimestamp, DateTime.timestamp());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.helidon.common.buffers.BufferData;
import io.helidon.common.buffers.DataReader;
import io.helidon.common.socket.HelidonSocket;
import io.helidon.common.socket.SocketContext;
import io.helidon.webclient.api.ClientConnection;
import io.helidon.websocket.ClientWsFrame;
import io.helidon.websocket.ServerWsFrame;
Expand Down Expand Up @@ -169,6 +170,11 @@ public Optional<String> subProtocol() {
return Optional.ofNullable(subProtocol);
}

@Override
public SocketContext socketContext() {
return helidonSocket;
}

private ClientWsConnection send(ClientWsFrame frame) {
WsOpCode opCode = frame.opCode();
if (opCode == WsOpCode.TEXT || opCode == WsOpCode.BINARY) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public void onOpen(WsSession session) {
if (subProtocol != null && !subProtocol.equals(p)) {
throw new InternalError("Invalid sub-protocol in session");
}
if (session.socketContext().remotePeer() == null) {
throw new InternalError("Unable to access remote peer info");
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ public void onMessage(WsSession session, String text, boolean last) {

@Override
public void onOpen(WsSession session) {
if (session.socketContext().remotePeer() == null) {
throw new InternalError("Unable to access remote peer info");
}
for (String s : text) {
session.send(s, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import io.helidon.common.buffers.BufferData;
import io.helidon.common.buffers.DataReader;
import io.helidon.common.socket.SocketContext;
import io.helidon.http.DateTime;
import io.helidon.http.Headers;
import io.helidon.http.HttpPrologue;
Expand Down Expand Up @@ -200,6 +201,11 @@ public Optional<String> subProtocol() {
return upgradeHeaders.first(WsUpgrader.PROTOCOL);
}

@Override
public SocketContext socketContext() {
return ctx;
}

@Override
public Duration idleTime() {
return Duration.between(lastRequestTimestamp, DateTime.timestamp());
Expand Down
10 changes: 9 additions & 1 deletion websocket/src/main/java/io/helidon/websocket/WsSession.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
* Copyright (c) 2022, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@
import java.util.Optional;

import io.helidon.common.buffers.BufferData;
import io.helidon.common.socket.SocketContext;

/**
* WebSocket session.
Expand Down Expand Up @@ -82,4 +83,11 @@ public interface WsSession {
default Optional<String> subProtocol() {
return Optional.empty();
}

/**
* The underlying socket context.
*
* @return socket context
*/
SocketContext socketContext();
}

0 comments on commit 5a54e24

Please sign in to comment.