mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-29 23:42:06 +01:00
1 - Small fix in Roster to return unavailable presence with from.
2 - Throw exception if parsing of vCard fails. git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@7217 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
0bd96e5156
commit
1f8cb4452b
3 changed files with 115 additions and 97 deletions
|
@ -36,7 +36,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
/**
|
/**
|
||||||
* Represents a user's roster, which is the collection of users a person receives
|
* Represents a user's roster, which is the collection of users a person receives
|
||||||
* presence updates for. Roster items are categorized into groups for easier management.<p>
|
* presence updates for. Roster items are categorized into groups for easier management.<p>
|
||||||
*
|
* <p/>
|
||||||
* Others users may attempt to subscribe to this user using a subscription request. Three
|
* Others users may attempt to subscribe to this user using a subscription request. Three
|
||||||
* modes are supported for handling these requests: <ul>
|
* modes are supported for handling these requests: <ul>
|
||||||
* <li>{@link SubscriptionMode#accept_all accept_all} -- accept all subscription requests.</li>
|
* <li>{@link SubscriptionMode#accept_all accept_all} -- accept all subscription requests.</li>
|
||||||
|
@ -44,8 +44,8 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
* <li>{@link SubscriptionMode#manual manual} -- manually process all subscription requests.</li>
|
* <li>{@link SubscriptionMode#manual manual} -- manually process all subscription requests.</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
* @see XMPPConnection#getRoster()
|
|
||||||
* @author Matt Tucker
|
* @author Matt Tucker
|
||||||
|
* @see XMPPConnection#getRoster()
|
||||||
*/
|
*/
|
||||||
public class Roster {
|
public class Roster {
|
||||||
|
|
||||||
|
@ -99,7 +99,7 @@ public class Roster {
|
||||||
*/
|
*/
|
||||||
Roster(final XMPPConnection connection) {
|
Roster(final XMPPConnection connection) {
|
||||||
this.connection = connection;
|
this.connection = connection;
|
||||||
groups = new ConcurrentHashMap<String,RosterGroup>();
|
groups = new ConcurrentHashMap<String, RosterGroup>();
|
||||||
unfiledEntries = new CopyOnWriteArrayList<RosterEntry>();
|
unfiledEntries = new CopyOnWriteArrayList<RosterEntry>();
|
||||||
entries = new CopyOnWriteArrayList<RosterEntry>();
|
entries = new CopyOnWriteArrayList<RosterEntry>();
|
||||||
rosterListeners = new CopyOnWriteArrayList<RosterListener>();
|
rosterListeners = new CopyOnWriteArrayList<RosterListener>();
|
||||||
|
@ -141,7 +141,7 @@ public class Roster {
|
||||||
* Returns the subscription processing mode, which dictates what action
|
* Returns the subscription processing mode, which dictates what action
|
||||||
* Smack will take when subscription requests from other users are made.
|
* Smack will take when subscription requests from other users are made.
|
||||||
* The default subscription mode is {@link SubscriptionMode#accept_all}.<p>
|
* The default subscription mode is {@link SubscriptionMode#accept_all}.<p>
|
||||||
*
|
* <p/>
|
||||||
* If using the manual mode, a PacketListener should be registered that
|
* If using the manual mode, a PacketListener should be registered that
|
||||||
* listens for Presence packets that have a type of
|
* listens for Presence packets that have a type of
|
||||||
* {@link org.jivesoftware.smack.packet.Presence.Type#subscribe}.
|
* {@link org.jivesoftware.smack.packet.Presence.Type#subscribe}.
|
||||||
|
@ -156,7 +156,7 @@ public class Roster {
|
||||||
* Sets the subscription processing mode, which dictates what action
|
* Sets the subscription processing mode, which dictates what action
|
||||||
* Smack will take when subscription requests from other users are made.
|
* Smack will take when subscription requests from other users are made.
|
||||||
* The default subscription mode is {@link SubscriptionMode#accept_all}.<p>
|
* The default subscription mode is {@link SubscriptionMode#accept_all}.<p>
|
||||||
*
|
* <p/>
|
||||||
* If using the manual mode, a PacketListener should be registered that
|
* If using the manual mode, a PacketListener should be registered that
|
||||||
* listens for Presence packets that have a type of
|
* listens for Presence packets that have a type of
|
||||||
* {@link org.jivesoftware.smack.packet.Presence.Type#subscribe}.
|
* {@link org.jivesoftware.smack.packet.Presence.Type#subscribe}.
|
||||||
|
@ -200,7 +200,7 @@ public class Roster {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new group.<p>
|
* Creates a new group.<p>
|
||||||
*
|
* <p/>
|
||||||
* Note: you must add at least one entry to the group for the group to be kept
|
* Note: you must add at least one entry to the group for the group to be kept
|
||||||
* after a logout/login. This is due to the way that XMPP stores group information.
|
* after a logout/login. This is due to the way that XMPP stores group information.
|
||||||
*
|
*
|
||||||
|
@ -226,7 +226,7 @@ public class Roster {
|
||||||
* the roster entry won't belong to a group.
|
* the roster entry won't belong to a group.
|
||||||
* @throws XMPPException if an XMPP exception occurs.
|
* @throws XMPPException if an XMPP exception occurs.
|
||||||
*/
|
*/
|
||||||
public void createEntry(String user, String name, String [] groups) throws XMPPException {
|
public void createEntry(String user, String name, String[] groups) throws XMPPException {
|
||||||
// Create and send roster entry creation packet.
|
// Create and send roster entry creation packet.
|
||||||
RosterPacket rosterPacket = new RosterPacket();
|
RosterPacket rosterPacket = new RosterPacket();
|
||||||
rosterPacket.setType(IQ.Type.SET);
|
rosterPacket.setType(IQ.Type.SET);
|
||||||
|
@ -243,7 +243,7 @@ public class Roster {
|
||||||
PacketCollector collector = connection.createPacketCollector(
|
PacketCollector collector = connection.createPacketCollector(
|
||||||
new PacketIDFilter(rosterPacket.getPacketID()));
|
new PacketIDFilter(rosterPacket.getPacketID()));
|
||||||
connection.sendPacket(rosterPacket);
|
connection.sendPacket(rosterPacket);
|
||||||
IQ response = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
|
IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
|
||||||
collector.cancel();
|
collector.cancel();
|
||||||
if (response == null) {
|
if (response == null) {
|
||||||
throw new XMPPException("No response from the server.");
|
throw new XMPPException("No response from the server.");
|
||||||
|
@ -283,7 +283,7 @@ public class Roster {
|
||||||
PacketCollector collector = connection.createPacketCollector(
|
PacketCollector collector = connection.createPacketCollector(
|
||||||
new PacketIDFilter(packet.getPacketID()));
|
new PacketIDFilter(packet.getPacketID()));
|
||||||
connection.sendPacket(packet);
|
connection.sendPacket(packet);
|
||||||
IQ response = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
|
IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
|
||||||
collector.cancel();
|
collector.cancel();
|
||||||
if (response == null) {
|
if (response == null) {
|
||||||
throw new XMPPException("No response from the server.");
|
throw new XMPPException("No response from the server.");
|
||||||
|
@ -410,14 +410,14 @@ public class Roster {
|
||||||
* Returns the presence info for a particular user. If the user is offline, or
|
* Returns the presence info for a particular user. If the user is offline, or
|
||||||
* if no presence data is available (such as when you are not subscribed to the
|
* if no presence data is available (such as when you are not subscribed to the
|
||||||
* user's presence updates), unavailable presence will be returned.<p>
|
* user's presence updates), unavailable presence will be returned.<p>
|
||||||
*
|
* <p/>
|
||||||
* If the user has several presences (one for each resource), then the presence with
|
* If the user has several presences (one for each resource), then the presence with
|
||||||
* highest priority will be returned. If multiple presences have the same priority,
|
* highest priority will be returned. If multiple presences have the same priority,
|
||||||
* the one with the "most available" presence mode will be returned. In order,
|
* the one with the "most available" presence mode will be returned. In order,
|
||||||
* that's {@link Presence.Mode#chat free to chat}, {@link Presence.Mode#available available},
|
* that's {@link Presence.Mode#chat free to chat}, {@link Presence.Mode#available available},
|
||||||
* {@link Presence.Mode#away away}, {@link Presence.Mode#xa extended away}, and
|
* {@link Presence.Mode#away away}, {@link Presence.Mode#xa extended away}, and
|
||||||
* {@link Presence.Mode#dnd do not disturb}.<p>
|
* {@link Presence.Mode#dnd do not disturb}.<p>
|
||||||
*
|
* <p/>
|
||||||
* Note that presence information is received asynchronously. So, just after logging
|
* Note that presence information is received asynchronously. So, just after logging
|
||||||
* in to the server, presence values for users in the roster may be unavailable
|
* in to the server, presence values for users in the roster may be unavailable
|
||||||
* even if they are actually online. In other words, the value returned by this
|
* even if they are actually online. In other words, the value returned by this
|
||||||
|
@ -436,7 +436,9 @@ public class Roster {
|
||||||
String key = getPresenceMapKey(StringUtils.parseBareAddress(user));
|
String key = getPresenceMapKey(StringUtils.parseBareAddress(user));
|
||||||
Map<String, Presence> userPresences = presenceMap.get(key);
|
Map<String, Presence> userPresences = presenceMap.get(key);
|
||||||
if (userPresences == null) {
|
if (userPresences == null) {
|
||||||
return new Presence(Presence.Type.unavailable);
|
Presence presence = new Presence(Presence.Type.unavailable);
|
||||||
|
presence.setFrom(user);
|
||||||
|
return presence;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Find the resource with the highest priority
|
// Find the resource with the highest priority
|
||||||
|
@ -467,7 +469,9 @@ public class Roster {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (presence == null) {
|
if (presence == null) {
|
||||||
return new Presence(Presence.Type.unavailable);
|
presence = new Presence(Presence.Type.unavailable);
|
||||||
|
presence.setFrom(user);
|
||||||
|
return presence;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return presence;
|
return presence;
|
||||||
|
@ -489,12 +493,16 @@ public class Roster {
|
||||||
String resource = StringUtils.parseResource(userWithResource);
|
String resource = StringUtils.parseResource(userWithResource);
|
||||||
Map<String, Presence> userPresences = presenceMap.get(key);
|
Map<String, Presence> userPresences = presenceMap.get(key);
|
||||||
if (userPresences == null) {
|
if (userPresences == null) {
|
||||||
return new Presence(Presence.Type.unavailable);
|
Presence presence = new Presence(Presence.Type.unavailable);
|
||||||
|
presence.setFrom(userWithResource);
|
||||||
|
return presence;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Presence presence = userPresences.get(resource);
|
Presence presence = userPresences.get(resource);
|
||||||
if (presence == null) {
|
if (presence == null) {
|
||||||
return new Presence(Presence.Type.unavailable);
|
presence = new Presence(Presence.Type.unavailable);
|
||||||
|
presence.setFrom(userWithResource);
|
||||||
|
return presence;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return presence;
|
return presence;
|
||||||
|
@ -517,7 +525,9 @@ public class Roster {
|
||||||
String key = getPresenceMapKey(user);
|
String key = getPresenceMapKey(user);
|
||||||
Map<String, Presence> userPresences = presenceMap.get(key);
|
Map<String, Presence> userPresences = presenceMap.get(key);
|
||||||
if (userPresences == null) {
|
if (userPresences == null) {
|
||||||
return Arrays.asList(new Presence(Presence.Type.unavailable)).iterator();
|
Presence presence = new Presence(Presence.Type.unavailable);
|
||||||
|
presence.setFrom(user);
|
||||||
|
return Arrays.asList(presence).iterator();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return userPresences.values().iterator();
|
return userPresences.values().iterator();
|
||||||
|
@ -584,8 +594,7 @@ public class Roster {
|
||||||
* @param deletedEntries the collection of address of the deleted contacts.
|
* @param deletedEntries the collection of address of the deleted contacts.
|
||||||
*/
|
*/
|
||||||
private void fireRosterChangedEvent(Collection<String> addedEntries, Collection<String> updatedEntries,
|
private void fireRosterChangedEvent(Collection<String> addedEntries, Collection<String> updatedEntries,
|
||||||
Collection<String> deletedEntries)
|
Collection<String> deletedEntries) {
|
||||||
{
|
|
||||||
for (RosterListener listener : rosterListeners) {
|
for (RosterListener listener : rosterListeners) {
|
||||||
if (!addedEntries.isEmpty()) {
|
if (!addedEntries.isEmpty()) {
|
||||||
listener.entriesAdded(addedEntries);
|
listener.entriesAdded(addedEntries);
|
||||||
|
@ -640,8 +649,9 @@ public class Roster {
|
||||||
* Listens for all presence packets and processes them.
|
* Listens for all presence packets and processes them.
|
||||||
*/
|
*/
|
||||||
private class PresencePacketListener implements PacketListener {
|
private class PresencePacketListener implements PacketListener {
|
||||||
|
|
||||||
public void processPacket(Packet packet) {
|
public void processPacket(Packet packet) {
|
||||||
Presence presence = (Presence)packet;
|
Presence presence = (Presence) packet;
|
||||||
String from = presence.getFrom();
|
String from = presence.getFrom();
|
||||||
String key = getPresenceMapKey(from);
|
String key = getPresenceMapKey(from);
|
||||||
|
|
||||||
|
|
|
@ -39,13 +39,14 @@ import java.util.List;
|
||||||
* vCard provider.
|
* vCard provider.
|
||||||
*
|
*
|
||||||
* @author Gaston Dombiak
|
* @author Gaston Dombiak
|
||||||
|
* @author Derek DeMoro
|
||||||
*/
|
*/
|
||||||
public class VCardProvider implements IQProvider {
|
public class VCardProvider implements IQProvider {
|
||||||
|
|
||||||
private final static String PREFERRED_ENCODING = "UTF-8";
|
private static final String PREFERRED_ENCODING = "UTF-8";
|
||||||
|
|
||||||
public IQ parseIQ(XmlPullParser parser) throws Exception {
|
public IQ parseIQ(XmlPullParser parser) throws Exception {
|
||||||
StringBuilder sb = new StringBuilder();
|
final StringBuilder sb = new StringBuilder();
|
||||||
try {
|
try {
|
||||||
int event = parser.getEventType();
|
int event = parser.getEventType();
|
||||||
// get the content
|
// get the content
|
||||||
|
@ -68,33 +69,39 @@ public class VCardProvider implements IQProvider {
|
||||||
|
|
||||||
event = parser.next();
|
event = parser.next();
|
||||||
}
|
}
|
||||||
} catch (XmlPullParserException e) {
|
}
|
||||||
|
catch (XmlPullParserException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (IOException e) {
|
}
|
||||||
|
catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
String xmlText = sb.toString();
|
String xmlText = sb.toString();
|
||||||
return _createVCardFromXml(xmlText);
|
return createVCardFromXML(xmlText);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static VCard _createVCardFromXml(String xmlText) {
|
/**
|
||||||
|
* Builds a users vCard from xml file.
|
||||||
|
*
|
||||||
|
* @param xml the xml representing a users vCard.
|
||||||
|
* @return the VCard.
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public static VCard createVCardFromXML(String xml) throws Exception {
|
||||||
VCard vCard = new VCard();
|
VCard vCard = new VCard();
|
||||||
try {
|
|
||||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||||
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
||||||
Document document = documentBuilder.parse(
|
Document document = documentBuilder.parse(
|
||||||
new ByteArrayInputStream(xmlText.getBytes(PREFERRED_ENCODING)));
|
new ByteArrayInputStream(xml.getBytes(PREFERRED_ENCODING)));
|
||||||
|
|
||||||
new VCardReader(vCard, document).initializeFields();
|
new VCardReader(vCard, document).initializeFields();
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace(System.err);
|
|
||||||
}
|
|
||||||
return vCard;
|
return vCard;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class VCardReader {
|
private static class VCardReader {
|
||||||
|
|
||||||
private final VCard vCard;
|
private final VCard vCard;
|
||||||
private final Document document;
|
private final Document document;
|
||||||
|
|
||||||
|
@ -180,7 +187,7 @@ public class VCardProvider implements IQProvider {
|
||||||
List code = new ArrayList();
|
List code = new ArrayList();
|
||||||
List value = new ArrayList();
|
List value = new ArrayList();
|
||||||
NodeList childNodes = addressNode.getChildNodes();
|
NodeList childNodes = addressNode.getChildNodes();
|
||||||
for(int j = 0; j < childNodes.getLength(); j++) {
|
for (int j = 0; j < childNodes.getLength(); j++) {
|
||||||
Node node = childNodes.item(j);
|
Node node = childNodes.item(j);
|
||||||
if (node.getNodeType() != Node.ELEMENT_NODE) continue;
|
if (node.getNodeType() != Node.ELEMENT_NODE) continue;
|
||||||
String nodeName = node.getNodeName();
|
String nodeName = node.getNodeName();
|
||||||
|
@ -221,7 +228,8 @@ public class VCardProvider implements IQProvider {
|
||||||
String field = element.getNodeName();
|
String field = element.getNodeName();
|
||||||
if (element.getChildNodes().getLength() == 0) {
|
if (element.getChildNodes().getLength() == 0) {
|
||||||
vCard.setField(field, "");
|
vCard.setField(field, "");
|
||||||
} else if (element.getChildNodes().getLength() == 1 &&
|
}
|
||||||
|
else if (element.getChildNodes().getLength() == 1 &&
|
||||||
element.getChildNodes().item(0) instanceof Text) {
|
element.getChildNodes().item(0) instanceof Text) {
|
||||||
vCard.setField(field, getTextContent(element));
|
vCard.setField(field, getTextContent(element));
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,23 +62,23 @@ public class VCardTest extends SmackTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNoWorkHomeSpecifier_EMAIL() throws Throwable {
|
public void testNoWorkHomeSpecifier_EMAIL() throws Throwable {
|
||||||
VCard card = VCardProvider._createVCardFromXml("<vcard><EMAIL><USERID>foo@fee.www.bar</USERID></EMAIL></vcard>");
|
VCard card = VCardProvider.createVCardFromXML("<vcard><EMAIL><USERID>foo@fee.www.bar</USERID></EMAIL></vcard>");
|
||||||
assertEquals("foo@fee.www.bar", card.getEmailHome());
|
assertEquals("foo@fee.www.bar", card.getEmailHome());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNoWorkHomeSpecifier_TEL() throws Throwable {
|
public void testNoWorkHomeSpecifier_TEL() throws Throwable {
|
||||||
VCard card = VCardProvider._createVCardFromXml("<vcard><TEL><FAX/><NUMBER>3443233</NUMBER></TEL></vcard>");
|
VCard card = VCardProvider.createVCardFromXML("<vcard><TEL><FAX/><NUMBER>3443233</NUMBER></TEL></vcard>");
|
||||||
assertEquals("3443233", card.getPhoneWork("FAX"));
|
assertEquals("3443233", card.getPhoneWork("FAX"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNoWorkHomeSpecifier_ADDR() throws Throwable {
|
public void testNoWorkHomeSpecifier_ADDR() throws Throwable {
|
||||||
VCard card = VCardProvider._createVCardFromXml("<vcard><ADR><STREET>Some street</STREET><FF>ddss</FF></ADR></vcard>");
|
VCard card = VCardProvider.createVCardFromXML("<vcard><ADR><STREET>Some street</STREET><FF>ddss</FF></ADR></vcard>");
|
||||||
assertEquals("Some street", card.getAddressFieldWork("STREET"));
|
assertEquals("Some street", card.getAddressFieldWork("STREET"));
|
||||||
assertEquals("ddss", card.getAddressFieldWork("FF"));
|
assertEquals("ddss", card.getAddressFieldWork("FF"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testFN() throws Throwable {
|
public void testFN() throws Throwable {
|
||||||
VCard card = VCardProvider._createVCardFromXml("<vcard><FN>kir max</FN></vcard>");
|
VCard card = VCardProvider.createVCardFromXML("<vcard><FN>kir max</FN></vcard>");
|
||||||
assertEquals("kir max", card.getField("FN"));
|
assertEquals("kir max", card.getField("FN"));
|
||||||
// assertEquals("kir max", card.getFullName());
|
// assertEquals("kir max", card.getFullName());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue