org.springframework.web.socket.sockjs.SockJsMessageDeliveryException Java Examples

The following examples show how to use org.springframework.web.socket.sockjs.SockJsMessageDeliveryException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractSockJsSession.java    From spring-analysis-note with MIT License 6 votes vote down vote up
public void delegateMessages(String... messages) throws SockJsMessageDeliveryException {
	List<String> undelivered = new ArrayList<>(Arrays.asList(messages));
	for (String message : messages) {
		try {
			if (isClosed()) {
				throw new SockJsMessageDeliveryException(this.id, undelivered, "Session closed");
			}
			else {
				this.handler.handleMessage(this, new TextMessage(message));
				undelivered.remove(0);
			}
		}
		catch (Throwable ex) {
			throw new SockJsMessageDeliveryException(this.id, undelivered, ex);
		}
	}
}
 
Example #2
Source File: SockJsSessionTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void delegateMessagesWithErrorAndConnectionClosing() throws Exception {
	WebSocketHandler wsHandler = new ExceptionWebSocketHandlerDecorator(this.webSocketHandler);
	TestSockJsSession sockJsSession = new TestSockJsSession(
			"1", this.sockJsConfig, wsHandler, Collections.<String, Object>emptyMap());

	String msg1 = "message 1";
	String msg2 = "message 2";
	String msg3 = "message 3";

	willThrow(new IOException()).given(this.webSocketHandler).handleMessage(sockJsSession, new TextMessage(msg2));

	sockJsSession.delegateConnectionEstablished();
	try {
		sockJsSession.delegateMessages(msg1, msg2, msg3);
		fail("expected exception");
	}
	catch (SockJsMessageDeliveryException ex) {
		assertEquals(Collections.singletonList(msg3), ex.getUndeliveredMessages());
		verify(this.webSocketHandler).afterConnectionEstablished(sockJsSession);
		verify(this.webSocketHandler).handleMessage(sockJsSession, new TextMessage(msg1));
		verify(this.webSocketHandler).handleMessage(sockJsSession, new TextMessage(msg2));
		verify(this.webSocketHandler).afterConnectionClosed(sockJsSession, CloseStatus.SERVER_ERROR);
		verifyNoMoreInteractions(this.webSocketHandler);
	}
}
 
Example #3
Source File: HttpReceivingTransportHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void delegateMessageException() throws Exception {
	StubSockJsServiceConfig sockJsConfig = new StubSockJsServiceConfig();
	this.servletRequest.setContent("[\"x\"]".getBytes("UTF-8"));

	WebSocketHandler wsHandler = mock(WebSocketHandler.class);
	TestHttpSockJsSession session = new TestHttpSockJsSession("1", sockJsConfig, wsHandler, null);
	session.delegateConnectionEstablished();

	willThrow(new Exception()).given(wsHandler).handleMessage(session, new TextMessage("x"));

	try {
		XhrReceivingTransportHandler transportHandler = new XhrReceivingTransportHandler();
		transportHandler.initialize(sockJsConfig);
		transportHandler.handleRequest(this.request, this.response, wsHandler, session);
		fail("Expected exception");
	}
	catch (SockJsMessageDeliveryException ex) {
		assertNull(session.getCloseStatus());
	}
}
 
Example #4
Source File: AbstractSockJsSession.java    From java-technology-stack with MIT License 6 votes vote down vote up
public void delegateMessages(String... messages) throws SockJsMessageDeliveryException {
	List<String> undelivered = new ArrayList<>(Arrays.asList(messages));
	for (String message : messages) {
		try {
			if (isClosed()) {
				throw new SockJsMessageDeliveryException(this.id, undelivered, "Session closed");
			}
			else {
				this.handler.handleMessage(this, new TextMessage(message));
				undelivered.remove(0);
			}
		}
		catch (Throwable ex) {
			throw new SockJsMessageDeliveryException(this.id, undelivered, ex);
		}
	}
}
 
Example #5
Source File: SockJsSessionTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void delegateMessagesWithErrorAndConnectionClosing() throws Exception {
	WebSocketHandler wsHandler = new ExceptionWebSocketHandlerDecorator(this.webSocketHandler);
	TestSockJsSession sockJsSession = new TestSockJsSession(
			"1", this.sockJsConfig, wsHandler, Collections.<String, Object>emptyMap());

	String msg1 = "message 1";
	String msg2 = "message 2";
	String msg3 = "message 3";

	willThrow(new IOException()).given(this.webSocketHandler).handleMessage(sockJsSession, new TextMessage(msg2));

	sockJsSession.delegateConnectionEstablished();
	try {
		sockJsSession.delegateMessages(msg1, msg2, msg3);
		fail("expected exception");
	}
	catch (SockJsMessageDeliveryException ex) {
		assertEquals(Collections.singletonList(msg3), ex.getUndeliveredMessages());
		verify(this.webSocketHandler).afterConnectionEstablished(sockJsSession);
		verify(this.webSocketHandler).handleMessage(sockJsSession, new TextMessage(msg1));
		verify(this.webSocketHandler).handleMessage(sockJsSession, new TextMessage(msg2));
		verify(this.webSocketHandler).afterConnectionClosed(sockJsSession, CloseStatus.SERVER_ERROR);
		verifyNoMoreInteractions(this.webSocketHandler);
	}
}
 
