001/**
002 *
003 * Copyright 2014 Vyacheslav Blinov
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * 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.jivesoftware.smackx.amp;
018
019import org.jivesoftware.smack.SmackException.NoResponseException;
020import org.jivesoftware.smack.SmackException.NotConnectedException;
021import org.jivesoftware.smack.XMPPConnection;
022import org.jivesoftware.smack.XMPPException.XMPPErrorException;
023
024import org.jivesoftware.smackx.amp.packet.AMPExtension;
025
026public class AMPMatchResourceCondition implements AMPExtension.Condition {
027
028    public static final String NAME = "match-resource";
029
030    /**
031     * Check if server supports match-resource condition.
032     * @param connection Smack connection instance
033     * @return true if match-resource condition is supported.
034     * @throws XMPPErrorException if there was an XMPP error returned.
035     * @throws NoResponseException if there was no response from the remote entity.
036     * @throws NotConnectedException if the XMPP connection is not connected.
037     * @throws InterruptedException if the calling thread was interrupted.
038     */
039    public static boolean isSupported(XMPPConnection connection) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
040        return AMPManager.isConditionSupported(connection, NAME);
041    }
042
043    private final Value value;
044
045    /**
046     * Create new amp match-resource condition with value set to one of defined by XEP-0079.
047     * See http://xmpp.org/extensions/xep-0079.html#conditions-def-match
048     * @param value AMPDeliveryCondition.Value instance that will be used as value parameter. Can't be null.
049     */
050    public AMPMatchResourceCondition(Value value) {
051        if (value == null)
052            throw new NullPointerException("Can't create AMPMatchResourceCondition with null value");
053        this.value = value;
054    }
055
056    @Override
057    public String getName() {
058        return NAME;
059    }
060
061    @Override
062    public String getValue() {
063        return value.toString();
064    }
065
066    /**
067     * match-resource amp condition value as defined by XEP-0079.
068     * See http://xmpp.org/extensions/xep-0079.html#conditions-def-match
069     */
070    public enum Value {
071        /**
072         * Destination resource matches any value, effectively ignoring the intended resource.
073         * Example: "home/laptop" matches "home", "home/desktop" or "work/desktop"
074         */
075        any,
076        /**
077         * Destination resource exactly matches the intended resource.
078         * Example: "home/laptop" only matches "home/laptop" and not "home/desktop" or "work/desktop"
079         */
080        exact,
081        /**
082         * Destination resource matches any value except for the intended resource.
083         * Example: "home/laptop" matches "work/desktop", "home" or "home/desktop", but not "home/laptop"
084         */
085        other
086    }
087}