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

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

Introduction

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

Prototype

@Nullable
    public String getDestination() 

Source Link

Usage

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;/* www.j a  va 2 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 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  ww .j  a v  a 2s .c  om
    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

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.jav  a  2  s  .  c om*/
    }

    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 .jav  a2 s . c om*/

    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.DefaultUserDestinationResolver.java

private UserDestinationInfo getUserDestinationInfo(SimpMessageHeaderAccessor headers) {

    String destination = headers.getDestination();

    String targetUser;/* w w w . j  a va  2 s.  com*/
    String targetDestination;

    Principal user = headers.getUser();
    SimpMessageType messageType = headers.getMessageType();

    if (SimpMessageType.SUBSCRIBE.equals(messageType) || SimpMessageType.UNSUBSCRIBE.equals(messageType)) {
        if (!checkDestination(destination, this.subscriptionDestinationPrefix)) {
            return null;
        }
        if (user == null) {
            logger.warn("Ignoring message, no user information");
            return null;
        }
        targetUser = user.getName();
        targetDestination = destination.substring(this.destinationPrefix.length() - 1);
    } else if (SimpMessageType.MESSAGE.equals(messageType)) {
        if (!checkDestination(destination, this.destinationPrefix)) {
            return null;
        }
        int startIndex = this.destinationPrefix.length();
        int endIndex = destination.indexOf('/', startIndex);
        Assert.isTrue(endIndex > 0, "Expected destination pattern \"/user/{userId}/**\"");
        targetUser = destination.substring(startIndex, endIndex);
        targetDestination = destination.substring(endIndex);

    } else {
        if (logger.isTraceEnabled()) {
            logger.trace("Ignoring " + messageType + " message");
        }
        return null;
    }

    return new UserDestinationInfo(targetUser, targetDestination);
}

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;/*from  w  w  w .  ja  v  a  2  s  .c om*/
    }

    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.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  a2s .c  om*/
    }

    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);
    }
}