1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-30 02:02:06 +01:00

Refactoring work.

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@5361 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Gaston Dombiak 2006-09-13 20:56:47 +00:00 committed by gato
parent e558ee8fa6
commit 1df8baa6f7
10 changed files with 238 additions and 252 deletions

View file

@ -27,7 +27,7 @@ public class PrivacyList {
/** Holds the list name used to print **/ /** Holds the list name used to print **/
private String listName; private String listName;
/** Holds the list of {@see PrivacyItem} **/ /** Holds the list of {@see PrivacyItem} **/
private List items; private List<PrivacyItem> items;
protected PrivacyList(boolean isActiveList, boolean isDefaultList, protected PrivacyList(boolean isActiveList, boolean isDefaultList,
String listName, List<PrivacyItem> privacyItems) { String listName, List<PrivacyItem> privacyItems) {
@ -46,7 +46,7 @@ public class PrivacyList {
return isDefaultList; return isDefaultList;
} }
public List getItems() { public List<PrivacyItem> getItems() {
return items; return items;
} }

View file

@ -1,5 +1,7 @@
package org.jivesoftware.smack; package org.jivesoftware.smack;
import org.jivesoftware.smack.packet.PrivacyItem;
import java.util.List; import java.util.List;
/** /**
@ -18,7 +20,7 @@ public interface PrivacyListListener {
* @param listName the name of the new or updated privacy list. * @param listName the name of the new or updated privacy list.
* @param listItem the PrivacyItems that rules the list. * @param listItem the PrivacyItems that rules the list.
*/ */
public void setPrivacyList(String listName, List listItem); public void setPrivacyList(String listName, List<PrivacyItem> listItem);
/** /**
* A privacy list has been modified by another. It gets notified. * A privacy list has been modified by another. It gets notified.

View file

@ -1,22 +1,13 @@
package org.jivesoftware.smack; package org.jivesoftware.smack;
import java.util.ArrayList; import org.jivesoftware.smack.filter.*;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.IQTypeFilter;
import org.jivesoftware.smack.filter.PacketExtensionFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Privacy; import org.jivesoftware.smack.packet.Privacy;
import org.jivesoftware.smack.packet.PrivacyItem; import org.jivesoftware.smack.packet.PrivacyItem;
import java.util.*;
/** /**
* A PrivacyListManager is used by XMPP clients to block or allow communications from other * A PrivacyListManager is used by XMPP clients to block or allow communications from other
* users. Use the manager to: <ul> * users. Use the manager to: <ul>
@ -33,10 +24,10 @@ import org.jivesoftware.smack.packet.PrivacyItem;
public class PrivacyListManager { public class PrivacyListManager {
// Keep the list of instances of this class. // Keep the list of instances of this class.
private static Map instances = new Hashtable(); private static Map<XMPPConnection, PrivacyListManager> instances = new Hashtable<XMPPConnection, PrivacyListManager>();
private XMPPConnection connection; private XMPPConnection connection;
private List listeners = new ArrayList(); private final List<PrivacyListListener> listeners = new ArrayList<PrivacyListListener>();
PacketFilter packetFilter = new AndFilter(new IQTypeFilter(IQ.Type.SET), PacketFilter packetFilter = new AndFilter(new IQTypeFilter(IQ.Type.SET),
new PacketExtensionFilter("query", "jabber:iq:privacy")); new PacketExtensionFilter("query", "jabber:iq:privacy"));
@ -85,8 +76,16 @@ public class PrivacyListManager {
} }
public void connectionClosedOnError(Exception e) { public void connectionClosedOnError(Exception e) {
// Unregister this instance since the connection has been closed // ignore
instances.remove(connection); }
public void reconnectionFailed(Exception e) {
// ignore
}
public void attemptToReconnectIn(int seconds) {
// ignore
}
public void conectionReestablished() {
// ignore
} }
}); });
@ -99,20 +98,13 @@ public class PrivacyListManager {
// The packet is correct. // The packet is correct.
Privacy privacy = (Privacy) packet; Privacy privacy = (Privacy) packet;
// Prepare the information before starting the notification
int listNameSize = privacy.getItemLists().size();
Object[] listItemsPairs = privacy.getItemLists().entrySet().toArray();
// Notifies the event to the listeners. // Notifies the event to the listeners.
synchronized (listeners) { synchronized (listeners) {
for (Iterator i = listeners.iterator(); i.hasNext();) { for (PrivacyListListener listener : listeners) {
PrivacyListListener listener = (PrivacyListListener) i.next();
// Notifies the created or updated privacy lists // Notifies the created or updated privacy lists
for (int j = 0; j < listNameSize; j++) { for (Map.Entry<String,List<PrivacyItem>> entry : privacy.getItemLists().entrySet()) {
Map.Entry entry = (Map.Entry) listItemsPairs[j]; String listName = entry.getKey();
String listName = (String) entry.getKey(); List<PrivacyItem> items = entry.getValue();
List items = (List) entry.getValue();
if (items.isEmpty()) { if (items.isEmpty()) {
listener.updatedPrivacyList(listName); listener.updatedPrivacyList(listName);
} else { } else {
@ -147,7 +139,7 @@ public class PrivacyListManager {
* @return the PrivacyListManager associated with a given XMPPConnection. * @return the PrivacyListManager associated with a given XMPPConnection.
*/ */
public static PrivacyListManager getInstanceFor(XMPPConnection connection) { public static PrivacyListManager getInstanceFor(XMPPConnection connection) {
return (PrivacyListManager) instances.get(connection); return instances.get(connection);
} }
/** /**
@ -275,11 +267,11 @@ public class PrivacyListManager {
* @param listName the name of the list to get the allowed and blocked permissions. * @param listName the name of the list to get the allowed and blocked permissions.
* @return a list of {@link PrivacyItem} under the list listName. * @return a list of {@link PrivacyItem} under the list listName.
*/ */
private List getPrivacyListItems(String listName) throws XMPPException { private List<PrivacyItem> getPrivacyListItems(String listName) throws XMPPException {
// The request of the list is an privacy message with an empty list // The request of the list is an privacy message with an empty list
Privacy request = new Privacy(); Privacy request = new Privacy();
request.setPrivacyList(listName, new ArrayList()); request.setPrivacyList(listName, new ArrayList<PrivacyItem>());
// Send the package to the server and get the answer // Send the package to the server and get the answer
Privacy privacyAnswer = getRequest(request); Privacy privacyAnswer = getRequest(request);
@ -295,8 +287,7 @@ public class PrivacyListManager {
*/ */
public PrivacyList getPrivacyList(String listName) throws XMPPException { public PrivacyList getPrivacyList(String listName) throws XMPPException {
PrivacyList list = new PrivacyList(false, false, listName, getPrivacyListItems(listName)); return new PrivacyList(false, false, listName, getPrivacyListItems(listName));
return list;
} }
/** /**
@ -306,14 +297,12 @@ public class PrivacyListManager {
*/ */
public PrivacyList[] getPrivacyLists() throws XMPPException { public PrivacyList[] getPrivacyLists() throws XMPPException {
Privacy privacyAnswer = this.getPrivacyWithListNames(); Privacy privacyAnswer = this.getPrivacyWithListNames();
Set names = privacyAnswer.getPrivacyListNames(); Set<String> names = privacyAnswer.getPrivacyListNames();
PrivacyList[] lists = new PrivacyList[names.size()]; PrivacyList[] lists = new PrivacyList[names.size()];
String listName;
boolean isActiveList; boolean isActiveList;
boolean isDefaultList; boolean isDefaultList;
int index=0; int index=0;
for (Iterator iter = names.iterator(); iter.hasNext();) { for (String listName : names) {
listName = (String) iter.next();
isActiveList = listName.equals(privacyAnswer.getActiveName()); isActiveList = listName.equals(privacyAnswer.getActiveName());
isDefaultList = listName.equals(privacyAnswer.getDefaultName()); isDefaultList = listName.equals(privacyAnswer.getDefaultName());
lists[index] = new PrivacyList(isActiveList, isDefaultList, lists[index] = new PrivacyList(isActiveList, isDefaultList,
@ -388,7 +377,7 @@ public class PrivacyListManager {
* @param listName the list that has changed its content. * @param listName the list that has changed its content.
* @param privacyItems a List with every {@link PrivacyItem} in the list. * @param privacyItems a List with every {@link PrivacyItem} in the list.
*/ */
public void createPrivacyList(String listName, List privacyItems) throws XMPPException { public void createPrivacyList(String listName, List<PrivacyItem> privacyItems) throws XMPPException {
this.updatePrivacyList(listName, privacyItems); this.updatePrivacyList(listName, privacyItems);
} }
@ -401,7 +390,7 @@ public class PrivacyListManager {
* @param listName the list that has changed its content. * @param listName the list that has changed its content.
* @param privacyItems a List with every {@link PrivacyItem} in the list. * @param privacyItems a List with every {@link PrivacyItem} in the list.
*/ */
public void updatePrivacyList(String listName, List privacyItems) throws XMPPException { public void updatePrivacyList(String listName, List<PrivacyItem> privacyItems) throws XMPPException {
// Build the privacy package to add or update the new list // Build the privacy package to add or update the new list
Privacy request = new Privacy(); Privacy request = new Privacy();
@ -420,7 +409,7 @@ public class PrivacyListManager {
// The request of the list is an privacy message with an empty list // The request of the list is an privacy message with an empty list
Privacy request = new Privacy(); Privacy request = new Privacy();
request.setPrivacyList(listName, new ArrayList()); request.setPrivacyList(listName, new ArrayList<PrivacyItem>());
// Send the package to the server // Send the package to the server
setRequest(request); setRequest(request);

View file

@ -1,10 +1,6 @@
package org.jivesoftware.smack.packet; package org.jivesoftware.smack.packet;
import java.util.HashMap; import java.util.*;
import java.util.Iterator;
import java.util.Map;
import java.util.List;
import java.util.Set;
/** /**
* A Privacy IQ Packet, is used by the {@see PrivacyListManager} and {@see PrivacyProvider} to allow * A Privacy IQ Packet, is used by the {@see PrivacyListManager} and {@see PrivacyProvider} to allow
@ -34,7 +30,7 @@ public class Privacy extends IQ {
private String defaultName; private String defaultName;
/** itemLists holds the set of privacy items classified in lists. It is a map where the /** itemLists holds the set of privacy items classified in lists. It is a map where the
* key is the name of the list and the value a collection with privacy items. **/ * key is the name of the list and the value a collection with privacy items. **/
private Map itemLists = new HashMap(); private Map<String, List<PrivacyItem>> itemLists = new HashMap<String, List<PrivacyItem>>();
/** /**
* Set or update a privacy list with {@link PrivacyItem}. * Set or update a privacy list with {@link PrivacyItem}.
@ -43,7 +39,7 @@ public class Privacy extends IQ {
* @param listItem the {@link PrivacyItem} that rules the list. * @param listItem the {@link PrivacyItem} that rules the list.
* @return the privacy List. * @return the privacy List.
*/ */
public List setPrivacyList(String listName, List listItem) { public List setPrivacyList(String listName, List<PrivacyItem> listItem) {
// Add new list to the itemLists // Add new list to the itemLists
this.getItemLists().put(listName, listItem); this.getItemLists().put(listName, listItem);
return listItem; return listItem;
@ -54,9 +50,9 @@ public class Privacy extends IQ {
* *
* @return the active List. * @return the active List.
*/ */
public List setActivePrivacyList() { public List<PrivacyItem> setActivePrivacyList() {
this.setActiveName(this.getDefaultName()); this.setActiveName(this.getDefaultName());
return (List) this.getItemLists().get(this.getActiveName()); return this.getItemLists().get(this.getActiveName());
} }
/** /**
@ -81,12 +77,12 @@ public class Privacy extends IQ {
* *
* @return list with {@link PrivacyItem} or <tt>null</tt> if none was found. * @return list with {@link PrivacyItem} or <tt>null</tt> if none was found.
*/ */
public List getActivePrivacyList() { public List<PrivacyItem> getActivePrivacyList() {
// Check if we have the default list // Check if we have the default list
if (this.getActiveName() == null) { if (this.getActiveName() == null) {
return null; return null;
} else { } else {
return (List) this.getItemLists().get(this.getActiveName()); return this.getItemLists().get(this.getActiveName());
} }
} }
@ -95,12 +91,12 @@ public class Privacy extends IQ {
* *
* @return list with {@link PrivacyItem} or <tt>null</tt> if none was found. * @return list with {@link PrivacyItem} or <tt>null</tt> if none was found.
*/ */
public List getDefaultPrivacyList() { public List<PrivacyItem> getDefaultPrivacyList() {
// Check if we have the default list // Check if we have the default list
if (this.getDefaultName() == null) { if (this.getDefaultName() == null) {
return null; return null;
} else { } else {
return (List) this.getItemLists().get(this.getDefaultName()); return this.getItemLists().get(this.getDefaultName());
} }
} }
@ -110,8 +106,8 @@ public class Privacy extends IQ {
* @param listName the name of the list to get. * @param listName the name of the list to get.
* @return a List with {@link PrivacyItem} * @return a List with {@link PrivacyItem}
*/ */
public List getPrivacyList(String listName) { public List<PrivacyItem> getPrivacyList(String listName) {
return (List) this.getItemLists().get(listName); return this.getItemLists().get(listName);
} }
/** /**
@ -121,10 +117,10 @@ public class Privacy extends IQ {
* @return a List with {@link PrivacyItem} * @return a List with {@link PrivacyItem}
*/ */
public PrivacyItem getItem(String listName, int order) { public PrivacyItem getItem(String listName, int order) {
Iterator values = getPrivacyList(listName).iterator(); Iterator<PrivacyItem> values = getPrivacyList(listName).iterator();
PrivacyItem itemFound = null; PrivacyItem itemFound = null;
while (itemFound == null && values.hasNext()) { while (itemFound == null && values.hasNext()) {
PrivacyItem element = (PrivacyItem) values.next(); PrivacyItem element = values.next();
if (element.getOrder() == order) { if (element.getOrder() == order) {
itemFound = element; itemFound = element;
} }
@ -211,7 +207,7 @@ public class Privacy extends IQ {
* @return a map where the key is the name of the list and the value the * @return a map where the key is the name of the list and the value the
* collection of privacy items. * collection of privacy items.
*/ */
public Map getItemLists() { public Map<String, List<PrivacyItem>> getItemLists() {
return itemLists; return itemLists;
} }
@ -245,7 +241,7 @@ public class Privacy extends IQ {
/** /**
* Sets whether the receiver allows or declines the use of a default list. * Sets whether the receiver allows or declines the use of a default list.
* *
* @param declineActiveList indicates if the receiver declines the use of a default list. * @param declineDefaultList indicates if the receiver declines the use of a default list.
*/ */
public void setDeclineDefaultList(boolean declineDefaultList) { public void setDeclineDefaultList(boolean declineDefaultList) {
this.declineDefaultList = declineDefaultList; this.declineDefaultList = declineDefaultList;
@ -256,7 +252,7 @@ public class Privacy extends IQ {
* *
* @return a Set with Strings containing every list names. * @return a Set with Strings containing every list names.
*/ */
public Set getPrivacyListNames() { public Set<String> getPrivacyListNames() {
return this.itemLists.keySet(); return this.itemLists.keySet();
} }
@ -282,20 +278,16 @@ public class Privacy extends IQ {
} }
// Add the list with their privacy items // Add the list with their privacy items
int listNameSize = this.getItemLists().size(); for (Map.Entry<String, List<PrivacyItem>> entry : this.getItemLists().entrySet()) {
Iterator listItemsPairs = this.getItemLists().entrySet().iterator(); String listName = entry.getKey();
for (int i = 0; i < listNameSize; i++) { List<PrivacyItem> items = entry.getValue();
Map.Entry entry = (Map.Entry) listItemsPairs.next();
String listName = (String) entry.getKey();
List items = (List) entry.getValue();
// Begin the list tag // Begin the list tag
if (items.isEmpty()) { if (items.isEmpty()) {
buf.append("<list name=\"").append(listName).append("\"/>"); buf.append("<list name=\"").append(listName).append("\"/>");
} else { } else {
buf.append("<list name=\"").append(listName).append("\">"); buf.append("<list name=\"").append(listName).append("\">");
} }
for (Iterator itemIterator = items.iterator(); itemIterator.hasNext();) { for (PrivacyItem item : items) {
PrivacyItem item = (PrivacyItem) itemIterator.next();
// Append the item xml representation // Append the item xml representation
buf.append(item.toXML()); buf.append(item.toXML());
} }
@ -308,8 +300,7 @@ public class Privacy extends IQ {
// Add packet extensions, if any are defined. // Add packet extensions, if any are defined.
buf.append(getExtensionsXML()); buf.append(getExtensionsXML());
buf.append("</query>"); buf.append("</query>");
String generatedXML = buf.toString(); return buf.toString();
return generatedXML;
} }
} }

View file

@ -196,7 +196,7 @@ public class PrivacyItem {
* *
* @return the type of communication it represent. * @return the type of communication it represent.
*/ */
public String getType() { public Type getType() {
if (this.getRule() == null) { if (this.getRule() == null) {
return null; return null;
} else { } else {
@ -298,7 +298,7 @@ public class PrivacyItem {
* Type defines if the rule is based on JIDs, roster groups or presence subscription types. * Type defines if the rule is based on JIDs, roster groups or presence subscription types.
* Available values are: [jid|group|subscription] * Available values are: [jid|group|subscription]
*/ */
private String type; private Type type;
/** /**
* The value hold the element identifier to apply the action. * The value hold the element identifier to apply the action.
* If the type is "jid", then the 'value' attribute MUST contain a valid Jabber ID. * If the type is "jid", then the 'value' attribute MUST contain a valid Jabber ID.
@ -309,25 +309,11 @@ public class PrivacyItem {
*/ */
private String value; private String value;
/**
* JID being analyzed should have a resource match, domain match or bare JID match.
*/
public static final String GROUP = "group";
/**
* JID being analyzed should belong to a roster group of the list's owner.
*/
public static final String JID = "jid";
/**
* JID being analyzed should belong to a contact present in the owner's roster with
* the specified subscription status.
*/
public static final String SUBSCRIPTION = "subscription";
/** /**
* If the type is "subscription", then the 'value' attribute MUST be one of "both", * If the type is "subscription", then the 'value' attribute MUST be one of "both",
* "to", "from", or "none" * "to", "from", or "none"
*/ */
public static final String SUBSCRIPTION_BOTH = "subscription"; public static final String SUBSCRIPTION_BOTH = "both";
public static final String SUBSCRIPTION_TO = "to"; public static final String SUBSCRIPTION_TO = "to";
public static final String SUBSCRIPTION_FROM = "from"; public static final String SUBSCRIPTION_FROM = "from";
public static final String SUBSCRIPTION_NONE = "none"; public static final String SUBSCRIPTION_NONE = "none";
@ -336,25 +322,11 @@ public class PrivacyItem {
* Returns the type constant associated with the String value. * Returns the type constant associated with the String value.
*/ */
protected static PrivacyRule fromString(String value) { protected static PrivacyRule fromString(String value) {
String type = null;
if (value == null) { if (value == null) {
return null; return null;
} }
if (SUBSCRIPTION.equalsIgnoreCase(value)) {
type = SUBSCRIPTION;
}
else if (GROUP.equalsIgnoreCase(value)) {
type = GROUP;
}
else if (JID.equalsIgnoreCase(value)) {
type = JID;
}
// Default to available.
else {
return null;
}
PrivacyRule rule = new PrivacyRule(); PrivacyRule rule = new PrivacyRule();
rule.setType(type); rule.setType(Type.valueOf(value.toLowerCase()));
return rule; return rule;
} }
@ -364,16 +336,16 @@ public class PrivacyItem {
* *
* @return the type of communication it represent. * @return the type of communication it represent.
*/ */
public String getType() { public Type getType() {
return type; return type;
} }
/** /**
* Sets the action associated with the item, it can allow or deny the communication. * Sets the action associated with the item, it can allow or deny the communication.
* *
* @param allow indicates if the receiver allows or denies the communication. * @param type indicates if the receiver allows or denies the communication.
*/ */
private void setType(String type) { private void setType(Type type) {
this.type = type; this.type = type;
} }
@ -419,9 +391,9 @@ public class PrivacyItem {
* @param value is the identifier to apply the action. * @param value is the identifier to apply the action.
*/ */
private void setSuscriptionValue(String value) { private void setSuscriptionValue(String value) {
String setValue = null; String setValue;
if (value == null) { if (value == null) {
setValue = null; // Do nothing
} }
if (SUBSCRIPTION_BOTH.equalsIgnoreCase(value)) { if (SUBSCRIPTION_BOTH.equalsIgnoreCase(value)) {
setValue = SUBSCRIPTION_BOTH; setValue = SUBSCRIPTION_BOTH;
@ -448,7 +420,26 @@ public class PrivacyItem {
* @return if the receiver represents a subscription rule. * @return if the receiver represents a subscription rule.
*/ */
public boolean isSuscription () { public boolean isSuscription () {
return this.getValue() == SUBSCRIPTION; return this.getType() == Type.subscription;
} }
} }
/**
* Type defines if the rule is based on JIDs, roster groups or presence subscription types.
*/
protected static enum Type {
/**
* JID being analyzed should belong to a roster group of the list's owner.
*/
group,
/**
* JID being analyzed should have a resource match, domain match or bare JID match.
*/
jid,
/**
* JID being analyzed should belong to a contact present in the owner's roster with
* the specified subscription status.
*/
subscription
}
} }

View file

@ -23,7 +23,6 @@ package org.jivesoftware.smack.packet;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.List; import java.util.List;
/** /**
@ -67,7 +66,7 @@ public class XMPPError {
private Type type; private Type type;
private String condition; private String condition;
private String message; private String message;
private List applicationExtensions = null; private List<PacketExtension> applicationExtensions = null;
/** /**
@ -77,7 +76,7 @@ public class XMPPError {
* If the Condition is not predefined, invocations should be like * If the Condition is not predefined, invocations should be like
* new XMPPError(new XMPPError.Condition("my_own_error")); * new XMPPError(new XMPPError.Condition("my_own_error"));
* *
* @param code the error code. * @param condition the error condition.
*/ */
public XMPPError(Condition condition) { public XMPPError(Condition condition) {
this.init(condition); this.init(condition);
@ -91,8 +90,8 @@ public class XMPPError {
* If the Condition is not predefined, invocations should be like * If the Condition is not predefined, invocations should be like
* new XMPPError(new XMPPError.Condition("my_own_error"), "Error Explanation"); * new XMPPError(new XMPPError.Condition("my_own_error"), "Error Explanation");
* *
* @param code the error code. * @param condition the error condition.
* @param message a message describing the error. * @param messageText a message describing the error.
*/ */
public XMPPError(Condition condition, String messageText) { public XMPPError(Condition condition, String messageText) {
this.init(condition); this.init(condition);
@ -103,7 +102,7 @@ public class XMPPError {
* Creates a new error with the specified code and no message. * Creates a new error with the specified code and no message.
* *
* @param code the error code. * @param code the error code.
* @Deprecated new errors should be created using the constructor XMPPError(condition) * @deprecated new errors should be created using the constructor XMPPError(condition)
*/ */
public XMPPError(int code) { public XMPPError(int code) {
this.code = code; this.code = code;
@ -116,7 +115,7 @@ public class XMPPError {
* *
* @param code the error code. * @param code the error code.
* @param message a message describing the error. * @param message a message describing the error.
* @Deprecated new errors should be created using the constructor XMPPError(condition, message) * @deprecated new errors should be created using the constructor XMPPError(condition, message)
*/ */
public XMPPError(int code, String message) { public XMPPError(int code, String message) {
this.code = code; this.code = code;
@ -135,7 +134,8 @@ public class XMPPError {
* @param condition the error condition. * @param condition the error condition.
* @param message a message describing the error. * @param message a message describing the error.
*/ */
public XMPPError(int code, Type type, String condition, String message, List extension) { public XMPPError(int code, Type type, String condition, String message,
List<PacketExtension> extension) {
this.code = code; this.code = code;
this.type = type; this.type = type;
this.condition = condition; this.condition = condition;
@ -205,7 +205,7 @@ public class XMPPError {
buf.append("<error code=\"").append(code).append("\""); buf.append("<error code=\"").append(code).append("\"");
if (type != null) { if (type != null) {
buf.append(" type=\""); buf.append(" type=\"");
buf.append(type.value); buf.append(type.name());
buf.append("\""); buf.append("\"");
} }
buf.append(">"); buf.append(">");
@ -218,8 +218,7 @@ public class XMPPError {
buf.append(message); buf.append(message);
buf.append("</text>"); buf.append("</text>");
} }
for (Iterator extensions = this.getExtensions(); extensions.hasNext();) { for (PacketExtension element : this.getExtensions()) {
PacketExtension element = (PacketExtension) extensions.next();
buf.append(element.toXML()); buf.append(element.toXML());
} }
buf.append("</error>"); buf.append("</error>");
@ -228,6 +227,9 @@ public class XMPPError {
public String toString() { public String toString() {
StringBuffer txt = new StringBuffer(); StringBuffer txt = new StringBuffer();
if (condition != null) {
txt.append(condition);
}
txt.append("(").append(code).append(")"); txt.append("(").append(code).append(")");
if (message != null) { if (message != null) {
txt.append(" ").append(message); txt.append(" ").append(message);
@ -242,11 +244,11 @@ public class XMPPError {
* *
* @return an Iterator for the error extensions. * @return an Iterator for the error extensions.
*/ */
public synchronized Iterator getExtensions() { public synchronized List<PacketExtension> getExtensions() {
if (applicationExtensions == null) { if (applicationExtensions == null) {
return Collections.EMPTY_LIST.iterator(); return Collections.emptyList();
} }
return Collections.unmodifiableList(new ArrayList(applicationExtensions)).iterator(); return Collections.unmodifiableList(applicationExtensions);
} }
/** /**
@ -261,8 +263,7 @@ public class XMPPError {
if (applicationExtensions == null || elementName == null || namespace == null) { if (applicationExtensions == null || elementName == null || namespace == null) {
return null; return null;
} }
for (Iterator i=applicationExtensions.iterator(); i.hasNext(); ) { for (PacketExtension ext : applicationExtensions) {
PacketExtension ext = (PacketExtension)i.next();
if (elementName.equals(ext.getElementName()) && namespace.equals(ext.getNamespace())) { if (elementName.equals(ext.getElementName()) && namespace.equals(ext.getNamespace())) {
return ext; return ext;
} }
@ -277,7 +278,7 @@ public class XMPPError {
*/ */
public synchronized void addExtension(PacketExtension extension) { public synchronized void addExtension(PacketExtension extension) {
if (applicationExtensions == null) { if (applicationExtensions == null) {
applicationExtensions = new ArrayList(); applicationExtensions = new ArrayList<PacketExtension>();
} }
applicationExtensions.add(extension); applicationExtensions.add(extension);
} }
@ -287,7 +288,7 @@ public class XMPPError {
* *
* @param extension a packet extension. * @param extension a packet extension.
*/ */
public synchronized void setExtension(List extension) { public synchronized void setExtension(List<PacketExtension> extension) {
applicationExtensions = extension; applicationExtensions = extension;
} }
@ -303,55 +304,12 @@ public class XMPPError {
* <li>XMPPError.Type.CONTINUE - proceed (the condition was only a warning) * <li>XMPPError.Type.CONTINUE - proceed (the condition was only a warning)
* </ul> * </ul>
*/ */
public static class Type { public static enum Type {
WAIT,
public static final Type WAIT = new Type("wait"); CANCEL,
public static final Type CANCEL = new Type("cancel"); MODIFY,
public static final Type MODIFY = new Type("modify"); AUTH,
public static final Type AUTH = new Type("auth"); CONTINUE
public static final Type CONTINUE = new Type("continue");
/**
* Converts a String into the corresponding types. Valid String values
* that can be converted to types are: "wait", "cancel", "modify", "auth" or a user defined.
*
* @param type the String value to covert.
* @return the corresponding Type.
*/
public static Type fromString(String type) {
if (type == null) {
return null;
}
type = type.toLowerCase();
if (CANCEL.toString().equals(type)) {
return CANCEL;
}
else if (CONTINUE.toString().equals(type)) {
return CONTINUE;
}
else if (WAIT.toString().equals(type)) {
return WAIT;
}
else if (MODIFY.toString().equals(type)) {
return MODIFY;
}
else if (AUTH.toString().equals(type)) {
return AUTH;
}
else {
return null;
}
}
private String value;
private Type(String value) {
this.value = value;
}
public String toString() {
return value;
}
} }
/** /**
@ -403,7 +361,7 @@ public class XMPPError {
private int code; private int code;
private Type type; private Type type;
private Condition condition; private Condition condition;
private static HashMap instances = errorSpecifications(); private static HashMap<Condition, ErrorSpecification> instances = errorSpecifications();
private ErrorSpecification(Condition condition, Type type, int code) { private ErrorSpecification(Condition condition, Type type, int code) {
this.code = code; this.code = code;
@ -411,8 +369,8 @@ public class XMPPError {
this.condition = condition; this.condition = condition;
} }
private static HashMap errorSpecifications() { private static HashMap<Condition, ErrorSpecification> errorSpecifications() {
HashMap instances = new HashMap(22); HashMap<Condition, ErrorSpecification> instances = new HashMap<Condition, ErrorSpecification>(22);
instances.put(Condition.interna_server_error, new ErrorSpecification( instances.put(Condition.interna_server_error, new ErrorSpecification(
Condition.interna_server_error, Type.WAIT, 500)); Condition.interna_server_error, Type.WAIT, 500));
instances.put(Condition.forbidden, new ErrorSpecification(Condition.forbidden, instances.put(Condition.forbidden, new ErrorSpecification(Condition.forbidden,
@ -466,7 +424,7 @@ public class XMPPError {
} }
protected static ErrorSpecification specFor(Condition condition) { protected static ErrorSpecification specFor(Condition condition) {
return (ErrorSpecification) instances.get(condition); return instances.get(condition);
} }
/** /**

View file

@ -1,13 +1,13 @@
package org.jivesoftware.smack.provider; package org.jivesoftware.smack.provider;
import java.util.ArrayList;
import org.jivesoftware.smack.packet.DefaultPacketExtension; import org.jivesoftware.smack.packet.DefaultPacketExtension;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Privacy; import org.jivesoftware.smack.packet.Privacy;
import org.jivesoftware.smack.packet.PrivacyItem; import org.jivesoftware.smack.packet.PrivacyItem;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import java.util.ArrayList;
/** /**
* The PrivacyProvider parses {@link Privacy} packets. {@link Privacy} * The PrivacyProvider parses {@link Privacy} packets. {@link Privacy}
* Parses the <tt>query</tt> sub-document and creates an instance of {@link Privacy}. * Parses the <tt>query</tt> sub-document and creates an instance of {@link Privacy}.
@ -64,7 +64,7 @@ public class PrivacyProvider implements IQProvider {
public void parseList(XmlPullParser parser, Privacy privacy) throws Exception { public void parseList(XmlPullParser parser, Privacy privacy) throws Exception {
boolean done = false; boolean done = false;
String listName = parser.getAttributeValue("", "name"); String listName = parser.getAttributeValue("", "name");
ArrayList items = new ArrayList(); ArrayList<PrivacyItem> items = new ArrayList<PrivacyItem>();
while (!done) { while (!done) {
int eventType = parser.next(); int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) { if (eventType == XmlPullParser.START_TAG) {

View file

@ -20,16 +20,19 @@
package org.jivesoftware.smack.util; package org.jivesoftware.smack.util;
import java.beans.PropertyDescriptor;
import java.util.*;
import java.io.ObjectInputStream;
import java.io.ByteArrayInputStream;
import org.jivesoftware.smack.packet.*; import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.provider.PacketExtensionProvider; import org.jivesoftware.smack.provider.PacketExtensionProvider;
import org.jivesoftware.smack.provider.ProviderManager; import org.jivesoftware.smack.provider.ProviderManager;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import java.beans.PropertyDescriptor;
import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** /**
* Utility class that helps to parse packets. Any parsing packets method that must be shared * Utility class that helps to parse packets. Any parsing packets method that must be shared
* between many clients must be placed in this utility class. * between many clients must be placed in this utility class.
@ -335,7 +338,7 @@ public class PacketParserUtils {
} }
} }
return new XMPPError(Integer.parseInt(errorCode), XMPPError.Type return new XMPPError(Integer.parseInt(errorCode), XMPPError.Type
.fromString(type), condition, message, extensions); .valueOf(type.toUpperCase()), condition, message, extensions);
} }
/** /**

View file

@ -1,13 +1,13 @@
package org.jivesoftware.smack.packet; package org.jivesoftware.smack.packet;
import java.io.StringReader;
import org.jivesoftware.smack.provider.PrivacyProvider; import org.jivesoftware.smack.provider.PrivacyProvider;
import org.jivesoftware.smack.test.SmackTestCase; import org.jivesoftware.smack.test.SmackTestCase;
import org.xmlpull.mxp1.MXParser; import org.xmlpull.mxp1.MXParser;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
import java.io.StringReader;
/** /**
* Test the PrivacyProvider class with valids privacy xmls * Test the PrivacyProvider class with valids privacy xmls
* *
@ -131,7 +131,7 @@ public class PrivacyProviderTest extends SmackTestCase {
// check each privacy item // check each privacy item
PrivacyItem item = packet.getItem("testGroup", 4); PrivacyItem item = packet.getItem("testGroup", 4);
assertEquals("Enemies", item.getValue()); assertEquals("Enemies", item.getValue());
assertEquals("group", item.getType()); assertEquals(PrivacyItem.Type.group, item.getType());
assertEquals(false, item.isAllow()); assertEquals(false, item.isAllow());
assertEquals(true, item.isFilterMessage()); assertEquals(true, item.isFilterMessage());
assertEquals(false, item.isFilterIQ()); assertEquals(false, item.isFilterIQ());
@ -141,7 +141,7 @@ public class PrivacyProviderTest extends SmackTestCase {
item = packet.getItem("testFilter", 1); item = packet.getItem("testFilter", 1);
assertEquals("tybalt@example.com", item.getValue()); assertEquals("tybalt@example.com", item.getValue());
assertEquals("jid", item.getType()); assertEquals(PrivacyItem.Type.jid, item.getType());
assertEquals(false, item.isAllow()); assertEquals(false, item.isAllow());
assertEquals(false, item.isFilterMessage()); assertEquals(false, item.isFilterMessage());
assertEquals(false, item.isFilterIQ()); assertEquals(false, item.isFilterIQ());
@ -162,7 +162,7 @@ public class PrivacyProviderTest extends SmackTestCase {
// TEST THE testSubscription LIST // TEST THE testSubscription LIST
item = packet.getItem("testSubscription", 10); item = packet.getItem("testSubscription", 10);
assertEquals("both", item.getValue()); assertEquals("both", item.getValue());
assertEquals("subscription", item.getType()); assertEquals(PrivacyItem.Type.subscription, item.getType());
assertEquals(true, item.isAllow()); assertEquals(true, item.isAllow());
assertEquals(false, item.isFilterMessage()); assertEquals(false, item.isFilterMessage());
assertEquals(false, item.isFilterIQ()); assertEquals(false, item.isFilterIQ());
@ -172,7 +172,7 @@ public class PrivacyProviderTest extends SmackTestCase {
item = packet.getItem("testSubscription", 11); item = packet.getItem("testSubscription", 11);
assertEquals("to", item.getValue()); assertEquals("to", item.getValue());
assertEquals("subscription", item.getType()); assertEquals(PrivacyItem.Type.subscription, item.getType());
assertEquals(true, item.isAllow()); assertEquals(true, item.isAllow());
assertEquals(false, item.isFilterMessage()); assertEquals(false, item.isFilterMessage());
assertEquals(false, item.isFilterIQ()); assertEquals(false, item.isFilterIQ());
@ -182,7 +182,7 @@ public class PrivacyProviderTest extends SmackTestCase {
item = packet.getItem("testSubscription", 12); item = packet.getItem("testSubscription", 12);
assertEquals("from", item.getValue()); assertEquals("from", item.getValue());
assertEquals("subscription", item.getType()); assertEquals(PrivacyItem.Type.subscription, item.getType());
assertEquals(true, item.isAllow()); assertEquals(true, item.isAllow());
assertEquals(false, item.isFilterMessage()); assertEquals(false, item.isFilterMessage());
assertEquals(false, item.isFilterIQ()); assertEquals(false, item.isFilterIQ());
@ -192,7 +192,7 @@ public class PrivacyProviderTest extends SmackTestCase {
item = packet.getItem("testSubscription", 5); item = packet.getItem("testSubscription", 5);
assertEquals("none", item.getValue()); assertEquals("none", item.getValue());
assertEquals("subscription", item.getType()); assertEquals(PrivacyItem.Type.subscription, item.getType());
assertEquals(false, item.isAllow()); assertEquals(false, item.isAllow());
assertEquals(true, item.isFilterMessage()); assertEquals(true, item.isFilterMessage());
assertEquals(false, item.isFilterIQ()); assertEquals(false, item.isFilterIQ());
@ -214,7 +214,7 @@ public class PrivacyProviderTest extends SmackTestCase {
item = packet.getItem("testJID", 6); item = packet.getItem("testJID", 6);
assertEquals("juliet@example.com", item.getValue()); assertEquals("juliet@example.com", item.getValue());
assertEquals("jid", item.getType()); assertEquals(PrivacyItem.Type.jid, item.getType());
assertEquals(true, item.isAllow()); assertEquals(true, item.isAllow());
assertEquals(false, item.isFilterMessage()); assertEquals(false, item.isFilterMessage());
assertEquals(false, item.isFilterIQ()); assertEquals(false, item.isFilterIQ());
@ -224,7 +224,7 @@ public class PrivacyProviderTest extends SmackTestCase {
item = packet.getItem("testJID", 7); item = packet.getItem("testJID", 7);
assertEquals("benvolio@example.org/palm", item.getValue()); assertEquals("benvolio@example.org/palm", item.getValue());
assertEquals("jid", item.getType()); assertEquals(PrivacyItem.Type.jid, item.getType());
assertEquals(false, item.isAllow()); assertEquals(false, item.isAllow());
assertEquals(false, item.isFilterMessage()); assertEquals(false, item.isFilterMessage());
assertEquals(false, item.isFilterIQ()); assertEquals(false, item.isFilterIQ());
@ -234,7 +234,7 @@ public class PrivacyProviderTest extends SmackTestCase {
item = packet.getItem("testJID", 42); item = packet.getItem("testJID", 42);
assertEquals(null, item.getValue()); assertEquals(null, item.getValue());
assertEquals("jid", item.getType()); assertEquals(PrivacyItem.Type.jid, item.getType());
assertEquals(true, item.isAllow()); assertEquals(true, item.isAllow());
assertEquals(false, item.isFilterMessage()); assertEquals(false, item.isFilterMessage());
assertEquals(false, item.isFilterIQ()); assertEquals(false, item.isFilterIQ());
@ -256,7 +256,7 @@ public class PrivacyProviderTest extends SmackTestCase {
item = packet.getItem("testGroup", 4); item = packet.getItem("testGroup", 4);
assertEquals("Enemies", item.getValue()); assertEquals("Enemies", item.getValue());
assertEquals("group", item.getType()); assertEquals(PrivacyItem.Type.group, item.getType());
assertEquals(false, item.isAllow()); assertEquals(false, item.isAllow());
assertEquals(true, item.isFilterMessage()); assertEquals(true, item.isFilterMessage());
assertEquals(false, item.isFilterIQ()); assertEquals(false, item.isFilterIQ());

View file

@ -1,13 +1,16 @@
package org.jivesoftware.smack; package org.jivesoftware.smack.packet;
import org.jivesoftware.smack.PrivacyList;
import org.jivesoftware.smack.PrivacyListListener;
import org.jivesoftware.smack.PrivacyListManager;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.PrivacyItem.PrivacyRule;
import org.jivesoftware.smack.test.SmackTestCase;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import org.jivesoftware.smack.packet.PrivacyItem;
import org.jivesoftware.smack.packet.PrivacyItem.PrivacyRule;
import org.jivesoftware.smack.test.SmackTestCase;
public class PrivacyTest extends SmackTestCase { public class PrivacyTest extends SmackTestCase {
public PrivacyTest(String arg0) { public PrivacyTest(String arg0) {
@ -27,8 +30,8 @@ public class PrivacyTest extends SmackTestCase {
privacyManager.addListener(client); privacyManager.addListener(client);
// Add the list that will be set as the active // Add the list that will be set as the active
ArrayList items = new ArrayList(); ArrayList<PrivacyItem> items = new ArrayList<PrivacyItem>();
PrivacyItem item = new PrivacyItem(PrivacyRule.JID, true, 1); PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid.name(), true, 1);
item.setValue(getConnection(0).getUser()); item.setValue(getConnection(0).getUser());
items.add(item); items.add(item);
privacyManager.createPrivacyList(listName, items); privacyManager.createPrivacyList(listName, items);
@ -48,8 +51,10 @@ public class PrivacyTest extends SmackTestCase {
// Assert the privacy item composition // Assert the privacy item composition
PrivacyItem receivedItem = (PrivacyItem) privacyItems.get(0); PrivacyItem receivedItem = (PrivacyItem) privacyItems.get(0);
assertEquals(1, receivedItem.getOrder()); assertEquals(1, receivedItem.getOrder());
assertEquals(PrivacyRule.JID, receivedItem.getType()); assertEquals(PrivacyItem.Type.jid, receivedItem.getType());
assertEquals(true, receivedItem.isAllow()); assertEquals(true, receivedItem.isAllow());
privacyManager.deletePrivacyList(listName);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
fail(e.getMessage()); fail(e.getMessage());
@ -70,15 +75,17 @@ public class PrivacyTest extends SmackTestCase {
privacyManager.addListener(client); privacyManager.addListener(client);
// Add a list // Add a list
ArrayList items = new ArrayList(); ArrayList<PrivacyItem> items = new ArrayList<PrivacyItem>();
PrivacyItem item = new PrivacyItem(PrivacyRule.JID, true, 1); PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid.name(), true, 1);
item.setValue(getConnection(0).getUser()); item.setValue(getConnection(0).getUser());
items.add(item); items.add(item);
privacyManager.createPrivacyList(listName1, items); privacyManager.createPrivacyList(listName1, items);
Thread.sleep(500);
// Add the another list // Add the another list
ArrayList itemsList2 = new ArrayList(); ArrayList<PrivacyItem> itemsList2 = new ArrayList<PrivacyItem>();
item = new PrivacyItem(PrivacyRule.GROUP, false, 2); item = new PrivacyItem(PrivacyItem.Type.group.name(), false, 2);
item.setValue(groupName); item.setValue(groupName);
item.setFilterMessage(true); item.setFilterMessage(true);
itemsList2.add(item); itemsList2.add(item);
@ -87,40 +94,45 @@ public class PrivacyTest extends SmackTestCase {
Thread.sleep(500); Thread.sleep(500);
// Assert the list composition. // Assert the list composition.
PrivacyList[] privacyItems = privacyManager.getPrivacyLists(); PrivacyList[] privacyLists = privacyManager.getPrivacyLists();
PrivacyList receivedList1 = null; PrivacyList receivedList1 = null;
PrivacyList receivedList2 = null; PrivacyList receivedList2 = null;
for (int i = 0; i < privacyItems.length; i++) { for (PrivacyList privacyList : privacyLists) {
if (listName1.equals(privacyItems[i].toString())) { if (listName1.equals(privacyList.toString())) {
receivedList1 = privacyItems[i]; receivedList1 = privacyList;
} }
if (listName2.equals(privacyItems[i].toString())) { if (listName2.equals(privacyList.toString())) {
receivedList2 = privacyItems[i]; receivedList2 = privacyList;
} }
} }
PrivacyItem receivedItem; PrivacyItem receivedItem;
// Assert the list 1 // Assert on the list 1
assertNotNull(receivedList1); assertNotNull(receivedList1);
assertEquals(1, receivedList1.getItems().size()); assertEquals(1, receivedList1.getItems().size());
receivedItem = (PrivacyItem) receivedList1.getItems().get(0); receivedItem = receivedList1.getItems().get(0);
assertEquals(1, receivedItem.getOrder()); assertEquals(1, receivedItem.getOrder());
assertEquals(PrivacyRule.JID, receivedItem.getType()); assertEquals(PrivacyItem.Type.jid, receivedItem.getType());
assertEquals(true, receivedItem.isAllow()); assertEquals(true, receivedItem.isAllow());
assertEquals(getConnection(0).getUser(), receivedItem.getValue());
// Assert the list 2 // Assert on the list 2
assertNotNull(receivedList2); assertNotNull(receivedList2);
assertEquals(1, receivedList2.getItems().size()); assertEquals(1, receivedList2.getItems().size());
receivedItem = (PrivacyItem) receivedList2.getItems().get(0); receivedItem = receivedList2.getItems().get(0);
assertEquals(2, receivedItem.getOrder()); assertEquals(2, receivedItem.getOrder());
assertEquals(PrivacyRule.GROUP, receivedItem.getType()); assertEquals(PrivacyItem.Type.group, receivedItem.getType());
assertEquals(groupName, receivedItem.getValue());
assertEquals(false, receivedItem.isAllow()); assertEquals(false, receivedItem.isAllow());
assertEquals(groupName, receivedItem.getValue()); assertEquals(groupName, receivedItem.getValue());
assertEquals(false, receivedItem.isFilterEverything()); assertEquals(false, receivedItem.isFilterEverything());
assertEquals(true, receivedItem.isFilterMessage()); assertEquals(true, receivedItem.isFilterMessage());
assertEquals(false, receivedItem.isFilterPresence_in()); assertEquals(false, receivedItem.isFilterPresence_in());
assertEquals(false, receivedItem.isFilterPresence_out()); assertEquals(false, receivedItem.isFilterPresence_out());
privacyManager.deletePrivacyList(listName1);
privacyManager.deletePrivacyList(listName2);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
fail(e.getMessage()); fail(e.getMessage());
@ -140,8 +152,8 @@ public class PrivacyTest extends SmackTestCase {
privacyManager.addListener(client); privacyManager.addListener(client);
// Add the list that will be set as the active // Add the list that will be set as the active
ArrayList items = new ArrayList(); ArrayList<PrivacyItem> items = new ArrayList<PrivacyItem>();
PrivacyItem item = new PrivacyItem(PrivacyRule.JID, true, 1); PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid.name(), true, 1);
item.setValue(getConnection(0).getUser()); item.setValue(getConnection(0).getUser());
items.add(item); items.add(item);
privacyManager.createPrivacyList(listName, items); privacyManager.createPrivacyList(listName, items);
@ -150,7 +162,7 @@ public class PrivacyTest extends SmackTestCase {
// Remove the existing item and add a new one. // Remove the existing item and add a new one.
items.remove(item); items.remove(item);
item = new PrivacyItem(PrivacyRule.JID, false, 2); item = new PrivacyItem(PrivacyItem.Type.jid.name(), false, 2);
item.setValue(user); item.setValue(user);
item.setFilterPresence_out(true); item.setFilterPresence_out(true);
item.setFilterPresence_in(true); item.setFilterPresence_in(true);
@ -167,9 +179,9 @@ public class PrivacyTest extends SmackTestCase {
assertEquals(1, list.getItems().size()); assertEquals(1, list.getItems().size());
// Assert the privacy item composition // Assert the privacy item composition
PrivacyItem receivedItem = (PrivacyItem) list.getItems().get(0); PrivacyItem receivedItem = list.getItems().get(0);
assertEquals(2, receivedItem.getOrder()); assertEquals(2, receivedItem.getOrder());
assertEquals(PrivacyRule.JID, receivedItem.getType()); assertEquals(PrivacyItem.Type.jid, receivedItem.getType());
assertEquals(false, receivedItem.isAllow()); assertEquals(false, receivedItem.isAllow());
assertEquals(user, receivedItem.getValue()); assertEquals(user, receivedItem.getValue());
assertEquals(false, receivedItem.isFilterEverything()); assertEquals(false, receivedItem.isFilterEverything());
@ -178,6 +190,7 @@ public class PrivacyTest extends SmackTestCase {
assertEquals(true, receivedItem.isFilterPresence_out()); assertEquals(true, receivedItem.isFilterPresence_out());
assertEquals(true, client.wasModified()); assertEquals(true, client.wasModified());
privacyManager.deletePrivacyList(listName);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
fail(e.getMessage()); fail(e.getMessage());
@ -250,8 +263,8 @@ public class PrivacyTest extends SmackTestCase {
privacyManager.addListener(client); privacyManager.addListener(client);
// Add the list that will be set as the Default // Add the list that will be set as the Default
ArrayList items = new ArrayList(); ArrayList<PrivacyItem> items = new ArrayList<PrivacyItem>();
PrivacyItem item = new PrivacyItem(PrivacyRule.JID, true, 1); PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid.name(), true, 1);
item.setValue(getConnection(0).getUser()); item.setValue(getConnection(0).getUser());
items.add(item); items.add(item);
privacyManager.createPrivacyList(listName, items); privacyManager.createPrivacyList(listName, items);
@ -271,8 +284,10 @@ public class PrivacyTest extends SmackTestCase {
// Assert the privacy item composition // Assert the privacy item composition
PrivacyItem receivedItem = (PrivacyItem) privacyItems.get(0); PrivacyItem receivedItem = (PrivacyItem) privacyItems.get(0);
assertEquals(1, receivedItem.getOrder()); assertEquals(1, receivedItem.getOrder());
assertEquals(PrivacyRule.JID, receivedItem.getType()); assertEquals(PrivacyItem.Type.jid, receivedItem.getType());
assertEquals(true, receivedItem.isAllow()); assertEquals(true, receivedItem.isAllow());
privacyManager.deletePrivacyList(listName);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
fail(e.getMessage()); fail(e.getMessage());
@ -291,8 +306,8 @@ public class PrivacyTest extends SmackTestCase {
privacyManager.addListener(client); privacyManager.addListener(client);
// Add the list that will be set as the Default // Add the list that will be set as the Default
ArrayList items = new ArrayList(); ArrayList<PrivacyItem> items = new ArrayList<PrivacyItem>();
PrivacyItem item = new PrivacyItem(PrivacyRule.JID, true, 1); PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid.name(), true, 1);
item.setValue(getConnection(0).getUser()); item.setValue(getConnection(0).getUser());
items.add(item); items.add(item);
privacyManager.createPrivacyList(listName, items); privacyManager.createPrivacyList(listName, items);
@ -314,7 +329,6 @@ public class PrivacyTest extends SmackTestCase {
} catch (XMPPException xmppException) { } catch (XMPPException xmppException) {
assertEquals(404, xmppException.getXMPPError().getCode()); assertEquals(404, xmppException.getXMPPError().getCode());
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
fail(e.getMessage()); fail(e.getMessage());
@ -338,52 +352,52 @@ public class PrivacyTest extends SmackTestCase {
int i=0; int i=0;
// Items to test JID // Items to test JID
PrivacyItem item = new PrivacyItem(PrivacyRule.JID, true, i); PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid.name(), true, i);
item.setValue(i + "_" + user); item.setValue(i + "_" + user);
originalPrivacyItems[i] = item; originalPrivacyItems[i] = item;
i = i + 1; i = i + 1;
item = new PrivacyItem(PrivacyRule.JID, false, i); item = new PrivacyItem(PrivacyItem.Type.jid.name(), false, i);
item.setValue(i + "_" + user); item.setValue(i + "_" + user);
originalPrivacyItems[i] = item; originalPrivacyItems[i] = item;
i = i + 1; i = i + 1;
// Items to test suscription // Items to test suscription
item = new PrivacyItem(PrivacyRule.SUBSCRIPTION, true, i); item = new PrivacyItem(PrivacyItem.Type.subscription.name(), true, i);
item.setValue(PrivacyRule.SUBSCRIPTION_BOTH); item.setValue(PrivacyRule.SUBSCRIPTION_BOTH);
originalPrivacyItems[i] = item; originalPrivacyItems[i] = item;
i = i + 1; i = i + 1;
item = new PrivacyItem(PrivacyRule.SUBSCRIPTION, false, i); item = new PrivacyItem(PrivacyItem.Type.subscription.name(), false, i);
item.setValue(PrivacyRule.SUBSCRIPTION_FROM); item.setValue(PrivacyRule.SUBSCRIPTION_FROM);
originalPrivacyItems[i] = item; originalPrivacyItems[i] = item;
i = i + 1; i = i + 1;
item = new PrivacyItem(PrivacyRule.SUBSCRIPTION, true, i); item = new PrivacyItem(PrivacyItem.Type.subscription.name(), true, i);
item.setValue(PrivacyRule.SUBSCRIPTION_TO); item.setValue(PrivacyRule.SUBSCRIPTION_TO);
originalPrivacyItems[i] = item; originalPrivacyItems[i] = item;
i = i + 1; i = i + 1;
item = new PrivacyItem(PrivacyRule.SUBSCRIPTION, false, i); item = new PrivacyItem(PrivacyItem.Type.subscription.name(), false, i);
item.setValue(PrivacyRule.SUBSCRIPTION_NONE); item.setValue(PrivacyRule.SUBSCRIPTION_NONE);
originalPrivacyItems[i] = item; originalPrivacyItems[i] = item;
i = i + 1; i = i + 1;
// Items to test Group // Items to test Group
item = new PrivacyItem(PrivacyRule.GROUP, false, i); item = new PrivacyItem(PrivacyItem.Type.group.name(), false, i);
item.setValue(groupName); item.setValue(groupName);
originalPrivacyItems[i] = item; originalPrivacyItems[i] = item;
i = i + 1; i = i + 1;
// Items to test messages // Items to test messages
item = new PrivacyItem(PrivacyRule.GROUP, false, i); item = new PrivacyItem(PrivacyItem.Type.group.name(), false, i);
item.setValue(groupName); item.setValue(groupName);
item.setFilterMessage(true); item.setFilterMessage(true);
originalPrivacyItems[i] = item; originalPrivacyItems[i] = item;
i = i + 1; i = i + 1;
// Items to test presence notifications // Items to test presence notifications
item = new PrivacyItem(PrivacyRule.GROUP, false, i); item = new PrivacyItem(PrivacyItem.Type.group.name(), false, i);
item.setValue(groupName); item.setValue(groupName);
item.setFilterMessage(true); item.setFilterMessage(true);
originalPrivacyItems[i] = item; originalPrivacyItems[i] = item;
@ -394,20 +408,18 @@ public class PrivacyTest extends SmackTestCase {
originalPrivacyItems[i] = item; originalPrivacyItems[i] = item;
i = i + 1; i = i + 1;
item = new PrivacyItem(PrivacyRule.SUBSCRIPTION, false, i); item = new PrivacyItem(PrivacyItem.Type.subscription.name(), false, i);
item.setValue(PrivacyRule.SUBSCRIPTION_TO); item.setValue(PrivacyRule.SUBSCRIPTION_TO);
item.setFilterPresence_out(true); item.setFilterPresence_out(true);
originalPrivacyItems[i] = item; originalPrivacyItems[i] = item;
i = i + 1; i = i + 1;
item = new PrivacyItem(PrivacyRule.JID, false, i); item = new PrivacyItem(PrivacyItem.Type.jid.name(), false, i);
item.setValue(i + "_" + user); item.setValue(i + "_" + user);
item.setFilterPresence_out(true); item.setFilterPresence_out(true);
item.setFilterPresence_in(true); item.setFilterPresence_in(true);
item.setFilterMessage(true); item.setFilterMessage(true);
originalPrivacyItems[i] = item; originalPrivacyItems[i] = item;
i = i + 1;
// Set the new privacy list // Set the new privacy list
privacyManager.createPrivacyList(listName, Arrays.asList(originalPrivacyItems)); privacyManager.createPrivacyList(listName, Arrays.asList(originalPrivacyItems));
@ -421,7 +433,7 @@ public class PrivacyTest extends SmackTestCase {
// Assert the local and server privacy item composition // Assert the local and server privacy item composition
PrivacyItem originalItem; PrivacyItem originalItem;
PrivacyItem receivedItem; PrivacyItem receivedItem;
int index = 0; int index;
for (int j = 0; j < originalPrivacyItems.length; j++) { for (int j = 0; j < originalPrivacyItems.length; j++) {
// Look for the same server and original items // Look for the same server and original items
receivedItem = (PrivacyItem) privacyItems.get(j); receivedItem = (PrivacyItem) privacyItems.get(j);
@ -443,6 +455,8 @@ public class PrivacyTest extends SmackTestCase {
assertEquals(originalItem.isFilterPresence_in(), receivedItem.isFilterPresence_in()); assertEquals(originalItem.isFilterPresence_in(), receivedItem.isFilterPresence_in());
assertEquals(originalItem.isFilterPresence_out(), receivedItem.isFilterPresence_out()); assertEquals(originalItem.isFilterPresence_out(), receivedItem.isFilterPresence_out());
} }
privacyManager.deletePrivacyList(listName);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
fail(e.getMessage()); fail(e.getMessage());
@ -452,4 +466,42 @@ public class PrivacyTest extends SmackTestCase {
protected int getMaxConnections() { protected int getMaxConnections() {
return 1; return 1;
} }
/**
* This class supports automated tests about privacy communication from the
* server to the client.
*
* @author Francisco Vives
*/
public class PrivacyClient implements PrivacyListListener {
/**
* holds if the receiver list was modified
*/
private boolean wasModified = false;
/**
* holds a privacy to hold server requests Clients should not use Privacy
* class since it is private for the smack framework.
*/
private Privacy privacy = new Privacy();
public PrivacyClient(PrivacyListManager manager) {
super();
}
public void setPrivacyList(String listName, List<PrivacyItem> listItem) {
privacy.setPrivacyList(listName, listItem);
}
public void updatedPrivacyList(String listName) {
this.wasModified = true;
}
public boolean wasModified() {
return this.wasModified;
}
}
} }