Skip to content

Commit 34c9503

Browse files
committed
Fix minor issue and polish
1 parent 36148b7 commit 34c9503

File tree

10 files changed

+66
-37
lines changed

10 files changed

+66
-37
lines changed

spring-websocket/src/main/java/org/springframework/sockjs/AbstractSockJsSession.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public void delegateMessages(String[] messages) throws Exception {
139139
}
140140
}
141141

142-
public void delegateError(Throwable ex) {
142+
public void delegateError(Throwable ex) throws Exception {
143143
this.handler.handleError(ex, this);
144144
}
145145

spring-websocket/src/main/java/org/springframework/sockjs/server/support/DefaultSockJsService.java

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.sockjs.SockJsSessionFactory;
3737
import org.springframework.sockjs.server.AbstractSockJsService;
3838
import org.springframework.sockjs.server.ConfigurableTransportHandler;
39+
import org.springframework.sockjs.server.SockJsService;
3940
import org.springframework.sockjs.server.TransportHandler;
4041
import org.springframework.sockjs.server.TransportType;
4142
import org.springframework.sockjs.server.transport.EventSourceTransportHandler;
@@ -54,7 +55,8 @@
5455

5556

5657
/**
57-
* TODO
58+
* A default implementation of {@link SockJsService} adding support for transport handling
59+
* and session management.
5860
*
5961
* @author Rossen Stoyanchev
6062
* @since 4.0
@@ -68,10 +70,31 @@ public class DefaultSockJsService extends AbstractSockJsService {
6870
private ScheduledFuture sessionCleanupTask;
6971

7072

73+
/**
74+
* Create an instance with default {@link TransportHandler transport handler} types.
75+
*
76+
* @param taskScheduler a task scheduler for heart-beat messages and removing
77+
* timed-out sessions; the provided TaskScheduler should be declared as a
78+
* Spring bean to ensure it is initialized at start up and shut down when the
79+
* application stops.
80+
*/
7181
public DefaultSockJsService(TaskScheduler taskScheduler) {
7282
this(taskScheduler, null);
7383
}
7484

85+
/**
86+
* Create an instance by overriding or replacing completely the default
87+
* {@link TransportHandler transport handler} types.
88+
*
89+
* @param taskScheduler a task scheduler for heart-beat messages and removing
90+
* timed-out sessions; the provided TaskScheduler should be declared as a
91+
* Spring bean to ensure it is initialized at start up and shut down when the
92+
* application stops.
93+
* @param transportHandlers the transport handlers to use (replaces the default ones);
94+
* can be {@code null}.
95+
* @param transportHandlerOverrides zero or more overrides to the default transport
96+
* handler types.
97+
*/
7598
public DefaultSockJsService(TaskScheduler taskScheduler, Set<TransportHandler> transportHandlers,
7699
TransportHandler... transportHandlerOverrides) {
77100

@@ -82,7 +105,7 @@ public DefaultSockJsService(TaskScheduler taskScheduler, Set<TransportHandler> t
82105
addTransportHandlers(Arrays.asList(transportHandlerOverrides));
83106
}
84107

85-
protected Set<TransportHandler> getDefaultTransportHandlers() {
108+
protected final Set<TransportHandler> getDefaultTransportHandlers() {
86109
Set<TransportHandler> result = new HashSet<TransportHandler>();
87110
result.add(new XhrPollingTransportHandler());
88111
result.add(new XhrTransportHandler());
@@ -91,14 +114,12 @@ protected Set<TransportHandler> getDefaultTransportHandlers() {
91114
result.add(new XhrStreamingTransportHandler());
92115
result.add(new EventSourceTransportHandler());
93116
result.add(new HtmlFileTransportHandler());
94-
if (isWebSocketEnabled()) {
95-
try {
96-
result.add(new WebSocketTransportHandler(new DefaultHandshakeHandler()));
97-
}
98-
catch (Exception ex) {
99-
if (logger.isWarnEnabled()) {
100-
logger.warn("Failed to add default WebSocketTransportHandler: " + ex.getMessage());
101-
}
117+
try {
118+
result.add(new WebSocketTransportHandler(new DefaultHandshakeHandler()));
119+
}
120+
catch (Exception ex) {
121+
if (logger.isWarnEnabled()) {
122+
logger.warn("Failed to add default WebSocketTransportHandler: " + ex.getMessage());
102123
}
103124
}
104125
return result;

spring-websocket/src/main/java/org/springframework/sockjs/server/transport/SockJsWebSocketHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public void afterConnectionClosed(CloseStatus status, WebSocketSession wsSession
102102
}
103103

104104
@Override
105-
public void handleError(Throwable exception, WebSocketSession webSocketSession) {
105+
public void handleError(Throwable exception, WebSocketSession webSocketSession) throws Exception {
106106
this.session.delegateError(exception);
107107
}
108108

spring-websocket/src/main/java/org/springframework/websocket/CloseStatus.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,19 +113,15 @@ public final class CloseStatus {
113113
public static final CloseStatus SERVER_ERROR = new CloseStatus(1011);
114114

115115
/**
116-
* 1012 indicates that the service is restarted. A client may reconnect, and if it
117-
* choses to do, should reconnect using a randomized delay of 5 - 30s.
118-
* <p>See <a href="https://www.ietf.org/mail-archive/web/hybi/current/msg09649.html">
119-
* [hybi] Additional WebSocket Close Error Codes</a>
116+
* "1012 indicates that the service is restarted. A client may reconnect, and if it
117+
* chooses to do, should reconnect using a randomized delay of 5 - 30s."
120118
*/
121119
public static final CloseStatus SERVICE_RESTARTED = new CloseStatus(1012);
122120

123121
/**
124-
* 1013 indicates that the service is experiencing overload. A client should only
122+
* "1013 indicates that the service is experiencing overload. A client should only
125123
* connect to a different IP (when there are multiple for the target) or reconnect to
126-
* the same IP upon user action.
127-
* <p>See <a href="https://www.ietf.org/mail-archive/web/hybi/current/msg09649.html">
128-
* [hybi] Additional WebSocket Close Error Codes</a>
124+
* the same IP upon user action."
129125
*/
130126
public static final CloseStatus SERVICE_OVERLOAD = new CloseStatus(1013);
131127

spring-websocket/src/main/java/org/springframework/websocket/WebSocketHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ public interface WebSocketHandler {
3939
/**
4040
* TODO
4141
*/
42-
void handleError(Throwable exception, WebSocketSession session);
42+
void handleError(Throwable exception, WebSocketSession session) throws Exception;
4343

4444
}

spring-websocket/src/main/java/org/springframework/websocket/client/WebSocketClient.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
public interface WebSocketClient {
3838

3939

40+
WebSocketSession doHandshake(WebSocketHandler handler,
41+
String uriTemplate, Object... uriVariables) throws WebSocketConnectFailureException;
42+
4043
WebSocketSession doHandshake(HandlerProvider<WebSocketHandler> handler,
4144
String uriTemplate, Object... uriVariables) throws WebSocketConnectFailureException;
4245

spring-websocket/src/main/java/org/springframework/websocket/client/endpoint/StandardWebSocketClient.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.websocket.client.WebSocketConnectFailureException;
3939
import org.springframework.websocket.endpoint.StandardWebSocketSession;
4040
import org.springframework.websocket.endpoint.WebSocketHandlerEndpoint;
41+
import org.springframework.websocket.support.SimpleHandlerProvider;
4142

4243

4344
/**
@@ -59,6 +60,13 @@ public void setWebSocketContainer(WebSocketContainer container) {
5960
this.webSocketContainer = container;
6061
}
6162

63+
@Override
64+
public WebSocketSession doHandshake(WebSocketHandler handler, String uriTemplate, Object... uriVariables)
65+
throws WebSocketConnectFailureException {
66+
67+
return doHandshake(new SimpleHandlerProvider<WebSocketHandler>(handler), uriTemplate, uriVariables);
68+
}
69+
6270
public WebSocketSession doHandshake(HandlerProvider<WebSocketHandler> handler,
6371
String uriTemplate, Object... uriVariables) throws WebSocketConnectFailureException {
6472

spring-websocket/src/main/java/org/springframework/websocket/endpoint/WebSocketHandlerEndpoint.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void onOpen(final javax.websocket.Session session, EndpointConfig config)
6767
Assert.isTrue(this.sessionCount.compareAndSet(0, 1), "Unexpected connection");
6868

6969
if (logger.isDebugEnabled()) {
70-
logger.debug("Client connected, javax.websocket.Session id="
70+
logger.debug("Connection established, javax.websocket.Session id="
7171
+ session.getId() + ", uri=" + session.getRequestURI());
7272
}
7373

@@ -110,7 +110,7 @@ public void onMessage(byte[] message) {
110110
this.handler.afterConnectionEstablished(this.webSocketSession);
111111
}
112112
catch (Throwable ex) {
113-
this.handler.handleError(ex, this.webSocketSession);
113+
onError(session, ex);
114114
}
115115
}
116116

@@ -123,7 +123,7 @@ private void handleTextMessage(javax.websocket.Session session, String message)
123123
((TextMessageHandler) handler).handleTextMessage(textMessage, this.webSocketSession);
124124
}
125125
catch (Throwable ex) {
126-
this.handler.handleError(ex, this.webSocketSession);
126+
onError(session, ex);
127127
}
128128
}
129129

@@ -136,21 +136,21 @@ private void handleBinaryMessage(javax.websocket.Session session, byte[] message
136136
((BinaryMessageHandler) handler).handleBinaryMessage(binaryMessage, this.webSocketSession);
137137
}
138138
catch (Throwable ex) {
139-
this.handler.handleError(ex, this.webSocketSession);
139+
onError(session, ex);
140140
}
141141
}
142142

143143
@Override
144144
public void onClose(javax.websocket.Session session, CloseReason reason) {
145145
if (logger.isDebugEnabled()) {
146-
logger.debug("Client disconnected, WebSocket session id=" + session.getId() + ", " + reason);
146+
logger.debug("Connection closed, WebSocket session id=" + session.getId() + ", " + reason);
147147
}
148148
try {
149149
CloseStatus closeStatus = new CloseStatus(reason.getCloseCode().getCode(), reason.getReasonPhrase());
150150
this.handler.afterConnectionClosed(closeStatus, this.webSocketSession);
151151
}
152152
catch (Throwable ex) {
153-
logger.error("Error while processing session closing", ex);
153+
onError(session, ex);
154154
}
155155
finally {
156156
this.handlerProvider.destroy(this.handler);

spring-websocket/src/main/java/org/springframework/websocket/server/support/JettyRequestUpgradeStrategy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public void onWebSocketConnect(Session session) {
143143
try {
144144
this.session = new WebSocketSessionAdapter(session);
145145
if (logger.isDebugEnabled()) {
146-
logger.debug("Client connected, WebSocket session id="
146+
logger.debug("Connection established, WebSocket session id="
147147
+ this.session.getId() + ", uri=" + this.session.getURI());
148148
}
149149
this.handler = this.provider.getHandler();
@@ -167,7 +167,7 @@ public void onWebSocketClose(int statusCode, String reason) {
167167
try {
168168
CloseStatus closeStatus = new CloseStatus(statusCode, reason);
169169
if (logger.isDebugEnabled()) {
170-
logger.debug("Client disconnected, WebSocket session id="
170+
logger.debug("Connection closed, WebSocket session id="
171171
+ this.session.getId() + ", " + closeStatus);
172172
}
173173
this.handler.afterConnectionClosed(closeStatus, this.session);

spring-websocket/src/main/java/org/springframework/websocket/server/support/WebSocketHttpRequestHandler.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,26 @@
4444
*/
4545
public class WebSocketHttpRequestHandler implements HttpRequestHandler {
4646

47-
private HandshakeHandler handshakeHandler;
47+
private final HandshakeHandler handshakeHandler;
4848

4949
private final HandlerProvider<WebSocketHandler> handlerProvider;
5050

5151

5252
public WebSocketHttpRequestHandler(WebSocketHandler webSocketHandler) {
53-
Assert.notNull(webSocketHandler, "webSocketHandler is required");
54-
this.handlerProvider = new SimpleHandlerProvider<WebSocketHandler>(webSocketHandler);
55-
this.handshakeHandler = new DefaultHandshakeHandler();
53+
this(new SimpleHandlerProvider<WebSocketHandler>(webSocketHandler));
5654
}
5755

5856
public WebSocketHttpRequestHandler( HandlerProvider<WebSocketHandler> handlerProvider) {
59-
Assert.notNull(handlerProvider, "handlerProvider is required");
60-
this.handlerProvider = handlerProvider;
57+
this(handlerProvider, new DefaultHandshakeHandler());
6158
}
6259

63-
public void setHandshakeHandler(HandshakeHandler handshakeHandler) {
60+
public WebSocketHttpRequestHandler( HandlerProvider<WebSocketHandler> handlerProvider,
61+
HandshakeHandler handshakeHandler) {
62+
63+
Assert.notNull(handlerProvider, "handlerProvider is required");
6464
Assert.notNull(handshakeHandler, "handshakeHandler is required");
65-
this.handshakeHandler = handshakeHandler;
65+
this.handlerProvider = handlerProvider;
66+
this.handshakeHandler = new DefaultHandshakeHandler();
6667
}
6768

6869
@Override

0 commit comments

Comments
 (0)