2013-02-04 10:53:56 +01:00
|
|
|
/**
|
2014-02-17 18:57:38 +01:00
|
|
|
*
|
2014-07-13 10:16:10 +02:00
|
|
|
* Copyright 2013-2014 Georg Lukas
|
2013-02-04 10:53:56 +01:00
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2013-02-04 10:53:56 +01:00
|
|
|
* 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.receipts;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.WeakHashMap;
|
2014-10-05 16:27:52 +02:00
|
|
|
import java.util.concurrent.CopyOnWriteArraySet;
|
2013-02-04 10:53:56 +01:00
|
|
|
|
2014-03-12 11:50:05 +01:00
|
|
|
import org.jivesoftware.smack.SmackException;
|
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;
|
2013-02-04 10:53:56 +01:00
|
|
|
import org.jivesoftware.smack.ConnectionCreationListener;
|
2014-03-09 11:19:44 +01:00
|
|
|
import org.jivesoftware.smack.Manager;
|
2013-02-04 10:53:56 +01:00
|
|
|
import org.jivesoftware.smack.PacketListener;
|
2014-05-25 12:28:08 +02:00
|
|
|
import org.jivesoftware.smack.XMPPConnectionRegistry;
|
2013-02-04 10:53:56 +01:00
|
|
|
import org.jivesoftware.smack.XMPPException;
|
|
|
|
import org.jivesoftware.smack.filter.PacketExtensionFilter;
|
|
|
|
import org.jivesoftware.smack.packet.Message;
|
|
|
|
import org.jivesoftware.smack.packet.Packet;
|
2014-02-15 11:35:08 +01:00
|
|
|
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
2013-02-04 10:53:56 +01:00
|
|
|
|
|
|
|
/**
|
2013-04-02 02:23:55 +02:00
|
|
|
* Manager for XEP-0184: Message Delivery Receipts. This class implements
|
2013-02-04 10:53:56 +01:00
|
|
|
* the manager for {@link DeliveryReceipt} support, enabling and disabling of
|
|
|
|
* automatic DeliveryReceipt transmission.
|
|
|
|
*
|
|
|
|
* @author Georg Lukas
|
|
|
|
*/
|
2014-03-09 11:19:44 +01:00
|
|
|
public class DeliveryReceiptManager extends Manager implements PacketListener {
|
2013-02-04 10:53:56 +01:00
|
|
|
|
2014-07-13 10:16:10 +02:00
|
|
|
private static Map<XMPPConnection, DeliveryReceiptManager> instances = new WeakHashMap<XMPPConnection, DeliveryReceiptManager>();
|
2013-02-04 10:53:56 +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) {
|
2013-11-03 22:49:58 +01:00
|
|
|
getInstanceFor(connection);
|
2013-02-04 10:53:56 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean auto_receipts_enabled = false;
|
2014-10-05 16:27:52 +02:00
|
|
|
private final Set<ReceiptReceivedListener> receiptReceivedListeners = new CopyOnWriteArraySet<ReceiptReceivedListener>();
|
2013-02-04 10:53:56 +01:00
|
|
|
|
2014-03-10 09:45:50 +01:00
|
|
|
private DeliveryReceiptManager(XMPPConnection connection) {
|
2014-03-09 11:19:44 +01:00
|
|
|
super(connection);
|
2013-02-04 10:53:56 +01:00
|
|
|
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
|
|
|
sdm.addFeature(DeliveryReceipt.NAMESPACE);
|
|
|
|
|
|
|
|
// register listener for delivery receipts and requests
|
2015-01-05 21:42:35 +01:00
|
|
|
connection.addAsyncPacketListener(this, new PacketExtensionFilter(DeliveryReceipt.NAMESPACE));
|
2013-02-04 10:53:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtain the DeliveryReceiptManager responsible for a connection.
|
|
|
|
*
|
|
|
|
* @param connection the connection object.
|
|
|
|
*
|
|
|
|
* @return the DeliveryReceiptManager instance for the given connection
|
|
|
|
*/
|
2014-03-10 09:45:50 +01:00
|
|
|
public static synchronized DeliveryReceiptManager getInstanceFor(XMPPConnection connection) {
|
2013-02-04 10:53:56 +01:00
|
|
|
DeliveryReceiptManager receiptManager = instances.get(connection);
|
|
|
|
|
|
|
|
if (receiptManager == null) {
|
|
|
|
receiptManager = new DeliveryReceiptManager(connection);
|
2014-07-13 10:16:10 +02:00
|
|
|
instances.put(connection, receiptManager);
|
2013-02-04 10:53:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return receiptManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if Delivery Receipts are supported by a given JID
|
|
|
|
*
|
|
|
|
* @param jid
|
|
|
|
* @return true if supported
|
2014-03-12 11:50:05 +01:00
|
|
|
* @throws SmackException if there was no response from the server.
|
|
|
|
* @throws XMPPException
|
2013-02-04 10:53:56 +01:00
|
|
|
*/
|
2014-03-12 11:50:05 +01:00
|
|
|
public boolean isSupported(String jid) throws SmackException, XMPPException {
|
|
|
|
return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(jid,
|
|
|
|
DeliveryReceipt.NAMESPACE);
|
2013-02-04 10:53:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// handle incoming receipts and receipt requests
|
|
|
|
@Override
|
2014-03-19 14:22:20 +01:00
|
|
|
public void processPacket(Packet packet) throws NotConnectedException {
|
2014-10-05 12:52:19 +02:00
|
|
|
DeliveryReceipt dr = DeliveryReceipt.from(packet);
|
2013-02-04 10:53:56 +01:00
|
|
|
if (dr != null) {
|
|
|
|
// notify listeners of incoming receipt
|
|
|
|
for (ReceiptReceivedListener l : receiptReceivedListeners) {
|
2014-10-05 12:54:12 +02:00
|
|
|
l.onReceiptReceived(packet.getFrom(), packet.getTo(), dr.getId(), packet);
|
2013-02-04 10:53:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if enabled, automatically send a receipt
|
|
|
|
if (auto_receipts_enabled) {
|
2014-10-05 12:52:19 +02:00
|
|
|
DeliveryReceiptRequest drr = DeliveryReceiptRequest.from(packet);
|
2013-02-04 10:53:56 +01:00
|
|
|
if (drr != null) {
|
2014-03-10 09:45:50 +01:00
|
|
|
XMPPConnection connection = connection();
|
2013-02-04 10:53:56 +01:00
|
|
|
Message ack = new Message(packet.getFrom(), Message.Type.normal);
|
|
|
|
ack.addExtension(new DeliveryReceipt(packet.getPacketID()));
|
|
|
|
connection.sendPacket(ack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configure whether the {@link DeliveryReceiptManager} should automatically
|
|
|
|
* reply to incoming {@link DeliveryReceipt}s. By default, this feature is off.
|
|
|
|
*
|
|
|
|
* @param new_state whether automatic transmission of
|
|
|
|
* DeliveryReceipts should be enabled or disabled
|
|
|
|
*/
|
|
|
|
public void setAutoReceiptsEnabled(boolean new_state) {
|
|
|
|
auto_receipts_enabled = new_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method to enable automatic DeliveryReceipt transmission.
|
|
|
|
*/
|
|
|
|
public void enableAutoReceipts() {
|
|
|
|
setAutoReceiptsEnabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method to disable automatic DeliveryReceipt transmission.
|
|
|
|
*/
|
|
|
|
public void disableAutoReceipts() {
|
|
|
|
setAutoReceiptsEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if AutoReceipts are enabled on this connection.
|
|
|
|
*/
|
|
|
|
public boolean getAutoReceiptsEnabled() {
|
|
|
|
return this.auto_receipts_enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get informed about incoming delivery receipts with a {@link ReceiptReceivedListener}.
|
|
|
|
*
|
|
|
|
* @param listener the listener to be informed about new receipts
|
|
|
|
*/
|
2013-04-02 02:23:55 +02:00
|
|
|
public void addReceiptReceivedListener(ReceiptReceivedListener listener) {
|
2013-02-04 10:53:56 +01:00
|
|
|
receiptReceivedListeners.add(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop getting informed about incoming delivery receipts.
|
|
|
|
*
|
|
|
|
* @param listener the listener to be removed
|
|
|
|
*/
|
2013-04-02 02:23:55 +02:00
|
|
|
public void removeReceiptReceivedListener(ReceiptReceivedListener listener) {
|
2013-02-04 10:53:56 +01:00
|
|
|
receiptReceivedListeners.remove(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if a packet requires a delivery receipt.
|
|
|
|
*
|
|
|
|
* @param p Packet object to check for a DeliveryReceiptRequest
|
|
|
|
*
|
|
|
|
* @return true if a delivery receipt was requested
|
|
|
|
*/
|
|
|
|
public static boolean hasDeliveryReceiptRequest(Packet p) {
|
2014-10-05 12:52:19 +02:00
|
|
|
return (DeliveryReceiptRequest.from(p) != null);
|
2013-02-04 10:53:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a delivery receipt request to an outgoing packet.
|
|
|
|
*
|
|
|
|
* Only message packets may contain receipt requests as of XEP-0184,
|
|
|
|
* therefore only allow Message as the parameter type.
|
|
|
|
*
|
|
|
|
* @param m Message object to add a request to
|
2014-07-13 10:16:10 +02:00
|
|
|
* @return the Message ID which will be used as receipt ID
|
2014-10-05 16:46:39 +02:00
|
|
|
* @deprecated use {@link DeliveryReceiptRequest#addTo(Message)}
|
2013-02-04 10:53:56 +01:00
|
|
|
*/
|
2014-10-05 16:46:39 +02:00
|
|
|
@Deprecated
|
2014-07-13 10:16:10 +02:00
|
|
|
public static String addDeliveryReceiptRequest(Message m) {
|
2014-10-05 16:46:39 +02:00
|
|
|
return DeliveryReceiptRequest.addTo(m);
|
2013-02-04 10:53:56 +01:00
|
|
|
}
|
|
|
|
}
|