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

View File

@ -1,5 +1,7 @@
package org.jivesoftware.smack;
import org.jivesoftware.smack.packet.PrivacyItem;
import java.util.List;
/**
@ -18,7 +20,7 @@ public interface PrivacyListListener {
* @param listName the name of the new or updated privacy 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.

View File

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

View File

@ -1,10 +1,6 @@
package org.jivesoftware.smack.packet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.List;
import java.util.Set;
import java.util.*;
/**
* 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;
/** 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. **/
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}.
@ -43,7 +39,7 @@ public class Privacy extends IQ {
* @param listItem the {@link PrivacyItem} that rules the 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
this.getItemLists().put(listName, listItem);
return listItem;
@ -54,9 +50,9 @@ public class Privacy extends IQ {
*
* @return the active List.
*/
public List setActivePrivacyList() {
public List<PrivacyItem> setActivePrivacyList() {
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.
*/
public List getActivePrivacyList() {
public List<PrivacyItem> getActivePrivacyList() {
// Check if we have the default list
if (this.getActiveName() == null) {
return null;
} 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.
*/
public List getDefaultPrivacyList() {
public List<PrivacyItem> getDefaultPrivacyList() {
// Check if we have the default list
if (this.getDefaultName() == null) {
return null;
} 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.
* @return a List with {@link PrivacyItem}
*/
public List getPrivacyList(String listName) {
return (List) this.getItemLists().get(listName);
public List<PrivacyItem> getPrivacyList(String listName) {
return this.getItemLists().get(listName);
}
/**
@ -121,10 +117,10 @@ public class Privacy extends IQ {
* @return a List with {@link PrivacyItem}
*/
public PrivacyItem getItem(String listName, int order) {
Iterator values = getPrivacyList(listName).iterator();
Iterator<PrivacyItem> values = getPrivacyList(listName).iterator();
PrivacyItem itemFound = null;
while (itemFound == null && values.hasNext()) {
PrivacyItem element = (PrivacyItem) values.next();
PrivacyItem element = values.next();
if (element.getOrder() == order) {
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
* collection of privacy items.
*/
public Map getItemLists() {
public Map<String, List<PrivacyItem>> getItemLists() {
return itemLists;
}
@ -245,7 +241,7 @@ public class Privacy extends IQ {
/**
* 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) {
this.declineDefaultList = declineDefaultList;
@ -256,7 +252,7 @@ public class Privacy extends IQ {
*
* @return a Set with Strings containing every list names.
*/
public Set getPrivacyListNames() {
public Set<String> getPrivacyListNames() {
return this.itemLists.keySet();
}
@ -282,20 +278,16 @@ public class Privacy extends IQ {
}
// Add the list with their privacy items
int listNameSize = this.getItemLists().size();
Iterator listItemsPairs = this.getItemLists().entrySet().iterator();
for (int i = 0; i < listNameSize; i++) {
Map.Entry entry = (Map.Entry) listItemsPairs.next();
String listName = (String) entry.getKey();
List items = (List) entry.getValue();
for (Map.Entry<String, List<PrivacyItem>> entry : this.getItemLists().entrySet()) {
String listName = entry.getKey();
List<PrivacyItem> items = entry.getValue();
// Begin the list tag
if (items.isEmpty()) {
buf.append("<list name=\"").append(listName).append("\"/>");
} else {
buf.append("<list name=\"").append(listName).append("\">");
}
for (Iterator itemIterator = items.iterator(); itemIterator.hasNext();) {
PrivacyItem item = (PrivacyItem) itemIterator.next();
for (PrivacyItem item : items) {
// Append the item xml representation
buf.append(item.toXML());
}
@ -308,8 +300,7 @@ public class Privacy extends IQ {
// Add packet extensions, if any are defined.
buf.append(getExtensionsXML());
buf.append("</query>");
String generatedXML = buf.toString();
return generatedXML;
return buf.toString();
}
}

View File

@ -196,7 +196,7 @@ public class PrivacyItem {
*
* @return the type of communication it represent.
*/
public String getType() {
public Type getType() {
if (this.getRule() == null) {
return null;
} else {
@ -298,7 +298,7 @@ public class PrivacyItem {
* Type defines if the rule is based on JIDs, roster groups or presence subscription types.
* Available values are: [jid|group|subscription]
*/
private String type;
private Type type;
/**
* 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.
@ -310,24 +310,10 @@ public class PrivacyItem {
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",
* "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_FROM = "from";
public static final String SUBSCRIPTION_NONE = "none";
@ -336,25 +322,11 @@ public class PrivacyItem {
* Returns the type constant associated with the String value.
*/
protected static PrivacyRule fromString(String value) {
String type = null;
if (value == 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();
rule.setType(type);
rule.setType(Type.valueOf(value.toLowerCase()));
return rule;
}
@ -364,16 +336,16 @@ public class PrivacyItem {
*
* @return the type of communication it represent.
*/
public String getType() {
public Type getType() {
return type;
}
/**
* 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;
}
@ -419,9 +391,9 @@ public class PrivacyItem {
* @param value is the identifier to apply the action.
*/
private void setSuscriptionValue(String value) {
String setValue = null;
String setValue;
if (value == null) {
setValue = null;
// Do nothing
}
if (SUBSCRIPTION_BOTH.equalsIgnoreCase(value)) {
setValue = SUBSCRIPTION_BOTH;
@ -448,7 +420,26 @@ public class PrivacyItem {
* @return if the receiver represents a subscription rule.
*/
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.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
/**
@ -67,7 +66,7 @@ public class XMPPError {
private Type type;
private String condition;
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
* new XMPPError(new XMPPError.Condition("my_own_error"));
*
* @param code the error code.
* @param condition the error condition.
*/
public XMPPError(Condition condition) {
this.init(condition);
@ -91,8 +90,8 @@ public class XMPPError {
* If the Condition is not predefined, invocations should be like
* new XMPPError(new XMPPError.Condition("my_own_error"), "Error Explanation");
*
* @param code the error code.
* @param message a message describing the error.
* @param condition the error condition.
* @param messageText a message describing the error.
*/
public XMPPError(Condition condition, String messageText) {
this.init(condition);
@ -103,7 +102,7 @@ public class XMPPError {
* Creates a new error with the specified code and no message.
*
* @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) {
this.code = code;
@ -116,7 +115,7 @@ public class XMPPError {
*
* @param code the error code.
* @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) {
this.code = code;
@ -135,7 +134,8 @@ public class XMPPError {
* @param condition the error condition.
* @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.type = type;
this.condition = condition;
@ -205,7 +205,7 @@ public class XMPPError {
buf.append("<error code=\"").append(code).append("\"");
if (type != null) {
buf.append(" type=\"");
buf.append(type.value);
buf.append(type.name());
buf.append("\"");
}
buf.append(">");
@ -218,8 +218,7 @@ public class XMPPError {
buf.append(message);
buf.append("</text>");
}
for (Iterator extensions = this.getExtensions(); extensions.hasNext();) {
PacketExtension element = (PacketExtension) extensions.next();
for (PacketExtension element : this.getExtensions()) {
buf.append(element.toXML());
}
buf.append("</error>");
@ -228,6 +227,9 @@ public class XMPPError {
public String toString() {
StringBuffer txt = new StringBuffer();
if (condition != null) {
txt.append(condition);
}
txt.append("(").append(code).append(")");
if (message != null) {
txt.append(" ").append(message);
@ -242,11 +244,11 @@ public class XMPPError {
*
* @return an Iterator for the error extensions.
*/
public synchronized Iterator getExtensions() {
public synchronized List<PacketExtension> getExtensions() {
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) {
return null;
}
for (Iterator i=applicationExtensions.iterator(); i.hasNext(); ) {
PacketExtension ext = (PacketExtension)i.next();
for (PacketExtension ext : applicationExtensions) {
if (elementName.equals(ext.getElementName()) && namespace.equals(ext.getNamespace())) {
return ext;
}
@ -277,7 +278,7 @@ public class XMPPError {
*/
public synchronized void addExtension(PacketExtension extension) {
if (applicationExtensions == null) {
applicationExtensions = new ArrayList();
applicationExtensions = new ArrayList<PacketExtension>();
}
applicationExtensions.add(extension);
}
@ -287,7 +288,7 @@ public class XMPPError {
*
* @param extension a packet extension.
*/
public synchronized void setExtension(List extension) {
public synchronized void setExtension(List<PacketExtension> extension) {
applicationExtensions = extension;
}
@ -303,55 +304,12 @@ public class XMPPError {
* <li>XMPPError.Type.CONTINUE - proceed (the condition was only a warning)
* </ul>
*/
public static class Type {
public static final Type WAIT = new Type("wait");
public static final Type CANCEL = new Type("cancel");
public static final Type MODIFY = new Type("modify");
public static final Type AUTH = new Type("auth");
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;
}
public static enum Type {
WAIT,
CANCEL,
MODIFY,
AUTH,
CONTINUE
}
/**
@ -403,7 +361,7 @@ public class XMPPError {
private int code;
private Type type;
private Condition condition;
private static HashMap instances = errorSpecifications();
private static HashMap<Condition, ErrorSpecification> instances = errorSpecifications();
private ErrorSpecification(Condition condition, Type type, int code) {
this.code = code;
@ -411,8 +369,8 @@ public class XMPPError {
this.condition = condition;
}
private static HashMap errorSpecifications() {
HashMap instances = new HashMap(22);
private static HashMap<Condition, ErrorSpecification> errorSpecifications() {
HashMap<Condition, ErrorSpecification> instances = new HashMap<Condition, ErrorSpecification>(22);
instances.put(Condition.interna_server_error, new ErrorSpecification(
Condition.interna_server_error, Type.WAIT, 500));
instances.put(Condition.forbidden, new ErrorSpecification(Condition.forbidden,
@ -466,7 +424,7 @@ public class XMPPError {
}
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;
import java.util.ArrayList;
import org.jivesoftware.smack.packet.DefaultPacketExtension;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Privacy;
import org.jivesoftware.smack.packet.PrivacyItem;
import org.xmlpull.v1.XmlPullParser;
import java.util.ArrayList;
/**
* The PrivacyProvider parses {@link Privacy} packets. {@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 {
boolean done = false;
String listName = parser.getAttributeValue("", "name");
ArrayList items = new ArrayList();
ArrayList<PrivacyItem> items = new ArrayList<PrivacyItem>();
while (!done) {
int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) {

View File

@ -20,16 +20,19 @@
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.provider.PacketExtensionProvider;
import org.jivesoftware.smack.provider.ProviderManager;
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
* 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
.fromString(type), condition, message, extensions);
.valueOf(type.toUpperCase()), condition, message, extensions);
}
/**

View File

@ -1,13 +1,13 @@
package org.jivesoftware.smack.packet;
import java.io.StringReader;
import org.jivesoftware.smack.provider.PrivacyProvider;
import org.jivesoftware.smack.test.SmackTestCase;
import org.xmlpull.mxp1.MXParser;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.StringReader;
/**
* Test the PrivacyProvider class with valids privacy xmls
*
@ -131,7 +131,7 @@ public class PrivacyProviderTest extends SmackTestCase {
// check each privacy item
PrivacyItem item = packet.getItem("testGroup", 4);
assertEquals("Enemies", item.getValue());
assertEquals("group", item.getType());
assertEquals(PrivacyItem.Type.group, item.getType());
assertEquals(false, item.isAllow());
assertEquals(true, item.isFilterMessage());
assertEquals(false, item.isFilterIQ());
@ -141,7 +141,7 @@ public class PrivacyProviderTest extends SmackTestCase {
item = packet.getItem("testFilter", 1);
assertEquals("tybalt@example.com", item.getValue());
assertEquals("jid", item.getType());
assertEquals(PrivacyItem.Type.jid, item.getType());
assertEquals(false, item.isAllow());
assertEquals(false, item.isFilterMessage());
assertEquals(false, item.isFilterIQ());
@ -162,7 +162,7 @@ public class PrivacyProviderTest extends SmackTestCase {
// TEST THE testSubscription LIST
item = packet.getItem("testSubscription", 10);
assertEquals("both", item.getValue());
assertEquals("subscription", item.getType());
assertEquals(PrivacyItem.Type.subscription, item.getType());
assertEquals(true, item.isAllow());
assertEquals(false, item.isFilterMessage());
assertEquals(false, item.isFilterIQ());
@ -172,7 +172,7 @@ public class PrivacyProviderTest extends SmackTestCase {
item = packet.getItem("testSubscription", 11);
assertEquals("to", item.getValue());
assertEquals("subscription", item.getType());
assertEquals(PrivacyItem.Type.subscription, item.getType());
assertEquals(true, item.isAllow());
assertEquals(false, item.isFilterMessage());
assertEquals(false, item.isFilterIQ());
@ -182,7 +182,7 @@ public class PrivacyProviderTest extends SmackTestCase {
item = packet.getItem("testSubscription", 12);
assertEquals("from", item.getValue());
assertEquals("subscription", item.getType());
assertEquals(PrivacyItem.Type.subscription, item.getType());
assertEquals(true, item.isAllow());
assertEquals(false, item.isFilterMessage());
assertEquals(false, item.isFilterIQ());
@ -192,7 +192,7 @@ public class PrivacyProviderTest extends SmackTestCase {
item = packet.getItem("testSubscription", 5);
assertEquals("none", item.getValue());
assertEquals("subscription", item.getType());
assertEquals(PrivacyItem.Type.subscription, item.getType());
assertEquals(false, item.isAllow());
assertEquals(true, item.isFilterMessage());
assertEquals(false, item.isFilterIQ());
@ -214,7 +214,7 @@ public class PrivacyProviderTest extends SmackTestCase {
item = packet.getItem("testJID", 6);
assertEquals("juliet@example.com", item.getValue());
assertEquals("jid", item.getType());
assertEquals(PrivacyItem.Type.jid, item.getType());
assertEquals(true, item.isAllow());
assertEquals(false, item.isFilterMessage());
assertEquals(false, item.isFilterIQ());
@ -224,7 +224,7 @@ public class PrivacyProviderTest extends SmackTestCase {
item = packet.getItem("testJID", 7);
assertEquals("benvolio@example.org/palm", item.getValue());
assertEquals("jid", item.getType());
assertEquals(PrivacyItem.Type.jid, item.getType());
assertEquals(false, item.isAllow());
assertEquals(false, item.isFilterMessage());
assertEquals(false, item.isFilterIQ());
@ -234,7 +234,7 @@ public class PrivacyProviderTest extends SmackTestCase {
item = packet.getItem("testJID", 42);
assertEquals(null, item.getValue());
assertEquals("jid", item.getType());
assertEquals(PrivacyItem.Type.jid, item.getType());
assertEquals(true, item.isAllow());
assertEquals(false, item.isFilterMessage());
assertEquals(false, item.isFilterIQ());
@ -256,7 +256,7 @@ public class PrivacyProviderTest extends SmackTestCase {
item = packet.getItem("testGroup", 4);
assertEquals("Enemies", item.getValue());
assertEquals("group", item.getType());
assertEquals(PrivacyItem.Type.group, item.getType());
assertEquals(false, item.isAllow());
assertEquals(true, item.isFilterMessage());
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.Arrays;
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 PrivacyTest(String arg0) {
@ -27,8 +30,8 @@ public class PrivacyTest extends SmackTestCase {
privacyManager.addListener(client);
// Add the list that will be set as the active
ArrayList items = new ArrayList();
PrivacyItem item = new PrivacyItem(PrivacyRule.JID, true, 1);
ArrayList<PrivacyItem> items = new ArrayList<PrivacyItem>();
PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid.name(), true, 1);
item.setValue(getConnection(0).getUser());
items.add(item);
privacyManager.createPrivacyList(listName, items);
@ -48,8 +51,10 @@ public class PrivacyTest extends SmackTestCase {
// Assert the privacy item composition
PrivacyItem receivedItem = (PrivacyItem) privacyItems.get(0);
assertEquals(1, receivedItem.getOrder());
assertEquals(PrivacyRule.JID, receivedItem.getType());
assertEquals(PrivacyItem.Type.jid, receivedItem.getType());
assertEquals(true, receivedItem.isAllow());
privacyManager.deletePrivacyList(listName);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
@ -70,15 +75,17 @@ public class PrivacyTest extends SmackTestCase {
privacyManager.addListener(client);
// Add a list
ArrayList items = new ArrayList();
PrivacyItem item = new PrivacyItem(PrivacyRule.JID, true, 1);
ArrayList<PrivacyItem> items = new ArrayList<PrivacyItem>();
PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid.name(), true, 1);
item.setValue(getConnection(0).getUser());
items.add(item);
privacyManager.createPrivacyList(listName1, items);
Thread.sleep(500);
// Add the another list
ArrayList itemsList2 = new ArrayList();
item = new PrivacyItem(PrivacyRule.GROUP, false, 2);
ArrayList<PrivacyItem> itemsList2 = new ArrayList<PrivacyItem>();
item = new PrivacyItem(PrivacyItem.Type.group.name(), false, 2);
item.setValue(groupName);
item.setFilterMessage(true);
itemsList2.add(item);
@ -87,40 +94,45 @@ public class PrivacyTest extends SmackTestCase {
Thread.sleep(500);
// Assert the list composition.
PrivacyList[] privacyItems = privacyManager.getPrivacyLists();
PrivacyList[] privacyLists = privacyManager.getPrivacyLists();
PrivacyList receivedList1 = null;
PrivacyList receivedList2 = null;
for (int i = 0; i < privacyItems.length; i++) {
if (listName1.equals(privacyItems[i].toString())) {
receivedList1 = privacyItems[i];
for (PrivacyList privacyList : privacyLists) {
if (listName1.equals(privacyList.toString())) {
receivedList1 = privacyList;
}
if (listName2.equals(privacyItems[i].toString())) {
receivedList2 = privacyItems[i];
if (listName2.equals(privacyList.toString())) {
receivedList2 = privacyList;
}
}
PrivacyItem receivedItem;
// Assert the list 1
// Assert on the list 1
assertNotNull(receivedList1);
assertEquals(1, receivedList1.getItems().size());
receivedItem = (PrivacyItem) receivedList1.getItems().get(0);
receivedItem = receivedList1.getItems().get(0);
assertEquals(1, receivedItem.getOrder());
assertEquals(PrivacyRule.JID, receivedItem.getType());
assertEquals(PrivacyItem.Type.jid, receivedItem.getType());
assertEquals(true, receivedItem.isAllow());
assertEquals(getConnection(0).getUser(), receivedItem.getValue());
// Assert the list 2
// Assert on the list 2
assertNotNull(receivedList2);
assertEquals(1, receivedList2.getItems().size());
receivedItem = (PrivacyItem) receivedList2.getItems().get(0);
receivedItem = receivedList2.getItems().get(0);
assertEquals(2, receivedItem.getOrder());
assertEquals(PrivacyRule.GROUP, receivedItem.getType());
assertEquals(PrivacyItem.Type.group, receivedItem.getType());
assertEquals(groupName, receivedItem.getValue());
assertEquals(false, receivedItem.isAllow());
assertEquals(groupName, receivedItem.getValue());
assertEquals(false, receivedItem.isFilterEverything());
assertEquals(true, receivedItem.isFilterMessage());
assertEquals(false, receivedItem.isFilterPresence_in());
assertEquals(false, receivedItem.isFilterPresence_out());
privacyManager.deletePrivacyList(listName1);
privacyManager.deletePrivacyList(listName2);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
@ -140,8 +152,8 @@ public class PrivacyTest extends SmackTestCase {
privacyManager.addListener(client);
// Add the list that will be set as the active
ArrayList items = new ArrayList();
PrivacyItem item = new PrivacyItem(PrivacyRule.JID, true, 1);
ArrayList<PrivacyItem> items = new ArrayList<PrivacyItem>();
PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid.name(), true, 1);
item.setValue(getConnection(0).getUser());
items.add(item);
privacyManager.createPrivacyList(listName, items);
@ -150,7 +162,7 @@ public class PrivacyTest extends SmackTestCase {
// Remove the existing item and add a new one.
items.remove(item);
item = new PrivacyItem(PrivacyRule.JID, false, 2);
item = new PrivacyItem(PrivacyItem.Type.jid.name(), false, 2);
item.setValue(user);
item.setFilterPresence_out(true);
item.setFilterPresence_in(true);
@ -167,9 +179,9 @@ public class PrivacyTest extends SmackTestCase {
assertEquals(1, list.getItems().size());
// Assert the privacy item composition
PrivacyItem receivedItem = (PrivacyItem) list.getItems().get(0);
PrivacyItem receivedItem = list.getItems().get(0);
assertEquals(2, receivedItem.getOrder());
assertEquals(PrivacyRule.JID, receivedItem.getType());
assertEquals(PrivacyItem.Type.jid, receivedItem.getType());
assertEquals(false, receivedItem.isAllow());
assertEquals(user, receivedItem.getValue());
assertEquals(false, receivedItem.isFilterEverything());
@ -178,6 +190,7 @@ public class PrivacyTest extends SmackTestCase {
assertEquals(true, receivedItem.isFilterPresence_out());
assertEquals(true, client.wasModified());
privacyManager.deletePrivacyList(listName);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
@ -250,8 +263,8 @@ public class PrivacyTest extends SmackTestCase {
privacyManager.addListener(client);
// Add the list that will be set as the Default
ArrayList items = new ArrayList();
PrivacyItem item = new PrivacyItem(PrivacyRule.JID, true, 1);
ArrayList<PrivacyItem> items = new ArrayList<PrivacyItem>();
PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid.name(), true, 1);
item.setValue(getConnection(0).getUser());
items.add(item);
privacyManager.createPrivacyList(listName, items);
@ -271,8 +284,10 @@ public class PrivacyTest extends SmackTestCase {
// Assert the privacy item composition
PrivacyItem receivedItem = (PrivacyItem) privacyItems.get(0);
assertEquals(1, receivedItem.getOrder());
assertEquals(PrivacyRule.JID, receivedItem.getType());
assertEquals(PrivacyItem.Type.jid, receivedItem.getType());
assertEquals(true, receivedItem.isAllow());
privacyManager.deletePrivacyList(listName);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
@ -291,8 +306,8 @@ public class PrivacyTest extends SmackTestCase {
privacyManager.addListener(client);
// Add the list that will be set as the Default
ArrayList items = new ArrayList();
PrivacyItem item = new PrivacyItem(PrivacyRule.JID, true, 1);
ArrayList<PrivacyItem> items = new ArrayList<PrivacyItem>();
PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid.name(), true, 1);
item.setValue(getConnection(0).getUser());
items.add(item);
privacyManager.createPrivacyList(listName, items);
@ -314,7 +329,6 @@ public class PrivacyTest extends SmackTestCase {
} catch (XMPPException xmppException) {
assertEquals(404, xmppException.getXMPPError().getCode());
}
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
@ -338,52 +352,52 @@ public class PrivacyTest extends SmackTestCase {
int i=0;
// 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);
originalPrivacyItems[i] = item;
i = i + 1;
item = new PrivacyItem(PrivacyRule.JID, false, i);
item = new PrivacyItem(PrivacyItem.Type.jid.name(), false, i);
item.setValue(i + "_" + user);
originalPrivacyItems[i] = item;
i = i + 1;
// 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);
originalPrivacyItems[i] = item;
i = i + 1;
item = new PrivacyItem(PrivacyRule.SUBSCRIPTION, false, i);
item = new PrivacyItem(PrivacyItem.Type.subscription.name(), false, i);
item.setValue(PrivacyRule.SUBSCRIPTION_FROM);
originalPrivacyItems[i] = item;
i = i + 1;
item = new PrivacyItem(PrivacyRule.SUBSCRIPTION, true, i);
item = new PrivacyItem(PrivacyItem.Type.subscription.name(), true, i);
item.setValue(PrivacyRule.SUBSCRIPTION_TO);
originalPrivacyItems[i] = item;
i = i + 1;
item = new PrivacyItem(PrivacyRule.SUBSCRIPTION, false, i);
item = new PrivacyItem(PrivacyItem.Type.subscription.name(), false, i);
item.setValue(PrivacyRule.SUBSCRIPTION_NONE);
originalPrivacyItems[i] = item;
i = i + 1;
// Items to test Group
item = new PrivacyItem(PrivacyRule.GROUP, false, i);
item = new PrivacyItem(PrivacyItem.Type.group.name(), false, i);
item.setValue(groupName);
originalPrivacyItems[i] = item;
i = i + 1;
// Items to test messages
item = new PrivacyItem(PrivacyRule.GROUP, false, i);
item = new PrivacyItem(PrivacyItem.Type.group.name(), false, i);
item.setValue(groupName);
item.setFilterMessage(true);
originalPrivacyItems[i] = item;
i = i + 1;
// 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.setFilterMessage(true);
originalPrivacyItems[i] = item;
@ -394,21 +408,19 @@ public class PrivacyTest extends SmackTestCase {
originalPrivacyItems[i] = item;
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.setFilterPresence_out(true);
originalPrivacyItems[i] = item;
i = i + 1;
item = new PrivacyItem(PrivacyRule.JID, false, i);
item = new PrivacyItem(PrivacyItem.Type.jid.name(), false, i);
item.setValue(i + "_" + user);
item.setFilterPresence_out(true);
item.setFilterPresence_in(true);
item.setFilterMessage(true);
originalPrivacyItems[i] = item;
i = i + 1;
// Set the new privacy list
privacyManager.createPrivacyList(listName, Arrays.asList(originalPrivacyItems));
@ -421,7 +433,7 @@ public class PrivacyTest extends SmackTestCase {
// Assert the local and server privacy item composition
PrivacyItem originalItem;
PrivacyItem receivedItem;
int index = 0;
int index;
for (int j = 0; j < originalPrivacyItems.length; j++) {
// Look for the same server and original items
receivedItem = (PrivacyItem) privacyItems.get(j);
@ -443,6 +455,8 @@ public class PrivacyTest extends SmackTestCase {
assertEquals(originalItem.isFilterPresence_in(), receivedItem.isFilterPresence_in());
assertEquals(originalItem.isFilterPresence_out(), receivedItem.isFilterPresence_out());
}
privacyManager.deletePrivacyList(listName);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
@ -452,4 +466,42 @@ public class PrivacyTest extends SmackTestCase {
protected int getMaxConnections() {
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;
}
}
}