1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-26 14:02: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:
Derek DeMoro 2007-02-20 17:02:39 +00:00 committed by derek
parent 0bd96e5156
commit 1f8cb4452b
3 changed files with 115 additions and 97 deletions

View file

@ -36,7 +36,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
/**
* 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>
*
* <p/>
* Others users may attempt to subscribe to this user using a subscription request. Three
* modes are supported for handling these requests: <ul>
* <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>
* </ul>
*
* @see XMPPConnection#getRoster()
* @author Matt Tucker
* @see XMPPConnection#getRoster()
*/
public class Roster {
@ -99,7 +99,7 @@ public class Roster {
*/
Roster(final XMPPConnection connection) {
this.connection = connection;
groups = new ConcurrentHashMap<String,RosterGroup>();
groups = new ConcurrentHashMap<String, RosterGroup>();
unfiledEntries = new CopyOnWriteArrayList<RosterEntry>();
entries = new CopyOnWriteArrayList<RosterEntry>();
rosterListeners = new CopyOnWriteArrayList<RosterListener>();
@ -141,7 +141,7 @@ public class Roster {
* Returns the subscription processing mode, which dictates what action
* Smack will take when subscription requests from other users are made.
* The default subscription mode is {@link SubscriptionMode#accept_all}.<p>
*
* <p/>
* If using the manual mode, a PacketListener should be registered that
* listens for Presence packets that have a type of
* {@link org.jivesoftware.smack.packet.Presence.Type#subscribe}.
@ -156,7 +156,7 @@ public class Roster {
* Sets the subscription processing mode, which dictates what action
* Smack will take when subscription requests from other users are made.
* The default subscription mode is {@link SubscriptionMode#accept_all}.<p>
*
* <p/>
* If using the manual mode, a PacketListener should be registered that
* listens for Presence packets that have a type of
* {@link org.jivesoftware.smack.packet.Presence.Type#subscribe}.
@ -200,7 +200,7 @@ public class Roster {
/**
* Creates a new group.<p>
*
* <p/>
* 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.
*
@ -226,7 +226,7 @@ public class Roster {
* the roster entry won't belong to a group.
* @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.
RosterPacket rosterPacket = new RosterPacket();
rosterPacket.setType(IQ.Type.SET);
@ -243,7 +243,7 @@ public class Roster {
PacketCollector collector = connection.createPacketCollector(
new PacketIDFilter(rosterPacket.getPacketID()));
connection.sendPacket(rosterPacket);
IQ response = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
collector.cancel();
if (response == null) {
throw new XMPPException("No response from the server.");
@ -283,7 +283,7 @@ public class Roster {
PacketCollector collector = connection.createPacketCollector(
new PacketIDFilter(packet.getPacketID()));
connection.sendPacket(packet);
IQ response = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
collector.cancel();
if (response == null) {
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
* 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>
*
* <p/>
* 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,
* 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},
* {@link Presence.Mode#away away}, {@link Presence.Mode#xa extended away}, and
* {@link Presence.Mode#dnd do not disturb}.<p>
*
* <p/>
* 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
* 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));
Map<String, Presence> userPresences = presenceMap.get(key);
if (userPresences == null) {
return new Presence(Presence.Type.unavailable);
Presence presence = new Presence(Presence.Type.unavailable);
presence.setFrom(user);
return presence;
}
else {
// Find the resource with the highest priority
@ -467,7 +469,9 @@ public class Roster {
}
}
if (presence == null) {
return new Presence(Presence.Type.unavailable);
presence = new Presence(Presence.Type.unavailable);
presence.setFrom(user);
return presence;
}
else {
return presence;
@ -489,12 +493,16 @@ public class Roster {
String resource = StringUtils.parseResource(userWithResource);
Map<String, Presence> userPresences = presenceMap.get(key);
if (userPresences == null) {
return new Presence(Presence.Type.unavailable);
Presence presence = new Presence(Presence.Type.unavailable);
presence.setFrom(userWithResource);
return presence;
}
else {
Presence presence = userPresences.get(resource);
if (presence == null) {
return new Presence(Presence.Type.unavailable);
presence = new Presence(Presence.Type.unavailable);
presence.setFrom(userWithResource);
return presence;
}
else {
return presence;
@ -517,7 +525,9 @@ public class Roster {
String key = getPresenceMapKey(user);
Map<String, Presence> userPresences = presenceMap.get(key);
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 {
return userPresences.values().iterator();
@ -584,8 +594,7 @@ public class Roster {
* @param deletedEntries the collection of address of the deleted contacts.
*/
private void fireRosterChangedEvent(Collection<String> addedEntries, Collection<String> updatedEntries,
Collection<String> deletedEntries)
{
Collection<String> deletedEntries) {
for (RosterListener listener : rosterListeners) {
if (!addedEntries.isEmpty()) {
listener.entriesAdded(addedEntries);
@ -640,8 +649,9 @@ public class Roster {
* Listens for all presence packets and processes them.
*/
private class PresencePacketListener implements PacketListener {
public void processPacket(Packet packet) {
Presence presence = (Presence)packet;
Presence presence = (Presence) packet;
String from = presence.getFrom();
String key = getPresenceMapKey(from);

View file

@ -39,13 +39,14 @@ import java.util.List;
* vCard provider.
*
* @author Gaston Dombiak
* @author Derek DeMoro
*/
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 {
StringBuilder sb = new StringBuilder();
final StringBuilder sb = new StringBuilder();
try {
int event = parser.getEventType();
// get the content
@ -68,33 +69,39 @@ public class VCardProvider implements IQProvider {
event = parser.next();
}
} catch (XmlPullParserException e) {
}
catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
}
catch (IOException e) {
e.printStackTrace();
}
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();
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(
new ByteArrayInputStream(xmlText.getBytes(PREFERRED_ENCODING)));
new ByteArrayInputStream(xml.getBytes(PREFERRED_ENCODING)));
new VCardReader(vCard, document).initializeFields();
} catch (Exception e) {
e.printStackTrace(System.err);
}
return vCard;
}
private static class VCardReader {
private final VCard vCard;
private final Document document;
@ -180,7 +187,7 @@ public class VCardProvider implements IQProvider {
List code = new ArrayList();
List value = new ArrayList();
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);
if (node.getNodeType() != Node.ELEMENT_NODE) continue;
String nodeName = node.getNodeName();
@ -221,7 +228,8 @@ public class VCardProvider implements IQProvider {
String field = element.getNodeName();
if (element.getChildNodes().getLength() == 0) {
vCard.setField(field, "");
} else if (element.getChildNodes().getLength() == 1 &&
}
else if (element.getChildNodes().getLength() == 1 &&
element.getChildNodes().item(0) instanceof Text) {
vCard.setField(field, getTextContent(element));
}

View file

@ -62,23 +62,23 @@ public class VCardTest extends SmackTestCase {
}
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());
}
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"));
}
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("ddss", card.getAddressFieldWork("FF"));
}
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.getFullName());
}