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 org.jivesoftware.smack.packet.IQ;
  19. import org.jivesoftware.smack.provider.IQProvider;
  20. import org.xmlpull.v1.XmlPullParser;
  21. import org.xmlpull.v1.XmlPullParserException;

  22. import java.io.IOException;
  23. import java.util.ArrayList;
  24. import java.util.Collections;
  25. import java.util.List;

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

  33.     public static final String ELEMENT = "offline";
  34.     public static final String NAMESPACE = "http://jabber.org/protocol/offline";

  35.     private List<Item> items = new ArrayList<Item>();
  36.     private boolean purge = false;
  37.     private boolean fetch = false;

  38.     public OfflineMessageRequest() {
  39.         super(ELEMENT, NAMESPACE);
  40.     }

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

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

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

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

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

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

  95.     @Override
  96.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
  97.         buf.rightAngleBracket();

  98.         synchronized (items) {
  99.             for (int i = 0; i < items.size(); i++) {
  100.                 Item item = items.get(i);
  101.                 buf.append(item.toXML());
  102.             }
  103.         }
  104.         if (purge) {
  105.             buf.append("<purge/>");
  106.         }
  107.         if (fetch) {
  108.             buf.append("<fetch/>");
  109.         }

  110.         return buf;
  111.     }

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

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

  129.         public String getNode() {
  130.             return node;
  131.         }

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

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

  151.         public String getJid() {
  152.             return jid;
  153.         }

  154.         public void setJid(String jid) {
  155.             this.jid = jid;
  156.         }

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

  173.     public static class Provider extends IQProvider<OfflineMessageRequest> {

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

  198.             return request;
  199.         }

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