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

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

Introduction

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

Prototype

@Nullable
    public String getDestination() 

Source Link

Usage

From source file:org.appverse.web.framework.backend.frontfacade.websocket.TestChannelInterceptor.java

@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
    if (this.destinationPatterns.isEmpty()) {
        this.messages.add(message);
    } else {/*  w  w w  .j ava  2s  .com*/
        StompHeaderAccessor headers = StompHeaderAccessor.wrap(message);
        if (headers.getDestination() != null) {
            for (String pattern : this.destinationPatterns) {
                if (this.matcher.match(pattern, headers.getDestination())) {
                    this.messages.add(message);
                    break;
                }
            }
        }
    }
    return message;
}

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

@Test
public void executeTrade() throws Exception {

    Trade trade = new Trade();
    trade.setAction(Trade.TradeAction.Buy);
    trade.setTicker("DELL");
    trade.setShares(25);/*from  w ww .  j  a  v  a  2  s. c  o m*/

    byte[] payload = new ObjectMapper().writeValueAsBytes(trade);

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

    this.brokerChannelInterceptor.setIncludedDestinations("/user/**");
    this.brokerChannelInterceptor.startRecording();

    this.clientInboundChannel.send(message);

    Message<?> positionUpdate = this.brokerChannelInterceptor.awaitMessage(5);
    assertNotNull(positionUpdate);

    StompHeaderAccessor positionUpdateHeaders = StompHeaderAccessor.wrap(positionUpdate);
    assertEquals("/user/fabrice/queue/position-updates", positionUpdateHeaders.getDestination());

    String json = new String((byte[]) positionUpdate.getPayload(), Charset.forName("UTF-8"));
    new JsonPathExpectationsHelper("$.ticker").assertValue(json, "DELL");
    new JsonPathExpectationsHelper("$.shares").assertValue(json, 75);
}

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: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);/* w w  w. jav a 2  s. com*/

    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.wk.lodge.composite.web.tomcat.IntegrationCompositeTests.java

@Test
public void testInit() throws Exception {

    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> failure = new AtomicReference<Throwable>();

    URI uri = new URI("ws://localhost:" + port + "/composite");
    WebSocketStompClient stompClient = new WebSocketStompClient(uri, this.headers, sockJsClient);
    stompClient.setMessageConverter(new MappingJackson2MessageConverter());

    stompClient.connect(new StompMessageHandler() {

        private StompSession stompSession;

        @Override//from w ww .j a  va2s. c o m
        public void afterConnected(StompSession stompSession, StompHeaderAccessor headers) {
            this.stompSession = stompSession;
            this.stompSession.subscribe("/user/queue/device", null);

            try {
                this.stompSession.send("/app/init", "");
            } catch (Throwable t) {
                failure.set(t);
                latch.countDown();
            }
        }

        @Override
        public void handleMessage(Message<byte[]> message) {
            StompHeaderAccessor headers = StompHeaderAccessor.wrap(message);
            if (!"/user/queue/device".equals(headers.getDestination())) {
                failure.set(new IllegalStateException("Unexpected message: " + message));
            }
            logger.debug("Got " + new String((byte[]) message.getPayload()));
            try {
                String json = parseMessageJson(message);
                new JsonPathExpectationsHelper("type").assertValue(json, "init");
                new JsonPathExpectationsHelper("uuid").exists(json);
            } catch (Throwable t) {
                failure.set(t);
            } finally {
                this.stompSession.disconnect();
                latch.countDown();
            }
        }

        @Override
        public void handleError(Message<byte[]> message) {
            StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message);
            String error = "[Producer] " + accessor.getShortLogMessage(message.getPayload());
            logger.error(error);
            failure.set(new Exception(error));
        }

        @Override
        public void handleReceipt(String receiptId) {
        }

        @Override
        public void afterDisconnected() {
        }
    });

    if (!latch.await(10, TimeUnit.SECONDS)) {
        fail("Init response not received");
    } else if (failure.get() != null) {
        throw new AssertionError("", failure.get());
    }
}

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 ww w . j a v  a2 s.com*/
        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);
    }
}