/** * Create a new {@link CloseStatus} from this one with the specified reason. * @param reason the reason * @return a new {@link CloseStatus} instance */ public CloseStatus withReason(String reason) { Assert.hasText(reason, "Reason must not be empty"); return new CloseStatus(this.code, reason); }
/** * Performs cleanup and notify the {@link WebSocketHandler}. */ @Override public final void close() throws IOException { close(new CloseStatus(3000, "Go away!")); }
@Override public void run() { try { closeInternal(new CloseStatus(2007, "Transport timed out")); } catch (Throwable ex) { if (logger.isWarnEnabled()) { logger.warn("Failed to close " + this + " after transport timeout", ex); } } } };
@OnWebSocketClose public void onWebSocketClose(int statusCode, String reason) { CloseStatus closeStatus = new CloseStatus(statusCode, reason); try { this.webSocketHandler.afterConnectionClosed(this.wsSession, closeStatus); } catch (Throwable ex) { if (logger.isWarnEnabled()) { logger.warn("Unhandled exception after connection closed for " + this, ex); } } }
@Override public void onClose(javax.websocket.Session session, CloseReason reason) { CloseStatus closeStatus = new CloseStatus(reason.getCloseCode().getCode(), reason.getReasonPhrase()); try { this.handler.afterConnectionClosed(this.wsSession, closeStatus); } catch (Throwable ex) { if (logger.isWarnEnabled()) { logger.warn("Unhandled on-close exception for " + this.wsSession, ex); } } }
private void handleCloseFrame(SockJsFrame frame) { CloseStatus closeStatus = CloseStatus.NO_STATUS_CODE; try { String frameData = frame.getFrameData(); if (frameData != null) { String[] data = getMessageCodec().decode(frameData); if (data != null && data.length == 2) { closeStatus = new CloseStatus(Integer.valueOf(data[0]), data[1]); } if (logger.isDebugEnabled()) { logger.debug("Processing SockJS close frame with " + closeStatus + " in " + this); } } } catch (IOException ex) { if (logger.isErrorEnabled()) { logger.error("Failed to decode data for " + frame + " in " + this, ex); } } silentClose(closeStatus); }
private void handleOpenFrame() { if (logger.isDebugEnabled()) { logger.debug("Processing SockJS open frame in " + this); } if (this.state == State.NEW) { this.state = State.OPEN; try { this.webSocketHandler.afterConnectionEstablished(this); this.connectFuture.set(this); } catch (Throwable ex) { if (logger.isErrorEnabled()) { logger.error("WebSocketHandler.afterConnectionEstablished threw exception in " + this, ex); } } } else { if (logger.isDebugEnabled()) { logger.debug("Open frame received in " + getId() + " but we're not connecting (current state " + this.state + "). The server might have been restarted and lost track of the session."); } silentClose(new CloseStatus(1006, "Server lost session")); } }
private void onFailure(Throwable failure) { if (connectFuture.setException(failure)) { return; } if (sockJsSession.isDisconnected()) { sockJsSession.afterTransportClosed(null); } else { sockJsSession.handleTransportError(failure); sockJsSession.afterTransportClosed(new CloseStatus(1006, failure.getMessage())); } } };
@Override public void onFailure(Response response, Throwable failure) { if (this.connectFuture.setException(failure)) { return; } if (this.sockJsSession.isDisconnected()) { this.sockJsSession.afterTransportClosed(null); } else { this.sockJsSession.handleTransportError(failure); this.sockJsSession.afterTransportClosed(new CloseStatus(1006, failure.getMessage())); } } }
@Test public void timeoutTask() { this.session.getTimeoutTask().run(); assertThat(this.session.disconnectStatus, equalTo(new CloseStatus(2007, "Transport timed out"))); }
public void onFailure(Throwable failure) { IoUtils.safeClose(this.connection); if (this.connectFuture.setException(failure)) { return; } if (this.session.isDisconnected()) { this.session.afterTransportClosed(null); } else { this.session.handleTransportError(failure); this.session.afterTransportClosed(new CloseStatus(1006, failure.getMessage())); } } }
@Test public void closeWithStatus() throws Exception { this.session.handleFrame(SockJsFrame.openFrame().getContent()); this.session.close(new CloseStatus(3000, "reason")); assertThat(this.session.disconnectStatus, equalTo(new CloseStatus(3000, "reason"))); }
@Test public void handleFrameOpenWhenStatusNotNew() throws Exception { this.session.handleFrame(SockJsFrame.openFrame().getContent()); assertThat(this.session.isOpen(), is(true)); this.session.handleFrame(SockJsFrame.openFrame().getContent()); assertThat(this.session.disconnectStatus, equalTo(new CloseStatus(1006, "Server lost session"))); }
@Test public void closeWithWriteFrameExceptions() throws Exception { this.session.setExceptionOnWrite(new IOException()); this.session.delegateConnectionEstablished(); this.session.setActive(true); this.session.close(); assertEquals(new CloseStatus(3000, "Go away!"), this.session.getCloseStatus()); assertClosed(); }
@Test public void closeWithStatusOutOfRange() throws Exception { this.session.handleFrame(SockJsFrame.openFrame().getContent()); this.thrown.expect(IllegalArgumentException.class); this.thrown.expectMessage("Invalid close status"); this.session.close(new CloseStatus(2999, "reason")); }
@Test public void close() throws Exception { this.session.delegateConnectionEstablished(); assertOpen(); this.session.setActive(true); this.session.close(); assertEquals(1, this.session.getSockJsFramesWritten().size()); assertEquals(SockJsFrame.closeFrameGoAway(), this.session.getSockJsFramesWritten().get(0)); assertEquals(1, this.session.getNumberOfLastActiveTimeUpdates()); assertTrue(this.session.didCancelHeartbeat()); assertEquals(new CloseStatus(3000, "Go away!"), this.session.getCloseStatus()); assertClosed(); verify(this.webSocketHandler).afterConnectionClosed(this.session, new CloseStatus(3000, "Go away!")); }
@Test public void handleFrameClose() throws Exception { this.session.handleFrame(SockJsFrame.openFrame().getContent()); this.session.handleFrame(SockJsFrame.closeFrame(1007, "").getContent()); assertThat(this.session.isOpen(), equalTo(false)); assertThat(this.session.disconnectStatus, equalTo(new CloseStatus(1007, ""))); verify(this.handler).afterConnectionEstablished(this.session); verifyNoMoreInteractions(this.handler); }
@Test public void connectReceiveAndCloseWithPrelude() throws Exception { StringBuilder sb = new StringBuilder(2048); for (int i = 0; i < 2048; i++) { sb.append('h'); } String body = sb.toString() + "\n" + "o\n" + "a[\"foo\"]\n" + "c[3000,\"Go away!\"]"; ClientHttpResponse response = response(HttpStatus.OK, body); connect(response); verify(this.webSocketHandler).afterConnectionEstablished(any()); verify(this.webSocketHandler).handleMessage(any(), eq(new TextMessage("foo"))); verify(this.webSocketHandler).afterConnectionClosed(any(), eq(new CloseStatus(3000, "Go away!"))); verifyNoMoreInteractions(this.webSocketHandler); }
@Test public void connectReceiveAndClose() throws Exception { String body = "o\n" + "a[\"foo\"]\n" + "c[3000,\"Go away!\"]"; ClientHttpResponse response = response(HttpStatus.OK, body); connect(response); verify(this.webSocketHandler).afterConnectionEstablished(any()); verify(this.webSocketHandler).handleMessage(any(), eq(new TextMessage("foo"))); verify(this.webSocketHandler).afterConnectionClosed(any(), eq(new CloseStatus(3000, "Go away!"))); verifyNoMoreInteractions(this.webSocketHandler); }
@Test public void connectReceiveAndCloseWithStompFrame() throws Exception { StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.SEND); accessor.setDestination("/destination"); MessageHeaders headers = accessor.getMessageHeaders(); Message<byte[]> message = MessageBuilder.createMessage("body".getBytes(StandardCharsets.UTF_8), headers); byte[] bytes = new StompEncoder().encode(message); TextMessage textMessage = new TextMessage(bytes); SockJsFrame frame = SockJsFrame.messageFrame(new Jackson2SockJsMessageCodec(), textMessage.getPayload()); String body = "o\n" + frame.getContent() + "\n" + "c[3000,\"Go away!\"]"; ClientHttpResponse response = response(HttpStatus.OK, body); connect(response); verify(this.webSocketHandler).afterConnectionEstablished(any()); verify(this.webSocketHandler).handleMessage(any(), eq(textMessage)); verify(this.webSocketHandler).afterConnectionClosed(any(), eq(new CloseStatus(3000, "Go away!"))); verifyNoMoreInteractions(this.webSocketHandler); }