Privacy.java

  1. /**
  2.  *
  3.  * Copyright 2006-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.privacy.packet;

  18. import java.util.HashMap;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.Set;

  23. import org.jivesoftware.smack.packet.IQ;

  24. /**
  25.  * A Privacy IQ Packet, is used by the {@link org.jivesoftware.smackx.privacy.PrivacyListManager}
  26.  * and {@link org.jivesoftware.smackx.privacy.provider.PrivacyProvider} to allow and block
  27.  * communications from other users. It contains the appropriate structure to suit
  28.  * user-defined privacy lists. Different configured Privacy packages are used in the
  29.  * server & manager communication in order to:
  30.  * <ul>
  31.  * <li>Retrieving one's privacy lists.
  32.  * <li>Adding, removing, and editing one's privacy lists.
  33.  * <li>Setting, changing, or declining active lists.
  34.  * <li>Setting, changing, or declining the default list (i.e., the list that is active by default).
  35.  * </ul>
  36.  * Privacy Items can handle different kind of blocking communications based on JID, group,
  37.  * subscription type or globally {@link PrivacyItem}
  38.  *
  39.  * @author Francisco Vives
  40.  */
  41. public class Privacy extends IQ {
  42.     public static final String ELEMENT = QUERY_ELEMENT;
  43.     public static final String NAMESPACE = "jabber:iq:privacy";

  44.     /** declineActiveList is true when the user declines the use of the active list **/
  45.     private boolean declineActiveList=false;
  46.     /** activeName is the name associated with the active list set for the session **/
  47.     private String activeName;
  48.     /** declineDefaultList is true when the user declines the use of the default list **/
  49.     private boolean declineDefaultList=false;
  50.     /** defaultName is the name of the default list that applies to the user as a whole **/
  51.     private String defaultName;
  52.     /** itemLists holds the set of privacy items classified in lists. It is a map where the
  53.      * key is the name of the list and the value a collection with privacy items. **/
  54.     private Map<String, List<PrivacyItem>> itemLists = new HashMap<String, List<PrivacyItem>>();

  55.     public Privacy() {
  56.         super(ELEMENT, NAMESPACE);
  57.     }

  58.     /**
  59.      * Set or update a privacy list with privacy items.
  60.      *
  61.      * @param listName the name of the new privacy list.
  62.      * @param listItem the {@link PrivacyItem} that rules the list.
  63.      * @return the privacy List.
  64.      */
  65.     public List<PrivacyItem> setPrivacyList(String listName, List<PrivacyItem> listItem) {
  66.         // Add new list to the itemLists
  67.         this.getItemLists().put(listName, listItem);
  68.         return listItem;
  69.     }

  70.     /**
  71.      * Set the active list based on the default list.
  72.      *
  73.      * @return the active List.
  74.      */
  75.     public List<PrivacyItem> setActivePrivacyList() {
  76.         this.setActiveName(this.getDefaultName());
  77.         return this.getItemLists().get(this.getActiveName());
  78.     }

  79.     /**
  80.      * Deletes an existing privacy list. If the privacy list being deleted was the default list
  81.      * then the user will end up with no default list. Therefore, the user will have to set a new
  82.      * default list.
  83.      *
  84.      * @param listName the name of the list being deleted.
  85.      */
  86.     public void deletePrivacyList(String listName) {
  87.         // Remove the list from the cache
  88.         this.getItemLists().remove(listName);

  89.         // Check if deleted list was the default list
  90.         if (this.getDefaultName() != null && listName.equals(this.getDefaultName())) {
  91.             this.setDefaultName(null);
  92.         }
  93.     }

  94.     /**
  95.      * Returns the active privacy list or <tt>null</tt> if none was found.
  96.      *
  97.      * @return list with {@link PrivacyItem} or <tt>null</tt> if none was found.
  98.      */
  99.     public List<PrivacyItem> getActivePrivacyList() {
  100.         // Check if we have the default list
  101.         if (this.getActiveName() == null) {
  102.             return null;
  103.         } else {
  104.             return this.getItemLists().get(this.getActiveName());
  105.         }
  106.     }

  107.     /**
  108.      * Returns the default privacy list or <tt>null</tt> if none was found.
  109.      *
  110.      * @return list with {@link PrivacyItem} or <tt>null</tt> if none was found.
  111.      */
  112.     public List<PrivacyItem> getDefaultPrivacyList() {
  113.         // Check if we have the default list
  114.         if (this.getDefaultName() == null) {
  115.             return null;
  116.         } else {
  117.             return this.getItemLists().get(this.getDefaultName());
  118.         }
  119.     }

  120.     /**
  121.      * Returns a specific privacy list.
  122.      *
  123.      * @param listName the name of the list to get.
  124.      * @return a List with {@link PrivacyItem}
  125.      */
  126.     public List<PrivacyItem> getPrivacyList(String listName) {
  127.         return this.getItemLists().get(listName);
  128.     }

  129.     /**
  130.      * Returns the privacy item in the specified order.
  131.      *
  132.      * @param listName the name of the privacy list.
  133.      * @param order the order of the element.
  134.      * @return a List with {@link PrivacyItem}
  135.      */
  136.     public PrivacyItem getItem(String listName, int order) {
  137.         Iterator<PrivacyItem> values = getPrivacyList(listName).iterator();
  138.         PrivacyItem itemFound = null;
  139.         while (itemFound == null && values.hasNext()) {
  140.             PrivacyItem element = values.next();
  141.             if (element.getOrder() == order) {
  142.                 itemFound = element;
  143.             }
  144.         }
  145.         return itemFound;
  146.     }

  147.     /**
  148.      * Sets a given privacy list as the new user default list.
  149.      *
  150.      * @param newDefault the new default privacy list.
  151.      * @return if the default list was changed.
  152.      */
  153.     public boolean changeDefaultList(String newDefault) {
  154.         if (this.getItemLists().containsKey(newDefault)) {
  155.            this.setDefaultName(newDefault);
  156.            return true;
  157.         } else {
  158.             return false;
  159.         }
  160.     }

  161.     /**
  162.      * Remove the list.
  163.      *
  164.      * @param listName name of the list to remove.
  165.      */
  166.      public void deleteList(String listName) {
  167.          this.getItemLists().remove(listName);
  168.      }

  169.     /**
  170.      * Returns the name associated with the active list set for the session. Communications
  171.      * will be verified against the active list.
  172.      *
  173.      * @return the name of the active list.
  174.      */
  175.     public String getActiveName() {
  176.         return activeName;
  177.     }

  178.     /**
  179.      * Sets the name associated with the active list set for the session. Communications
  180.      * will be verified against the active list.
  181.      *
  182.      * @param activeName is the name of the active list.
  183.      */
  184.     public void setActiveName(String activeName) {
  185.         this.activeName = activeName;
  186.     }

  187.     /**
  188.      * Returns the name of the default list that applies to the user as a whole. Default list is
  189.      * processed if there is no active list set for the target session/resource to which a stanza
  190.      * is addressed, or if there are no current sessions for the user.
  191.      *
  192.      * @return the name of the default list.
  193.      */
  194.     public String getDefaultName() {
  195.         return defaultName;
  196.     }

  197.     /**
  198.      * Sets the name of the default list that applies to the user as a whole. Default list is
  199.      * processed if there is no active list set for the target session/resource to which a stanza
  200.      * is addressed, or if there are no current sessions for the user.
  201.      *
  202.      * If there is no default list set, then all Privacy Items are processed.
  203.      *
  204.      * @param defaultName is the name of the default list.
  205.      */
  206.     public void setDefaultName(String defaultName) {
  207.         this.defaultName = defaultName;
  208.     }

  209.     /**
  210.      * Returns the collection of privacy list that the user holds. A Privacy List contains a set of
  211.      * rules that define if communication with the list owner is allowed or denied.
  212.      * Users may have zero, one or more privacy items.
  213.      *
  214.      * @return a map where the key is the name of the list and the value the
  215.      * collection of privacy items.
  216.      */
  217.     public Map<String, List<PrivacyItem>> getItemLists() {
  218.         return itemLists;
  219.     }

  220.     /**
  221.      * Returns whether the receiver allows or declines the use of an active list.
  222.      *
  223.      * @return the decline status of the list.
  224.      */
  225.     public boolean isDeclineActiveList() {
  226.         return declineActiveList;
  227.     }

  228.     /**
  229.      * Sets whether the receiver allows or declines the use of an active list.
  230.      *
  231.      * @param declineActiveList indicates if the receiver declines the use of an active list.
  232.      */
  233.     public void setDeclineActiveList(boolean declineActiveList) {
  234.         this.declineActiveList = declineActiveList;
  235.     }

  236.     /**
  237.      * Returns whether the receiver allows or declines the use of a default list.
  238.      *
  239.      * @return the decline status of the list.
  240.      */
  241.     public boolean isDeclineDefaultList() {
  242.         return declineDefaultList;
  243.     }

  244.     /**
  245.      * Sets whether the receiver allows or declines the use of a default list.
  246.      *
  247.      * @param declineDefaultList indicates if the receiver declines the use of a default list.
  248.      */
  249.     public void setDeclineDefaultList(boolean declineDefaultList) {
  250.         this.declineDefaultList = declineDefaultList;
  251.     }

  252.     /**
  253.      * Returns all the list names the user has defined to group restrictions.
  254.      *
  255.      * @return a Set with Strings containing every list names.
  256.      */
  257.     public Set<String> getPrivacyListNames() {
  258.         return this.itemLists.keySet();
  259.     }

  260.     @Override
  261.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
  262.         buf.rightAngleBracket();

  263.         // Add the active tag
  264.         if (this.isDeclineActiveList()) {
  265.             buf.append("<active/>");
  266.         } else {
  267.             if (this.getActiveName() != null) {
  268.                 buf.append("<active name=\"").escape(getActiveName()).append("\"/>");
  269.             }
  270.         }
  271.         // Add the default tag
  272.         if (this.isDeclineDefaultList()) {
  273.             buf.append("<default/>");
  274.         } else {
  275.             if (this.getDefaultName() != null) {
  276.                 buf.append("<default name=\"").escape(getDefaultName()).append("\"/>");
  277.             }
  278.         }
  279.        
  280.         // Add the list with their privacy items
  281.         for (Map.Entry<String, List<PrivacyItem>> entry : this.getItemLists().entrySet()) {
  282.           String listName = entry.getKey();
  283.           List<PrivacyItem> items = entry.getValue();
  284.             // Begin the list tag
  285.             if (items.isEmpty()) {
  286.                 buf.append("<list name=\"").escape(listName).append("\"/>");
  287.             } else {
  288.                 buf.append("<list name=\"").escape(listName).append("\">");
  289.             }
  290.             for (PrivacyItem item : items) {
  291.                 // Append the item xml representation
  292.                 buf.append(item.toXML());
  293.             }
  294.             // Close the list tag
  295.             if (!items.isEmpty()) {
  296.                 buf.append("</list>");
  297.             }
  298.         }

  299.         return buf;
  300.     }
  301.    
  302. }