mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-10 09:39:39 +02:00
Initial account manager functionality.
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@1846 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
996f50449c
commit
7fa5b3608e
6 changed files with 362 additions and 31 deletions
109
source/org/jivesoftware/smack/AccountManager.java
Normal file
109
source/org/jivesoftware/smack/AccountManager.java
Normal file
|
@ -0,0 +1,109 @@
|
|||
package org.jivesoftware.smack;
|
||||
|
||||
import org.jivesoftware.smack.packet.Registration;
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.filter.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Allows creation and management of accounts on an XMPP server.
|
||||
*
|
||||
* @see XMPPConnection#getAccountManager();
|
||||
* @author Matt Tucker
|
||||
*/
|
||||
public class AccountManager {
|
||||
|
||||
private XMPPConnection connection;
|
||||
private Registration info = null;
|
||||
|
||||
public AccountManager(XMPPConnection connection) {
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the server supports creating new accounts. Many servers require that
|
||||
* you not be currently authenticated when creating new accounts, so the safest
|
||||
* behavior is to only create new accounts when
|
||||
*
|
||||
* @return true if the server support creating new accounts.
|
||||
*/
|
||||
public boolean supportsAccountCreation() {
|
||||
try {
|
||||
if (info == null) {
|
||||
getRegistrationInfo();
|
||||
}
|
||||
return info.getType() != IQ.Type.ERROR;
|
||||
}
|
||||
catch (XMPPException xe) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator getAccountAttributes() {
|
||||
try {
|
||||
if (info == null) {
|
||||
getRegistrationInfo();
|
||||
}
|
||||
Map attributes = info.getAttributes();
|
||||
if (attributes != null) {
|
||||
return attributes.keySet().iterator();
|
||||
}
|
||||
}
|
||||
catch (XMPPException xe) { }
|
||||
return Collections.EMPTY_LIST.iterator();
|
||||
}
|
||||
|
||||
public void createAccount(String username, String password) throws XMPPException {
|
||||
if (!supportsAccountCreation()) {
|
||||
throw new XMPPException("Server does not support account creation.");
|
||||
}
|
||||
}
|
||||
|
||||
public void createAccount(String username, String password, Map attributes)
|
||||
throws XMPPException
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void deleteAccount() throws XMPPException {
|
||||
if (!connection.isAuthenticated()) {
|
||||
throw new IllegalStateException("Must be logged in to delete a account.");
|
||||
}
|
||||
Registration reg = new Registration();
|
||||
reg.setType(IQ.Type.SET);
|
||||
Map attributes = new HashMap();
|
||||
// To delete an account, we add a single attribute, "remove", that is blank.
|
||||
attributes.put("remove", "");
|
||||
reg.setAttributes(attributes);
|
||||
PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()),
|
||||
new PacketTypeFilter(Registration.class));
|
||||
PacketCollector collector = connection.createPacketCollector(filter);
|
||||
connection.sendPacket(reg);
|
||||
IQ result = (IQ)collector.nextResult(5000);
|
||||
if (result == null) {
|
||||
throw new XMPPException("No response from server.");
|
||||
}
|
||||
else if (result.getType() == IQ.Type.ERROR) {
|
||||
throw new XMPPException(result.getError());
|
||||
}
|
||||
}
|
||||
|
||||
private synchronized void getRegistrationInfo() throws XMPPException {
|
||||
Registration reg = new Registration();
|
||||
PacketFilter filter = new AndFilter(new PacketIDFilter(reg.getPacketID()),
|
||||
new PacketTypeFilter(IQ.class));
|
||||
PacketCollector collector = connection.createPacketCollector(filter);
|
||||
connection.sendPacket(reg);
|
||||
IQ result = (IQ)collector.nextResult(5000);
|
||||
if (result == null) {
|
||||
throw new XMPPException("No response from server.");
|
||||
}
|
||||
else if (result.getType() == IQ.Type.ERROR) {
|
||||
throw new XMPPException(result.getError());
|
||||
}
|
||||
else {
|
||||
info = (Registration)result;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue