-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
11 changed files
with
1,006 additions
and
72 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
147 changes: 147 additions & 0 deletions
147
.../java/org/springframework/web/reactive/socket/adapter/Jetty10WebSocketHandlerAdapter.java
This file contains 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,147 @@ | ||
/* | ||
* Copyright 2002-2021 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.web.reactive.socket.adapter; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.function.Function; | ||
|
||
import org.eclipse.jetty.websocket.api.Session; | ||
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose; | ||
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect; | ||
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError; | ||
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage; | ||
import org.eclipse.jetty.websocket.api.annotations.WebSocket; | ||
import org.eclipse.jetty.websocket.api.extensions.Frame; | ||
|
||
import org.springframework.core.io.buffer.DataBuffer; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.util.Assert; | ||
import org.springframework.web.reactive.socket.CloseStatus; | ||
import org.springframework.web.reactive.socket.WebSocketHandler; | ||
import org.springframework.web.reactive.socket.WebSocketMessage; | ||
import org.springframework.web.reactive.socket.WebSocketMessage.Type; | ||
import org.springframework.web.reactive.socket.WebSocketSession; | ||
|
||
/** | ||
* Identical to {@link JettyWebSocketHandlerAdapter}, only excluding the | ||
* {@code onWebSocketFrame} method, since the {@link Frame} argument has moved | ||
* to a different package in Jetty 10. | ||
* | ||
* @author Rossen Stoyanchev | ||
* @since 5.3.4 | ||
*/ | ||
@WebSocket | ||
public class Jetty10WebSocketHandlerAdapter { | ||
|
||
private static final ByteBuffer EMPTY_PAYLOAD = ByteBuffer.wrap(new byte[0]); | ||
|
||
|
||
private final WebSocketHandler delegateHandler; | ||
|
||
private final Function<Session, JettyWebSocketSession> sessionFactory; | ||
|
||
@Nullable | ||
private JettyWebSocketSession delegateSession; | ||
|
||
|
||
public Jetty10WebSocketHandlerAdapter(WebSocketHandler handler, | ||
Function<Session, JettyWebSocketSession> sessionFactory) { | ||
|
||
Assert.notNull(handler, "WebSocketHandler is required"); | ||
Assert.notNull(sessionFactory, "'sessionFactory' is required"); | ||
this.delegateHandler = handler; | ||
this.sessionFactory = sessionFactory; | ||
} | ||
|
||
|
||
@OnWebSocketConnect | ||
public void onWebSocketConnect(Session session) { | ||
this.delegateSession = this.sessionFactory.apply(session); | ||
this.delegateHandler.handle(this.delegateSession) | ||
.checkpoint(session.getUpgradeRequest().getRequestURI() + " [JettyWebSocketHandlerAdapter]") | ||
.subscribe(this.delegateSession); | ||
} | ||
|
||
@OnWebSocketMessage | ||
public void onWebSocketText(String message) { | ||
if (this.delegateSession != null) { | ||
WebSocketMessage webSocketMessage = toMessage(Type.TEXT, message); | ||
this.delegateSession.handleMessage(webSocketMessage.getType(), webSocketMessage); | ||
} | ||
} | ||
|
||
@OnWebSocketMessage | ||
public void onWebSocketBinary(byte[] message, int offset, int length) { | ||
if (this.delegateSession != null) { | ||
ByteBuffer buffer = ByteBuffer.wrap(message, offset, length); | ||
WebSocketMessage webSocketMessage = toMessage(Type.BINARY, buffer); | ||
this.delegateSession.handleMessage(webSocketMessage.getType(), webSocketMessage); | ||
} | ||
} | ||
|
||
// TODO: onWebSocketFrame can't be declared without compiling against Jetty 10 | ||
// Jetty 10: org.eclipse.jetty.websocket.api.Frame | ||
// Jetty 9: org.eclipse.jetty.websocket.api.extensions.Frame | ||
|
||
// @OnWebSocketFrame | ||
// public void onWebSocketFrame(Frame frame) { | ||
// if (this.delegateSession != null) { | ||
// if (OpCode.PONG == frame.getOpCode()) { | ||
// ByteBuffer buffer = (frame.getPayload() != null ? frame.getPayload() : EMPTY_PAYLOAD); | ||
// WebSocketMessage webSocketMessage = toMessage(Type.PONG, buffer); | ||
// this.delegateSession.handleMessage(webSocketMessage.getType(), webSocketMessage); | ||
// } | ||
// } | ||
// } | ||
|
||
private <T> WebSocketMessage toMessage(Type type, T message) { | ||
WebSocketSession session = this.delegateSession; | ||
Assert.state(session != null, "Cannot create message without a session"); | ||
if (Type.TEXT.equals(type)) { | ||
byte[] bytes = ((String) message).getBytes(StandardCharsets.UTF_8); | ||
DataBuffer buffer = session.bufferFactory().wrap(bytes); | ||
return new WebSocketMessage(Type.TEXT, buffer); | ||
} | ||
else if (Type.BINARY.equals(type)) { | ||
DataBuffer buffer = session.bufferFactory().wrap((ByteBuffer) message); | ||
return new WebSocketMessage(Type.BINARY, buffer); | ||
} | ||
else if (Type.PONG.equals(type)) { | ||
DataBuffer buffer = session.bufferFactory().wrap((ByteBuffer) message); | ||
return new WebSocketMessage(Type.PONG, buffer); | ||
} | ||
else { | ||
throw new IllegalArgumentException("Unexpected message type: " + message); | ||
} | ||
} | ||
|
||
@OnWebSocketClose | ||
public void onWebSocketClose(int statusCode, String reason) { | ||
if (this.delegateSession != null) { | ||
this.delegateSession.handleClose(CloseStatus.create(statusCode, reason)); | ||
} | ||
} | ||
|
||
@OnWebSocketError | ||
public void onWebSocketError(Throwable cause) { | ||
if (this.delegateSession != null) { | ||
this.delegateSession.handleError(cause); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.