Search in sources :

Example 6 with SimpMessageType

use of org.springframework.messaging.simp.SimpMessageType in project spring-framework by spring-projects.

the class StompSubProtocolHandler method getStompHeaderAccessor.

private StompHeaderAccessor getStompHeaderAccessor(Message<?> message) {
    MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
    if (accessor instanceof StompHeaderAccessor) {
        return (StompHeaderAccessor) accessor;
    } else {
        StompHeaderAccessor stompAccessor = StompHeaderAccessor.wrap(message);
        SimpMessageType messageType = SimpMessageHeaderAccessor.getMessageType(message.getHeaders());
        if (SimpMessageType.CONNECT_ACK.equals(messageType)) {
            stompAccessor = convertConnectAcktoStompConnected(stompAccessor);
        } else if (SimpMessageType.DISCONNECT_ACK.equals(messageType)) {
            String receipt = getDisconnectReceipt(stompAccessor);
            if (receipt != null) {
                stompAccessor = StompHeaderAccessor.create(StompCommand.RECEIPT);
                stompAccessor.setReceiptId(receipt);
            } else {
                stompAccessor = StompHeaderAccessor.create(StompCommand.ERROR);
                stompAccessor.setMessage("Session closed.");
            }
        } else if (SimpMessageType.HEARTBEAT.equals(messageType)) {
            stompAccessor = StompHeaderAccessor.createForHeartbeat();
        } else if (stompAccessor.getCommand() == null || StompCommand.SEND.equals(stompAccessor.getCommand())) {
            stompAccessor.updateStompCommandAsServerMessage();
        }
        return stompAccessor;
    }
}
Also used : SimpMessageType(org.springframework.messaging.simp.SimpMessageType) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor)

Example 7 with SimpMessageType

use of org.springframework.messaging.simp.SimpMessageType in project spring-security by spring-projects.

the class WebSocketMessageBrokerSecurityBeanDefinitionParser method createMatcher.

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();
    }
    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();
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) SimpMessageType(org.springframework.messaging.simp.SimpMessageType) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 8 with SimpMessageType

use of org.springframework.messaging.simp.SimpMessageType in project spring-framework by spring-projects.

the class AbstractSubscriptionRegistry method registerSubscription.

@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);
    }
    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);
}
Also used : SimpMessageType(org.springframework.messaging.simp.SimpMessageType) MessageHeaders(org.springframework.messaging.MessageHeaders)

Example 9 with SimpMessageType

use of org.springframework.messaging.simp.SimpMessageType in project spring-framework by spring-projects.

the class SimpleBrokerMessageHandler method handleMessageInternal.

@Override
protected void handleMessageInternal(Message<?> message) {
    MessageHeaders headers = message.getHeaders();
    SimpMessageType messageType = SimpMessageHeaderAccessor.getMessageType(headers);
    String destination = SimpMessageHeaderAccessor.getDestination(headers);
    String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
    Principal user = SimpMessageHeaderAccessor.getUser(headers);
    updateSessionReadTime(sessionId);
    if (!checkDestinationPrefix(destination)) {
        return;
    }
    if (SimpMessageType.MESSAGE.equals(messageType)) {
        logMessage(message);
        sendMessageToSubscribers(destination, message);
    } else if (SimpMessageType.CONNECT.equals(messageType)) {
        logMessage(message);
        long[] clientHeartbeat = SimpMessageHeaderAccessor.getHeartbeat(headers);
        long[] serverHeartbeat = getHeartbeatValue();
        this.sessions.put(sessionId, new SessionInfo(sessionId, user, clientHeartbeat, serverHeartbeat));
        SimpMessageHeaderAccessor connectAck = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT_ACK);
        initHeaders(connectAck);
        connectAck.setSessionId(sessionId);
        connectAck.setUser(SimpMessageHeaderAccessor.getUser(headers));
        connectAck.setHeader(SimpMessageHeaderAccessor.CONNECT_MESSAGE_HEADER, message);
        connectAck.setHeader(SimpMessageHeaderAccessor.HEART_BEAT_HEADER, serverHeartbeat);
        Message<byte[]> messageOut = MessageBuilder.createMessage(EMPTY_PAYLOAD, connectAck.getMessageHeaders());
        getClientOutboundChannel().send(messageOut);
    } else if (SimpMessageType.DISCONNECT.equals(messageType)) {
        logMessage(message);
        handleDisconnect(sessionId, user, message);
    } else if (SimpMessageType.SUBSCRIBE.equals(messageType)) {
        logMessage(message);
        this.subscriptionRegistry.registerSubscription(message);
    } else if (SimpMessageType.UNSUBSCRIBE.equals(messageType)) {
        logMessage(message);
        this.subscriptionRegistry.unregisterSubscription(message);
    }
}
Also used : SimpMessageType(org.springframework.messaging.simp.SimpMessageType) Message(org.springframework.messaging.Message) MessageHeaders(org.springframework.messaging.MessageHeaders) Principal(java.security.Principal) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor)

