Example usage for org.springframework.messaging.simp.stomp StompHeaderAccessor getSessionId

List of usage examples for org.springframework.messaging.simp.stomp StompHeaderAccessor getSessionId

Introduction

In this page you can find the example usage for org.springframework.messaging.simp.stomp StompHeaderAccessor getSessionId.

Prototype

@Nullable
public String getSessionId() 

Source Link

Document

Return the id of the current session.

Usage

From source file:com.company.project.config.StompConnectedEvent.java

@Override
public void onApplicationEvent(SessionConnectedEvent event) {
    StompHeaderAccessor sha = StompHeaderAccessor.wrap(event.getMessage());

    logger.debug("Connected event [sessionId: " + sha.getSessionId() + " ]");
    System.out.println("Connected event [sessionId: " + sha.getSessionId() + " ]");

}

From source file:org.springframework.samples.portfolio.web.standalone.StandalonePortfolioControllerTests.java

@Test
public void getPositions() throws Exception {

    StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SUBSCRIBE);
    headers.setSubscriptionId("0");
    headers.setDestination("/app/positions");
    headers.setSessionId("0");
    headers.setUser(new TestPrincipal("fabrice"));
    headers.setSessionAttributes(new HashMap<String, Object>());
    Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();

    this.annotationMethodMessageHandler.handleMessage(message);

    assertEquals(1, this.clientOutboundChannel.getMessages().size());
    Message<?> reply = this.clientOutboundChannel.getMessages().get(0);

    StompHeaderAccessor replyHeaders = StompHeaderAccessor.wrap(reply);
    assertEquals("0", replyHeaders.getSessionId());
    assertEquals("0", replyHeaders.getSubscriptionId());
    assertEquals("/app/positions", replyHeaders.getDestination());

    String json = new String((byte[]) reply.getPayload(), Charset.forName("UTF-8"));
    new JsonPathExpectationsHelper("$[0].company").assertValue(json, "Citrix Systems, Inc.");
    new JsonPathExpectationsHelper("$[1].company").assertValue(json, "Dell Inc.");
    new JsonPathExpectationsHelper("$[2].company").assertValue(json, "Microsoft");
    new JsonPathExpectationsHelper("$[3].company").assertValue(json, "Oracle");
}

From source file:com.company.project.config.PresenceChannelInterceptor.java

@Override
public void postSend(Message<?> message, MessageChannel channel, boolean sent) {

    StompHeaderAccessor sha = StompHeaderAccessor.wrap(message);

    // ignore non-STOMP messages like heartbeat messages
    if (sha.getCommand() == null) {
        return;//from   w w w .j  a  v  a2s .c om
    }

    String sessionId = sha.getSessionId();

    switch (sha.getCommand()) {
    case CONNECT:
        logger.debug("STOMP Connect [sessionId: " + sessionId + "]");
        break;
    case CONNECTED:
        logger.debug("STOMP Connected [sessionId: " + sessionId + "]");
        break;
    case DISCONNECT:
        logger.debug("STOMP Disconnect [sessionId: " + sessionId + "]");
        break;
    default:
        break;

    }
}

From source file:com.company.project.config.StompConnectEvent.java

@Override
public void onApplicationEvent(SessionConnectEvent event) {
    StompHeaderAccessor sha = StompHeaderAccessor.wrap(event.getMessage());

    String username = sha.getNativeHeader("username").get(0); // from jsp : stompClient.connect({username: "${pageContext.request.userPrincipal.name}"}, function(frame) {
    logger.debug("Connect event [sessionId: " + sha.getSessionId() + "; username: " + username + " ]");
    System.out.println("Connect event [sessionId: " + sha.getSessionId() + "; username: " + username + " ]");
}

From source file:org.springframework.samples.portfolio.web.context.ContextPortfolioControllerTests.java

