Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  
  /*
   * Copyright 2014 dc-square GmbH
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *       http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.dcsquare.hivemq.spi.topic;
 
 
A Permission which represents the concept of a topic which can be restricted in the following parts:

  • Topic (name)
  • Quality of Service
  • Activity (Publish/Subscribe)

Author(s):
Christian Goetz
Dominik Obermaier
Since:
1.4
 
 public class MqttTopicPermission {
 
     private final String topic;
     private final ALLOWED_QOS qos;
     private final ALLOWED_ACTIVITY activity;
     private TopicMatcher topicMatcher = new PermissionTopicMatcher();

    
Represents the allowed Quality of Service levels for a MqttTopicPermission
 
     public enum ALLOWED_QOS {
        
Only QoS 0 is allowed
 
         ZERO,
        
Only QoS 1 is allowed
 
         ONE,
        
Only QoS 2 is allowed
 
         TWO,
        
Only QoS 0 and 1 are allowed
 
         ZERO_ONE,
        
Only QoS 0 and 2 are allowed
 
         ZERO_TWO,
        
Only QoS 1 and 2 are allowed
 
         ONE_TWO,
        
All QoS levels are allowed
 
         ALL;
 
         private ALLOWED_QOS() {
         }
 
 
         public static ALLOWED_QOS from(final QoS from) {
 
             switch (from) {
                 case :
                     return .;
                 case :
                     return .;
                 case :
                     return .;
                 default:
                     //Should never happen
                     throw new RuntimeException("Invalid Qos");
 
             }
         }
     }

    
Represents the allowed activity on a topic
    public enum ALLOWED_ACTIVITY {

        
Only publishing on this topic is allowed
        PUBLISH,
        
Only subscribing on this topic is allowed
        SUBSCRIBE,
        
Publishing and subscribing is allowed on this topic
        ALL
    }

    
Creates a topic permission where publishing and subscribing on all QoS level is allowed on a given topic

Parameters:
topic the topic
    public MqttTopicPermission(final String topic) {
        this(topic..);
    }

    
Creates a topic where a given activity is allowed on all QoS levels on a given topic

Parameters:
topic the topic
activity the activity
    public MqttTopicPermission(final String topicfinal ALLOWED_ACTIVITY activity) {
        this(topic.activity);
    }

    
Creates a topic where publishing and subscribing on a given QoS is allowed for a given topic

Parameters:
topic the topic
qos the QoS level
    public MqttTopicPermission(final String topicfinal ALLOWED_QOS qos) {
        this(topicqos.);
    }

    
Creates a topic where a given activity is allowed on a given QoS for a given topic

Parameters:
topic the topic
qos the QoS
activity the activity
    public MqttTopicPermission(final String topicfinal ALLOWED_QOS qosfinal ALLOWED_ACTIVITY activity) {
        this. = topic;
        this. = qos;
        this. = activity;
    }

    
Checks the MqttTopicPermission implies a given MqttTopicPermission

Parameters:
other the MqttTopicPermission which should be checked if it is implied by the current MqttTopicPermission
Returns:
true if the given MqttTopicPermission is implied
    public boolean implies(final MqttTopicPermission other) {
        if (other == null) {
            return false;
        }
        final boolean activityImplies = getActivityImplicity(other);
        if (!activityImplies) {
            return false;
        }
        final boolean qosImplies = getQosImplicity(other);
        if (!qosImplies) {
            return false;
        }
        return topicImplicity(other);
    }

    
Checks if the topic implies a given MqttTopicPermissions topic

Parameters:
other the other MqttTopicPermission
Returns:
true if the given MqttTopicPermissions topic is implied by the current one
    private boolean topicImplicity(final MqttTopicPermission other) {
        try {
            return .matches(this.getTopic(), other.getTopic());
        } catch (InvalidTopicException e) {
            return false;
        }
    }

    
Checks if the MqttTopicPermission implies a given QoS

Parameters:
other the other MqttTopicPermission
Returns:
true if the QoS level implies the given QoS
    private boolean getQosImplicity(final MqttTopicPermission other) {
        if (this.getQos() == .) {
            return true;
        }
        if (other.getQos() == . && this.getQos() != .) {
            return false;
        } else if (this.getQos() == .) {
            return (other.getQos() == .) || (other.getQos() == .) || (other.getQos() == .);
        } else if (this.getQos() == .) {
            return (other.getQos() == .) || (other.getQos() == .) || (other.getQos() == .);
        } else if (this.getQos() == .) {
            return (other.getQos() == .) || (other.getQos() == .) || (other.getQos() == .);
        }
        return this.getQos() == other.getQos();
    }

    
Checks if an activity implies another activity

Parameters:
other the other permission with an activity
Returns:
true if the permission activity imply the other permission activity
    private boolean getActivityImplicity(final MqttTopicPermission other) {
        if (this.getActivity() == .) {
            return true;
        }
        if ((other.getActivity() == .) && (this. == .)) {
            return false;
        } else if ((other.getActivity() == .) && (this. == .)) {
            return false;
        } else if (other.getActivity() == . && this.getActivity() != .) {
            return false;
        }
        return true;
    }
    public String getTopic() {
        return ;
    }
    public ALLOWED_QOS getQos() {
        return ;
    }
    public ALLOWED_ACTIVITY getActivity() {
        return ;
    }
New to GrepCode? Check out our FAQ X