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.XMPPConnectionRegistry; 024import org.jivesoftware.smack.XMPPException.XMPPErrorException; 025import org.jivesoftware.smackx.amp.packet.AMPExtension; 026import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; 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 XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() { 043 @Override 044 public void connectionCreated(XMPPConnection connection) { 045 AMPManager.setServiceEnabled(connection, true); 046 } 047 }); 048 } 049 050 /** 051 * Enables or disables the AMP support on a given connection.<p> 052 * 053 * Before starting to send AMP messages to a user, check that the user can handle XHTML 054 * messages. Enable the AMP support to indicate that this client handles XHTML messages. 055 * 056 * @param connection the connection where the service will be enabled or disabled 057 * @param enabled indicates if the service will be enabled or disabled 058 */ 059 public synchronized static void setServiceEnabled(XMPPConnection connection, boolean enabled) { 060 if (isServiceEnabled(connection) == enabled) 061 return; 062 063 if (enabled) { 064 ServiceDiscoveryManager.getInstanceFor(connection).addFeature(AMPExtension.NAMESPACE); 065 } 066 else { 067 ServiceDiscoveryManager.getInstanceFor(connection).removeFeature(AMPExtension.NAMESPACE); 068 } 069 } 070 071 /** 072 * Returns true if the AMP support is enabled for the given connection. 073 * 074 * @param connection the connection to look for AMP support 075 * @return a boolean indicating if the AMP support is enabled for the given connection 076 */ 077 public static boolean isServiceEnabled(XMPPConnection connection) { 078 connection.getXMPPServiceDomain(); 079 return ServiceDiscoveryManager.getInstanceFor(connection).includesFeature(AMPExtension.NAMESPACE); 080 } 081 082 /** 083 * Check if server supports specified action. 084 * @param connection active xmpp connection 085 * @param action action to check 086 * @return true if this action is supported. 087 * @throws XMPPErrorException 088 * @throws NoResponseException 089 * @throws NotConnectedException 090 * @throws InterruptedException 091 */ 092 public static boolean isActionSupported(XMPPConnection connection, AMPExtension.Action action) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { 093 String featureName = AMPExtension.NAMESPACE + "?action=" + action.toString(); 094 return isFeatureSupportedByServer(connection, featureName); 095 } 096 097 /** 098 * Check if server supports specified condition. 099 * @param connection active xmpp connection 100 * @param conditionName name of condition to check 101 * @return true if this condition is supported. 102 * @throws XMPPErrorException 103 * @throws NoResponseException 104 * @throws NotConnectedException 105 * @throws InterruptedException 106 * @see AMPDeliverCondition 107 * @see AMPExpireAtCondition 108 * @see AMPMatchResourceCondition 109 */ 110 public static boolean isConditionSupported(XMPPConnection connection, String conditionName) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { 111 String featureName = AMPExtension.NAMESPACE + "?condition=" + conditionName; 112 return isFeatureSupportedByServer(connection, featureName); 113 } 114 115 private static boolean isFeatureSupportedByServer(XMPPConnection connection, String featureName) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { 116 return ServiceDiscoveryManager.getInstanceFor(connection).serverSupportsFeature(featureName); 117 } 118}