org.springframework.web.socket.sockjs.transport.SockJsSession Java Examples

The following examples show how to use org.springframework.web.socket.sockjs.transport.SockJsSession. 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: StompSubProtocolHandlerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void handleMessageToClientWithHeartbeatSuppressingSockJsHeartbeat() throws IOException {

	SockJsSession sockJsSession = Mockito.mock(SockJsSession.class);
	StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.CONNECTED);
	accessor.setHeartbeat(0, 10);
	Message<byte[]> message = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
	this.protocolHandler.handleMessageToClient(sockJsSession, message);

	verify(sockJsSession).getPrincipal();
	verify(sockJsSession).disableHeartbeat();
	verify(sockJsSession).sendMessage(any(WebSocketMessage.class));
	verifyNoMoreInteractions(sockJsSession);

	sockJsSession = Mockito.mock(SockJsSession.class);
	accessor = StompHeaderAccessor.create(StompCommand.CONNECTED);
	accessor.setHeartbeat(0, 0);
	message = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
	this.protocolHandler.handleMessageToClient(sockJsSession, message);

	verify(sockJsSession).getPrincipal();
	verify(sockJsSession).sendMessage(any(WebSocketMessage.class));
	verifyNoMoreInteractions(sockJsSession);
}
 
Example #2
Source File: StompSubProtocolHandler.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
private StompHeaderAccessor afterStompSessionConnected(Message<?> message, StompHeaderAccessor accessor,
		WebSocketSession session) {

	Principal principal = session.getPrincipal();
	if (principal != null) {
		accessor = toMutableAccessor(accessor, message);
		accessor.setNativeHeader(CONNECTED_USER_HEADER, principal.getName());
		if (this.userSessionRegistry != null) {
			String userName = getSessionRegistryUserName(principal);
			this.userSessionRegistry.registerSessionId(userName, session.getId());
		}
	}

	long[] heartbeat = accessor.getHeartbeat();
	if (heartbeat[1] > 0) {
		session = WebSocketSessionDecorator.unwrap(session);
		if (session instanceof SockJsSession) {
			((SockJsSession) session).disableHeartbeat();
		}
	}

	return accessor;
}
 
Example #3
Source File: StompSubProtocolHandler.java    From java-technology-stack with MIT License 6 votes vote down vote up
private StompHeaderAccessor afterStompSessionConnected(Message<?> message, StompHeaderAccessor accessor,
		WebSocketSession session) {

	Principal principal = getUser(session);
	if (principal != null) {
		accessor = toMutableAccessor(accessor, message);
		accessor.setNativeHeader(CONNECTED_USER_HEADER, principal.getName());
	}

	long[] heartbeat = accessor.getHeartbeat();
	if (heartbeat[1] > 0) {
		session = WebSocketSessionDecorator.unwrap(session);
		if (session instanceof SockJsSession) {
			((SockJsSession) session).disableHeartbeat();
		}
	}

	return accessor;
}
 
Example #4
Source File: StompSubProtocolHandler.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private StompHeaderAccessor afterStompSessionConnected(Message<?> message, StompHeaderAccessor accessor,
		WebSocketSession session) {

	Principal principal = getUser(session);
	if (principal != null) {
		accessor = toMutableAccessor(accessor, message);
		accessor.setNativeHeader(CONNECTED_USER_HEADER, principal.getName());
	}

	long[] heartbeat = accessor.getHeartbeat();
	if (heartbeat[1] > 0) {
		session = WebSocketSessionDecorator.unwrap(session);
		if (session instanceof SockJsSession) {
			((SockJsSession) session).disableHeartbeat();
		}
	}

	return accessor;
}
 
Example #5
Source File: AbstractHttpReceivingTransportHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public final void handleRequest(ServerHttpRequest request, ServerHttpResponse response,
		WebSocketHandler wsHandler, SockJsSession wsSession) throws SockJsException {

	Assert.notNull(wsSession, "No session");
	AbstractHttpSockJsSession sockJsSession = (AbstractHttpSockJsSession) wsSession;

	handleRequestInternal(request, response, wsHandler, sockJsSession);
}
 
