001/* 002 * Copyright 2002-2015 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.Arrays; 020import java.util.Collection; 021 022import org.springframework.messaging.Message; 023import org.springframework.messaging.handler.AbstractMessageCondition; 024import org.springframework.util.Assert; 025 026/** 027 * A message condition that checks the message type. 028 * 029 * @author Rossen Stoyanchev 030 * @since 4.0 031 */ 032public class SimpMessageTypeMessageCondition extends AbstractMessageCondition<SimpMessageTypeMessageCondition> { 033 034 public static final SimpMessageTypeMessageCondition MESSAGE = 035 new SimpMessageTypeMessageCondition(SimpMessageType.MESSAGE); 036 037 public static final SimpMessageTypeMessageCondition SUBSCRIBE = 038 new SimpMessageTypeMessageCondition(SimpMessageType.SUBSCRIBE); 039 040 041 private final SimpMessageType messageType; 042 043 044 /** 045 * A constructor accepting a message type. 046 * @param messageType the message type to match messages to 047 */ 048 public SimpMessageTypeMessageCondition(SimpMessageType messageType) { 049 Assert.notNull(messageType, "MessageType must not be null"); 050 this.messageType = messageType; 051 } 052 053 054 public SimpMessageType getMessageType() { 055 return this.messageType; 056 } 057 058 @Override 059 protected Collection<?> getContent() { 060 return Arrays.asList(this.messageType); 061 } 062 063 @Override 064 protected String getToStringInfix() { 065 return " || "; 066 } 067 068 @Override 069 public SimpMessageTypeMessageCondition combine(SimpMessageTypeMessageCondition other) { 070 return other; 071 } 072 073 @Override 074 public SimpMessageTypeMessageCondition getMatchingCondition(Message<?> message) { 075 Object actualMessageType = SimpMessageHeaderAccessor.getMessageType(message.getHeaders()); 076 if (actualMessageType == null) { 077 return null; 078 } 079 return this; 080 } 081 082 @Override 083 public int compareTo(SimpMessageTypeMessageCondition other, Message<?> message) { 084 Object actualMessageType = SimpMessageHeaderAccessor.getMessageType(message.getHeaders()); 085 if (actualMessageType != null) { 086 if (actualMessageType.equals(this.getMessageType()) && actualMessageType.equals(other.getMessageType())) { 087 return 0; 088 } 089 else if (actualMessageType.equals(this.getMessageType())) { 090 return -1; 091 } 092 else if (actualMessageType.equals(other.getMessageType())) { 093 return 1; 094 } 095 } 096 return 0; 097 } 098 099}