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

Make ServiceDiscoveryManager use constant PacketFilters

This commit is contained in:
Florian Schmaus 2014-08-26 13:33:02 +02:00
parent 191c4429da
commit 5d159c4be2

View file

@ -24,6 +24,8 @@ import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.PacketListener; import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnectionRegistry; import org.jivesoftware.smack.XMPPConnectionRegistry;
import org.jivesoftware.smack.XMPPException.XMPPErrorException; import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.IQTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter; import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter; import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
@ -63,9 +65,14 @@ import java.util.logging.Logger;
public class ServiceDiscoveryManager extends Manager { public class ServiceDiscoveryManager extends Manager {
private static final Logger LOGGER = Logger.getLogger(ServiceDiscoveryManager.class.getName()); private static final Logger LOGGER = Logger.getLogger(ServiceDiscoveryManager.class.getName());
private static final PacketFilter GET_DISCOVER_ITEMS = new AndFilter(IQTypeFilter.GET, new PacketTypeFilter(DiscoverItems.class));
private static final PacketFilter GET_DISCOVER_INFO = new AndFilter(IQTypeFilter.GET, new PacketTypeFilter(DiscoverInfo.class));
private static final String DEFAULT_IDENTITY_NAME = "Smack"; private static final String DEFAULT_IDENTITY_NAME = "Smack";
private static final String DEFAULT_IDENTITY_CATEGORY = "client"; private static final String DEFAULT_IDENTITY_CATEGORY = "client";
private static final String DEFAULT_IDENTITY_TYPE = "pc"; private static final String DEFAULT_IDENTITY_TYPE = "pc";
private static DiscoverInfo.Identity defaultIdentity = new Identity(DEFAULT_IDENTITY_CATEGORY, private static DiscoverInfo.Identity defaultIdentity = new Identity(DEFAULT_IDENTITY_CATEGORY,
DEFAULT_IDENTITY_NAME, DEFAULT_IDENTITY_TYPE); DEFAULT_IDENTITY_NAME, DEFAULT_IDENTITY_TYPE);
@ -117,87 +124,72 @@ public class ServiceDiscoveryManager extends Manager {
addFeature(DiscoverItems.NAMESPACE); addFeature(DiscoverItems.NAMESPACE);
// Listen for disco#items requests and answer with an empty result // Listen for disco#items requests and answer with an empty result
PacketFilter packetFilter = new PacketTypeFilter(DiscoverItems.class);
PacketListener packetListener = new PacketListener() { PacketListener packetListener = new PacketListener() {
public void processPacket(Packet packet) throws NotConnectedException { public void processPacket(Packet packet) throws NotConnectedException {
XMPPConnection connection = connection();
if (connection == null) return;
DiscoverItems discoverItems = (DiscoverItems) packet; DiscoverItems discoverItems = (DiscoverItems) packet;
// Send back the items defined in the client if the request is of type GET DiscoverItems response = new DiscoverItems();
if (discoverItems != null && discoverItems.getType() == IQ.Type.get) { response.setType(IQ.Type.result);
DiscoverItems response = new DiscoverItems(); response.setTo(discoverItems.getFrom());
response.setType(IQ.Type.result); response.setPacketID(discoverItems.getPacketID());
response.setTo(discoverItems.getFrom()); response.setNode(discoverItems.getNode());
response.setPacketID(discoverItems.getPacketID());
response.setNode(discoverItems.getNode());
// Add the defined items related to the requested node. Look for // Add the defined items related to the requested node. Look for
// the NodeInformationProvider associated with the requested node. // the NodeInformationProvider associated with the requested node.
NodeInformationProvider nodeInformationProvider = NodeInformationProvider nodeInformationProvider = getNodeInformationProvider(discoverItems.getNode());
getNodeInformationProvider(discoverItems.getNode()); if (nodeInformationProvider != null) {
if (nodeInformationProvider != null) { // Specified node was found, add node items
// Specified node was found, add node items response.addItems(nodeInformationProvider.getNodeItems());
response.addItems(nodeInformationProvider.getNodeItems()); // Add packet extensions
// Add packet extensions response.addExtensions(nodeInformationProvider.getNodePacketExtensions());
response.addExtensions(nodeInformationProvider.getNodePacketExtensions()); } else if(discoverItems.getNode() != null) {
} else if(discoverItems.getNode() != null) { // Return <item-not-found/> error since client doesn't contain
// Return <item-not-found/> error since client doesn't contain // the specified node
// the specified node response.setType(IQ.Type.error);
response.setType(IQ.Type.error); response.setError(new XMPPError(XMPPError.Condition.item_not_found));
response.setError(new XMPPError(XMPPError.Condition.item_not_found));
}
connection.sendPacket(response);
} }
connection().sendPacket(response);
} }
}; };
connection.addPacketListener(packetListener, packetFilter); connection.addPacketListener(packetListener, GET_DISCOVER_ITEMS);
// Listen for disco#info requests and answer the client's supported features // Listen for disco#info requests and answer the client's supported features
// To add a new feature as supported use the #addFeature message // To add a new feature as supported use the #addFeature message
packetFilter = new PacketTypeFilter(DiscoverInfo.class);
packetListener = new PacketListener() { packetListener = new PacketListener() {
public void processPacket(Packet packet) throws NotConnectedException { public void processPacket(Packet packet) throws NotConnectedException {
XMPPConnection connection = connection();
if (connection == null) return;
DiscoverInfo discoverInfo = (DiscoverInfo) packet; DiscoverInfo discoverInfo = (DiscoverInfo) packet;
// Answer the client's supported features if the request is of the GET type // Answer the client's supported features if the request is of the GET type
if (discoverInfo != null && discoverInfo.getType() == IQ.Type.get) { DiscoverInfo response = new DiscoverInfo();
DiscoverInfo response = new DiscoverInfo(); response.setType(IQ.Type.result);
response.setType(IQ.Type.result); response.setTo(discoverInfo.getFrom());
response.setTo(discoverInfo.getFrom()); response.setPacketID(discoverInfo.getPacketID());
response.setPacketID(discoverInfo.getPacketID()); response.setNode(discoverInfo.getNode());
response.setNode(discoverInfo.getNode()); // Add the client's identity and features only if "node" is null
// Add the client's identity and features only if "node" is null // and if the request was not send to a node. If Entity Caps are
// and if the request was not send to a node. If Entity Caps are // enabled the client's identity and features are may also added
// enabled the client's identity and features are may also added // if the right node is chosen
// if the right node is chosen if (discoverInfo.getNode() == null) {
if (discoverInfo.getNode() == null) { addDiscoverInfoTo(response);
addDiscoverInfoTo(response); } else {
// Disco#info was sent to a node. Check if we have information of the
// specified node
NodeInformationProvider nodeInformationProvider = getNodeInformationProvider(discoverInfo.getNode());
if (nodeInformationProvider != null) {
// Node was found. Add node features
response.addFeatures(nodeInformationProvider.getNodeFeatures());
// Add node identities
response.addIdentities(nodeInformationProvider.getNodeIdentities());
// Add packet extensions
response.addExtensions(nodeInformationProvider.getNodePacketExtensions());
} else {
// Return <item-not-found/> error since specified node was not found
response.setType(IQ.Type.error);
response.setError(new XMPPError(XMPPError.Condition.item_not_found));
} }
else {
// Disco#info was sent to a node. Check if we have information of the
// specified node
NodeInformationProvider nodeInformationProvider =
getNodeInformationProvider(discoverInfo.getNode());
if (nodeInformationProvider != null) {
// Node was found. Add node features
response.addFeatures(nodeInformationProvider.getNodeFeatures());
// Add node identities
response.addIdentities(nodeInformationProvider.getNodeIdentities());
// Add packet extensions
response.addExtensions(nodeInformationProvider.getNodePacketExtensions());
}
else {
// Return <item-not-found/> error since specified node was not found
response.setType(IQ.Type.error);
response.setError(new XMPPError(XMPPError.Condition.item_not_found));
}
}
connection.sendPacket(response);
} }
connection().sendPacket(response);
} }
}; };
connection.addPacketListener(packetListener, packetFilter); connection.addPacketListener(packetListener, GET_DISCOVER_INFO);
} }
/** /**