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 org.jivesoftware.smack.packet.IQ; 021import org.jivesoftware.smack.provider.IQProvider; 022import org.xmlpull.v1.XmlPullParser; 023import org.xmlpull.v1.XmlPullParserException; 024 025import java.io.IOException; 026import java.util.ArrayList; 027import java.util.Collections; 028import java.util.List; 029 030/** 031 * Represents a request to get some or all the offline messages of a user. This class can also 032 * be used for deleting some or all the offline messages of a user. 033 * 034 * @author Gaston Dombiak 035 */ 036public class OfflineMessageRequest extends IQ { 037 038 public static final String ELEMENT = "offline"; 039 public static final String NAMESPACE = "http://jabber.org/protocol/offline"; 040 041 private List<Item> items = new ArrayList<Item>(); 042 private boolean purge = false; 043 private boolean fetch = false; 044 045 public OfflineMessageRequest() { 046 super(ELEMENT, NAMESPACE); 047 } 048 049 /** 050 * Returns a List of item childs that holds information about offline messages to 051 * view or delete. 052 * 053 * @return a List of item childs that holds information about offline messages to 054 * view or delete. 055 */ 056 public List<Item> getItems() { 057 synchronized (items) { 058 return Collections.unmodifiableList(new ArrayList<Item>(items)); 059 } 060 } 061 062 /** 063 * Adds an item child that holds information about offline messages to view or delete. 064 * 065 * @param item the item child that holds information about offline messages to view or delete. 066 */ 067 public void addItem(Item item) { 068 synchronized (items) { 069 items.add(item); 070 } 071 } 072 073 /** 074 * Returns true if all the offline messages of the user should be deleted. 075 * 076 * @return true if all the offline messages of the user should be deleted. 077 */ 078 public boolean isPurge() { 079 return purge; 080 } 081 082 /** 083 * Sets if all the offline messages of the user should be deleted. 084 * 085 * @param purge true if all the offline messages of the user should be deleted. 086 */ 087 public void setPurge(boolean purge) { 088 this.purge = purge; 089 } 090 091 /** 092 * Returns true if all the offline messages of the user should be retrieved. 093 * 094 * @return true if all the offline messages of the user should be retrieved. 095 */ 096 public boolean isFetch() { 097 return fetch; 098 } 099 100 /** 101 * Sets if all the offline messages of the user should be retrieved. 102 * 103 * @param fetch true if all the offline messages of the user should be retrieved. 104 */ 105 public void setFetch(boolean fetch) { 106 this.fetch = fetch; 107 } 108 109 @Override 110 protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) { 111 buf.rightAngleBracket(); 112 113 synchronized (items) { 114 for (int i = 0; i < items.size(); i++) { 115 Item item = items.get(i); 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) throws XmlPullParserException, 203 IOException { 204 OfflineMessageRequest request = new OfflineMessageRequest(); 205 boolean done = false; 206 while (!done) { 207 int eventType = parser.next(); 208 if (eventType == XmlPullParser.START_TAG) { 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.END_TAG) { 219 if (parser.getName().equals("offline")) { 220 done = true; 221 } 222 } 223 } 224 225 return request; 226 } 227 228 private 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 int eventType = parser.next(); 236 if (eventType == XmlPullParser.END_TAG) { 237 if (parser.getName().equals("item")) { 238 done = true; 239 } 240 } 241 } 242 return item; 243 } 244 } 245}