1 20 21 package org.jivesoftware.smackx; 22 23 import org.jivesoftware.smack.*; 24 import org.jivesoftware.smack.provider.IQProvider; 25 import org.jivesoftware.smack.filter.PacketIDFilter; 26 import org.jivesoftware.smack.packet.IQ; 27 import org.jivesoftware.smackx.packet.*; 28 import org.jivesoftware.smackx.provider.*; 29 import org.xmlpull.v1.XmlPullParser; 30 31 import java.util.Map ; 32 import java.util.Hashtable ; 33 34 56 public class PrivateDataManager { 57 58 61 private static Map privateDataProviders = new Hashtable (); 62 63 85 public static PrivateDataProvider getPrivateDataProvider(String elementName, String namespace) { 86 String key = getProviderKey(elementName, namespace); 87 return (PrivateDataProvider)privateDataProviders.get(key); 88 } 89 90 98 public static void addPrivateDataProvider(String elementName, String namespace, 99 PrivateDataProvider provider) 100 { 101 String key = getProviderKey(elementName, namespace); 102 privateDataProviders.put(key, provider); 103 } 104 105 106 private XMPPConnection connection; 107 108 113 private String user; 114 115 123 public PrivateDataManager(XMPPConnection connection) { 124 if (!connection.isAuthenticated()) { 125 throw new IllegalStateException ("Must be logged in to XMPP server."); 126 } 127 this.connection = connection; 128 } 129 130 142 public PrivateDataManager(XMPPConnection connection, String user) { 143 if (!connection.isAuthenticated()) { 144 throw new IllegalStateException ("Must be logged in to XMPP server."); 145 } 146 this.connection = connection; 147 this.user = user; 148 } 149 150 163 public PrivateData getPrivateData(final String elementName, final String namespace) 164 throws XMPPException 165 { 166 IQ privateDataGet = new IQ() { 168 public String getChildElementXML() { 169 StringBuffer buf = new StringBuffer (); 170 buf.append("<query xmlns=\"jabber:iq:private\">"); 171 buf.append("<").append(elementName).append(" xmlns=\"").append(namespace).append("\"/>"); 172 buf.append("</query>"); 173 return buf.toString(); 174 } 175 }; 176 privateDataGet.setType(IQ.Type.GET); 177 if (user != null) { 179 privateDataGet.setTo(user); 180 } 181 182 String packetID = privateDataGet.getPacketID(); 184 PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(packetID)); 185 186 connection.sendPacket(privateDataGet); 188 189 IQ response = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); 191 collector.cancel(); 193 if (response == null) { 194 throw new XMPPException("No response from the server."); 195 } 196 else if (response.getType() == IQ.Type.ERROR) { 198 throw new XMPPException(response.getError()); 199 } 200 return ((PrivateDataResult)response).getPrivateData(); 201 } 202 203 211 public void setPrivateData(final PrivateData privateData) throws XMPPException { 212 IQ privateDataSet = new IQ() { 214 public String getChildElementXML() { 215 StringBuffer buf = new StringBuffer (); 216 buf.append("<query xmlns=\"jabber:iq:private\">"); 217 buf.append(privateData.toXML()); 218 buf.append("</query>"); 219 return buf.toString(); 220 } 221 }; 222 privateDataSet.setType(IQ.Type.SET); 223 if (user != null) { 225 privateDataSet.setTo(user); 226 } 227 228 String packetID = privateDataSet.getPacketID(); 230 PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(packetID)); 231 232 connection.sendPacket(privateDataSet); 234 235 IQ response = (IQ)collector.nextResult(5000); 237 collector.cancel(); 239 if (response == null) { 240 throw new XMPPException("No response from the server."); 241 } 242 else if (response.getType() == IQ.Type.ERROR) { 244 throw new XMPPException(response.getError()); 245 } 246 } 247 248 255 private static String getProviderKey(String elementName, String namespace) { 256 StringBuffer buf = new StringBuffer (); 257 buf.append("<").append(elementName).append("/><").append(namespace).append("/>"); 258 return buf.toString(); 259 } 260 261 264 public static class PrivateDataIQProvider implements IQProvider { 265 public IQ parseIQ(XmlPullParser parser) throws Exception { 266 PrivateData privateData = null; 267 boolean done = false; 268 while (!done) { 269 int eventType = parser.next(); 270 if (eventType == XmlPullParser.START_TAG) { 271 String elementName = parser.getName(); 272 String namespace = parser.getNamespace(); 273 PrivateDataProvider provider = getPrivateDataProvider(elementName, namespace); 275 if (provider != null) { 277 privateData = provider.parsePrivateData(parser); 278 } 279 else { 281 DefaultPrivateData data = new DefaultPrivateData(elementName, namespace); 282 boolean finished = false; 283 while (!finished) { 284 int event = parser.next(); 285 if (event == XmlPullParser.START_TAG) { 286 String name = parser.getName(); 287 if (parser.isEmptyElementTag()) { 289 data.setValue(name,""); 290 } 291 else { 293 event = parser.next(); 294 if (event == XmlPullParser.TEXT) { 295 String value = parser.getText(); 296 data.setValue(name, value); 297 } 298 } 299 } 300 else if (event == XmlPullParser.END_TAG) { 301 if (parser.getName().equals(elementName)) { 302 finished = true; 303 } 304 } 305 } 306 privateData = data; 307 } 308 } 309 else if (eventType == XmlPullParser.END_TAG) { 310 if (parser.getName().equals("query")) { 311 done = true; 312 } 313 } 314 } 315 IQ result = new PrivateDataResult(privateData); 316 return result; 317 } 318 } 319 320 323 private static class PrivateDataResult extends IQ { 324 325 private PrivateData privateData; 326 327 PrivateDataResult(PrivateData privateData) { 328 this.privateData = privateData; 329 } 330 331 public PrivateData getPrivateData() { 332 return privateData; 333 } 334 335 public String getChildElementXML() { 336 StringBuffer buf = new StringBuffer (); 337 buf.append("<query xmlns=\"jabber:iq:private\">"); 338 if (privateData != null) { 339 privateData.toXML(); 340 } 341 buf.append("</query>"); 342 return buf.toString(); 343 } 344 } 345 } | Popular Tags |