Example 10 with SimpMessageType

use of org.springframework.messaging.simp.SimpMessageType in project spring-framework by spring-projects.

the class DefaultUserDestinationResolver method parse.

private ParseResult parse(Message<?> message) {
    MessageHeaders headers = message.getHeaders();
    String destination = SimpMessageHeaderAccessor.getDestination(headers);
    if (destination == null || !checkDestination(destination, this.prefix)) {
        return null;
    }
    SimpMessageType messageType = SimpMessageHeaderAccessor.getMessageType(headers);
    Principal principal = SimpMessageHeaderAccessor.getUser(headers);
    String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
    if (SimpMessageType.SUBSCRIBE.equals(messageType) || SimpMessageType.UNSUBSCRIBE.equals(messageType)) {
        if (sessionId == null) {
            logger.error("No session id. Ignoring " + message);
            return null;
        }
        int prefixEnd = this.prefix.length() - 1;
        String actualDestination = destination.substring(prefixEnd);
        if (!this.keepLeadingSlash) {
            actualDestination = actualDestination.substring(1);
        }
        String user = (principal != null ? principal.getName() : null);
        return new ParseResult(actualDestination, destination, Collections.singleton(sessionId), user);
    } else if (SimpMessageType.MESSAGE.equals(messageType)) {
        int prefixEnd = this.prefix.length();
        int userEnd = destination.indexOf('/', prefixEnd);
        Assert.isTrue(userEnd > 0, "Expected destination pattern \"/user/{userId}/**\"");
        String actualDestination = destination.substring(userEnd);
        String subscribeDestination = this.prefix.substring(0, prefixEnd - 1) + actualDestination;
        String userName = destination.substring(prefixEnd, userEnd);
        userName = StringUtils.replace(userName, "%2F", "/");
        Set<String> sessionIds;
        if (userName.equals(sessionId)) {
            userName = null;
            sessionIds = Collections.singleton(sessionId);
        } else {
            SimpUser user = this.userRegistry.getUser(userName);
            if (user != null) {
                if (user.getSession(sessionId) != null) {
                    sessionIds = Collections.singleton(sessionId);
                } else {
                    Set<SimpSession> sessions = user.getSessions();
                    sessionIds = new HashSet<>(sessions.size());
                    for (SimpSession session : sessions) {
                        sessionIds.add(session.getId());
                    }
                }
            } else {
                sessionIds = Collections.emptySet();
            }
        }
        if (!this.keepLeadingSlash) {
            actualDestination = actualDestination.substring(1);
        }
        return new ParseResult(actualDestination, subscribeDestination, sessionIds, userName);
    } else {
        return null;
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) SimpMessageType(org.springframework.messaging.simp.SimpMessageType) MessageHeaders(org.springframework.messaging.MessageHeaders) Principal(java.security.Principal) HashSet(java.util.HashSet)

Aggregations

SimpMessageType (org.springframework.messaging.simp.SimpMessageType)10 MessageHeaders (org.springframework.messaging.MessageHeaders)6 Principal (java.security.Principal)2 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)2 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Test (org.junit.Test)1 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)1 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)1 Message (org.springframework.messaging.Message)1 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)1 MessageHeaderAccessor (org.springframework.messaging.support.MessageHeaderAccessor)1 MessageMatcher (org.springframework.security.messaging.util.matcher.MessageMatcher)1 SimpDestinationMessageMatcher (org.springframework.security.messaging.util.matcher.SimpDestinationMessageMatcher)1 SimpMessageTypeMatcher (org.springframework.security.messaging.util.matcher.SimpMessageTypeMatcher)1