@Test
public void getPositions() throws Exception {

    StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SUBSCRIBE);
    headers.setSubscriptionId("0");
    headers.setDestination("/app/positions");
    headers.setSessionId("0");
    headers.setUser(new TestPrincipal("fabrice"));
    headers.setSessionAttributes(new HashMap<String, Object>());
    Message<byte[]> message = MessageBuilder.createMessage(new byte[0], headers.getMessageHeaders());

    this.clientOutboundChannelInterceptor.setIncludedDestinations("/app/positions");
    this.clientOutboundChannelInterceptor.startRecording();

    this.clientInboundChannel.send(message);

    Message<?> reply = this.clientOutboundChannelInterceptor.awaitMessage(5);
    assertNotNull(reply);/*ww w. j  a v  a  2  s .  c  o m*/

    StompHeaderAccessor replyHeaders = StompHeaderAccessor.wrap(reply);
    assertEquals("0", replyHeaders.getSessionId());
    assertEquals("0", replyHeaders.getSubscriptionId());
    assertEquals("/app/positions", replyHeaders.getDestination());

    String json = new String((byte[]) reply.getPayload(), Charset.forName("UTF-8"));
    new JsonPathExpectationsHelper("$[0].company").assertValue(json, "Citrix Systems, Inc.");
    new JsonPathExpectationsHelper("$[1].company").assertValue(json, "Dell Inc.");
    new JsonPathExpectationsHelper("$[2].company").assertValue(json, "Microsoft");
    new JsonPathExpectationsHelper("$[3].company").assertValue(json, "Oracle");
}

From source file:com.company.project.config.StompDisconnectEvent.java

@Override
public void onApplicationEvent(SessionDisconnectEvent event) {
    StompHeaderAccessor sha = StompHeaderAccessor.wrap(event.getMessage());
    CloseStatus status = event.getCloseStatus();

    if (status.equals(CloseStatus.BAD_DATA)) {
        System.out.println("CloseStatus.BAD_DATA");
    }//from w  w  w .  j  a  va2  s. c  o  m
    if (status.equals(CloseStatus.GOING_AWAY)) {
        System.out.println("CloseStatus.GOING_AWAY");
    }
    if (status.equals(CloseStatus.NORMAL)) {
        System.out.println("CloseStatus.NORMAL");
    }
    if (status.equals(CloseStatus.NOT_ACCEPTABLE)) {
        System.out.println("CloseStatus.NOT_ACCEPTABLE");
    }
    if (status.equals(CloseStatus.NO_CLOSE_FRAME)) {
        System.out.println("CloseStatus.NO_CLOSE_FRAME");
    }
    if (status.equals(CloseStatus.NO_STATUS_CODE)) {
        System.out.println("CloseStatus.NO_STATUS_CODE");
    }
    if (status.equals(CloseStatus.POLICY_VIOLATION)) {
        System.out.println("CloseStatus.POLICY_VIOLATION");
    }
    if (status.equals(CloseStatus.PROTOCOL_ERROR)) {
        System.out.println("CloseStatus.PROTOCOL_ERROR");
    }
    if (status.equals(CloseStatus.REQUIRED_EXTENSION)) {
        System.out.println("CloseStatus.REQUIRED_EXTENSION");
    }
    if (status.equals(CloseStatus.SERVER_ERROR)) {
        System.out.println("CloseStatus.SERVER_ERROR");
    }
    if (status.equals(CloseStatus.SERVICE_RESTARTED)) {
        System.out.println("CloseStatus.SERVICE_RESTARTED");
    }
    if (status.equals(CloseStatus.SESSION_NOT_RELIABLE)) {
        System.out.println("CloseStatus.SESSION_NOT_RELIABLE");
    }
    if (status.equals(CloseStatus.TLS_HANDSHAKE_FAILURE)) {
        System.out.println("CloseStatus.TLS_HANDSHAKE_FAILURE");
    }
    if (status.equals(CloseStatus.TOO_BIG_TO_PROCESS)) {
        System.out.println("CloseStatus.TOO_BIG_TO_PROCESS");
    }

    System.out.println("CloseStatus: " + status);

    logger.debug("Disconnect event [sessionId: " + sha.getSessionId() + " ]");
    System.out.println("Disconnect event [sessionId: " + event.getSessionId() + " ]");
}

From source file:org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler.java

