1 20 21 package org.jivesoftware.smack; 22 23 import org.jivesoftware.smack.packet.Registration; 24 import org.jivesoftware.smack.packet.IQ; 25 import org.jivesoftware.smack.filter.*; 26 import org.jivesoftware.smack.util.StringUtils; 27 28 import java.util.*; 29 30 36 public class AccountManager { 37 38 private XMPPConnection connection; 39 private Registration info = null; 40 41 46 public AccountManager(XMPPConnection connection) { 47 this.connection = connection; 48 } 49 50 57 public boolean supportsAccountCreation() { 58 try { 59 if (info == null) { 60 getRegistrationInfo(); 61 } 62 return info.getType() != IQ.Type.ERROR; 63 } 64 catch (XMPPException xe) { 65 return false; 66 } 67 } 68 69 93 public Iterator getAccountAttributes() { 94 try { 95 if (info == null) { 96 getRegistrationInfo(); 97 } 98 Map attributes = info.getAttributes(); 99 if (attributes != null) { 100 return attributes.keySet().iterator(); 101 } 102 } 103 catch (XMPPException xe) { } 104 return Collections.EMPTY_LIST.iterator(); 105 } 106 107 115 public String getAccountAttribute(String name) { 116 try { 117 if (info == null) { 118 getRegistrationInfo(); 119 } 120 return (String ) info.getAttributes().get(name); 121 } 122 catch (XMPPException xe) { } 123 return null; 124 } 125 126 133 public String getAccountInstructions() { 134 try { 135 if (info == null) { 136 getRegistrationInfo(); 137 } 138 return info.getInstructions(); 139 } 140 catch (XMPPException xe) { 141 return null; 142 } 143 } 144 145 157 public void createAccount(String username, String password) throws XMPPException { 158 if (!supportsAccountCreation()) { 159 throw new XMPPException("Server does not support account creation."); 160 } 161 Map attributes = new HashMap(); 163 for (Iterator i=getAccountAttributes(); i.hasNext(); ) { 164 String attributeName = (String )i.next(); 165 attributes.put(attributeName, ""); 166 } 167 createAccount(username, password, attributes); 168 } 169 170 181 public void createAccount(String username, String password, Map attributes) 182 throws XMPPException 183 { 184 if (!supportsAccountCreation()) { 185 throw new XMPPException("Server does not support account creation."); 186 } 187 Registration reg = new Registration(); 188 reg.setType(IQ.Type.SET); 189 reg.setTo(connection.getServiceName()); 190 attributes.put("username",username); 191 attributes.put("password",password); 192 reg.setAttributes(attributes); 193 PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()), 194 new PacketTypeFilter(IQ.class)); 195 PacketCollector collector = connection.createPacketCollector(filter); 196 connection.sendPacket(reg); 197 IQ result = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); 198 collector.cancel(); 200 if (result == null) { 201 throw new XMPPException("No response from server."); 202 } 203 else if (result.getType() == IQ.Type.ERROR) { 204 throw new XMPPException(result.getError()); 205 } 206 } 207 208 216 public void changePassword(String newPassword) throws XMPPException { 217 Registration reg = new Registration(); 218 reg.setType(IQ.Type.SET); 219 reg.setTo(connection.getServiceName()); 220 HashMap map = new HashMap(); 221 map.put("username",StringUtils.parseName(connection.getUser())); 222 map.put("password",newPassword); 223 reg.setAttributes(map); 224 PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()), 225 new PacketTypeFilter(IQ.class)); 226 PacketCollector collector = connection.createPacketCollector(filter); 227 connection.sendPacket(reg); 228 IQ result = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); 229 collector.cancel(); 231 if (result == null) { 232 throw new XMPPException("No response from server."); 233 } 234 else if (result.getType() == IQ.Type.ERROR) { 235 throw new XMPPException(result.getError()); 236 } 237 } 238 239 247 public void deleteAccount() throws XMPPException { 248 if (!connection.isAuthenticated()) { 249 throw new IllegalStateException ("Must be logged in to delete a account."); 250 } 251 Registration reg = new Registration(); 252 reg.setType(IQ.Type.SET); 253 reg.setTo(connection.getServiceName()); 254 Map attributes = new HashMap(); 255 attributes.put("remove", ""); 257 reg.setAttributes(attributes); 258 PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()), 259 new PacketTypeFilter(IQ.class)); 260 PacketCollector collector = connection.createPacketCollector(filter); 261 connection.sendPacket(reg); 262 IQ result = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); 263 collector.cancel(); 265 if (result == null) { 266 throw new XMPPException("No response from server."); 267 } 268 else if (result.getType() == IQ.Type.ERROR) { 269 throw new XMPPException(result.getError()); 270 } 271 } 272 273 278 private synchronized void getRegistrationInfo() throws XMPPException { 279 Registration reg = new Registration(); 280 reg.setTo(connection.getServiceName()); 281 PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()), 282 new PacketTypeFilter(IQ.class)); 283 PacketCollector collector = connection.createPacketCollector(filter); 284 connection.sendPacket(reg); 285 IQ result = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); 286 collector.cancel(); 288 if (result == null) { 289 throw new XMPPException("No response from server."); 290 } 291 else if (result.getType() == IQ.Type.ERROR) { 292 throw new XMPPException(result.getError()); 293 } 294 else { 295 info = (Registration)result; 296 } 297 } 298 } | Popular Tags |