mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-22 03:52:06 +01:00
Improve getMultipleRecipientServiceAddress
and MultipleRecipient (XEP-33) related code in general (e.g. use constants where possible). For getMultipleRecipientServiceAddress() this means that - the "synchronized (services)" block is removed - ServiceDiscoveryManager is only retrieved once
This commit is contained in:
parent
0680553ef7
commit
36a86f2dfc
2 changed files with 39 additions and 36 deletions
|
@ -22,7 +22,6 @@ import org.jivesoftware.smack.SmackException.NoResponseException;
|
|||
import org.jivesoftware.smack.SmackException.FeatureNotSupportedException;
|
||||
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
||||
import org.jivesoftware.smack.XMPPConnection;
|
||||
import org.jivesoftware.smack.XMPPException;
|
||||
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
|
@ -36,6 +35,8 @@ import org.jivesoftware.smackx.disco.packet.DiscoverItems;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* A MultipleRecipientManager allows to send packets to multiple recipients by making use of
|
||||
|
@ -45,7 +46,9 @@ import java.util.List;
|
|||
* @author Gaston Dombiak
|
||||
*/
|
||||
public class MultipleRecipientManager {
|
||||
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(MultipleRecipientManager.class.getName());
|
||||
|
||||
/**
|
||||
* Create a cache to hold the 100 most recently accessed elements for a period of
|
||||
* 24 hours.
|
||||
|
@ -205,7 +208,7 @@ public class MultipleRecipientManager {
|
|||
*/
|
||||
public static MultipleRecipientInfo getMultipleRecipientInfo(Packet packet) {
|
||||
MultipleAddresses extension = (MultipleAddresses) packet
|
||||
.getExtension("addresses", "http://jabber.org/protocol/address");
|
||||
.getExtension(MultipleAddresses.ELEMENT, MultipleAddresses.NAMESPACE);
|
||||
return extension == null ? null : new MultipleRecipientInfo(extension);
|
||||
}
|
||||
|
||||
|
@ -295,40 +298,37 @@ public class MultipleRecipientManager {
|
|||
String serviceName = connection.getServiceName();
|
||||
String serviceAddress = (String) services.get(serviceName);
|
||||
if (serviceAddress == null) {
|
||||
synchronized (services) {
|
||||
serviceAddress = (String) services.get(serviceName);
|
||||
if (serviceAddress == null) {
|
||||
|
||||
// Send the disco packet to the server itself
|
||||
DiscoverInfo info = ServiceDiscoveryManager.getInstanceFor(connection).discoverInfo(
|
||||
serviceName);
|
||||
// Check if the server supports JEP-33
|
||||
if (info.containsFeature("http://jabber.org/protocol/address")) {
|
||||
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
||||
// Send the disco packet to the server itself
|
||||
DiscoverInfo info = sdm.discoverInfo(serviceName);
|
||||
// Check if the server supports XEP-33
|
||||
if (info.containsFeature(MultipleAddresses.NAMESPACE)) {
|
||||
serviceAddress = serviceName;
|
||||
}
|
||||
else {
|
||||
// Get the disco items and send the disco packet to each server item
|
||||
DiscoverItems items = sdm.discoverItems(serviceName);
|
||||
for (DiscoverItems.Item item : items.getItems()) {
|
||||
try {
|
||||
info = sdm.discoverInfo(item.getEntityID(), item.getNode());
|
||||
}
|
||||
catch (XMPPErrorException|NoResponseException e) {
|
||||
// Don't throw this exceptions if one of the server's items fail
|
||||
LOGGER.log(Level.WARNING,
|
||||
"Exception while discovering info of " + item.getEntityID()
|
||||
+ " node: " + item.getNode(), e);
|
||||
continue;
|
||||
}
|
||||
if (info.containsFeature(MultipleAddresses.NAMESPACE)) {
|
||||
serviceAddress = serviceName;
|
||||
break;
|
||||
}
|
||||
else {
|
||||
// Get the disco items and send the disco packet to each server item
|
||||
DiscoverItems items = ServiceDiscoveryManager.getInstanceFor(connection).discoverItems(
|
||||
serviceName);
|
||||
for (DiscoverItems.Item item : items.getItems()) {
|
||||
try {
|
||||
info = ServiceDiscoveryManager.getInstanceFor(connection).discoverInfo(
|
||||
item.getEntityID(), item.getNode());
|
||||
}
|
||||
catch (XMPPErrorException|NoResponseException e) {
|
||||
// Don't throw this exceptions if one of the server's items fail
|
||||
continue;
|
||||
}
|
||||
if (info.containsFeature("http://jabber.org/protocol/address")) {
|
||||
serviceAddress = serviceName;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Cache the discovered information
|
||||
services.put(serviceName, serviceAddress == null ? "" : serviceAddress);
|
||||
}
|
||||
}
|
||||
// Use the empty string to indicate that no service is known for this connection
|
||||
serviceAddress = serviceAddress == null ? "" : serviceAddress;
|
||||
// Cache the discovered information
|
||||
services.put(serviceName, serviceAddress);
|
||||
}
|
||||
|
||||
return "".equals(serviceAddress) ? null : serviceAddress;
|
||||
|
|
|
@ -30,6 +30,9 @@ import java.util.List;
|
|||
*/
|
||||
public class MultipleAddresses implements PacketExtension {
|
||||
|
||||
public static final String NAMESPACE = "http://jabber.org/protocol/address";
|
||||
public static final String ELEMENT = "addresses";
|
||||
|
||||
public static final String BCC = "bcc";
|
||||
public static final String CC = "cc";
|
||||
public static final String NO_REPLY = "noreply";
|
||||
|
@ -94,17 +97,17 @@ public class MultipleAddresses implements PacketExtension {
|
|||
}
|
||||
|
||||
public String getElementName() {
|
||||
return "addresses";
|
||||
return ELEMENT;
|
||||
}
|
||||
|
||||
public String getNamespace() {
|
||||
return "http://jabber.org/protocol/address";
|
||||
return NAMESPACE;
|
||||
}
|
||||
|
||||
public String toXML() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("<").append(getElementName());
|
||||
buf.append(" xmlns=\"").append(getNamespace()).append("\">");
|
||||
buf.append(" xmlns=\"").append(NAMESPACE).append("\">");
|
||||
// Loop through all the addresses and append them to the string buffer
|
||||
for (Iterator<Address> i = addresses.iterator(); i.hasNext();) {
|
||||
Address address = (Address) i.next();
|
||||
|
|
Loading…
Reference in a new issue