Skip to content
Open
Show file tree
Hide file tree
Changes from all 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: 2 additions & 1 deletion changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ Changes log
"websocket.maxMessageSize", "websocket.maxFrameSize" and "websocket.compression"
parameters. Server and client support "permessage-deflate" compression (RFC 7692),
enabled by default. `WebSocketClient.setClient` shares the I/O threads of a borrowed,
started Netty HTTP `Client` instead of creating its own.
started Netty HTTP `Client` instead of creating its own, with or without a context
(issue #1599). `Client.getHelper()` is now public, as an engine extension point.
- Added the `NettyConnectionUpgrade` engine hook: a Restlet returning a "101 Switching
Protocols" response with this response attribute takes over the HTTP/1.1 connection
(`NettyUpgradeContext`), which the WebSocket extension relies on.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,10 @@ public synchronized Client getClient() {

/**
* Sets a borrowed HTTP client whose I/O threads are shared, instead of creating new ones. The
* client must use the Netty HTTP connector of the core, have a context, and be started before
* this WebSocket client. Its owner stops it, after this WebSocket client: stopping it closes
* the sessions. Only the I/O threads are shared; the other settings, including TLS, are those
* of this WebSocket client.
* client must use the Netty HTTP connector of the core and be started before this WebSocket
* client. Its owner stops it, after this WebSocket client: stopping it closes the sessions.
* Only the I/O threads are shared; the other settings, including TLS, are those of this
* WebSocket client.
*
* @param client The borrowed client, or null to create new I/O threads.
*/
Expand Down Expand Up @@ -313,13 +313,9 @@ private EventLoopGroup borrowedEventLoopGroup() {
if (client == null) {
return null;
}
Object helper =
(client.getContext() == null)
? null
: client.getContext().getAttributes().get("org.restlet.engine.helper");
if (!(helper instanceof NettyClientHelper nettyHelper)) {
if (!(client.getHelper() instanceof NettyClientHelper nettyHelper)) {
throw new IllegalStateException(
"The borrowed client must have a context and use the Netty HTTP connector");
"The borrowed client must use the Netty HTTP connector");
}
EventLoopGroup group = nettyHelper.getEventLoopGroup();
if (group == null || group.isShuttingDown()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,20 @@ void acceptsAClientWhoseContextIsSetLater() throws Exception {
}
}

@Test
void acceptsAClientWithoutContext() throws Exception {
Client plain = new Client(Protocol.HTTP);
plain.start();
try (WebSocketClient client = new WebSocketClient()) {
client.setClient(plain);
WebSocketSession session = client.connect(uri);
session.sendText("plain");
assertEquals("plain", session.receive().getText());
} finally {
plain.stop();
}
}

@Test
void rejectsUnsuitableClients() throws Exception {
WebSocketClient client = new WebSocketClient();
Expand Down
9 changes: 6 additions & 3 deletions org.restlet/src/main/java/org/restlet/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,14 @@ public Client(String protocolName) {
}

/**
* Returns the helper provided by the implementation.
* Returns the helper provided by the implementation. This is an engine extension point, meant
* for Restlet extensions that share the connector's resources, such as its I/O threads. It
* doesn't depend on the client having a context.
*
* @return The helper provided by the implementation.
* @return The helper provided by the implementation, or null if no connector supports the
* protocols.
*/
private RestletHelper<Client> getHelper() {
public RestletHelper<Client> getHelper() {
return this.helper;
}

Expand Down
Loading