diff --git a/documentation/extensions/privacy.html b/documentation/extensions/privacy.html index 1e50f1314..025905aef 100644 --- a/documentation/extensions/privacy.html +++ b/documentation/extensions/privacy.html @@ -63,23 +63,20 @@ Now the client is able to show every PrivacyItem of the server and also String groupName = "enemies"; ArrayList privacyItems = new ArrayList(); - PrivacyItem item = new PrivacyItem(PrivacyRule.JID, true, 1); - item.setValue(user); + PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid, user, true, 1); privacyItems.add(item); - item = new PrivacyItem(PrivacyRule.SUBSCRIPTION, true, 2); - item.setValue(PrivacyRule.SUBSCRIPTION_BOTH); + item = new PrivacyItem(PrivacyItem.Type.subscription, PrivacyItem.SUBSCRIPTION_BOTH, true, 2); privacyItems.add(item); - item = new PrivacyItem(PrivacyRule.GROUP, false, 3); - item.setValue(groupName); + item = new PrivacyItem(PrivacyItem.Type.group, groupName, false, 3); item.setFilterMessage(true); privacyItems.add(item); // Get the privacy manager for the current connection. PrivacyListManager privacyManager = PrivacyListManager.getInstanceFor(myConnection); // Create the new list. - privacyManager.createPrivacyList(listName, Arrays.asList(privacyItems)); + privacyManager.createPrivacyList(listName, privacyItems); diff --git a/extensions/src/main/java/org/jivesoftware/smackx/privacy/PrivacyList.java b/extensions/src/main/java/org/jivesoftware/smackx/privacy/PrivacyList.java index dcd3830cf..50e30215d 100644 --- a/extensions/src/main/java/org/jivesoftware/smackx/privacy/PrivacyList.java +++ b/extensions/src/main/java/org/jivesoftware/smackx/privacy/PrivacyList.java @@ -37,13 +37,13 @@ import java.util.List; public class PrivacyList { /** Holds if it is an active list or not **/ - private boolean isActiveList; + private final boolean isActiveList; /** Holds if it is an default list or not **/ - private boolean isDefaultList; + private final boolean isDefaultList; /** Holds the list name used to print **/ - private String listName; + private final String listName; /** Holds the list of {@see PrivacyItem} **/ - private List items; + private final List items; protected PrivacyList(boolean isActiveList, boolean isDefaultList, String listName, List privacyItems) { @@ -54,6 +54,10 @@ public class PrivacyList { this.items = privacyItems; } + public String getName() { + return listName; + } + public boolean isActiveList() { return isActiveList; } @@ -66,8 +70,4 @@ public class PrivacyList { return items; } - public String toString() { - return listName; - } - } diff --git a/extensions/src/main/java/org/jivesoftware/smackx/privacy/PrivacyListManager.java b/extensions/src/main/java/org/jivesoftware/smackx/privacy/PrivacyListManager.java index 05c1b7bbc..b8ce18d79 100644 --- a/extensions/src/main/java/org/jivesoftware/smackx/privacy/PrivacyListManager.java +++ b/extensions/src/main/java/org/jivesoftware/smackx/privacy/PrivacyListManager.java @@ -28,9 +28,13 @@ import org.jivesoftware.smack.ConnectionCreationListener; import org.jivesoftware.smack.Manager; import org.jivesoftware.smack.PacketListener; import org.jivesoftware.smack.XMPPException; -import org.jivesoftware.smack.filter.*; +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.packet.IQ; import org.jivesoftware.smack.packet.Packet; +import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; import org.jivesoftware.smackx.privacy.packet.Privacy; import org.jivesoftware.smackx.privacy.packet.PrivacyItem; @@ -46,27 +50,29 @@ import org.jivesoftware.smackx.privacy.packet.PrivacyItem; * subscription type or globally (@see PrivacyItem). * * @author Francisco Vives + * @see XEP-16: Privacy Lists */ public class PrivacyListManager extends Manager { + public static final String NAMESPACE = "jabber:iq:privacy"; + + private static final PacketFilter PACKET_FILTER = new AndFilter(new IQTypeFilter(IQ.Type.SET), + new PacketExtensionFilter("query", "jabber:iq:privacy")); // Keep the list of instances of this class. - private static Map instances = Collections + private static final Map instances = Collections .synchronizedMap(new WeakHashMap()); private final List listeners = new ArrayList(); - PacketFilter packetFilter = new AndFilter(new IQTypeFilter(IQ.Type.SET), - new PacketExtensionFilter("query", "jabber:iq:privacy")); static { - // Create a new PrivacyListManager on every established connection. In the init() - // method of PrivacyListManager, we'll add a listener that will delete the - // instance when the connection is closed. + // Create a new PrivacyListManager on every established connection. XMPPConnection.addConnectionCreationListener(new ConnectionCreationListener() { public void connectionCreated(XMPPConnection connection) { getInstanceFor(connection); } }); } + /** * Creates a new privacy manager to maintain the communication privacy. Note: no * information is sent to or received from the server until you attempt to @@ -80,14 +86,10 @@ public class PrivacyListManager extends Manager { instances.put(connection, this); connection.addPacketListener(new PacketListener() { + @Override public void processPacket(Packet packet) { - - if (packet == null || packet.getError() != null) { - return; - } - // The packet is correct. Privacy privacy = (Privacy) packet; - + // Notifies the event to the listeners. synchronized (listeners) { for (PrivacyListListener listener : listeners) { @@ -103,23 +105,13 @@ public class PrivacyListManager extends Manager { } } } - - // Send a result package acknowledging the reception of a privacy package. - - // Prepare the IQ packet to send - IQ iq = new IQ() { - public String getChildElementXML() { - return ""; - } - }; - iq.setType(IQ.Type.RESULT); - iq.setFrom(packet.getFrom()); - iq.setPacketID(packet.getPacketID()); - // Send create & join packet. + // Send a result package acknowledging the reception of a privacy package. + IQ iq = IQ.createResultIQ(privacy); connection.sendPacket(iq); } - }, packetFilter); } + }, PACKET_FILTER); + } /** Answer the connection userJID that owns the privacy. * @return the userJID that owns the privacy @@ -139,7 +131,7 @@ public class PrivacyListManager extends Manager { if (plm == null) plm = new PrivacyListManager(connection); return plm; } - + /** * Send the {@link Privacy} packet to the server in order to know some privacy content and then * waits for the answer. @@ -176,20 +168,19 @@ public class PrivacyListManager extends Manager { } /** - * Answer a privacy containing the list structre without {@link PrivacyItem}. + * Answer a privacy containing the list structure without {@link PrivacyItem}. * * @return a Privacy with the list names. * @throws XMPPException if an error occurs. */ private Privacy getPrivacyWithListNames() throws XMPPException { - // The request of the list is an empty privacy message Privacy request = new Privacy(); // Send the package to the server and get the answer return getRequest(request); } - + /** * Answer the active privacy list. * @@ -205,7 +196,7 @@ public class PrivacyListManager extends Manager { privacyAnswer.getDefaultName()); return new PrivacyList(true, isDefaultAndActive, listName, getPrivacyListItems(listName)); } - + /** * Answer the default privacy list. * @@ -221,7 +212,7 @@ public class PrivacyListManager extends Manager { privacyAnswer.getDefaultName()); return new PrivacyList(isDefaultAndActive, true, listName, getPrivacyListItems(listName)); } - + /** * Answer the privacy list items under listName with the allowed and blocked permissions. * @@ -230,7 +221,6 @@ public class PrivacyListManager extends Manager { * @throws XMPPException if an error occurs. */ private List 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()); @@ -240,7 +230,7 @@ public class PrivacyListManager extends Manager { return privacyAnswer.getPrivacyList(listName); } - + /** * Answer the privacy list items under listName with the allowed and blocked permissions. * @@ -249,10 +239,9 @@ public class PrivacyListManager extends Manager { * @throws XMPPException if an error occurs. */ public PrivacyList getPrivacyList(String listName) throws XMPPException { - return new PrivacyList(false, false, listName, getPrivacyListItems(listName)); } - + /** * Answer every privacy list with the allowed and blocked permissions. * @@ -276,7 +265,6 @@ public class PrivacyListManager extends Manager { return lists; } - /** * Set or change the active list to listName. * @@ -284,7 +272,6 @@ public class PrivacyListManager extends Manager { * @exception XMPPException if the request or the answer failed, it raises an exception. */ public void setActiveListName(String listName) throws XMPPException { - // The request of the list is an privacy message with an empty list Privacy request = new Privacy(); request.setActiveName(listName); @@ -299,7 +286,6 @@ public class PrivacyListManager extends Manager { * @throws XMPPException if an error occurs. */ public void declineActiveList() throws XMPPException { - // The request of the list is an privacy message with an empty list Privacy request = new Privacy(); request.setDeclineActiveList(true); @@ -315,7 +301,6 @@ public class PrivacyListManager extends Manager { * @exception XMPPException if the request or the answer failed, it raises an exception. */ public void setDefaultListName(String listName) throws XMPPException { - // The request of the list is an privacy message with an empty list Privacy request = new Privacy(); request.setDefaultName(listName); @@ -323,14 +308,13 @@ public class PrivacyListManager extends Manager { // Send the package to the server setRequest(request); } - + /** * Client declines the use of default lists. * * @throws XMPPException if an error occurs. */ public void declineDefaultList() throws XMPPException { - // The request of the list is an privacy message with an empty list Privacy request = new Privacy(); request.setDeclineDefaultList(true); @@ -338,7 +322,7 @@ public class PrivacyListManager extends Manager { // Send the package to the server setRequest(request); } - + /** * The client has created a new list. It send the new one to the server. * @@ -347,7 +331,6 @@ public class PrivacyListManager extends Manager { * @throws XMPPException if an error occurs. */ public void createPrivacyList(String listName, List privacyItems) throws XMPPException { - this.updatePrivacyList(listName, privacyItems); } @@ -361,7 +344,6 @@ public class PrivacyListManager extends Manager { * @throws XMPPException if an error occurs. */ public void updatePrivacyList(String listName, List privacyItems) throws XMPPException { - // Build the privacy package to add or update the new list Privacy request = new Privacy(); request.setPrivacyList(listName, privacyItems); @@ -369,7 +351,7 @@ public class PrivacyListManager extends Manager { // Send the package to the server setRequest(request); } - + /** * Remove a privacy list. * @@ -377,7 +359,6 @@ public class PrivacyListManager extends Manager { * @throws XMPPException if an error occurs. */ public void deletePrivacyList(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()); @@ -385,7 +366,7 @@ public class PrivacyListManager extends Manager { // Send the package to the server setRequest(request); } - + /** * Adds a packet listener that will be notified of any new update in the user * privacy communication. @@ -398,5 +379,16 @@ public class PrivacyListManager extends Manager { synchronized (listeners) { listeners.add(listener); } - } + } + + /** + * Check if the user's server supports privacy lists. + * + * @return true, if the server supports privacy lists, false otherwise. + * @throws XMPPException + */ + public boolean isSupported() throws XMPPException { + return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature( + connection().getServiceName(), NAMESPACE); + } } diff --git a/extensions/src/main/java/org/jivesoftware/smackx/privacy/packet/Privacy.java b/extensions/src/main/java/org/jivesoftware/smackx/privacy/packet/Privacy.java index 92eabfdce..93df18d6b 100644 --- a/extensions/src/main/java/org/jivesoftware/smackx/privacy/packet/Privacy.java +++ b/extensions/src/main/java/org/jivesoftware/smackx/privacy/packet/Privacy.java @@ -19,6 +19,7 @@ package org.jivesoftware.smackx.privacy.packet; import java.util.*; import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smackx.privacy.PrivacyListManager; /** * A Privacy IQ Packet, is used by the {@link org.jivesoftware.smackx.privacy.PrivacyListManager} @@ -274,8 +275,8 @@ public class Privacy extends IQ { public String getChildElementXML() { StringBuilder buf = new StringBuilder(); - buf.append(""); - + buf.append(""); + // Add the active tag if (this.isDeclineActiveList()) { buf.append(""); diff --git a/extensions/src/main/java/org/jivesoftware/smackx/privacy/packet/PrivacyItem.java b/extensions/src/main/java/org/jivesoftware/smackx/privacy/packet/PrivacyItem.java index 54240c957..2b8d2af55 100644 --- a/extensions/src/main/java/org/jivesoftware/smackx/privacy/packet/PrivacyItem.java +++ b/extensions/src/main/java/org/jivesoftware/smackx/privacy/packet/PrivacyItem.java @@ -30,37 +30,74 @@ package org.jivesoftware.smackx.privacy.packet; * @author Francisco Vives */ public class PrivacyItem { - /** allow is the action associated with the item, it can allow or deny the communication. */ - private boolean allow; - /** order is a non-negative integer that is unique among all items in the list. */ - private int order; - /** rule hold the kind of communication ([jid|group|subscription]) it will allow or block and - * identifier to apply the action. - * If the type is "jid", then the 'value' attribute MUST contain a valid Jabber ID. - * If the type is "group", then the 'value' attribute SHOULD contain the name of a group - * in the user's roster. - * If the type is "subscription", then the 'value' attribute MUST be one of "both", "to", - * "from", or "none". */ - private PrivacyRule rule; + /** + * Value for subscription type rules. + */ + 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"; + + /** allow is the action associated with the item, it can allow or deny the communication. */ + private final boolean allow; + /** order is a non-negative integer that is unique among all items in the list. */ + private final int order; + + /** + * Type defines if the rule is based on JIDs, roster groups or presence subscription types. + * Available values are: [jid|group|subscription] + */ + private final 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. If the type is "group", then the + * 'value' attribute SHOULD contain the name of a group in the user's roster. If the type is + * "subscription", then the 'value' attribute MUST be one of "both", "to", "from", or + * "none". + */ + private final String value; /** blocks incoming IQ stanzas. */ private boolean filterIQ = false; /** filterMessage blocks incoming message stanzas. */ private boolean filterMessage = false; /** blocks incoming presence notifications. */ - private boolean filterPresence_in = false; + private boolean filterPresenceIn = false; /** blocks outgoing presence notifications. */ - private boolean filterPresence_out = false; + private boolean filterPresenceOut = false; + + /** + * Creates a new fall-through privacy item. + * + * This is usually the last item in a privacy list and has no 'type' attribute. + * + * @param allow true if this is an allow item + * @param order the order of this privacy item + */ + public PrivacyItem(boolean allow, int order) { + this(null, null, allow, order); + } /** * Creates a new privacy item. * + * If the type is "jid", then the 'value' attribute MUST contain a valid Jabber ID. + * If the type is "group", then the 'value' attribute SHOULD contain the name of a group + * in the user's roster. + * If the type is "subscription", then the 'value' attribute MUST be one of "both", "to", + * "from", or "none". + * * @param type the type. + * @param value the value of the privacy item + * @param allow true if this is an allow item + * @param order the order of this privacy item */ - public PrivacyItem(String type, boolean allow, int order) { - this.setRule(PrivacyRule.fromString(type)); - this.setAllow(allow); - this.setOrder(order); + public PrivacyItem(Type type, String value, boolean allow, int order) { + this.type = type; + this.value = value; + this.allow = allow; + this.order = order; } /** @@ -73,16 +110,6 @@ public class PrivacyItem { return allow; } - /** - * Sets the action associated with the item, it can allow or deny the communication. - * - * @param allow indicates if the receiver allow or deny the communication. - */ - private void setAllow(boolean allow) { - this.allow = allow; - } - - /** * Returns whether the receiver allow or deny incoming IQ stanzas or not. * @@ -92,7 +119,6 @@ public class PrivacyItem { return filterIQ; } - /** * Sets whether the receiver allows or denies incoming IQ stanzas or not. * @@ -102,7 +128,6 @@ public class PrivacyItem { this.filterIQ = filterIQ; } - /** * Returns whether the receiver allows or denies incoming messages or not. * @@ -112,7 +137,6 @@ public class PrivacyItem { return filterMessage; } - /** * Sets wheather the receiver allows or denies incoming messages or not. * @@ -122,47 +146,42 @@ public class PrivacyItem { this.filterMessage = filterMessage; } - /** * Returns whether the receiver allows or denies incoming presence or not. * * @return the iq filtering incoming presence status. */ - public boolean isFilterPresence_in() { - return filterPresence_in; + public boolean isFilterPresenceIn() { + return filterPresenceIn; } - /** * Sets whether the receiver allows or denies incoming presence or not. * - * @param filterPresence_in indicates if the receiver allows or denies filtering incoming presence. + * @param filterPresenceIn indicates if the receiver allows or denies filtering incoming presence. */ - public void setFilterPresence_in(boolean filterPresence_in) { - this.filterPresence_in = filterPresence_in; + public void setFilterPresenceIn(boolean filterPresenceIn) { + this.filterPresenceIn = filterPresenceIn; } - /** * Returns whether the receiver allows or denies incoming presence or not. * * @return the iq filtering incoming presence status. */ - public boolean isFilterPresence_out() { - return filterPresence_out; + public boolean isFilterPresenceOut() { + return filterPresenceOut; } - /** * Sets whether the receiver allows or denies outgoing presence or not. * - * @param filterPresence_out indicates if the receiver allows or denies filtering outgoing presence + * @param filterPresenceOut indicates if the receiver allows or denies filtering outgoing presence */ - public void setFilterPresence_out(boolean filterPresence_out) { - this.filterPresence_out = filterPresence_out; + public void setFilterPresenceOut(boolean filterPresenceOut) { + this.filterPresenceOut = filterPresenceOut; } - /** * Returns the order where the receiver is processed. List items are processed in * ascending order. @@ -176,36 +195,6 @@ public class PrivacyItem { return order; } - - /** - * Sets the order where the receiver is processed. - * - * The order MUST be filled and its value MUST be a non-negative integer - * that is unique among all items in the list. - * - * @param order indicates the order in the list. - */ - public void setOrder(int order) { - this.order = order; - } - - /** - * Sets 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 "group", then the 'value' attribute SHOULD contain the name of a group - * in the user's roster. - * If the type is "subscription", then the 'value' attribute MUST be one of "both", "to", - * "from", or "none". - * - * @param value is the identifier to apply the action. - */ - public void setValue(String value) { - if (!(this.getRule() == null && value == null)) { - this.getRule().setValue(value); - } - } - /** * Returns the type hold the kind of communication it will allow or block. * It MUST be filled with one of these values: jid, group or subscription. @@ -213,11 +202,7 @@ public class PrivacyItem { * @return the type of communication it represent. */ public Type getType() { - if (this.getRule() == null) { - return null; - } else { - return this.getRule().getType(); - } + return type; } /** @@ -232,35 +217,22 @@ public class PrivacyItem { * @return the identifier to apply the action. */ public String getValue() { - if (this.getRule() == null) { - return null; - } else { - return this.getRule().getValue(); - } + return value; } - /** * Returns whether the receiver allows or denies every kind of communication. * - * When filterIQ, filterMessage, filterPresence_in and filterPresence_out are not set + * When filterIQ, filterMessage, filterPresenceIn and filterPresenceOut are not set * the receiver will block all communications. * * @return the all communications status. */ public boolean isFilterEverything() { - return !(this.isFilterIQ() || this.isFilterMessage() || this.isFilterPresence_in() - || this.isFilterPresence_out()); + return !(this.isFilterIQ() || this.isFilterMessage() || this.isFilterPresenceIn() + || this.isFilterPresenceOut()); } - - private PrivacyRule getRule() { - return rule; - } - - private void setRule(PrivacyRule rule) { - this.rule = rule; - } /** * Answer an xml representation of the receiver according to the RFC 3921. * @@ -291,10 +263,10 @@ public class PrivacyItem { if (this.isFilterMessage()) { buf.append(""); } - if (this.isFilterPresence_in()) { + if (this.isFilterPresenceIn()) { buf.append(""); } - if (this.isFilterPresence_out()) { + if (this.isFilterPresenceOut()) { buf.append(""); } buf.append(""); @@ -302,144 +274,6 @@ public class PrivacyItem { return buf.toString(); } - - /** - * Privacy Rule represents the kind of action to apply. - * It holds the kind of communication ([jid|group|subscription]) it will allow or block and - * identifier to apply the action. - */ - - public static class PrivacyRule { - /** - * Type defines if the rule is based on JIDs, roster groups or presence subscription types. - * Available values are: [jid|group|subscription] - */ - 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. - * If the type is "group", then the 'value' attribute SHOULD contain the name of a group - * in the user's roster. - * If the type is "subscription", then the 'value' attribute MUST be one of "both", "to", - * "from", or "none". - */ - private String value; - - /** - * If the type is "subscription", then the 'value' attribute MUST be one of "both", - * "to", "from", or "none" - */ - 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"; - - /** - * Returns the type constant associated with the String value. - */ - protected static PrivacyRule fromString(String value) { - if (value == null) { - return null; - } - PrivacyRule rule = new PrivacyRule(); - rule.setType(Type.valueOf(value.toLowerCase())); - return rule; - } - - /** - * Returns the type hold the kind of communication it will allow or block. - * It MUST be filled with one of these values: jid, group or subscription. - * - * @return the type of communication it represent. - */ - public Type getType() { - return type; - } - - /** - * Sets the action associated with the item, it can allow or deny the communication. - * - * @param type indicates if the receiver allows or denies the communication. - */ - private void setType(Type type) { - this.type = type; - } - - /** - * Returns 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 "group", then the 'value' attribute SHOULD contain the name of a group - * in the user's roster. - * If the type is "subscription", then the 'value' attribute MUST be one of "both", "to", - * "from", or "none". - * - * @return the identifier to apply the action. - */ - public String getValue() { - return value; - } - - /** - * Sets 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 "group", then the 'value' attribute SHOULD contain the name of a group - * in the user's roster. - * If the type is "subscription", then the 'value' attribute MUST be one of "both", "to", - * "from", or "none". - * - * @param value is the identifier to apply the action. - */ - protected void setValue(String value) { - if (this.isSuscription()) { - setSuscriptionValue(value); - } else { - this.value = value; - } - } - - /** - * Sets the element identifier to apply the action. - * - * The 'value' attribute MUST be one of "both", "to", "from", or "none". - * - * @param value is the identifier to apply the action. - */ - private void setSuscriptionValue(String value) { - String setValue; - if (value == null) { - // Do nothing - } - if (SUBSCRIPTION_BOTH.equalsIgnoreCase(value)) { - setValue = SUBSCRIPTION_BOTH; - } - else if (SUBSCRIPTION_TO.equalsIgnoreCase(value)) { - setValue = SUBSCRIPTION_TO; - } - else if (SUBSCRIPTION_FROM.equalsIgnoreCase(value)) { - setValue = SUBSCRIPTION_FROM; - } - else if (SUBSCRIPTION_NONE.equalsIgnoreCase(value)) { - setValue = SUBSCRIPTION_NONE; - } - // Default to available. - else { - setValue = null; - } - this.value = setValue; - } - - /** - * Returns if the receiver represents a subscription rule. - * - * @return if the receiver represents a subscription rule. - */ - public boolean isSuscription () { - return this.getType() == Type.subscription; - } - } - /** * Type defines if the rule is based on JIDs, roster groups or presence subscription types. */ @@ -453,8 +287,8 @@ public class PrivacyItem { */ jid, /** - * JID being analyzed should belong to a contact present in the owner's roster with - * the specified subscription status. + * JID being analyzed should belong to a contact present in the owner's roster with the + * specified subscription status. */ subscription } diff --git a/extensions/src/main/java/org/jivesoftware/smackx/privacy/provider/PrivacyProvider.java b/extensions/src/main/java/org/jivesoftware/smackx/privacy/provider/PrivacyProvider.java index 39913f9c7..b6b3130a1 100644 --- a/extensions/src/main/java/org/jivesoftware/smackx/privacy/provider/PrivacyProvider.java +++ b/extensions/src/main/java/org/jivesoftware/smackx/privacy/provider/PrivacyProvider.java @@ -29,7 +29,7 @@ import java.util.ArrayList; * The PrivacyProvider parses {@link Privacy} packets. {@link Privacy} * Parses the query sub-document and creates an instance of {@link Privacy}. * For each item in the list element, it creates an instance - * of {@link PrivacyItem} and {@link org.jivesoftware.smackx.privacy.packet.PrivacyItem.PrivacyRule}. + * of {@link PrivacyItem}. * * @author Francisco Vives */ @@ -106,7 +106,7 @@ public class PrivacyProvider implements IQProvider { String actionValue = parser.getAttributeValue("", "action"); String orderValue = parser.getAttributeValue("", "order"); String type = parser.getAttributeValue("", "type"); - + /* * According the action value it sets the allow status. The fall-through action is assumed * to be "allow" @@ -120,32 +120,39 @@ public class PrivacyProvider implements IQProvider { // Set the order number int order = Integer.parseInt(orderValue); - // Create the privacy item - PrivacyItem item = new PrivacyItem(type, allow, order); - item.setValue(parser.getAttributeValue("", "value")); + PrivacyItem item; + if (type != null) { + // If the type is not null, then we are dealing with a standard privacy item + String value = parser.getAttributeValue("", "value"); + item = new PrivacyItem(PrivacyItem.Type.valueOf(type), value, allow, order); - while (!done) { - int eventType = parser.next(); - if (eventType == XmlPullParser.START_TAG) { - if (parser.getName().equals("iq")) { - item.setFilterIQ(true); + while (!done) { + int eventType = parser.next(); + if (eventType == XmlPullParser.START_TAG) { + if (parser.getName().equals("iq")) { + item.setFilterIQ(true); + } + if (parser.getName().equals("message")) { + item.setFilterMessage(true); + } + if (parser.getName().equals("presence-in")) { + item.setFilterPresenceIn(true); + } + if (parser.getName().equals("presence-out")) { + item.setFilterPresenceOut(true); + } } - if (parser.getName().equals("message")) { - item.setFilterMessage(true); - } - if (parser.getName().equals("presence-in")) { - item.setFilterPresence_in(true); - } - if (parser.getName().equals("presence-out")) { - item.setFilterPresence_out(true); - } - } - else if (eventType == XmlPullParser.END_TAG) { - if (parser.getName().equals("item")) { - done = true; + else if (eventType == XmlPullParser.END_TAG) { + if (parser.getName().equals("item")) { + done = true; + } } } } + else { + // If the type is null, then we are dealing with the fall-through privacy item. + item = new PrivacyItem(allow, order); + } return item; } } diff --git a/extensions/src/test/java/org/jivesoftware/smackx/privacy/provider/PrivacyProviderTest.java b/extensions/src/test/java/org/jivesoftware/smackx/privacy/provider/PrivacyProviderTest.java new file mode 100644 index 000000000..20301532a --- /dev/null +++ b/extensions/src/test/java/org/jivesoftware/smackx/privacy/provider/PrivacyProviderTest.java @@ -0,0 +1,69 @@ +/** + * + * Copyright 2012 Florian Schmaus + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.privacy.provider; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.List; + +import org.jivesoftware.smack.DummyConnection; +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smack.test.util.TestUtils; +import org.jivesoftware.smack.util.PacketParserUtils; +import org.jivesoftware.smackx.InitExtensions; +import org.jivesoftware.smackx.privacy.packet.Privacy; +import org.jivesoftware.smackx.privacy.packet.PrivacyItem; +import org.junit.Test; + +public class PrivacyProviderTest extends InitExtensions { + + @Test + public void parsePrivacyList() throws Exception { + DummyConnection connection = new DummyConnection(); + + // @formatter:off + final String xmlPrivacyList = + "" + + "" + + "" + + "" + + "" + + "" + + "" + + ""; + // @formatter:on + IQ iqPrivacyList = PacketParserUtils.parseIQ(TestUtils.getIQParser(xmlPrivacyList), connection); + assertTrue(iqPrivacyList instanceof Privacy); + + Privacy privacyList = (Privacy) iqPrivacyList; + List pl = privacyList.getPrivacyList("public"); + + PrivacyItem first = pl.get(0); + assertEquals(PrivacyItem.Type.jid, first.getType()); + assertEquals("tybalt@example.com", first.getValue()); + assertEquals(false, first.isAllow()); + assertEquals(1, first.getOrder()); + + PrivacyItem second = pl.get(1); + assertEquals(true, second.isAllow()); + assertEquals(2, second.getOrder()); + } +}