All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.vrspace.server.config.WebSocketConfig Maven / Gradle / Ivy

The newest version!
package org.vrspace.server.config;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;
import org.vrspace.server.core.ServerSessionManager;
import org.vrspace.server.core.SessionManager;

import jakarta.servlet.http.HttpSession;

/**
 * Configures WebSocket path (default:/vrspace) and allowed origins (default:*)
 * 
 * @author joe
 * @see SessionManager
 *
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
  @Value("${org.vrspace.server.socketPath:/vrspace/client}")
  private String clientPath;
  @Value("${org.vrspace.server.socketPath:/vrspace/server}")
  private String serverPath;
  @Value("${org.vrspace.server.allowedOrigins:*}")
  private String origins;

  @Autowired
  private SessionManager sessionManager;
  @Autowired
  private ServerSessionManager serverSessionManager;

  public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
    // setAllowedOrigins or 403 forbidden when behind proxy
    registry.addHandler(sessionManager, clientPath).setAllowedOrigins(origins)
        .addInterceptors(new CustomSessionHandshakeInterceptor());
    registry.addHandler(serverSessionManager, serverPath).setAllowedOrigins(origins)
        .addInterceptors(new CustomSessionHandshakeInterceptor());
  }

  public class CustomSessionHandshakeInterceptor extends HttpSessionHandshakeInterceptor {
    public static final String HTTP_SESSION_ATTR_NAME = "HTTP.SESSION";

    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
        Map attributes) throws Exception {
      boolean ret = super.beforeHandshake(request, response, wsHandler, attributes);
      if (request instanceof ServletServerHttpRequest) {
        ServletServerHttpRequest serverRequest = (ServletServerHttpRequest) request;
        HttpSession httpSession = serverRequest.getServletRequest().getSession(isCreateSession());
        attributes.put(HTTP_SESSION_ATTR_NAME, httpSession);
      }
      return ret;
    }

  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy