001/*
002 * Copyright 2002-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.messaging.simp;
018
019import java.util.Collection;
020import java.util.Collections;
021import java.util.Map;
022
023import org.springframework.lang.Nullable;
024import org.springframework.messaging.Message;
025import org.springframework.messaging.handler.AbstractMessageCondition;
026import org.springframework.util.Assert;
027
028/**
029 * {@code MessageCondition} that matches by the message type obtained via
030 * {@link SimpMessageHeaderAccessor#getMessageType(Map)}.
031 *
032 * @author Rossen Stoyanchev
033 * @since 4.0
034 */
035public class SimpMessageTypeMessageCondition extends AbstractMessageCondition<SimpMessageTypeMessageCondition> {
036
037        public static final SimpMessageTypeMessageCondition MESSAGE =
038                        new SimpMessageTypeMessageCondition(SimpMessageType.MESSAGE);
039
040        public static final SimpMessageTypeMessageCondition SUBSCRIBE =
041                        new SimpMessageTypeMessageCondition(SimpMessageType.SUBSCRIBE);
042
043
044        private final SimpMessageType messageType;
045
046
047        /**
048         * A constructor accepting a message type.
049         * @param messageType the message type to match messages to
050         */
051        public SimpMessageTypeMessageCondition(SimpMessageType messageType) {
052                Assert.notNull(messageType, "MessageType must not be null");
053                this.messageType = messageType;
054        }
055
056
057        public SimpMessageType getMessageType() {
058                return this.messageType;
059        }
060
061        @Override
062        protected Collection<?> getContent() {
063                return Collections.singletonList(this.messageType);
064        }
065
066        @Override
067        protected String getToStringInfix() {
068                return " || ";
069        }
070
071        @Override
072        public SimpMessageTypeMessageCondition combine(SimpMessageTypeMessageCondition other) {
073                return other;
074        }
075
076        @Override
077        @Nullable
078        public SimpMessageTypeMessageCondition getMatchingCondition(Message<?> message) {
079                SimpMessageType actual = SimpMessageHeaderAccessor.getMessageType(message.getHeaders());
080                return (actual != null && actual.equals(this.messageType) ? this : null);
081        }
082
083        @Override
084        public int compareTo(SimpMessageTypeMessageCondition other, Message<?> message) {
085                Object actual = SimpMessageHeaderAccessor.getMessageType(message.getHeaders());
086                if (actual != null) {
087                        if (actual.equals(this.messageType) && actual.equals(other.getMessageType())) {
088                                return 0;
089                        }
090                        else if (actual.equals(this.messageType)) {
091                                return -1;
092                        }
093                        else if (actual.equals(other.getMessageType())) {
094                                return 1;
095                        }
096                }
097                return 0;
098        }
099
100}