001/** 002 * 003 * Copyright 2003-2007 Jive Software. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.jivesoftware.smackx.offline.packet; 019 020import java.io.IOException; 021import java.util.ArrayList; 022import java.util.Collections; 023import java.util.List; 024 025import org.jivesoftware.smack.packet.IQ; 026import org.jivesoftware.smack.packet.XmlEnvironment; 027import org.jivesoftware.smack.provider.IQProvider; 028import org.jivesoftware.smack.xml.XmlPullParser; 029import org.jivesoftware.smack.xml.XmlPullParserException; 030 031/** 032 * Represents a request to get some or all the offline messages of a user. This class can also 033 * be used for deleting some or all the offline messages of a user. 034 * 035 * @author Gaston Dombiak 036 */ 037public class OfflineMessageRequest extends IQ { 038 039 public static final String ELEMENT = "offline"; 040 public static final String NAMESPACE = "http://jabber.org/protocol/offline"; 041 042 private final List<Item> items = new ArrayList<>(); 043 private boolean purge = false; 044 private boolean fetch = false; 045 046 public OfflineMessageRequest() { 047 super(ELEMENT, NAMESPACE); 048 } 049 050 /** 051 * Returns a List of item children that holds information about offline messages to 052 * view or delete. 053 * 054 * @return a List of item children that holds information about offline messages to 055 * view or delete. 056 */ 057 public List<Item> getItems() { 058 synchronized (items) { 059 return Collections.unmodifiableList(new ArrayList<>(items)); 060 } 061 } 062 063 /** 064 * Adds an item child that holds information about offline messages to view or delete. 065 * 066 * @param item the item child that holds information about offline messages to view or delete. 067 */ 068 public void addItem(Item item) { 069 synchronized (items) { 070 items.add(item); 071 } 072 } 073 074 /** 075 * Returns true if all the offline messages of the user should be deleted. 076 * 077 * @return true if all the offline messages of the user should be deleted. 078 */ 079 public boolean isPurge() { 080 return purge; 081 } 082 083 /** 084 * Sets if all the offline messages of the user should be deleted. 085 * 086 * @param purge true if all the offline messages of the user should be deleted. 087 */ 088 public void setPurge(boolean purge) { 089 this.purge = purge; 090 } 091 092 /** 093 * Returns true if all the offline messages of the user should be retrieved. 094 * 095 * @return true if all the offline messages of the user should be retrieved. 096 */ 097 public boolean isFetch() { 098 return fetch; 099 } 100 101 /** 102 * Sets if all the offline messages of the user should be retrieved. 103 * 104 * @param fetch true if all the offline messages of the user should be retrieved. 105 */ 106 public void setFetch(boolean fetch) { 107 this.fetch = fetch; 108 } 109 110 @Override 111 protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) { 112 buf.rightAngleBracket(); 113 114 synchronized (items) { 115 for (Item item : items) { 116 buf.append(item.toXML()); 117 } 118 } 119 if (purge) { 120 buf.append("<purge/>"); 121 } 122 if (fetch) { 123 buf.append("<fetch/>"); 124 } 125 126 return buf; 127 } 128 129 /** 130 * Item child that holds information about offline messages to view or delete. 131 * 132 * @author Gaston Dombiak 133 */ 134 public static class Item { 135 private String action; 136 private String jid; 137 private String node; 138 139 /** 140 * Creates a new item child. 141 * 142 * @param node the actor's affiliation to the room 143 */ 144 public Item(String node) { 145 this.node = node; 146 } 147 148 public String getNode() { 149 return node; 150 } 151 152 /** 153 * Returns "view" or "remove" that indicate if the server should return the specified 154 * offline message or delete it. 155 * 156 * @return "view" or "remove" that indicate if the server should return the specified 157 * offline message or delete it. 158 */ 159 public String getAction() { 160 return action; 161 } 162 163 /** 164 * Sets if the server should return the specified offline message or delete it. Possible 165 * values are "view" or "remove". 166 * 167 * @param action if the server should return the specified offline message or delete it. 168 */ 169 public void setAction(String action) { 170 this.action = action; 171 } 172 173 public String getJid() { 174 return jid; 175 } 176 177 public void setJid(String jid) { 178 this.jid = jid; 179 } 180 181 public String toXML() { 182 StringBuilder buf = new StringBuilder(); 183 buf.append("<item"); 184 if (getAction() != null) { 185 buf.append(" action=\"").append(getAction()).append('"'); 186 } 187 if (getJid() != null) { 188 buf.append(" jid=\"").append(getJid()).append('"'); 189 } 190 if (getNode() != null) { 191 buf.append(" node=\"").append(getNode()).append('"'); 192 } 193 buf.append("/>"); 194 return buf.toString(); 195 } 196 } 197 198 public static class Provider extends IQProvider<OfflineMessageRequest> { 199 200 @Override 201 public OfflineMessageRequest parse(XmlPullParser parser, 202 int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, 203 IOException { 204 OfflineMessageRequest request = new OfflineMessageRequest(); 205 boolean done = false; 206 while (!done) { 207 XmlPullParser.Event eventType = parser.next(); 208 if (eventType == XmlPullParser.Event.START_ELEMENT) { 209 if (parser.getName().equals("item")) { 210 request.addItem(parseItem(parser)); 211 } 212 else if (parser.getName().equals("purge")) { 213 request.setPurge(true); 214 } 215 else if (parser.getName().equals("fetch")) { 216 request.setFetch(true); 217 } 218 } else if (eventType == XmlPullParser.Event.END_ELEMENT) { 219 if (parser.getName().equals("offline")) { 220 done = true; 221 } 222 } 223 } 224 225 return request; 226 } 227 228 private static Item parseItem(XmlPullParser parser) 229 throws XmlPullParserException, IOException { 230 boolean done = false; 231 Item item = new Item(parser.getAttributeValue("", "node")); 232 item.setAction(parser.getAttributeValue("", "action")); 233 item.setJid(parser.getAttributeValue("", "jid")); 234 while (!done) { 235 XmlPullParser.Event eventType = parser.next(); 236 if (eventType == XmlPullParser.Event.END_ELEMENT) { 237 if (parser.getName().equals("item")) { 238 done = true; 239 } 240 } 241 } 242 return item; 243 } 244 } 245}