Example usage for org.springframework.messaging.simp SimpMessageType SUBSCRIBE

List of usage examples for org.springframework.messaging.simp SimpMessageType SUBSCRIBE

Introduction

In this page you can find the example usage for org.springframework.messaging.simp SimpMessageType SUBSCRIBE.

Prototype

SimpMessageType SUBSCRIBE

To view the source code for org.springframework.messaging.simp SimpMessageType SUBSCRIBE.

Click Source Link

Usage

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

@Override
public final void registerSubscription(Message<?> message) {
    MessageHeaders headers = message.getHeaders();

    SimpMessageType messageType = SimpMessageHeaderAccessor.getMessageType(headers);
    if (!SimpMessageType.SUBSCRIBE.equals(messageType)) {
        throw new IllegalArgumentException("Expected SUBSCRIBE: " + message);
    }//from   w  ww . j  a v  a  2s.  c  om

    String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
    if (sessionId == null) {
        if (logger.isErrorEnabled()) {
            logger.error("No sessionId in  " + message);
        }
        return;
    }

    String subscriptionId = SimpMessageHeaderAccessor.getSubscriptionId(headers);
    if (subscriptionId == null) {
        if (logger.isErrorEnabled()) {
            logger.error("No subscriptionId in " + message);
        }
        return;
    }

    String destination = SimpMessageHeaderAccessor.getDestination(headers);
    if (destination == null) {
        if (logger.isErrorEnabled()) {
            logger.error("No destination in " + message);
        }
        return;
    }

    addSubscriptionInternal(sessionId, subscriptionId, destination, 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 w w.ja  v  a  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;
    }
    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.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);
    }/*from   ww w  . j  av  a  2s  .  c  o  m*/
}

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

private UserDestinationInfo getUserDestinationInfo(SimpMessageHeaderAccessor headers) {

    String destination = headers.getDestination();

    String targetUser;//w  ww .ja  v a2 s. c  o  m
    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;/*  www.  j a  va2s. 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.security.config.websocket.WebSocketMessageBrokerSecurityBeanDefinitionParser.java

private BeanDefinition createMatcher(String matcherPattern, String messageType, ParserContext parserContext,
        Element interceptMessage) {
    boolean hasPattern = StringUtils.hasText(matcherPattern);
    boolean hasMessageType = StringUtils.hasText(messageType);
    if (!hasPattern) {
        BeanDefinitionBuilder matcher = BeanDefinitionBuilder.rootBeanDefinition(SimpMessageTypeMatcher.class);
        matcher.addConstructorArgValue(messageType);
        return matcher.getBeanDefinition();
    }/*from   w w w. j a  v a  2s  . c  o m*/

    String factoryName = null;
    if (hasPattern && hasMessageType) {
        SimpMessageType type = SimpMessageType.valueOf(messageType);
        if (SimpMessageType.MESSAGE == type) {
            factoryName = "createMessageMatcher";
        } else if (SimpMessageType.SUBSCRIBE == type) {
            factoryName = "createSubscribeMatcher";
        } else {
            parserContext.getReaderContext()
                    .error("Cannot use intercept-websocket@message-type=" + messageType
                            + " with a pattern because the type does not have a destination.",
                            interceptMessage);
        }
    }

    BeanDefinitionBuilder matcher = BeanDefinitionBuilder
            .rootBeanDefinition(SimpDestinationMessageMatcher.class);
    matcher.setFactoryMethod(factoryName);
    matcher.addConstructorArgValue(matcherPattern);
    matcher.addConstructorArgValue(new RuntimeBeanReference("springSecurityMessagePathMatcher"));
    return matcher.getBeanDefinition();
}