Example usage for org.springframework.messaging.simp SimpMessageHeaderAccessor wrap

List of usage examples for org.springframework.messaging.simp SimpMessageHeaderAccessor wrap

Introduction

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

Prototype

public static SimpMessageHeaderAccessor wrap(Message<?> message) 

Source Link

Document

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

Usage

From source file:org.springframework.cloud.stream.app.websocket.sink.WebsocketSinkConfiguration.java

@ServiceActivator(inputChannel = Sink.INPUT)
public void websocketSink(Message<?> message) {
    if (logger.isTraceEnabled()) {
        logger.trace(String.format("Handling message: %s", message));
    }//from   w ww .  j  av a  2 s.  c o m

    SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
    headers.setMessageTypeIfNotSet(SimpMessageType.MESSAGE);
    String messagePayload = message.getPayload().toString();
    for (Channel channel : WebsocketSinkServer.channels) {
        if (logger.isTraceEnabled()) {
            logger.trace(
                    String.format("Writing message %s to channel %s", messagePayload, channel.localAddress()));
        }

        channel.write(new TextWebSocketFrame(messagePayload));
        channel.flush();
    }

    if (traceEndpointEnabled) {
        addMessageToTraceRepository(message);
    }
}

From source file:org.springframework.messaging.simp.handler.AbstractSubscriptionRegistry.java

@Override
public final void registerSubscription(Message<?> message) {
    SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
    if (!SimpMessageType.SUBSCRIBE.equals(headers.getMessageType())) {
        logger.error("Expected SUBSCRIBE message: " + message);
        return;/* w ww . j av a2 s .c  om*/
    }
    String sessionId = headers.getSessionId();
    if (sessionId == null) {
        logger.error("Ignoring subscription. No sessionId in message: " + message);
        return;
    }
    String subscriptionId = headers.getSubscriptionId();
    if (subscriptionId == null) {
        logger.error("Ignoring subscription. No subscriptionId in message: " + message);
        return;
    }
    String destination = headers.getDestination();
    if (destination == null) {
        logger.error("Ignoring destination. No destination in message: " + message);
        return;
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Subscribe request: " + message);
    }
    addSubscriptionInternal(sessionId, subscriptionId, destination, message);
}

From source file:org.springframework.messaging.simp.handler.AbstractSubscriptionRegistry.java

@Override
public final void unregisterSubscription(Message<?> message) {
    SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
    if (!SimpMessageType.UNSUBSCRIBE.equals(headers.getMessageType())) {
        logger.error("Expected UNSUBSCRIBE message: " + message);
        return;/*from w ww  . ja  va 2 s  .c o m*/
    }
    String sessionId = headers.getSessionId();
    if (sessionId == null) {
        logger.error("Ignoring subscription. No sessionId in message: " + message);
        return;
    }
    String subscriptionId = headers.getSubscriptionId();
    if (subscriptionId == null) {
        logger.error("Ignoring subscription. No subscriptionId in message: " + message);
        return;
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Unubscribe request: " + message);
    }
    removeSubscriptionInternal(sessionId, subscriptionId, message);
}

From source file:org.springframework.messaging.simp.handler.AbstractSubscriptionRegistry.java

@Override
public final MultiValueMap<String, String> findSubscriptions(Message<?> message) {
    SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
    if (!SimpMessageType.MESSAGE.equals(headers.getMessageType())) {
        logger.error("Unexpected message type: " + message);
        return null;
    }//from   w w  w.j  av  a  2s .c o  m
    String destination = headers.getDestination();
    if (destination == null) {
        logger.error("Ignoring destination. No destination in message: " + message);
        return null;
    }
    if (logger.isTraceEnabled()) {
        logger.trace("Find subscriptions, destination=" + headers.getDestination());
    }
    return findSubscriptionsInternal(destination, message);
}

From source file:org.springframework.messaging.simp.handler.AnnotationMethodMessageHandler.java

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

    SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
    SimpMessageType messageType = headers.getMessageType();

    if (SimpMessageType.MESSAGE.equals(messageType)) {
        handleMessageInternal(message, this.messageMethods);
    } else if (SimpMessageType.SUBSCRIBE.equals(messageType)) {
        handleMessageInternal(message, this.subscribeMethods);
    } else if (SimpMessageType.UNSUBSCRIBE.equals(messageType)) {
        handleMessageInternal(message, this.unsubscribeMethods);
    }// ww  w  .j a  v  a 2 s  . c  o m
}

From source file:org.springframework.messaging.simp.handler.AnnotationMethodMessageHandler.java

