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.ConnectionCreationListener;
023import org.jivesoftware.smack.XMPPException.XMPPErrorException;
024import org.jivesoftware.smackx.amp.packet.AMPExtension;
025import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
026import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
027
028/**
029 * Manages AMP stanzas within messages. A AMPManager provides a high level access to
030 * get and set AMP rules to messages.
031 *
032 * See http://xmpp.org/extensions/xep-0079.html for AMP extension details
033 *
034 * @author Vyacheslav Blinov
035 */
036public class AMPManager {
037
038
039    // Enable the AMP support on every established connection
040    // The ServiceDiscoveryManager class should have been already initialized
041    static {
042        XMPPConnection.addConnectionCreationListener(new ConnectionCreationListener() {
043            public void connectionCreated(XMPPConnection connection) {
044                AMPManager.setServiceEnabled(connection, true);
045            }
046        });
047    }
048
049    /**
050     * Enables or disables the AMP support on a given connection.<p>
051     *
052     * Before starting to send AMP messages to a user, check that the user can handle XHTML
053     * messages. Enable the AMP support to indicate that this client handles XHTML messages.
054     *
055     * @param connection the connection where the service will be enabled or disabled
056     * @param enabled indicates if the service will be enabled or disabled
057     */
058    public synchronized static void setServiceEnabled(XMPPConnection connection, boolean enabled) {
059        if (isServiceEnabled(connection) == enabled)
060            return;
061
062        if (enabled) {
063            ServiceDiscoveryManager.getInstanceFor(connection).addFeature(AMPExtension.NAMESPACE);
064        }
065        else {
066            ServiceDiscoveryManager.getInstanceFor(connection).removeFeature(AMPExtension.NAMESPACE);
067        }
068    }
069
070    /**
071     * Returns true if the AMP support is enabled for the given connection.
072     *
073     * @param connection the connection to look for AMP support
074     * @return a boolean indicating if the AMP support is enabled for the given connection
075     */
076    public static boolean isServiceEnabled(XMPPConnection connection) {
077        connection.getServiceName();
078        return ServiceDiscoveryManager.getInstanceFor(connection).includesFeature(AMPExtension.NAMESPACE);
079    }
080
081    /**
082     * Check if server supports specified action
083     * @param connection active xmpp connection
084     * @param action action to check
085     * @return true if this action is supported.
086     * @throws XMPPErrorException 
087     * @throws NoResponseException 
088     * @throws NotConnectedException 
089     */
090    public static boolean isActionSupported(XMPPConnection connection, AMPExtension.Action action) throws NoResponseException, XMPPErrorException, NotConnectedException {
091        String featureName = AMPExtension.NAMESPACE + "?action=" + action.toString();
092        return isFeatureSupportedByServer(connection, featureName, AMPExtension.NAMESPACE);
093    }
094
095    /**
096     * Check if server supports specified condition
097     * @param connection active xmpp connection
098     * @param conditionName name of condition to check
099     * @return true if this condition is supported.
100     * @throws XMPPErrorException 
101     * @throws NoResponseException 
102     * @throws NotConnectedException 
103     * @see AMPDeliverCondition
104     * @see AMPExpireAtCondition
105     * @see AMPMatchResourceCondition
106     */
107    public static boolean isConditionSupported(XMPPConnection connection, String conditionName) throws NoResponseException, XMPPErrorException, NotConnectedException {
108        String featureName = AMPExtension.NAMESPACE + "?condition=" + conditionName;
109        return isFeatureSupportedByServer(connection, featureName, AMPExtension.NAMESPACE);
110    }
111
112    private static boolean isFeatureSupportedByServer(XMPPConnection connection, String featureName, String node) throws NoResponseException, XMPPErrorException, NotConnectedException {
113        ServiceDiscoveryManager discoveryManager = ServiceDiscoveryManager.getInstanceFor(connection);
114        DiscoverInfo info = discoveryManager.discoverInfo(connection.getServiceName(), node);
115        for (DiscoverInfo.Feature feature : info.getFeatures()){
116            if (featureName.equals(feature.getVar())) {
117                return true;
118            }
119        }
120        return false;
121    }
122}