2014-02-17 23:58:40 +01:00
|
|
|
/**
|
|
|
|
*
|
2014-07-16 11:46:30 +02:00
|
|
|
* Copyright 2013-2014 Georg Lukas
|
2014-02-17 23:58:40 +01:00
|
|
|
*
|
|
|
|
* 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 org.jivesoftware.smackx.carbons;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.WeakHashMap;
|
|
|
|
|
2014-03-12 11:50:05 +01:00
|
|
|
import org.jivesoftware.smack.SmackException;
|
|
|
|
import org.jivesoftware.smack.SmackException.NoResponseException;
|
2014-03-19 14:22:20 +01:00
|
|
|
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
2014-03-10 09:45:50 +01:00
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
2014-02-17 23:58:40 +01:00
|
|
|
import org.jivesoftware.smack.ConnectionCreationListener;
|
2014-03-09 11:19:44 +01:00
|
|
|
import org.jivesoftware.smack.Manager;
|
2014-02-17 23:58:40 +01:00
|
|
|
import org.jivesoftware.smack.PacketListener;
|
2014-05-25 12:28:08 +02:00
|
|
|
import org.jivesoftware.smack.XMPPConnectionRegistry;
|
2014-02-17 23:58:40 +01:00
|
|
|
import org.jivesoftware.smack.XMPPException;
|
2014-03-12 11:50:05 +01:00
|
|
|
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
|
2014-02-23 21:08:35 +01:00
|
|
|
import org.jivesoftware.smack.filter.IQReplyFilter;
|
2014-02-17 23:58:40 +01:00
|
|
|
import org.jivesoftware.smack.packet.IQ;
|
|
|
|
import org.jivesoftware.smack.packet.Message;
|
|
|
|
import org.jivesoftware.smack.packet.Packet;
|
|
|
|
import org.jivesoftware.smackx.carbons.packet.CarbonExtension;
|
2014-08-13 12:10:18 +02:00
|
|
|
import org.jivesoftware.smackx.carbons.packet.Carbon;
|
|
|
|
import org.jivesoftware.smackx.carbons.packet.CarbonExtension.Private;
|
2014-02-17 23:58:40 +01:00
|
|
|
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Packet extension for XEP-0280: Message Carbons. This class implements
|
|
|
|
* the manager for registering {@link CarbonExtension} support, enabling and disabling
|
|
|
|
* message carbons.
|
|
|
|
*
|
|
|
|
* You should call enableCarbons() before sending your first undirected
|
|
|
|
* presence.
|
|
|
|
*
|
|
|
|
* @author Georg Lukas
|
|
|
|
*/
|
2014-03-09 11:19:44 +01:00
|
|
|
public class CarbonManager extends Manager {
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2014-07-16 11:46:30 +02:00
|
|
|
private static Map<XMPPConnection, CarbonManager> INSTANCES = new WeakHashMap<XMPPConnection, CarbonManager>();
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
static {
|
2014-05-25 12:28:08 +02:00
|
|
|
XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
|
2014-03-10 09:45:50 +01:00
|
|
|
public void connectionCreated(XMPPConnection connection) {
|
2014-02-17 23:58:40 +01:00
|
|
|
getInstanceFor(connection);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private volatile boolean enabled_state = false;
|
|
|
|
|
2014-03-10 09:45:50 +01:00
|
|
|
private CarbonManager(XMPPConnection connection) {
|
2014-03-09 11:19:44 +01:00
|
|
|
super(connection);
|
2014-02-17 23:58:40 +01:00
|
|
|
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
|
|
|
sdm.addFeature(CarbonExtension.NAMESPACE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtain the CarbonManager responsible for a connection.
|
|
|
|
*
|
|
|
|
* @param connection the connection object.
|
|
|
|
*
|
|
|
|
* @return a CarbonManager instance
|
|
|
|
*/
|
2014-03-10 09:45:50 +01:00
|
|
|
public static synchronized CarbonManager getInstanceFor(XMPPConnection connection) {
|
2014-07-16 11:46:30 +02:00
|
|
|
CarbonManager carbonManager = INSTANCES.get(connection);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
if (carbonManager == null) {
|
|
|
|
carbonManager = new CarbonManager(connection);
|
2014-07-16 11:46:30 +02:00
|
|
|
INSTANCES.put(connection, carbonManager);
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return carbonManager;
|
|
|
|
}
|
|
|
|
|
2014-07-16 11:46:30 +02:00
|
|
|
private static IQ carbonsEnabledIQ(final boolean new_state) {
|
2014-08-13 12:10:18 +02:00
|
|
|
IQ request;
|
|
|
|
if (new_state) {
|
|
|
|
request = new Carbon.Enable();
|
|
|
|
} else {
|
|
|
|
request = new Carbon.Disable();
|
|
|
|
}
|
|
|
|
return request;
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if XMPP Carbons are supported by the server.
|
|
|
|
*
|
|
|
|
* @return true if supported
|
2014-07-16 11:46:30 +02:00
|
|
|
* @throws NotConnectedException
|
|
|
|
* @throws XMPPErrorException
|
|
|
|
* @throws NoResponseException
|
2014-02-17 23:58:40 +01:00
|
|
|
*/
|
2014-07-16 11:46:30 +02:00
|
|
|
public boolean isSupportedByServer() throws NoResponseException, XMPPErrorException, NotConnectedException {
|
2014-03-12 11:50:05 +01:00
|
|
|
return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(
|
|
|
|
connection().getServiceName(), CarbonExtension.NAMESPACE);
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify server to change the carbons state. This method returns
|
|
|
|
* immediately and changes the variable when the reply arrives.
|
|
|
|
*
|
|
|
|
* You should first check for support using isSupportedByServer().
|
|
|
|
*
|
|
|
|
* @param new_state whether carbons should be enabled or disabled
|
2014-03-19 14:22:20 +01:00
|
|
|
* @throws NotConnectedException
|
2014-02-17 23:58:40 +01:00
|
|
|
*/
|
2014-03-19 14:22:20 +01:00
|
|
|
public void sendCarbonsEnabled(final boolean new_state) throws NotConnectedException {
|
2014-02-17 23:58:40 +01:00
|
|
|
IQ setIQ = carbonsEnabledIQ(new_state);
|
|
|
|
|
2014-03-09 11:19:44 +01:00
|
|
|
connection().addPacketListener(new PacketListener() {
|
2014-02-17 23:58:40 +01:00
|
|
|
public void processPacket(Packet packet) {
|
|
|
|
IQ result = (IQ)packet;
|
2014-06-06 02:20:45 +02:00
|
|
|
if (result.getType() == IQ.Type.result) {
|
2014-02-17 23:58:40 +01:00
|
|
|
enabled_state = new_state;
|
|
|
|
}
|
2014-03-09 11:19:44 +01:00
|
|
|
connection().removePacketListener(this);
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
2014-03-09 11:19:44 +01:00
|
|
|
}, new IQReplyFilter(setIQ, connection()));
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2014-03-09 11:19:44 +01:00
|
|
|
connection().sendPacket(setIQ);
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify server to change the carbons state. This method blocks
|
|
|
|
* some time until the server replies to the IQ and returns true on
|
|
|
|
* success.
|
|
|
|
*
|
|
|
|
* You should first check for support using isSupportedByServer().
|
|
|
|
*
|
|
|
|
* @param new_state whether carbons should be enabled or disabled
|
2014-03-12 11:50:05 +01:00
|
|
|
* @throws XMPPErrorException
|
|
|
|
* @throws NoResponseException
|
2014-03-19 14:22:20 +01:00
|
|
|
* @throws NotConnectedException
|
2014-02-17 23:58:40 +01:00
|
|
|
*
|
|
|
|
*/
|
2014-03-12 11:50:05 +01:00
|
|
|
public synchronized void setCarbonsEnabled(final boolean new_state) throws NoResponseException,
|
2014-03-19 14:22:20 +01:00
|
|
|
XMPPErrorException, NotConnectedException {
|
2014-03-12 11:50:05 +01:00
|
|
|
if (enabled_state == new_state)
|
|
|
|
return;
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
IQ setIQ = carbonsEnabledIQ(new_state);
|
|
|
|
|
2014-03-09 11:19:44 +01:00
|
|
|
connection().createPacketCollectorAndSend(setIQ).nextResultOrThrow();
|
2014-02-18 15:05:19 +01:00
|
|
|
enabled_state = new_state;
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method to enable carbons.
|
|
|
|
*
|
2014-02-18 15:05:19 +01:00
|
|
|
* @throws XMPPException
|
2014-03-12 11:50:05 +01:00
|
|
|
* @throws SmackException if there was no response from the server.
|
2014-02-17 23:58:40 +01:00
|
|
|
*/
|
2014-03-12 11:50:05 +01:00
|
|
|
public void enableCarbons() throws XMPPException, SmackException {
|
2014-02-18 15:05:19 +01:00
|
|
|
setCarbonsEnabled(true);
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method to disable carbons.
|
|
|
|
*
|
2014-02-18 15:05:19 +01:00
|
|
|
* @throws XMPPException
|
2014-03-12 11:50:05 +01:00
|
|
|
* @throws SmackException if there was no response from the server.
|
2014-02-17 23:58:40 +01:00
|
|
|
*/
|
2014-03-12 11:50:05 +01:00
|
|
|
public void disableCarbons() throws XMPPException, SmackException {
|
2014-02-18 15:05:19 +01:00
|
|
|
setCarbonsEnabled(false);
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if carbons are enabled on this connection.
|
|
|
|
*/
|
|
|
|
public boolean getCarbonsEnabled() {
|
|
|
|
return this.enabled_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark a message as "private", so it will not be carbon-copied.
|
|
|
|
*
|
|
|
|
* @param msg Message object to mark private
|
2014-08-13 12:10:18 +02:00
|
|
|
* @deprecated use {@link Private#addTo(Message)}
|
2014-02-17 23:58:40 +01:00
|
|
|
*/
|
2014-08-13 12:10:18 +02:00
|
|
|
@Deprecated
|
2014-02-17 23:58:40 +01:00
|
|
|
public static void disableCarbons(Message msg) {
|
2014-08-13 12:10:18 +02:00
|
|
|
msg.addExtension(Private.INSTANCE);
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
}
|