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.provider.IQProvider; 027 028import org.xmlpull.v1.XmlPullParser; 029import org.xmlpull.v1.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 childs that holds information about offline messages to 052 * view or delete. 053 * 054 * @return a List of item childs 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<Item>(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 (int i = 0; i < items.size(); i++) { 116 Item item = items.get(i); 117 buf.append(item.toXML()); 118 } 119 } 120 if (purge) { 121 buf.append("<purge/>"); 122 } 123 if (fetch) { 124 buf.append("<fetch/>"); 125 } 126 127 return buf; 128 } 129 130 /** 131 * Item child that holds information about offline messages to view or delete. 132 * 133 * @author Gaston Dombiak 134 */ 135 public static class Item { 136 private String action; 137 private String jid; 138 private String node; 139 140 /** 141 * Creates a new item child. 142 * 143 * @param node the actor's affiliation to the room 144 */ 145 public Item(String node) { 146 this.node = node; 147 } 148 149 public String getNode() { 150 return node; 151 } 152 153 /** 154 * Returns "view" or "remove" that indicate if the server should return the specified 155 * offline message or delete it. 156 * 157 * @return "view" or "remove" that indicate if the server should return the specified 158 * offline message or delete it. 159 */ 160 public String getAction() { 161 return action; 162 } 163 164 /** 165 * Sets if the server should return the specified offline message or delete it. Possible 166 * values are "view" or "remove". 167 * 168 * @param action if the server should return the specified offline message or delete it. 169 */ 170 public void setAction(String action) { 171 this.action = action; 172 } 173 174 public String getJid() { 175 return jid; 176 } 177 178 public void setJid(String jid) { 179 this.jid = jid; 180 } 181 182 public String toXML() { 183 StringBuilder buf = new StringBuilder(); 184 buf.append("<item"); 185 if (getAction() != null) { 186 buf.append(" action=\"").append(getAction()).append('"'); 187 } 188 if (getJid() != null) { 189 buf.append(" jid=\"").append(getJid()).append('"'); 190 } 191 if (getNode() != null) { 192 buf.append(" node=\"").append(getNode()).append('"'); 193 } 194 buf.append("/>"); 195 return buf.toString(); 196 } 197 } 198 199 public static class Provider extends IQProvider<OfflineMessageRequest> { 200 201 @Override 202 public OfflineMessageRequest parse(XmlPullParser parser, 203 int initialDepth) throws XmlPullParserException, 204 IOException { 205 OfflineMessageRequest request = new OfflineMessageRequest(); 206 boolean done = false; 207 while (!done) { 208 int eventType = parser.next(); 209 if (eventType == XmlPullParser.START_TAG) { 210 if (parser.getName().equals("item")) { 211 request.addItem(parseItem(parser)); 212 } 213 else if (parser.getName().equals("purge")) { 214 request.setPurge(true); 215 } 216 else if (parser.getName().equals("fetch")) { 217 request.setFetch(true); 218 } 219 } else if (eventType == XmlPullParser.END_TAG) { 220 if (parser.getName().equals("offline")) { 221 done = true; 222 } 223 } 224 } 225 226 return request; 227 } 228 229 private static Item parseItem(XmlPullParser parser) 230 throws XmlPullParserException, IOException { 231 boolean done = false; 232 Item item = new Item(parser.getAttributeValue("", "node")); 233 item.setAction(parser.getAttributeValue("", "action")); 234 item.setJid(parser.getAttributeValue("", "jid")); 235 while (!done) { 236 int eventType = parser.next(); 237 if (eventType == XmlPullParser.END_TAG) { 238 if (parser.getName().equals("item")) { 239 done = true; 240 } 241 } 242 } 243 return item; 244 } 245 } 246}