Skip to content

Commit

Permalink
Rework Roster IQ and DirectoryRosterStore
Browse files Browse the repository at this point in the history
remove (set|get)ItemStatus. This was always the ask status, which can
only be set to 'subscribe' or is non-existent.

Use the standard (de-)serialization facilities for DirectoryRosterStore.

Fixes Smack-657.
  • Loading branch information
Flowdalic committed Sep 29, 2015
1 parent b746e5c commit 86548b8
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 303 deletions.
Expand Up @@ -132,13 +132,16 @@ public static String readFile(File file) {
return null;
}

public static void writeFileOrThrow(File file, String content) throws IOException {
public static void writeFileOrThrow(File file, CharSequence content) throws IOException {
FileWriter writer = new FileWriter(file, false);
writer.write(content);
writer.close();
try {
writer.write(content.toString());
} finally {
writer.close();
}
}

public static boolean writeFile(File file, String content) {
public static boolean writeFile(File file, CharSequence content) {
try {
writeFileOrThrow(file, content);
return true;
Expand Down
Expand Up @@ -43,6 +43,11 @@ public static void assertAtStartTag(XmlPullParser parser) throws XmlPullParserEx
assert(parser.getEventType() == XmlPullParser.START_TAG);
}

public static void assertAtStartTag(XmlPullParser parser, String name) throws XmlPullParserException {
assertAtStartTag(parser);
assert name.equals(parser.getName());
}

public static void assertAtEndTag(XmlPullParser parser) throws XmlPullParserException {
assert(parser.getEventType() == XmlPullParser.END_TAG);
}
Expand Down
Expand Up @@ -1396,8 +1396,7 @@ public void processPacket(Stanza packet) {
}

for (RosterPacket.Item item : validItems) {
RosterEntry entry = new RosterEntry(item.getJid(), item.getName(),
item.getItemType(), item.getItemStatus(), item.isApproved(), Roster.this, connection);
RosterEntry entry = new RosterEntry(item, Roster.this, connection);
addUpdateEntry(addedEntries, updatedEntries, unchangedEntries, item, entry);
}

Expand Down Expand Up @@ -1426,8 +1425,7 @@ public void processPacket(Stanza packet) {
// version we presented the server. So we simply load the roster from the store and
// await possible further roster pushes.
for (RosterPacket.Item item : rosterStore.getEntries()) {
RosterEntry entry = new RosterEntry(item.getJid(), item.getName(),
item.getItemType(), item.getItemStatus(), item.isApproved(), Roster.this, connection);
RosterEntry entry = new RosterEntry(item, Roster.this, connection);
addUpdateEntry(addedEntries, updatedEntries, unchangedEntries, item, entry);
}
}
Expand Down Expand Up @@ -1496,8 +1494,7 @@ public IQ handleIQRequest(IQ iqRequest) {
// We assured above that the size of items is exaclty 1, therefore we are able to
// safely retrieve this single item here.
Item item = items.iterator().next();
RosterEntry entry = new RosterEntry(item.getJid(), item.getName(),
item.getItemType(), item.getItemStatus(), item.isApproved(), Roster.this, connection);
RosterEntry entry = new RosterEntry(item, Roster.this, connection);
String version = rosterPacket.getVersion();

if (item.getItemType().equals(RosterPacket.ItemType.remove)) {
Expand Down
102 changes: 31 additions & 71 deletions smack-im/src/main/java/org/jivesoftware/smack/roster/RosterEntry.java
Expand Up @@ -37,38 +37,23 @@
* JID and a name or nickname you assign.
*
* @author Matt Tucker
* @author Florian Schmaus
*/
public final class RosterEntry extends Manager {

/**
* The JID of the entity/user.
*/
private final BareJid jid;

private String name;
private RosterPacket.ItemType type;
private RosterPacket.ItemStatus status;
private final boolean approved;
private final RosterPacket.Item item;
final private Roster roster;

/**
* Creates a new roster entry.
*
* @param user the user.
* @param name the nickname for the entry.
* @param type the subscription type.
* @param status the subscription status (related to subscriptions pending to be approbed).
* @param approved the pre-approval flag.
* @param item the Roster Stanza's Item entry.
* @param roster The Roster managing this entry.
* @param connection a connection to the XMPP server.
*/
RosterEntry(BareJid user, String name, RosterPacket.ItemType type,
RosterPacket.ItemStatus status, boolean approved, Roster roster, XMPPConnection connection) {
RosterEntry(RosterPacket.Item item, Roster roster, XMPPConnection connection) {
super(connection);
this.jid = user;
this.name = name;
this.type = type;
this.status = status;
this.approved = approved;
this.item = item;
this.roster = roster;
}

Expand All @@ -80,7 +65,7 @@ public final class RosterEntry extends Manager {
*/
@Deprecated
public String getUser() {
return jid.toString();
return getJid().toString();
}

/**
Expand All @@ -89,7 +74,7 @@ public String getUser() {
* @return the user associated with this entry.
*/
public BareJid getJid() {
return jid;
return item.getJid();
}

/**
Expand All @@ -98,7 +83,7 @@ public BareJid getJid() {
* @return the name.
*/
public String getName() {
return name;
return item.getName();
}

/**
Expand All @@ -112,7 +97,7 @@ public String getName() {
*/
public synchronized void setName(String name) throws NotConnectedException, NoResponseException, XMPPErrorException, InterruptedException {
// Do nothing if the name hasn't changed.
if (name != null && name.equals(this.name)) {
if (name != null && name.equals(getName())) {
return;
}

Expand All @@ -125,20 +110,20 @@ public synchronized void setName(String name) throws NotConnectedException, NoRe
connection().createPacketCollectorAndSend(packet).nextResultOrThrow();

// We have received a result response to the IQ set, the name was successfully changed
this.name = name;
item.setName(name);
}

/**
* Updates the state of the entry with the new values.
*
* @param name the nickname for the entry.
* @param type the subscription type.
* @param status the subscription status (related to subscriptions pending to be approbed).
* @param subscriptionPending TODO
*/
void updateState(String name, RosterPacket.ItemType type, RosterPacket.ItemStatus status) {
this.name = name;
this.type = type;
this.status = status;
void updateState(String name, RosterPacket.ItemType type, boolean subscriptionPending) {
item.setName(name);
item.setItemType(type);
item.setSubscriptionPending(subscriptionPending);
}

/**
Expand All @@ -147,7 +132,7 @@ void updateState(String name, RosterPacket.ItemType type, RosterPacket.ItemStatu
* @return the pre-approval state.
*/
public boolean isApproved() {
return approved;
return item.isApproved();
}

/**
Expand Down Expand Up @@ -176,26 +161,27 @@ public List<RosterGroup> getGroups() {
* @return the type.
*/
public RosterPacket.ItemType getType() {
return type;
return item.getItemType();
}

/**
* Returns the roster subscription status of the entry. When the status is
* RosterPacket.ItemStatus.SUBSCRIPTION_PENDING, the contact has to answer the
* subscription request.
* Returns the roster subscription request status of the entry. If
* {@code true}, then the contact did not answer the subscription request
* yet.
*
* @return the status.
* @since 4.2
*/
public RosterPacket.ItemStatus getStatus() {
return status;
public boolean isSubscriptionPending() {
return item.isSubscriptionPending();
}

public String toString() {
StringBuilder buf = new StringBuilder();
if (name != null) {
buf.append(name).append(": ");
if (getName() != null) {
buf.append(getName()).append(": ");
}
buf.append(jid);
buf.append(getJid());
Collection<RosterGroup> groups = getGroups();
if (!groups.isEmpty()) {
buf.append(" [");
Expand All @@ -214,15 +200,15 @@ public String toString() {

@Override
public int hashCode() {
return (jid == null ? 0 : jid.hashCode());
return getJid().hashCode();
}

public boolean equals(Object object) {
if (this == object) {
return true;
}
if (object != null && object instanceof RosterEntry) {
return jid.equals(((RosterEntry)object).getUser());
return getJid().equals(((RosterEntry)object).getJid());
}
else {
return false;
Expand All @@ -246,33 +232,7 @@ public boolean equalsDeep(Object obj) {
if (getClass() != obj.getClass())
return false;
RosterEntry other = (RosterEntry) obj;
if (name == null) {
if (other.name != null)
return false;
}
else if (!name.equals(other.name))
return false;
if (status == null) {
if (other.status != null)
return false;
}
else if (!status.equals(other.status))
return false;
if (type == null) {
if (other.type != null)
return false;
}
else if (!type.equals(other.type))
return false;
if (jid == null) {
if (other.jid != null)
return false;
}
else if (!jid.equals(other.jid))
return false;
if (approved != other.approved)
return false;
return true;
return other.item.equals(this.item);
}

static RosterPacket.Item toRosterItem(RosterEntry entry) {
Expand All @@ -282,7 +242,7 @@ static RosterPacket.Item toRosterItem(RosterEntry entry) {
private static RosterPacket.Item toRosterItem(RosterEntry entry, String name) {
RosterPacket.Item item = new RosterPacket.Item(entry.getJid(), name);
item.setItemType(entry.getType());
item.setItemStatus(entry.getStatus());
item.setSubscriptionPending(entry.isSubscriptionPending());
item.setApproved(entry.isApproved());
// Set the correct group names for the item.
for (RosterGroup group : entry.getGroups()) {
Expand Down

0 comments on commit 86548b8

Please sign in to comment.