List of usage examples for org.springframework.messaging.simp.stomp StompHeaderAccessor setSessionId
public void setSessionId(@Nullable String sessionId)
From source file:org.springframework.samples.portfolio.web.standalone.StandalonePortfolioControllerTests.java
@Test public void executeTrade() throws Exception { Trade trade = new Trade(); trade.setAction(Trade.TradeAction.Buy); trade.setTicker("DELL"); trade.setShares(25);/*from w w 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.withPayload(payload).setHeaders(headers).build(); this.annotationMethodMessageHandler.handleMessage(message); assertEquals(1, this.tradeService.getTrades().size()); Trade actual = this.tradeService.getTrades().get(0); assertEquals(Trade.TradeAction.Buy, actual.getAction()); assertEquals("DELL", actual.getTicker()); assertEquals(25, actual.getShares()); assertEquals("fabrice", actual.getUsername()); }
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 executeTrade() throws Exception { Trade trade = new Trade(); trade.setAction(Trade.TradeAction.Buy); trade.setTicker("DELL"); trade.setShares(25);/*from w w w. j av a 2s. 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.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);//from w ww.j av a2 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:org.springframework.messaging.simp.stomp.DefaultStompSession.java
private StompHeaderAccessor createHeaderAccessor(StompCommand command) { StompHeaderAccessor accessor = StompHeaderAccessor.create(command); accessor.setSessionId(this.sessionId); accessor.setLeaveMutable(true);//from w w w.j a va 2s . c om return accessor; }
From source file:org.springframework.messaging.simp.stomp.DefaultStompSession.java
@Override public void handleMessage(Message<byte[]> message) { StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class); Assert.state(accessor != null, "No StompHeaderAccessor"); accessor.setSessionId(this.sessionId); StompCommand command = accessor.getCommand(); Map<String, List<String>> nativeHeaders = accessor.getNativeHeaders(); StompHeaders stompHeaders = StompHeaders.readOnlyStompHeaders(nativeHeaders); boolean isHeartbeat = accessor.isHeartbeat(); if (logger.isTraceEnabled()) { logger.trace("Received " + accessor.getDetailedLogMessage(message.getPayload())); }//from w w w. j a v a 2 s . c o m try { if (StompCommand.MESSAGE.equals(command)) { DefaultSubscription subscription = this.subscriptions.get(stompHeaders.getSubscription()); if (subscription != null) { invokeHandler(subscription.getHandler(), message, stompHeaders); } else if (logger.isDebugEnabled()) { logger.debug("No handler for: " + accessor.getDetailedLogMessage(message.getPayload()) + ". Perhaps just unsubscribed?"); } } else { if (StompCommand.RECEIPT.equals(command)) { String receiptId = stompHeaders.getReceiptId(); ReceiptHandler handler = this.receiptHandlers.get(receiptId); if (handler != null) { handler.handleReceiptReceived(); } else if (logger.isDebugEnabled()) { logger.debug( "No matching receipt: " + accessor.getDetailedLogMessage(message.getPayload())); } } else if (StompCommand.CONNECTED.equals(command)) { initHeartbeatTasks(stompHeaders); this.version = stompHeaders.getFirst("version"); this.sessionFuture.set(this); this.sessionHandler.afterConnected(this, stompHeaders); } else if (StompCommand.ERROR.equals(command)) { invokeHandler(this.sessionHandler, message, stompHeaders); } else if (!isHeartbeat && logger.isTraceEnabled()) { logger.trace("Message not handled."); } } } catch (Throwable ex) { this.sessionHandler.handleException(this, command, stompHeaders, message.getPayload(), ex); } }
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 w w .j a va 2 s . c om 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
/** * Handle incoming WebSocket messages from clients. *//*from w w w . ja va 2 s. c o m*/ public void handleMessageFromClient(WebSocketSession session, WebSocketMessage webSocketMessage, MessageChannel outputChannel) { try { Assert.isInstanceOf(TextMessage.class, webSocketMessage); String payload = ((TextMessage) webSocketMessage).getPayload(); Message<?> message = this.stompMessageConverter.toMessage(payload); // TODO: validate size limits // http://stomp.github.io/stomp-specification-1.2.html#Size_Limits if (logger.isTraceEnabled()) { logger.trace("Processing STOMP message: " + message); } try { StompHeaderAccessor headers = StompHeaderAccessor.wrap(message); headers.setSessionId(session.getId()); headers.setUser(session.getPrincipal()); message = MessageBuilder.withPayloadAndHeaders(message.getPayload(), headers).build(); if (SimpMessageType.CONNECT.equals(headers.getMessageType())) { handleConnect(session, message); } outputChannel.send(message); } catch (Throwable t) { logger.error("Terminating STOMP session due to failure to send message: ", t); sendErrorMessage(session, t); } // TODO: send RECEIPT message if incoming message has "receipt" header // http://stomp.github.io/stomp-specification-1.2.html#Header_receipt } catch (Throwable error) { sendErrorMessage(session, error); } }
From source file:org.springframework.messaging.simp.stomp.StompProtocolHandler.java
@Override public void afterSessionEnded(WebSocketSession session, CloseStatus closeStatus, MessageChannel outputChannel) { if ((this.queueSuffixResolver != null) && (session.getPrincipal() != null)) { this.queueSuffixResolver.removeQueueSuffix(session.getPrincipal().getName(), session.getId()); }//ww w . j a v a 2s .c o m StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.DISCONNECT); headers.setSessionId(session.getId()); Message<?> message = MessageBuilder.withPayloadAndHeaders(new byte[0], headers).build(); outputChannel.send(message); }
From source file:org.springframework.messaging.simp.stomp.StompWebSocketHandler.java
/** * Handle incoming WebSocket messages from clients. *//* w ww. j a v a 2s. c o m*/ @Override protected void handleTextMessage(WebSocketSession session, TextMessage textMessage) { try { String payload = textMessage.getPayload(); Message<?> message = this.stompMessageConverter.toMessage(payload); // TODO: validate size limits // http://stomp.github.io/stomp-specification-1.2.html#Size_Limits if (logger.isTraceEnabled()) { logger.trace("Processing STOMP message: " + message); } try { StompHeaderAccessor headers = StompHeaderAccessor.wrap(message); headers.setSessionId(session.getId()); headers.setUser(session.getPrincipal()); message = MessageBuilder.withPayloadAndHeaders(message.getPayload(), headers).build(); if (SimpMessageType.CONNECT.equals(headers.getMessageType())) { handleConnect(session, message); } this.dispatchChannel.send(message); } catch (Throwable t) { logger.error("Terminating STOMP session due to failure to send message: ", t); sendErrorMessage(session, t); } // TODO: send RECEIPT message if incoming message has "receipt" header // http://stomp.github.io/stomp-specification-1.2.html#Header_receipt } catch (Throwable error) { sendErrorMessage(session, error); } }