1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-22 12:02:05 +01:00

Improve DeliveryReceipt code, add javadoc

Also make Packet.getExtension() use Generics
This commit is contained in:
Florian Schmaus 2014-07-13 10:16:10 +02:00
parent c063fb1376
commit bd0ada480b
5 changed files with 48 additions and 17 deletions

View file

@ -240,7 +240,8 @@ public abstract class Packet {
* @param namespace the XML element namespace of the packet extension. * @param namespace the XML element namespace of the packet extension.
* @return the extension, or <tt>null</tt> if it doesn't exist. * @return the extension, or <tt>null</tt> if it doesn't exist.
*/ */
public PacketExtension getExtension(String elementName, String namespace) { @SuppressWarnings("unchecked")
public <PE extends PacketExtension> PE getExtension(String elementName, String namespace) {
if (namespace == null) { if (namespace == null) {
return null; return null;
} }
@ -248,7 +249,7 @@ public abstract class Packet {
if ((elementName == null || elementName.equals(ext.getElementName())) if ((elementName == null || elementName.equals(ext.getElementName()))
&& namespace.equals(ext.getNamespace())) && namespace.equals(ext.getNamespace()))
{ {
return ext; return (PE) ext;
} }
} }
return null; return null;

View file

@ -1,6 +1,6 @@
/** /**
* *
* Copyright the original author or authors * Copyright 2013-2014 the original author or authors
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,6 +19,7 @@ package org.jivesoftware.smackx.receipts;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension; import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.provider.EmbeddedExtensionProvider; import org.jivesoftware.smack.provider.EmbeddedExtensionProvider;
@ -63,6 +64,16 @@ public class DeliveryReceipt implements PacketExtension
return "<received xmlns='" + NAMESPACE + "' id='" + id + "'/>"; return "<received xmlns='" + NAMESPACE + "' id='" + id + "'/>";
} }
/**
* Get the {@link DeliveryReceipt} extension of the packet, if any.
*
* @param p the packet
* @return the {@link DeliveryReceipt} extension or {@code null}
*/
public static DeliveryReceipt getFrom(Packet p) {
return p.getExtension(ELEMENT, NAMESPACE);
}
/** /**
* This Provider parses and returns DeliveryReceipt packets. * This Provider parses and returns DeliveryReceipt packets.
*/ */

View file

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2013 Georg Lukas * Copyright 2013-2014 Georg Lukas
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -43,8 +43,7 @@ import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
*/ */
public class DeliveryReceiptManager extends Manager implements PacketListener { public class DeliveryReceiptManager extends Manager implements PacketListener {
private static Map<XMPPConnection, DeliveryReceiptManager> instances = private static Map<XMPPConnection, DeliveryReceiptManager> instances = new WeakHashMap<XMPPConnection, DeliveryReceiptManager>();
Collections.synchronizedMap(new WeakHashMap<XMPPConnection, DeliveryReceiptManager>());
static { static {
XMPPConnection.addConnectionCreationListener(new ConnectionCreationListener() { XMPPConnection.addConnectionCreationListener(new ConnectionCreationListener() {
@ -62,7 +61,6 @@ public class DeliveryReceiptManager extends Manager implements PacketListener {
super(connection); super(connection);
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection); ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
sdm.addFeature(DeliveryReceipt.NAMESPACE); sdm.addFeature(DeliveryReceipt.NAMESPACE);
instances.put(connection, this);
// register listener for delivery receipts and requests // register listener for delivery receipts and requests
connection.addPacketListener(this, new PacketExtensionFilter(DeliveryReceipt.NAMESPACE)); connection.addPacketListener(this, new PacketExtensionFilter(DeliveryReceipt.NAMESPACE));
@ -80,6 +78,7 @@ public class DeliveryReceiptManager extends Manager implements PacketListener {
if (receiptManager == null) { if (receiptManager == null) {
receiptManager = new DeliveryReceiptManager(connection); receiptManager = new DeliveryReceiptManager(connection);
instances.put(connection, receiptManager);
} }
return receiptManager; return receiptManager;
@ -101,20 +100,17 @@ public class DeliveryReceiptManager extends Manager implements PacketListener {
// handle incoming receipts and receipt requests // handle incoming receipts and receipt requests
@Override @Override
public void processPacket(Packet packet) throws NotConnectedException { public void processPacket(Packet packet) throws NotConnectedException {
DeliveryReceipt dr = (DeliveryReceipt)packet.getExtension( DeliveryReceipt dr = DeliveryReceipt.getFrom(packet);
DeliveryReceipt.ELEMENT, DeliveryReceipt.NAMESPACE);
if (dr != null) { if (dr != null) {
// notify listeners of incoming receipt // notify listeners of incoming receipt
for (ReceiptReceivedListener l : receiptReceivedListeners) { for (ReceiptReceivedListener l : receiptReceivedListeners) {
l.onReceiptReceived(packet.getFrom(), packet.getTo(), dr.getId()); l.onReceiptReceived(packet.getFrom(), packet.getTo(), dr.getId());
} }
} }
// if enabled, automatically send a receipt // if enabled, automatically send a receipt
if (auto_receipts_enabled) { if (auto_receipts_enabled) {
DeliveryReceiptRequest drr = (DeliveryReceiptRequest)packet.getExtension( DeliveryReceiptRequest drr = DeliveryReceiptRequest.getFrom(packet);
DeliveryReceiptRequest.ELEMENT, DeliveryReceipt.NAMESPACE);
if (drr != null) { if (drr != null) {
XMPPConnection connection = connection(); XMPPConnection connection = connection();
Message ack = new Message(packet.getFrom(), Message.Type.normal); Message ack = new Message(packet.getFrom(), Message.Type.normal);
@ -182,8 +178,7 @@ public class DeliveryReceiptManager extends Manager implements PacketListener {
* @return true if a delivery receipt was requested * @return true if a delivery receipt was requested
*/ */
public static boolean hasDeliveryReceiptRequest(Packet p) { public static boolean hasDeliveryReceiptRequest(Packet p) {
return (p.getExtension(DeliveryReceiptRequest.ELEMENT, return (DeliveryReceiptRequest.getFrom(p) != null);
DeliveryReceipt.NAMESPACE) != null);
} }
/** /**
@ -193,8 +188,10 @@ public class DeliveryReceiptManager extends Manager implements PacketListener {
* therefore only allow Message as the parameter type. * therefore only allow Message as the parameter type.
* *
* @param m Message object to add a request to * @param m Message object to add a request to
* @return the Message ID which will be used as receipt ID
*/ */
public static void addDeliveryReceiptRequest(Message m) { public static String addDeliveryReceiptRequest(Message m) {
m.addExtension(new DeliveryReceiptRequest()); m.addExtension(new DeliveryReceiptRequest());
return m.getPacketID();
} }
} }

View file

@ -1,6 +1,6 @@
/** /**
* *
* Copyright the original author or authors * Copyright 2013-2014 the original author or authors
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,6 +16,7 @@
*/ */
package org.jivesoftware.smackx.receipts; package org.jivesoftware.smackx.receipts;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension; import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.provider.PacketExtensionProvider; import org.jivesoftware.smack.provider.PacketExtensionProvider;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
@ -45,6 +46,16 @@ public class DeliveryReceiptRequest implements PacketExtension
return "<request xmlns='" + DeliveryReceipt.NAMESPACE + "'/>"; return "<request xmlns='" + DeliveryReceipt.NAMESPACE + "'/>";
} }
/**
* Get the {@link DeliveryReceiptRequest} extension of the packet, if any.
*
* @param p the packet
* @return the {@link DeliveryReceiptRequest} extension or {@code null}
*/
public static DeliveryReceiptRequest getFrom(Packet p) {
return p.getExtension(ELEMENT, DeliveryReceipt.NAMESPACE);
}
/** /**
* This Provider parses and returns DeliveryReceiptRequest packets. * This Provider parses and returns DeliveryReceiptRequest packets.
*/ */

View file

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2013 Georg Lukas * Copyright 2013-2014 Georg Lukas
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,5 +22,16 @@ package org.jivesoftware.smackx.receipts;
* Implement this and add a listener to get notified. * Implement this and add a listener to get notified.
*/ */
public interface ReceiptReceivedListener { public interface ReceiptReceivedListener {
/**
* Callback invoked when a new receipt got received.
* <p>
* {@code receiptId} correspondents to the message ID, which can be obtained with
* {@link org.jivesoftware.smack.packet.Packet#getPacketID()}.
* </p>
*
* @param fromJid the jid that send this receipt
* @param toJid the jid which received this receipt
* @param receiptId the message ID of the packet which has been received and this receipt is for
*/
void onReceiptReceived(String fromJid, String toJid, String receiptId); void onReceiptReceived(String fromJid, String toJid, String receiptId);
} }