001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.activemq.broker.region.policy; 018 019import java.util.List; 020 021import org.apache.activemq.broker.region.MessageReference; 022import org.apache.activemq.broker.region.Subscription; 023import org.apache.activemq.command.ActiveMQDestination; 024import org.apache.activemq.filter.MessageEvaluationContext; 025 026/** 027 * ClientIdFilterDispatchPolicy dispatches messages in a topic to a given 028 * client. Then the message with a "PTP_CLIENTID" property, can be received by a 029 * mqtt client with the same clientId. 030 * 031 * @author kimmking (kimmking@163.com) 032 * @date 2013-12-20 033 * @org.apache.xbean.XBean 034 */ 035public class ClientIdFilterDispatchPolicy extends SimpleDispatchPolicy { 036 037 public static final String PTP_CLIENTID = "PTP_CLIENTID"; 038 public static final String PTP_SUFFIX = ".PTP"; 039 040 private String ptpClientId = PTP_CLIENTID; 041 private String ptpSuffix = PTP_SUFFIX; 042 043 public boolean dispatch(MessageReference node, MessageEvaluationContext msgContext, List<Subscription> consumers) throws Exception { 044 045 Object _clientId = node.getMessage().getProperty(ptpClientId); 046 if (_clientId == null) return super.dispatch(node, msgContext, consumers); 047 048 ActiveMQDestination _destination = node.getMessage().getDestination(); 049 int count = 0; 050 for (Subscription sub : consumers) { 051 // Don't deliver to browsers 052 if (sub.getConsumerInfo().isBrowser()) { 053 continue; 054 } 055 // Only dispatch to interested subscriptions 056 if (!sub.matches(node, msgContext)) { 057 sub.unmatched(node); 058 continue; 059 } 060 if (_clientId != null && _destination.isTopic() && _clientId.equals(sub.getContext().getClientId()) 061 && _destination.getQualifiedName().endsWith(this.ptpSuffix)) { 062 sub.add(node); 063 count++; 064 } else { 065 sub.unmatched(node); 066 } 067 } 068 069 return count > 0; 070 } 071 072 public String getPtpClientId() { 073 return ptpClientId; 074 } 075 076 public void setPtpClientId(String ptpClientId) { 077 this.ptpClientId = ptpClientId; 078 } 079 080 public String getPtpSuffix() { 081 return ptpSuffix; 082 } 083 084 public void setPtpSuffix(String ptpSuffix) { 085 this.ptpSuffix = ptpSuffix; 086 } 087 088}