OfflineMessageRequest.java

  1. /**
  2.  *
  3.  * Copyright 2003-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.offline.packet;

  18. import java.io.IOException;
  19. import java.util.ArrayList;
  20. import java.util.Collections;
  21. import java.util.List;

  22. import org.jivesoftware.smack.packet.IQ;
  23. import org.jivesoftware.smack.packet.IqData;
  24. import org.jivesoftware.smack.packet.XmlEnvironment;
  25. import org.jivesoftware.smack.provider.IqProvider;
  26. import org.jivesoftware.smack.xml.XmlPullParser;
  27. import org.jivesoftware.smack.xml.XmlPullParserException;

  28. import org.jivesoftware.smackx.offline.OfflineMessageManager;

  29. /**
  30.  * Represents a request to get some or all the offline messages of a user. This class can also
  31.  * be used for deleting some or all the offline messages of a user.
  32.  *
  33.  * @author Gaston Dombiak
  34.  */
  35. public class OfflineMessageRequest extends IQ {

  36.     public static final String ELEMENT = "offline";
  37.     public static final String NAMESPACE = OfflineMessageManager.NAMESPACE;

  38.     private final List<Item> items = new ArrayList<>();
  39.     private boolean purge = false;
  40.     private boolean fetch = false;

  41.     public OfflineMessageRequest() {
  42.         super(ELEMENT, NAMESPACE);
  43.     }

  44.     /**
  45.      * Returns a List of item children that holds information about offline messages to
  46.      * view or delete.
  47.      *
  48.      * @return a List of item children that holds information about offline messages to
  49.      *         view or delete.
  50.      */
  51.     public List<Item> getItems() {
  52.         synchronized (items) {
  53.             return Collections.unmodifiableList(new ArrayList<>(items));
  54.         }
  55.     }

  56.     /**
  57.      * Adds an item child that holds information about offline messages to view or delete.
  58.      *
  59.      * @param item the item child that holds information about offline messages to view or delete.
  60.      */
  61.     public void addItem(Item item) {
  62.         synchronized (items) {
  63.             items.add(item);
  64.         }
  65.     }

  66.     /**
  67.      * Returns true if all the offline messages of the user should be deleted.
  68.      *
  69.      * @return true if all the offline messages of the user should be deleted.
  70.      */
  71.     public boolean isPurge() {
  72.         return purge;
  73.     }

  74.     /**
  75.      * Sets if all the offline messages of the user should be deleted.
  76.      *
  77.      * @param purge true if all the offline messages of the user should be deleted.
  78.      */
  79.     public void setPurge(boolean purge) {
  80.         this.purge = purge;
  81.     }

  82.     /**
  83.      * Returns true if all the offline messages of the user should be retrieved.
  84.      *
  85.      * @return true if all the offline messages of the user should be retrieved.
  86.      */
  87.     public boolean isFetch() {
  88.         return fetch;
  89.     }

  90.     /**
  91.      * Sets if all the offline messages of the user should be retrieved.
  92.      *
  93.      * @param fetch true if all the offline messages of the user should be retrieved.
  94.      */
  95.     public void setFetch(boolean fetch) {
  96.         this.fetch = fetch;
  97.     }

  98.     @Override
  99.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
  100.         buf.rightAngleBracket();

  101.         synchronized (items) {
  102.             for (Item item : items) {
  103.                 buf.append(item.toXML());
  104.             }
  105.         }
  106.         if (purge) {
  107.             buf.append("<purge/>");
  108.         }
  109.         if (fetch) {
  110.             buf.append("<fetch/>");
  111.         }

  112.         return buf;
  113.     }

  114.     /**
  115.      * Item child that holds information about offline messages to view or delete.
  116.      *
  117.      * @author Gaston Dombiak
  118.      */
  119.     public static class Item {
  120.         private String action;
  121.         private String jid;
  122.         private String node;

  123.         /**
  124.          * Creates a new item child.
  125.          *
  126.          * @param node the actor's affiliation to the room
  127.          */
  128.         public Item(String node) {
  129.             this.node = node;
  130.         }

  131.         public String getNode() {
  132.             return node;
  133.         }

  134.         /**
  135.          * Returns "view" or "remove" that indicate if the server should return the specified
  136.          * offline message or delete it.
  137.          *
  138.          * @return "view" or "remove" that indicate if the server should return the specified
  139.          *         offline message or delete it.
  140.          */
  141.         public String getAction() {
  142.             return action;
  143.         }

  144.         /**
  145.          * Sets if the server should return the specified offline message or delete it. Possible
  146.          * values are "view" or "remove".
  147.          *
  148.          * @param action if the server should return the specified offline message or delete it.
  149.          */
  150.         public void setAction(String action) {
  151.             this.action = action;
  152.         }

  153.         public String getJid() {
  154.             return jid;
  155.         }

  156.         public void setJid(String jid) {
  157.             this.jid = jid;
  158.         }

  159.         public String toXML() {
  160.             StringBuilder buf = new StringBuilder();
  161.             buf.append("<item");
  162.             if (getAction() != null) {
  163.                 buf.append(" action=\"").append(getAction()).append('"');
  164.             }
  165.             if (getJid() != null) {
  166.                 buf.append(" jid=\"").append(getJid()).append('"');
  167.             }
  168.             if (getNode() != null) {
  169.                 buf.append(" node=\"").append(getNode()).append('"');
  170.             }
  171.             buf.append("/>");
  172.             return buf.toString();
  173.         }
  174.     }

  175.     public static class Provider extends IqProvider<OfflineMessageRequest> {

  176.         @Override
  177.         public OfflineMessageRequest parse(XmlPullParser parser,
  178.                         int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException,
  179.                         IOException {
  180.             OfflineMessageRequest request = new OfflineMessageRequest();
  181.             boolean done = false;
  182.             while (!done) {
  183.                 XmlPullParser.Event eventType = parser.next();
  184.                 if (eventType == XmlPullParser.Event.START_ELEMENT) {
  185.                     if (parser.getName().equals("item")) {
  186.                         request.addItem(parseItem(parser));
  187.                     }
  188.                     else if (parser.getName().equals("purge")) {
  189.                         request.setPurge(true);
  190.                     }
  191.                     else if (parser.getName().equals("fetch")) {
  192.                         request.setFetch(true);
  193.                     }
  194.                 } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
  195.                     if (parser.getName().equals("offline")) {
  196.                         done = true;
  197.                     }
  198.                 }
  199.             }

  200.             return request;
  201.         }

  202.         private static Item parseItem(XmlPullParser parser)
  203.                         throws XmlPullParserException, IOException {
  204.             boolean done = false;
  205.             Item item = new Item(parser.getAttributeValue("", "node"));
  206.             item.setAction(parser.getAttributeValue("", "action"));
  207.             item.setJid(parser.getAttributeValue("", "jid"));
  208.             while (!done) {
  209.                 XmlPullParser.Event eventType = parser.next();
  210.                 if (eventType == XmlPullParser.Event.END_ELEMENT) {
  211.                     if (parser.getName().equals("item")) {
  212.                         done = true;
  213.                     }
  214.                 }
  215.             }
  216.             return item;
  217.         }
  218.     }
  219. }