AMPMatchResourceCondition.java

  1. /**
  2.  *
  3.  * Copyright 2014 Vyacheslav Blinov
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.amp;

  18. import org.jivesoftware.smack.SmackException.NoResponseException;
  19. import org.jivesoftware.smack.SmackException.NotConnectedException;
  20. import org.jivesoftware.smack.XMPPConnection;
  21. import org.jivesoftware.smack.XMPPException.XMPPErrorException;

  22. import org.jivesoftware.smackx.amp.packet.AMPExtension;

  23. public class AMPMatchResourceCondition implements AMPExtension.Condition {

  24.     public static final String NAME = "match-resource";

  25.     /**
  26.      * Check if server supports match-resource condition.
  27.      * @param connection Smack connection instance
  28.      * @return true if match-resource condition is supported.
  29.      * @throws XMPPErrorException if there was an XMPP error returned.
  30.      * @throws NoResponseException if there was no response from the remote entity.
  31.      * @throws NotConnectedException if the XMPP connection is not connected.
  32.      * @throws InterruptedException if the calling thread was interrupted.
  33.      */
  34.     public static boolean isSupported(XMPPConnection connection) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  35.         return AMPManager.isConditionSupported(connection, NAME);
  36.     }

  37.     private final Value value;

  38.     /**
  39.      * Create new amp match-resource condition with value set to one of defined by XEP-0079.
  40.      * See http://xmpp.org/extensions/xep-0079.html#conditions-def-match
  41.      * @param value AMPDeliveryCondition.Value instance that will be used as value parameter. Can't be null.
  42.      */
  43.     public AMPMatchResourceCondition(Value value) {
  44.         if (value == null)
  45.             throw new NullPointerException("Can't create AMPMatchResourceCondition with null value");
  46.         this.value = value;
  47.     }

  48.     @Override
  49.     public String getName() {
  50.         return NAME;
  51.     }

  52.     @Override
  53.     public String getValue() {
  54.         return value.toString();
  55.     }

  56.     /**
  57.      * match-resource amp condition value as defined by XEP-0079.
  58.      * See http://xmpp.org/extensions/xep-0079.html#conditions-def-match
  59.      */
  60.     public enum Value {
  61.         /**
  62.          * Destination resource matches any value, effectively ignoring the intended resource.
  63.          * Example: "home/laptop" matches "home", "home/desktop" or "work/desktop"
  64.          */
  65.         any,
  66.         /**
  67.          * Destination resource exactly matches the intended resource.
  68.          * Example: "home/laptop" only matches "home/laptop" and not "home/desktop" or "work/desktop"
  69.          */
  70.         exact,
  71.         /**
  72.          * Destination resource matches any value except for the intended resource.
  73.          * Example: "home/laptop" matches "work/desktop", "home" or "home/desktop", but not "home/laptop"
  74.          */
  75.         other
  76.     }
  77. }