Example usage for org.springframework.web.socket WebSocketSession getAttributes

List of usage examples for org.springframework.web.socket WebSocketSession getAttributes

Introduction

In this page you can find the example usage for org.springframework.web.socket WebSocketSession getAttributes.

Prototype

Map<String, Object> getAttributes();

Source Link

Document

Return the map with attributes associated with the WebSocket session.

Usage

From source file:com.devicehive.websockets.HiveWebsocketSessionState.java

public static HiveWebsocketSessionState get(WebSocketSession session) {
    return (HiveWebsocketSessionState) session.getAttributes().get(HiveWebsocketSessionState.KEY);
}

From source file:com.devicehive.auth.websockets.WebSocketActionAuthenticationAspect.java

@Before("publicHandlerMethod(session)")
public void authenticate(WebSocketSession session) throws Exception {
    HiveAuthentication authentication = (HiveAuthentication) session.getAttributes()
            .get(WebSocketAuthenticationManager.SESSION_ATTR_AUTHENTICATION);

    //if not authenticated - authenticate as device or anonymous
    if (authentication == null
            || authentication.getAuthorities().contains(new SimpleGrantedAuthority("ROLE_ANONYMOUS"))) {
        HiveAuthentication.HiveAuthDetails details = authenticationManager.getDetails(session);
        authentication = authenticationManager.authenticateAnonymous(details);
        session.getAttributes().put(WebSocketAuthenticationManager.SESSION_ATTR_AUTHENTICATION, authentication);
    }/*from  w  w w .j  av  a2s.co  m*/
    SecurityContextHolder.getContext().setAuthentication(authentication);
}

From source file:io.sevenluck.chat.websocket.EchoWebSocketHandler.java

@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
    logger.info("handleTextMessage " + session.getAttributes().toString());

    String payload = message.getPayload();

    for (WebSocketSession webSocketSession : sessions) {
        webSocketSession.sendMessage(new TextMessage(payload));
    }//from  w ww  .  j a va  2  s.com
}

From source file:ch.rasc.wampspring.session.WebSocketRegistryListener.java

private void registerWsSession(WebSocketSession wsSession) {
    String httpSessionId = (String) wsSession.getAttributes().get(SessionSupport.SPRING_SESSION_ID_ATTR_NAME);
    if (httpSessionId == null) {
        return;//from   w  w  w.  j a  va2 s .c o m
    }

    Map<String, WebSocketSession> sessions = this.httpSessionIdToWsSessions.get(httpSessionId);
    if (sessions == null) {
        sessions = new ConcurrentHashMap<>();
        this.httpSessionIdToWsSessions.putIfAbsent(httpSessionId, sessions);
        sessions = this.httpSessionIdToWsSessions.get(httpSessionId);
    }
    sessions.put(wsSession.getId(), wsSession);
}

From source file:com.devicehive.websockets.ClientWebSocketHandler.java

@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
    super.afterConnectionEstablished(session);

    HiveWebsocketSessionState state = (HiveWebsocketSessionState) session.getAttributes()
            .get(HiveWebsocketSessionState.KEY);
    session.getAttributes().put(WebSocketAuthenticationManager.SESSION_ATTR_AUTHENTICATION,
            session.getPrincipal());/*  w  w w  .j a  v  a 2  s.  c om*/
    state.setEndpoint(HiveEndpoint.CLIENT);
}

From source file:com.devicehive.websockets.DeviceWebSocketHandler.java

@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
    super.afterConnectionEstablished(session);

    HiveWebsocketSessionState state = (HiveWebsocketSessionState) session.getAttributes()
            .get(HiveWebsocketSessionState.KEY);
    session.getAttributes().put(WebSocketAuthenticationManager.SESSION_ATTR_AUTHENTICATION,
            session.getPrincipal());//from   ww w . ja v a2  s.com
    state.setEndpoint(HiveEndpoint.DEVICE);
}

From source file:ch.rasc.wampspring.session.WebSocketRegistryListener.java

private void afterWsConnectionClosed(WebSocketSession wsSession) {
    String httpSessionId = (String) wsSession.getAttributes().get(SessionSupport.SPRING_SESSION_ID_ATTR_NAME);
    if (httpSessionId == null) {
        return;/*from w ww. j  a va2s.  c o m*/
    }

    Map<String, WebSocketSession> sessions = this.httpSessionIdToWsSessions.get(httpSessionId);
    if (sessions != null) {
        String wsSessionId = wsSession.getId();
        boolean result = sessions.remove(wsSessionId) != null;
        if (logger.isDebugEnabled()) {
            logger.debug("Removal of " + wsSessionId + " was " + result);
        }
        if (sessions.isEmpty()) {
            this.httpSessionIdToWsSessions.remove(httpSessionId);
            if (logger.isDebugEnabled()) {
                logger.debug("Removed the corresponding HTTP Session for " + wsSessionId
                        + " since it contained no WebSocket mappings");
            }
        }
    }
}

From source file:io.sevenluck.chat.websocket.EchoWebSocketHandler.java

@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
    logger.info("afterConnectionEstablished" + session.getRemoteAddress().getAddress() + ", attr "
            + session.getAttributes().toString());
    List<ChatRoom> rooms = repository.findByName("bla");

    this.sessions.add(session);
}

From source file:io.sevenluck.chat.websocket.ChatWebSocketHandler.java

@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
    final String token = (String) session.getAttributes().get(HttpAuthTokenHandShakeInterceptor.X_TOKEN);

    logger.info("afterConnectionClosed with token {} and cause {}", token, status.getReason());
    if (sessionMap.containsKey(token)) {
        sessionMap.remove(token);//from  w w w.j a va2 s. c  o m
    } else {
        logger.info("could not find any websession for token {}", token);
    }
}

From source file:io.sevenluck.chat.websocket.ChatWebSocketHandler.java

private ChatMessage newInstance(WebSocketSession wsSession, ChatRoom room, ChatMessageDTO chatMessage) {

    final String token = (String) wsSession.getAttributes().get(HttpAuthTokenHandShakeInterceptor.X_TOKEN);
    final ChatMember author = sessionMap.get(token).getChatSession().getMember();
    ChatMessage message = ChatMessageMapper.toEntity(chatMessage);
    message.setAuthor(author);//from w ww .j  a  va2 s .co  m
    message.setChatRoom(room);

    return message;
}