Example #6
Source File: HttpReceivingTransportHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void delegateMessageException() throws Exception {
	StubSockJsServiceConfig sockJsConfig = new StubSockJsServiceConfig();
	this.servletRequest.setContent("[\"x\"]".getBytes("UTF-8"));

	WebSocketHandler wsHandler = mock(WebSocketHandler.class);
	TestHttpSockJsSession session = new TestHttpSockJsSession("1", sockJsConfig, wsHandler, null);
	session.delegateConnectionEstablished();

	willThrow(new Exception()).given(wsHandler).handleMessage(session, new TextMessage("x"));

	try {
		XhrReceivingTransportHandler transportHandler = new XhrReceivingTransportHandler();
		transportHandler.initialize(sockJsConfig);
		transportHandler.handleRequest(this.request, this.response, wsHandler, session);
		fail("Expected exception");
	}
	catch (SockJsMessageDeliveryException ex) {
		assertNull(session.getCloseStatus());
	}
}
 
Example #7
Source File: AbstractSockJsSession.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
public void delegateMessages(String... messages) throws SockJsMessageDeliveryException {
	List<String> undelivered = new ArrayList<String>(Arrays.asList(messages));
	for (String message : messages) {
		try {
			if (isClosed()) {
				throw new SockJsMessageDeliveryException(this.id, undelivered, "Session closed");
			}
			else {
				this.handler.handleMessage(this, new TextMessage(message));
				undelivered.remove(0);
			}
		}
		catch (Throwable ex) {
			throw new SockJsMessageDeliveryException(this.id, undelivered, ex);
		}
	}
}
 
Example #8
Source File: HttpReceivingTransportHandlerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void delegateMessageException() throws Exception {
	StubSockJsServiceConfig sockJsConfig = new StubSockJsServiceConfig();
	this.servletRequest.setContent("[\"x\"]".getBytes("UTF-8"));

	WebSocketHandler wsHandler = mock(WebSocketHandler.class);
	TestHttpSockJsSession session = new TestHttpSockJsSession("1", sockJsConfig, wsHandler, null);
	session.delegateConnectionEstablished();

	willThrow(new Exception()).given(wsHandler).handleMessage(session, new TextMessage("x"));

	try {
		XhrReceivingTransportHandler transportHandler = new XhrReceivingTransportHandler();
		transportHandler.initialize(sockJsConfig);
		transportHandler.handleRequest(this.request, this.response, wsHandler, session);
		fail("Expected exception");
	}
	catch (SockJsMessageDeliveryException ex) {
		assertNull(session.getCloseStatus());
	}
}
 
Example #9
Source File: SockJsSessionTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void delegateMessagesWithErrorAndConnectionClosing() throws Exception {

	WebSocketHandler wsHandler = new ExceptionWebSocketHandlerDecorator(this.webSocketHandler);
	TestSockJsSession sockJsSession = new TestSockJsSession("1", this.sockJsConfig,
			wsHandler, Collections.<String, Object>emptyMap());

	String msg1 = "message 1";
	String msg2 = "message 2";
	String msg3 = "message 3";

	willThrow(new IOException()).given(this.webSocketHandler).handleMessage(sockJsSession, new TextMessage(msg2));

	sockJsSession.delegateConnectionEstablished();
	try {
		sockJsSession.delegateMessages(new String[] { msg1, msg2, msg3 });
		fail("expected exception");
	}
	catch (SockJsMessageDeliveryException ex) {
		assertEquals(Arrays.asList(msg3), ex.getUndeliveredMessages());
		verify(this.webSocketHandler).afterConnectionEstablished(sockJsSession);
		verify(this.webSocketHandler).handleMessage(sockJsSession, new TextMessage(msg1));
		verify(this.webSocketHandler).handleMessage(sockJsSession, new TextMessage(msg2));
		verify(this.webSocketHandler).afterConnectionClosed(sockJsSession, CloseStatus.SERVER_ERROR);
		verifyNoMoreInteractions(this.webSocketHandler);
	}
}