Example #6
Source File: AbstractHttpSendingTransportHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public final void handleRequest(ServerHttpRequest request, ServerHttpResponse response,
		WebSocketHandler wsHandler, SockJsSession wsSession) throws SockJsException {

	AbstractHttpSockJsSession sockJsSession = (AbstractHttpSockJsSession) wsSession;

	// https://github.com/sockjs/sockjs-client/issues/130
	// sockJsSession.setAcceptedProtocol(protocol);

	// Set content type before writing
	response.getHeaders().setContentType(getContentType());

	handleRequestInternal(request, response, sockJsSession);
}
 
Example #7
Source File: WebSocketStompClient.java    From java-technology-stack with MIT License 5 votes vote down vote up
public WebSocketMessage<?> encode(Message<byte[]> message, Class<? extends WebSocketSession> sessionType) {
	StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
	Assert.notNull(accessor, "No StompHeaderAccessor available");
	byte[] payload = message.getPayload();
	byte[] bytes = ENCODER.encode(accessor.getMessageHeaders(), payload);

	boolean useBinary = (payload.length > 0  &&
			!(SockJsSession.class.isAssignableFrom(sessionType)) &&
			MimeTypeUtils.APPLICATION_OCTET_STREAM.isCompatibleWith(accessor.getContentType()));

	return (useBinary ? new BinaryMessage(bytes) : new TextMessage(bytes));
}
 
Example #8
Source File: WebSocketTransportHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void handleRequest(ServerHttpRequest request, ServerHttpResponse response,
		WebSocketHandler wsHandler, SockJsSession wsSession) throws SockJsException {

	WebSocketServerSockJsSession sockJsSession = (WebSocketServerSockJsSession) wsSession;
	try {
		wsHandler = new SockJsWebSocketHandler(getServiceConfig(), wsHandler, sockJsSession);
		this.handshakeHandler.doHandshake(request, response, wsHandler, sockJsSession.getAttributes());
	}
	catch (Throwable ex) {
		sockJsSession.tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);
		throw new SockJsTransportFailureException("WebSocket handshake failure", wsSession.getId(), ex);
	}
}
 
Example #9
Source File: StompSubProtocolHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void handleMessageToClientWithHeartbeatSuppressingSockJsHeartbeat() throws IOException {

	SockJsSession sockJsSession = Mockito.mock(SockJsSession.class);
	when(sockJsSession.getId()).thenReturn("s1");
	StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.CONNECTED);
	accessor.setHeartbeat(0, 10);
	Message<byte[]> message = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
	this.protocolHandler.handleMessageToClient(sockJsSession, message);

	verify(sockJsSession).getId();
	verify(sockJsSession).getPrincipal();
	verify(sockJsSession).disableHeartbeat();
	verify(sockJsSession).sendMessage(any(WebSocketMessage.class));
	verifyNoMoreInteractions(sockJsSession);

	sockJsSession = Mockito.mock(SockJsSession.class);
	when(sockJsSession.getId()).thenReturn("s1");
	accessor = StompHeaderAccessor.create(StompCommand.CONNECTED);
	accessor.setHeartbeat(0, 0);
	message = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
	this.protocolHandler.handleMessageToClient(sockJsSession, message);

	verify(sockJsSession).getId();
	verify(sockJsSession).getPrincipal();
	verify(sockJsSession).sendMessage(any(WebSocketMessage.class));
	verifyNoMoreInteractions(sockJsSession);
}
 
Example #10
Source File: WebSocketTransportHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(ServerHttpRequest request, ServerHttpResponse response,
		WebSocketHandler wsHandler, SockJsSession wsSession) throws SockJsException {

	WebSocketServerSockJsSession sockJsSession = (WebSocketServerSockJsSession) wsSession;
	try {
		wsHandler = new SockJsWebSocketHandler(getServiceConfig(), wsHandler, sockJsSession);
		this.handshakeHandler.doHandshake(request, response, wsHandler, sockJsSession.getAttributes());
	}
	catch (Throwable ex) {
		sockJsSession.tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);
		throw new SockJsTransportFailureException("WebSocket handshake failure", wsSession.getId(), ex);
	}
}
 
