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.datatypes.UInt32;
  24. import org.jivesoftware.smack.packet.IQ;

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

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

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

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

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

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

  92.         // Check if deleted list was the default list
  93.         if (this.getDefaultName() != null && listName.equals(this.getDefaultName())) {
  94.         // CHECKSTYLE:OFF
  95.             this.setDefaultName(null);
  96.         // CHECKSTYLE:ON
  97.         }
  98.     }

  99.     /**
  100.      * Returns the active privacy list or <code>null</code> if none was found.
  101.      *
  102.      * @return list with {@link PrivacyItem} or <code>null</code> if none was found.
  103.      */
  104.     public List<PrivacyItem> getActivePrivacyList() {
  105.         // Check if we have the default list
  106.         // CHECKSTYLE:OFF
  107.         if (this.getActiveName() == null) {
  108.             return null;
  109.         } else {
  110.             return this.getItemLists().get(this.getActiveName());
  111.         }
  112.         // CHECKSTYLE:ON
  113.     }

  114.     /**
  115.      * Returns the default privacy list or <code>null</code> if none was found.
  116.      *
  117.      * @return list with {@link PrivacyItem} or <code>null</code> if none was found.
  118.      */
  119.     public List<PrivacyItem> getDefaultPrivacyList() {
  120.         // Check if we have the default list
  121.         // CHECKSTYLE:OFF
  122.         if (this.getDefaultName() == null) {
  123.             return null;
  124.         } else {
  125.             return this.getItemLists().get(this.getDefaultName());
  126.         }
  127.         // CHECKSTYLE:ON
  128.     }

  129.     /**
  130.      * Returns a specific privacy list.
  131.      *
  132.      * @param listName the name of the list to get.
  133.      * @return a List with {@link PrivacyItem}
  134.      */
  135.     public List<PrivacyItem> getPrivacyList(String listName) {
  136.         return this.getItemLists().get(listName);
  137.     }

  138.     public PrivacyItem getItem(String listName, int order) {
  139.         return getItem(listName, UInt32.from(order));
  140.     }

  141.     /**
  142.      * Returns the privacy item in the specified order.
  143.      *
  144.      * @param listName the name of the privacy list.
  145.      * @param order the order of the element.
  146.      * @return a List with {@link PrivacyItem}
  147.      */
  148.     public PrivacyItem getItem(String listName, UInt32 order) {
  149.         // CHECKSTYLE:OFF
  150.         Iterator<PrivacyItem> values = getPrivacyList(listName).iterator();
  151.         PrivacyItem itemFound = null;
  152.         while (itemFound == null && values.hasNext()) {
  153.             PrivacyItem element = values.next();
  154.             if (element.getOrder().equals(order)) {
  155.                 itemFound = element;
  156.             }
  157.         }
  158.         return itemFound;
  159.         // CHECKSTYLE:ON
  160.     }

  161.     /**
  162.      * Sets a given privacy list as the new user default list.
  163.      *
  164.      * @param newDefault the new default privacy list.
  165.      * @return if the default list was changed.
  166.      */
  167.     public boolean changeDefaultList(String newDefault) {
  168.         if (this.getItemLists().containsKey(newDefault)) {
  169.            this.setDefaultName(newDefault);
  170.            return true;
  171.         } else {
  172.             // CHECKSTYLE:OFF
  173.             return false;
  174.             // CHECKSTYLE:ON
  175.         }
  176.     }

  177.     /**
  178.      * Remove the list.
  179.      *
  180.      * @param listName name of the list to remove.
  181.      */
  182.      public void deleteList(String listName) {
  183.      // CHECKSTYLE:OFF
  184.          this.getItemLists().remove(listName);
  185.      // CHECKSTYLE:ON
  186.      }

  187.     /**
  188.      * Returns the name associated with the active list set for the session. Communications
  189.      * will be verified against the active list.
  190.      *
  191.      * @return the name of the active list.
  192.      */
  193.     // CHECKSTYLE:OFF
  194.     public String getActiveName() {
  195.         return activeName;
  196.     }
  197.     // CHECKSTYLE:ON

  198.     /**
  199.      * Sets the name associated with the active list set for the session. Communications
  200.      * will be verified against the active list.
  201.      *
  202.      * @param activeName is the name of the active list.
  203.      */
  204.     // CHECKSTYLE:OFF
  205.     public void setActiveName(String activeName) {
  206.         this.activeName = activeName;
  207.     }
  208.     // CHECKSTYLE:ON

  209.     /**
  210.      * Returns the name of the default list that applies to the user as a whole. Default list is
  211.      * processed if there is no active list set for the target session/resource to which a stanza
  212.      * is addressed, or if there are no current sessions for the user.
  213.      *
  214.      * @return the name of the default list.
  215.      */
  216.     // CHECKSTYLE:OFF
  217.     public String getDefaultName() {
  218.         return defaultName;
  219.     }
  220.     // CHECKSTYLE:ON

  221.     /**
  222.      * Sets the name of the default list that applies to the user as a whole. Default list is
  223.      * processed if there is no active list set for the target session/resource to which a stanza
  224.      * is addressed, or if there are no current sessions for the user.
  225.      *
  226.      * If there is no default list set, then all Privacy Items are processed.
  227.      *
  228.      * @param defaultName is the name of the default list.
  229.      */
  230.     // CHECKSTYLE:OFF
  231.     public void setDefaultName(String defaultName) {
  232.         this.defaultName = defaultName;
  233.     }
  234.     // CHECKSTYLE:ON

  235.     /**
  236.      * Returns the collection of privacy list that the user holds. A Privacy List contains a set of
  237.      * rules that define if communication with the list owner is allowed or denied.
  238.      * Users may have zero, one or more privacy items.
  239.      *
  240.      * @return a map where the key is the name of the list and the value the
  241.      * collection of privacy items.
  242.      */
  243.     // CHECKSTYLE:OFF
  244.     public Map<String, List<PrivacyItem>> getItemLists() {
  245.         return itemLists;
  246.     }
  247.     // CHECKSTYLE:ON

  248.     /**
  249.      * Returns whether the receiver allows or declines the use of an active list.
  250.      *
  251.      * @return the decline status of the list.
  252.      */
  253.     // CHECKSTYLE:OFF
  254.     public boolean isDeclineActiveList() {
  255.         return declineActiveList;
  256.     }
  257.     // CHECKSTYLE:ON

  258.     /**
  259.      * Sets whether the receiver allows or declines the use of an active list.
  260.      *
  261.      * @param declineActiveList indicates if the receiver declines the use of an active list.
  262.      */
  263.     // CHECKSTYLE:OFF
  264.     public void setDeclineActiveList(boolean declineActiveList) {
  265.         this.declineActiveList = declineActiveList;
  266.     }
  267.     // CHECKSTYLE:ON

  268.     /**
  269.      * Returns whether the receiver allows or declines the use of a default list.
  270.      *
  271.      * @return the decline status of the list.
  272.      */
  273.     // CHECKSTYLE:OFF
  274.     public boolean isDeclineDefaultList() {
  275.         return declineDefaultList;
  276.     }
  277.     // CHECKSTYLE:ON

  278.     /**
  279.      * Sets whether the receiver allows or declines the use of a default list.
  280.      *
  281.      * @param declineDefaultList indicates if the receiver declines the use of a default list.
  282.      */
  283.     // CHECKSTYLE:OFF
  284.     public void setDeclineDefaultList(boolean declineDefaultList) {
  285.         this.declineDefaultList = declineDefaultList;
  286.     }

  287.     /**
  288.      * Returns all the list names the user has defined to group restrictions.
  289.      *
  290.      * @return a Set with Strings containing every list names.
  291.      */
  292.     public Set<String> getPrivacyListNames() {
  293.         return this.itemLists.keySet();
  294.     }
  295.     // CHECKSTYLE:ON

  296.     @Override
  297.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
  298.         buf.rightAngleBracket();
  299.         // CHECKSTYLE:OFF

  300.         // Add the active tag
  301.         if (this.isDeclineActiveList()) {
  302.             buf.append("<active/>");
  303.         } else {
  304.             if (this.getActiveName() != null) {
  305.                 buf.append("<active name=\"").escape(getActiveName()).append("\"/>");
  306.             }
  307.         }
  308.         // Add the default tag
  309.         if (this.isDeclineDefaultList()) {
  310.             buf.append("<default/>");
  311.         } else {
  312.             if (this.getDefaultName() != null) {
  313.                 buf.append("<default name=\"").escape(getDefaultName()).append("\"/>");
  314.             }
  315.         }

  316.         // Add the list with their privacy items
  317.         for (Map.Entry<String, List<PrivacyItem>> entry : this.getItemLists().entrySet()) {
  318.           String listName = entry.getKey();
  319.           List<PrivacyItem> items = entry.getValue();
  320.             // Begin the list tag
  321.             if (items.isEmpty()) {
  322.                 buf.append("<list name=\"").escape(listName).append("\"/>");
  323.             } else {
  324.                 buf.append("<list name=\"").escape(listName).append("\">");
  325.             }
  326.             for (PrivacyItem item : items) {
  327.                 // Append the item xml representation
  328.                 buf.append(item.toXML());
  329.             }
  330.             // Close the list tag
  331.             if (!items.isEmpty()) {
  332.                 buf.append("</list>");
  333.             }
  334.         }
  335.     // CHECKSTYLE:ON
  336.         return buf;
  337.     }

  338. }