org.bidib.wizard.server.config.WebSocketConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bidibwizard-server Show documentation
Show all versions of bidibwizard-server Show documentation
jBiDiB BiDiB Wizard Server POM
package org.bidib.wizard.server.config;
import java.security.Principal;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.simp.broker.BrokerAvailabilityEvent;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.scheduling.concurrent.DefaultManagedTaskScheduler;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration;
import org.springframework.web.socket.server.HandshakeInterceptor;
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketConfig.class);
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config
.enableSimpleBroker("/topic", "/queue").setTaskScheduler(new DefaultManagedTaskScheduler())
.setHeartbeatValue(new long[] { 30000, 30000 }); // enable heartbeats
config.setApplicationDestinationPrefixes("/app", "/system");
}
@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
LOGGER.info("Register the UserInterceptor.");
registration.interceptors(new UserInterceptor());
registration.interceptors(new ChannelInterceptor() {
@Override
public Message> preSend(Message> message, MessageChannel channel) {
StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message);
if (accessor.getCommand() == StompCommand.SUBSCRIBE) {
LOGGER.info("{}: {}", channel, message);
}
return message;
}
});
}
@Override
public void configureWebSocketTransport(WebSocketTransportRegistration registry) {
registry.setSendTimeLimit(15 * 1000).setSendBufferSizeLimit(512 * 1024);
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws").setAllowedOrigins("*").addInterceptors(new HttpSessionIdHandshakeInterceptor()).setHandshakeHandler(new DefaultHandshakeHandler() {
@Override
protected Principal determineUser(
ServerHttpRequest request, WebSocketHandler wsHandler, Map attributes) {
return request.getPrincipal();
}
});
}
public void onApplicationEvent(BrokerAvailabilityEvent e) {
LOGGER.info(">>> Broker available: {}", e.isBrokerAvailable());
}
private static final String SESSION_ATTR = "httpSession.id";
static class HttpSessionIdHandshakeInterceptor implements HandshakeInterceptor {
@Override
public boolean beforeHandshake(
ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
Map attributes) throws Exception {
if (request instanceof ServletServerHttpRequest) {
ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
HttpSession session = servletRequest.getServletRequest().getSession(false /* true */);
LOGGER.info("### >>> Current session: {}", session);
if (session != null) {
attributes.put(SESSION_ATTR, session.getId());
}
}
return true;
}
public void afterHandshake(
ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception ex) {
}
}
}