Example #11
Source File: StompSubProtocolHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void handleMessageToClientWithHeartbeatSuppressingSockJsHeartbeat() throws IOException {

	SockJsSession sockJsSession = Mockito.mock(SockJsSession.class);
	given(sockJsSession.getId()).willReturn("s1");
	StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.CONNECTED);
	accessor.setHeartbeat(0, 10);
	Message<byte[]> message = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
	this.protocolHandler.handleMessageToClient(sockJsSession, message);

	verify(sockJsSession).getId();
	verify(sockJsSession).getPrincipal();
	verify(sockJsSession).disableHeartbeat();
	verify(sockJsSession).sendMessage(any(WebSocketMessage.class));
	verifyNoMoreInteractions(sockJsSession);

	sockJsSession = Mockito.mock(SockJsSession.class);
	given(sockJsSession.getId()).willReturn("s1");
	accessor = StompHeaderAccessor.create(StompCommand.CONNECTED);
	accessor.setHeartbeat(0, 0);
	message = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
	this.protocolHandler.handleMessageToClient(sockJsSession, message);

	verify(sockJsSession).getId();
	verify(sockJsSession).getPrincipal();
	verify(sockJsSession).sendMessage(any(WebSocketMessage.class));
	verifyNoMoreInteractions(sockJsSession);
}
 
Example #12
Source File: WebSocketStompClient.java    From spring-analysis-note with MIT License 5 votes vote down vote up
public WebSocketMessage<?> encode(Message<byte[]> message, Class<? extends WebSocketSession> sessionType) {
	StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
	Assert.notNull(accessor, "No StompHeaderAccessor available");
	byte[] payload = message.getPayload();
	byte[] bytes = ENCODER.encode(accessor.getMessageHeaders(), payload);

	boolean useBinary = (payload.length > 0  &&
			!(SockJsSession.class.isAssignableFrom(sessionType)) &&
			MimeTypeUtils.APPLICATION_OCTET_STREAM.isCompatibleWith(accessor.getContentType()));

	return (useBinary ? new BinaryMessage(bytes) : new TextMessage(bytes));
}
 
Example #13
Source File: AbstractHttpSendingTransportHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public final void handleRequest(ServerHttpRequest request, ServerHttpResponse response,
		WebSocketHandler wsHandler, SockJsSession wsSession) throws SockJsException {

	AbstractHttpSockJsSession sockJsSession = (AbstractHttpSockJsSession) wsSession;

	// https://github.com/sockjs/sockjs-client/issues/130
	// sockJsSession.setAcceptedProtocol(protocol);

	// Set content type before writing
	response.getHeaders().setContentType(getContentType());

	handleRequestInternal(request, response, sockJsSession);
}
 
Example #14
Source File: AbstractHttpReceivingTransportHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public final void handleRequest(ServerHttpRequest request, ServerHttpResponse response,
		WebSocketHandler wsHandler, SockJsSession wsSession) throws SockJsException {

	Assert.notNull(wsSession, "No session");
	AbstractHttpSockJsSession sockJsSession = (AbstractHttpSockJsSession) wsSession;

	handleRequestInternal(request, response, wsHandler, sockJsSession);
}
 
Example #15
Source File: AbstractHttpReceivingTransportHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public final void handleRequest(ServerHttpRequest request, ServerHttpResponse response,
		WebSocketHandler wsHandler, SockJsSession wsSession) throws SockJsException {

	Assert.notNull(wsSession, "No session");
	AbstractHttpSockJsSession sockJsSession = (AbstractHttpSockJsSession) wsSession;

	handleRequestInternal(request, response, wsHandler, sockJsSession);
}
 