private void handleMessageInternal(final Message<?> message, Map<MappingInfo, HandlerMethod> handlerMethods) {

    SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
    String destination = headers.getDestination();

    if (!checkDestinationPrefix(destination)) {
        return;/*from   w  w  w. j  av  a 2  s.co  m*/
    }

    HandlerMethod match = getHandlerMethod(destination, handlerMethods);
    if (match == null) {
        return;
    }

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

    HandlerMethod handlerMethod = match.createWithResolvedBean();

    InvocableHandlerMethod invocableHandlerMethod = new InvocableHandlerMethod(handlerMethod);
    invocableHandlerMethod.setMessageMethodArgumentResolvers(this.argumentResolvers);

    try {
        Object returnValue = invocableHandlerMethod.invoke(message);

        MethodParameter returnType = handlerMethod.getReturnType();
        if (void.class.equals(returnType.getParameterType())) {
            return;
        }
        this.returnValueHandlers.handleReturnValue(returnValue, returnType, message);
    } catch (Exception ex) {
        invokeExceptionHandler(message, handlerMethod, ex);
    } catch (Throwable ex) {
        // TODO
        ex.printStackTrace();
    }
}

From source file:org.springframework.messaging.simp.handler.DefaultUserDestinationResolver.java

@Override
public Set<String> resolveDestination(Message<?> message) {

    SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
    UserDestinationInfo info = getUserDestinationInfo(headers);
    if (info == null) {
        return Collections.emptySet();
    }/*from   w  w  w.  j a  v a 2 s.c o m*/

    Set<String> set = new HashSet<String>();
    for (String sessionId : this.userSessionRegistry.getSessionIds(info.getUser())) {
        set.add(getTargetDestination(headers.getDestination(), info.getDestination(), sessionId,
                info.getUser()));
    }
    return set;
}

From source file:org.springframework.messaging.simp.handler.SimpleBrokerMessageHandler.java

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

    SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
    SimpMessageType messageType = headers.getMessageType();
    String destination = headers.getDestination();

    if (!checkDestinationPrefix(destination)) {
        return;// w w  w  .j  a va 2s . c o  m
    }

    if (SimpMessageType.SUBSCRIBE.equals(messageType)) {
        preProcessMessage(message);
        this.subscriptionRegistry.registerSubscription(message);
    } else if (SimpMessageType.UNSUBSCRIBE.equals(messageType)) {
        preProcessMessage(message);
        this.subscriptionRegistry.unregisterSubscription(message);
    } else if (SimpMessageType.MESSAGE.equals(messageType)) {
        preProcessMessage(message);
        sendMessageToSubscribers(headers.getDestination(), message);
    } else if (SimpMessageType.DISCONNECT.equals(messageType)) {
        preProcessMessage(message);
        String sessionId = SimpMessageHeaderAccessor.wrap(message).getSessionId();
        this.subscriptionRegistry.unregisterAllSubscriptions(sessionId);
    }
}

From source file:org.springframework.messaging.simp.handler.SimpleBrokerMessageHandler.java

protected void sendMessageToSubscribers(String destination, Message<?> message) {
    MultiValueMap<String, String> subscriptions = this.subscriptionRegistry.findSubscriptions(message);
    for (String sessionId : subscriptions.keySet()) {
        for (String subscriptionId : subscriptions.get(sessionId)) {

            SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
            headers.setSessionId(sessionId);
            headers.setSubscriptionId(subscriptionId);

            Object payload = message.getPayload();
            Message<?> clientMessage = MessageBuilder.withPayloadAndHeaders(payload, headers).build();
            try {
                this.messageChannel.send(clientMessage);
            } catch (Throwable ex) {
                logger.error("Failed to send message to destination=" + destination + ", sessionId=" + sessionId
                        + ", subscriptionId=" + subscriptionId, ex);
            }/*from w  w w. j  av a  2  s.  co  m*/
        }
    }
}

From source file:org.springframework.messaging.simp.handler.UserDestinationMessageHandler.java

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

    SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
    SimpMessageType messageType = headers.getMessageType();
    String destination = headers.getDestination();

    if (!SimpMessageType.MESSAGE.equals(messageType)) {
        return;//from w w  w. j  av  a  2 s .co m
    }

    if (!checkDestination(destination)) {
        return;
    }

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

    UserDestinationParser destinationParser = new UserDestinationParser(destination);
    String user = destinationParser.getUser();

    if (user == null) {
        if (logger.isErrorEnabled()) {
            logger.error("Ignoring message, expected destination pattern \"" + this.destinationPrefix
                    + "{userId}/**\": " + destination);
        }
        return;
    }

    for (String sessionId : this.userQueueSuffixResolver.getUserQueueSuffixes(user)) {

        String targetDestination = destinationParser.getTargetDestination(sessionId);
        headers.setDestination(targetDestination);
        message = MessageBuilder.withPayloadAndHeaders(message.getPayload(), headers).build();

        if (logger.isTraceEnabled()) {
            logger.trace("Sending message to resolved target destination " + targetDestination);
        }
        this.messagingTemplate.send(targetDestination, message);
    }
}