@Override
public void handleMessage(Message<?> message) {

    StompHeaderAccessor headers = StompHeaderAccessor.wrap(message);
    String sessionId = headers.getSessionId();
    String destination = headers.getDestination();
    StompCommand command = headers.getCommand();
    SimpMessageType messageType = headers.getMessageType();

    if (!this.running) {
        if (logger.isTraceEnabled()) {
            logger.trace("STOMP broker relay not running. Ignoring message id=" + headers.getId());
        }/*from w  ww .  j  av a2  s .  co  m*/
        return;
    }

    if (SimpMessageType.MESSAGE.equals(messageType)) {
        sessionId = (sessionId == null) ? STOMP_RELAY_SYSTEM_SESSION_ID : sessionId;
        headers.setSessionId(sessionId);
        command = (command == null) ? StompCommand.SEND : command;
        headers.setCommandIfNotSet(command);
        message = MessageBuilder.withPayloadAndHeaders(message.getPayload(), headers).build();
    }

    if (headers.getCommand() == null) {
        logger.error("Ignoring message, no STOMP command: " + message);
        return;
    }
    if (sessionId == null) {
        logger.error("Ignoring message, no sessionId: " + message);
        return;
    }

    try {
        if (checkDestinationPrefix(command, destination)) {

            if (logger.isTraceEnabled()) {
                logger.trace("Processing message: " + message);
            }

            if (SimpMessageType.CONNECT.equals(messageType)) {
                headers.setHeartbeat(0, 0); // TODO: disable for now
                message = MessageBuilder.withPayloadAndHeaders(message.getPayload(), headers).build();
                RelaySession session = new RelaySession(sessionId);
                this.relaySessions.put(sessionId, session);
                session.open(message);
            } else if (SimpMessageType.DISCONNECT.equals(messageType)) {
                RelaySession session = this.relaySessions.remove(sessionId);
                if (session == null) {
                    if (logger.isTraceEnabled()) {
                        logger.trace("Session already removed, sessionId=" + sessionId);
                    }
                    return;
                }
                session.forward(message);
            } else {
                RelaySession session = this.relaySessions.get(sessionId);
                if (session == null) {
                    logger.warn("Session id=" + sessionId + " not found. Ignoring message: " + message);
                    return;
                }
                session.forward(message);
            }
        }
    } catch (Throwable t) {
        logger.error("Failed to handle message " + message, t);
    }
}

From source file:org.springframework.messaging.simp.stomp.StompProtocolHandler.java

@Override
public String resolveSessionId(Message<?> message) {
    StompHeaderAccessor headers = StompHeaderAccessor.wrap(message);
    return headers.getSessionId();
}

From source file:org.springframework.messaging.simp.stomp.StompWebSocketHandler.java

/**
 * Handle STOMP messages going back out to WebSocket clients.
 *//*  w  w w.java  2 s  .  c  om*/
@Override
public void handleMessage(Message<?> message) {

    StompHeaderAccessor headers = StompHeaderAccessor.wrap(message);
    headers.setCommandIfNotSet(StompCommand.MESSAGE);

    if (StompCommand.CONNECTED.equals(headers.getCommand())) {
        // Ignore for now since we already sent it
        return;
    }

    String sessionId = headers.getSessionId();
    if (sessionId == null) {
        // TODO: failed message delivery mechanism
        logger.error("Ignoring message, no sessionId header: " + message);
        return;
    }

    WebSocketSession session = this.sessions.get(sessionId);
    if (session == null) {
        // TODO: failed message delivery mechanism
        logger.error("Ignoring message, sessionId not found: " + message);
        return;
    }

    if (StompCommand.MESSAGE.equals(headers.getCommand()) && (headers.getSubscriptionId() == null)) {
        // TODO: failed message delivery mechanism
        logger.error("Ignoring message, no subscriptionId header: " + message);
        return;
    }

    if (!(message.getPayload() instanceof byte[])) {
        // TODO: failed message delivery mechanism
        logger.error("Ignoring message, expected byte[] content: " + message);
        return;
    }

    try {
        message = MessageBuilder.withPayloadAndHeaders(message.getPayload(), headers).build();
        byte[] bytes = this.stompMessageConverter.fromMessage(message);
        session.sendMessage(new TextMessage(new String(bytes, Charset.forName("UTF-8"))));
    } catch (Throwable t) {
        sendErrorMessage(session, t);
    } finally {
        if (StompCommand.ERROR.equals(headers.getCommand())) {
            try {
                session.close(CloseStatus.PROTOCOL_ERROR);
            } catch (IOException e) {
            }
        }
    }
}