Example #16
Source File: AbstractHttpSendingTransportHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public final void handleRequest(ServerHttpRequest request, ServerHttpResponse response,
		WebSocketHandler wsHandler, SockJsSession wsSession) throws SockJsException {

	AbstractHttpSockJsSession sockJsSession = (AbstractHttpSockJsSession) wsSession;

	String protocol = null;  // https://github.com/sockjs/sockjs-client/issues/130
	sockJsSession.setAcceptedProtocol(protocol);

	// Set content type before writing
	response.getHeaders().setContentType(getContentType());

	handleRequestInternal(request, response, sockJsSession);
}
 
Example #17
Source File: WebSocketStompClient.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public WebSocketMessage<?> encode(Message<byte[]> message, Class<? extends WebSocketSession> sessionType) {
	StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
	Assert.notNull(accessor);
	byte[] payload = message.getPayload();
	byte[] bytes = ENCODER.encode(accessor.getMessageHeaders(), payload);

	boolean useBinary = (payload.length > 0  &&
			!(SockJsSession.class.isAssignableFrom(sessionType)) &&
			MimeTypeUtils.APPLICATION_OCTET_STREAM.isCompatibleWith(accessor.getContentType()));

	return (useBinary ? new BinaryMessage(bytes) : new TextMessage(bytes));
}
 
Example #18
Source File: WebSocketTransportHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void handleRequest(ServerHttpRequest request, ServerHttpResponse response,
		WebSocketHandler wsHandler, SockJsSession wsSession) throws SockJsException {

	WebSocketServerSockJsSession sockJsSession = (WebSocketServerSockJsSession) wsSession;
	try {
		wsHandler = new SockJsWebSocketHandler(getServiceConfig(), wsHandler, sockJsSession);
		this.handshakeHandler.doHandshake(request, response, wsHandler, sockJsSession.getAttributes());
	}
	catch (Throwable ex) {
		sockJsSession.tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);
		throw new SockJsTransportFailureException("WebSocket handshake failure", wsSession.getId(), ex);
	}
}
 
Example #19
Source File: EventSourceTransportHandler.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public boolean checkSessionType(SockJsSession session) {
	return session instanceof EventSourceStreamingSockJsSession;
}
 
Example #20
Source File: AbstractHttpReceivingTransportHandler.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public boolean checkSessionType(SockJsSession session) {
	return (session instanceof AbstractHttpSockJsSession);
}
 
Example #21
Source File: XhrStreamingTransportHandler.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public boolean checkSessionType(SockJsSession session) {
	return session instanceof XhrStreamingSockJsSession;
}
 
Example #22
Source File: XhrPollingTransportHandler.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public boolean checkSessionType(SockJsSession session) {
	return session instanceof PollingSockJsSession;
}
 
Example #23
Source File: HtmlFileTransportHandler.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public boolean checkSessionType(SockJsSession session) {
	return session instanceof HtmlFileStreamingSockJsSession;
}
 
Example #24
Source File: WebSocketTransportHandler.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public boolean checkSessionType(SockJsSession session) {
	return session instanceof WebSocketServerSockJsSession;
}
 
Example #25
Source File: HtmlFileTransportHandler.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public boolean checkSessionType(SockJsSession session) {
	return session instanceof HtmlFileStreamingSockJsSession;
}
 
Example #26
Source File: AbstractHttpReceivingTransportHandler.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public boolean checkSessionType(SockJsSession session) {
	return (session instanceof AbstractHttpSockJsSession);
}
 
Example #27
Source File: XhrStreamingTransportHandler.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public boolean checkSessionType(SockJsSession session) {
	return session instanceof XhrStreamingSockJsSession;
}
 
Example #28
Source File: XhrPollingTransportHandler.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public boolean checkSessionType(SockJsSession session) {
	return session instanceof PollingSockJsSession;
}
 
Example #29
Source File: EventSourceTransportHandler.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public boolean checkSessionType(SockJsSession session) {
	return session instanceof EventSourceStreamingSockJsSession;
}
 
Example #30
Source File: WebSocketTransportHandler.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public boolean checkSessionType(SockJsSession session) {
	return session instanceof WebSocketServerSockJsSession;
}