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

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

Introduction

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

Prototype

public static StompHeaderAccessor wrap(Message<?> message) 

Source Link

Document

Create an instance from the payload and headers of the given Message.

Usage

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);//ww w  .  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: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  ww w . j  av a2s .  com
        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:com.wk.lodge.composite.web.tomcat.IntegrationCompositeTests.java

@Test
public void testJoin() 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  ww  w. j  a  v  a 2s .c o m*/
        public void afterConnected(StompSession stompSession, StompHeaderAccessor headers) {
            this.stompSession = stompSession;
            this.stompSession.subscribe("/user/queue/device", null);

            try {
                JoinMessage join = new JoinMessage();
                Device d = new Device();
                d.setUuid(UUID.randomUUID());
                join.setDevice(d);
                join.setGeo(new float[] { lat, lon });
                join.setType(JoinMessage.Types.exit);
                join.setPoint(new int[] { 0, 0 });
                join.setVector(new float[] { 1, 1 });

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

        @Override
        public void handleMessage(Message<byte[]> message) {
            try {
                String json = parseMessageJson(message);
                new JsonPathExpectationsHelper("devices").exists(json);
                new JsonPathExpectationsHelper("devices").assertValueIsArray(json);
                new JsonPathExpectationsHelper("type").assertValue(json, "join");
            } 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("Join response not received");
    } else if (failure.get() != null) {
        throw new AssertionError("", failure.get());
    }
}

From source file:com.wk.lodge.composite.web.tomcat.IntegrationCompositeTests.java

@Test
public void testPair() 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 w  w  .j  av a  2 s. c o  m*/
        public void afterConnected(StompSession stompSession, StompHeaderAccessor headers) {

            this.stompSession = stompSession;
            this.stompSession.subscribe("/user/queue/device", null);

            try {
                PairMessage pair = new PairMessage();
                Device d = new Device();
                d.setUuid(UUID.randomUUID());
                pair.setDevice(d);
                pair.setType(PairMessage.Types.exit);
                pair.setGeo(new float[] { lat, lon });
                pair.setPoint(new int[] { 0, 0 });
                pair.setVector(new float[] { 1, 1 });

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

        @Override
        public void handleMessage(Message<byte[]> message) {
            try {
                String json = parseMessageJson(message);
                new JsonPathExpectationsHelper("devices").exists(json);
                new JsonPathExpectationsHelper("devices").assertValueIsArray(json);
                new JsonPathExpectationsHelper("type").assertValue(json, "pair");
            } 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("Pair response not received");
    } else if (failure.get() != null) {
        throw new AssertionError("", failure.get());
    }

}

From source file:com.wk.lodge.composite.web.tomcat.IntegrationCompositeTests.java

@Test
public void testDevices() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> failure = new AtomicReference<>();

    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   www  .  ja  v  a  2 s. c om*/
        public void afterConnected(StompSession stompSession, StompHeaderAccessor headers) {
            this.stompSession = stompSession;
            String topicUuid = simulateJoinEvent();

            // Call again to get a second device in the session
            simulateJoinEvent();

            this.stompSession.subscribe("/user/queue/device", null);
            this.stompSession.subscribe(String.format("/topic/%s", topicUuid), null);

            try {
                // send a message to the devices endpoint
                HashMap<String, Object> devices = new HashMap<String, Object>();
                devices.put("type", "devices");
                this.stompSession.send(String.format("/app/%s", topicUuid), devices);
            } catch (Throwable t) {
                failure.set(t);
                latch.countDown();
            }
        }

        @Override
        public void handleMessage(Message<byte[]> message) {
            try {
                String json = parseMessageJson(message);
                new JsonPathExpectationsHelper("devices").exists(json);
                new JsonPathExpectationsHelper("type").exists(json);
                new JsonPathExpectationsHelper("type").assertValue(json, "devices");
                new JsonPathExpectationsHelper("serverTime").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("Devices response not received");
    } else if (failure.get() != null) {
        throw new AssertionError("", failure.get());
    }
}

From source file:com.wk.lodge.composite.web.tomcat.IntegrationCompositeTests.java

@Test
public void testSync() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> failure = new AtomicReference<>();

    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  va2  s . c o m
        public void afterConnected(StompSession stompSession, StompHeaderAccessor headers) {
            this.stompSession = stompSession;
            this.stompSession.subscribe("/user/queue/device", null);

            try {
                SyncMessage sync = new SyncMessage();
                sync.setType("sync");
                sync.setTime(new Date().getTime());
                stompSession.send("/app/sync", sync);
            } catch (Throwable t) {
                failure.set(t);
                latch.countDown();
            }
        }

        @Override
        public void handleMessage(Message<byte[]> message) {
            try {
                String json = parseMessageJson(message);
                new JsonPathExpectationsHelper("type").assertValue(json, "sync");
                new JsonPathExpectationsHelper("time").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("Sync response not received");
    } else if (failure.get() != null) {
        throw new AssertionError("", failure.get());
    }

}

From source file:com.wk.lodge.composite.web.tomcat.IntegrationCompositeTests.java

@Test
public void testStart() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> failure = new AtomicReference<>();

    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  w w . j ava2s .  com
        public void afterConnected(StompSession stompSession, StompHeaderAccessor headers) {
            this.stompSession = stompSession;
            String topicUuid = simulateJoinEvent();

            this.stompSession.subscribe("/user/queue/device", null);
            this.stompSession.subscribe(String.format("/topic/%s", topicUuid), null);

            try {
                // send a message to the start endpoint
                HashMap<String, Object> start = new HashMap<String, Object>();
                start.put("data", "Some Data");
                start.put("type", "start");
                this.stompSession.send(String.format("/app/%s", topicUuid), start);
            } catch (Throwable t) {
                failure.set(t);
                latch.countDown();
            }
        }

        @Override
        public void handleMessage(Message<byte[]> message) {
            try {
                String json = parseMessageJson(message);
                new JsonPathExpectationsHelper("type").exists(json);
                new JsonPathExpectationsHelper("type").assertValue(json, "start");
                new JsonPathExpectationsHelper("serverTime").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("Start response not received");
    } else if (failure.get() != null) {
        throw new AssertionError("", failure.get());
    }
}

From source file:com.wk.lodge.composite.web.tomcat.IntegrationCompositeTests.java

@Test
public void testStop() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> failure = new AtomicReference<>();

    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   ww w. j  a  v  a2  s .c  om
        public void afterConnected(StompSession stompSession, StompHeaderAccessor headers) {
            this.stompSession = stompSession;
            String topicUuid = simulateJoinEvent();

            this.stompSession.subscribe("/user/queue/device", null);
            this.stompSession.subscribe(String.format("/topic/%s", topicUuid), null);

            try {
                HashMap<String, Object> stop = new HashMap<String, Object>();
                stop.put("type", "stop");
                this.stompSession.send(String.format("/app/%s", topicUuid), stop);

            } catch (Throwable t) {
                failure.set(t);
                latch.countDown();
            }
        }

        @Override
        public void handleMessage(Message<byte[]> message) {
            try {
                String json = parseMessageJson(message);
                new JsonPathExpectationsHelper("type").exists(json);
                new JsonPathExpectationsHelper("type").assertValue(json, "stop");
                new JsonPathExpectationsHelper("serverTime").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("Stop response not received");
    } else if (failure.get() != null) {
        throw new AssertionError("", failure.get());
    }

}

From source file:com.wk.lodge.composite.web.tomcat.IntegrationCompositeTests.java

@Test
public void testUpdate() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> failure = new AtomicReference<>();

    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 av a  2s  .c om*/
        public void afterConnected(StompSession stompSession, StompHeaderAccessor headers) {
            this.stompSession = stompSession;
            String topicUuid = simulateJoinEvent();

            this.stompSession.subscribe("/user/queue/device", null);
            this.stompSession.subscribe(String.format("/topic/%s", topicUuid), null);

            try {
                HashMap<String, Object> update = new HashMap<String, Object>();
                update.put("data", "TEST-UPDATE Data");
                update.put("type", "update");
                this.stompSession.send(String.format("/app/%s", topicUuid), update);
            } catch (Throwable t) {
                failure.set(t);
                latch.countDown();
            }

        }

        @Override
        public void handleMessage(Message<byte[]> message) throws MessagingException {
            try {
                String json = parseMessageJson(message);
                new JsonPathExpectationsHelper("type").exists(json);
                new JsonPathExpectationsHelper("type").assertValue(json, "update");
                new JsonPathExpectationsHelper("serverTime").exists(json);
                new JsonPathExpectationsHelper("data").assertValue(json, "TEST-UPDATE Data");
            } 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("Update response not received");
    } else if (failure.get() != null) {
        throw new AssertionError("", failure.get());
    }
}

From source file:com.wk.lodge.composite.web.tomcat.IntegrationCompositeTests.java

@Test
public void testData() throws Exception {

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

    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 w w .  j ava 2s . c o m
        public void afterConnected(StompSession stompSession, StompHeaderAccessor headers) {
            this.stompSession = stompSession;
            String topicUuid = simulateJoinEvent();

            this.stompSession.subscribe("/user/queue/device", null);
            this.stompSession.subscribe(String.format("/topic/%s", topicUuid), null);

            try {
                // send a simple hashmap to the data endpoint
                HashMap<String, Object> data = new HashMap<String, Object>();
                data.put("data", "TEST-DATA Data");
                data.put("type", "data");
                this.stompSession.send(String.format("/app/%s", topicUuid), data);
            } catch (Throwable t) {
                failure.set(t);
                latch.countDown();
            }
        }

        @Override
        public void handleMessage(Message<byte[]> message) {
            try {
                String json = parseMessageJson(message);
                new JsonPathExpectationsHelper("type").exists(json);
                new JsonPathExpectationsHelper("type").assertValue(json, "data");
                new JsonPathExpectationsHelper("serverTime").exists(json);
                new JsonPathExpectationsHelper("data").assertValue(json, "TEST-DATA Data");
            } 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("Data response not received");
    } else if (failure.get() != null) {
        throw new AssertionError("", failure.get());
    }
}