KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smack > AccountManager


1 /**
2  * $RCSfile$
3  * $Revision: 2731 $
4  * $Date: 2005-08-26 23:24:38 -0300 (Fri, 26 Aug 2005) $
5  *
6  * Copyright 2003-2004 Jive Software.
7  *
8  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */

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 /**
31  * Allows creation and management of accounts on an XMPP server.
32  *
33  * @see XMPPConnection#getAccountManager()
34  * @author Matt Tucker
35  */

36 public class AccountManager {
37
38     private XMPPConnection connection;
39     private Registration info = null;
40
41     /**
42      * Creates a new AccountManager instance.
43      *
44      * @param connection a connection to a XMPP server.
45      */

46     public AccountManager(XMPPConnection connection) {
47         this.connection = connection;
48     }
49
50     /**
51      * Returns true if the server supports creating new accounts. Many servers require
52      * that you not be currently authenticated when creating new accounts, so the safest
53      * behavior is to only create new accounts before having logged in to a server.
54      *
55      * @return true if the server support creating new accounts.
56      */

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     /**
70      * Returns an Iterator for the (String) names of the required account attributes.
71      * All attributes must be set when creating new accounts. The standard
72      * attributes are as follows: <ul>
73      * <li>name -- the user's name.
74      * <li>first -- the user's first name.
75      * <li>last -- the user's last name.
76      * <li>email -- the user's email address.
77      * <li>city -- the user's city.
78      * <li>state -- the user's state.
79      * <li>zip -- the user's ZIP code.
80      * <li>phone -- the user's phone number.
81      * <li>url -- the user's website.
82      * <li>date -- the date the registration took place.
83      * <li>misc -- other miscellaneous information to associate with the account.
84      * <li>text -- textual information to associate with the account.
85      * <li>remove -- empty flag to remove account.
86      * </ul><p>
87      *
88      * Typically, servers require no attributes when creating new accounts, or just
89      * the user's email address.
90      *
91      * @return the required account attributes.
92      */

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     /**
108      * Returns the value of a given account attribute or <tt>null</tt> if the account
109      * attribute wasn't found.
110      *
111      * @param name the name of the account attribute to return its value.
112      * @return the value of the account attribute or <tt>null</tt> if an account
113      * attribute wasn't found for the requested name.
114      */

115     public String JavaDoc getAccountAttribute(String JavaDoc name) {
116         try {
117             if (info == null) {
118                 getRegistrationInfo();
119             }
120             return (String JavaDoc) info.getAttributes().get(name);
121         }
122         catch (XMPPException xe) { }
123         return null;
124     }
125
126     /**
127      * Returns the instructions for creating a new account, or <tt>null</tt> if there
128      * are no instructions. If present, instructions should be displayed to the end-user
129      * that will complete the registration process.
130      *
131      * @return the account creation instructions, or <tt>null</tt> if there are none.
132      */

133     public String JavaDoc 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     /**
146      * Creates a new account using the specified username and password. The server may
147      * require a number of extra account attributes such as an email address and phone
148      * number. In that case, Smack will attempt to automatically set all required
149      * attributes with blank values, which may or may not be accepted by the server.
150      * Therefore, it's recommended to check the required account attributes and to let
151      * the end-user populate them with real values instead.
152      *
153      * @param username the username.
154      * @param password the password.
155      * @throws XMPPException if an error occurs creating the account.
156      */

157     public void createAccount(String JavaDoc username, String JavaDoc password) throws XMPPException {
158         if (!supportsAccountCreation()) {
159             throw new XMPPException("Server does not support account creation.");
160         }
161         // Create a map for all the required attributes, but give them blank values.
162
Map attributes = new HashMap();
163         for (Iterator i=getAccountAttributes(); i.hasNext(); ) {
164             String JavaDoc attributeName = (String JavaDoc)i.next();
165             attributes.put(attributeName, "");
166         }
167         createAccount(username, password, attributes);
168     }
169
170     /**
171      * Creates a new account using the specified username, password and account attributes.
172      * The attributes Map must contain only String name/value pairs and must also have values
173      * for all required attributes.
174      *
175      * @param username the username.
176      * @param password the password.
177      * @param attributes the account attributes.
178      * @throws XMPPException if an error occurs creating the account.
179      * @see #getAccountAttributes()
180      */

181     public void createAccount(String JavaDoc username, String JavaDoc 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         // Stop queuing results
199
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     /**
209      * Changes the password of the currently logged-in account. This operation can only
210      * be performed after a successful login operation has been completed. Not all servers
211      * support changing passwords; an XMPPException will be thrown when that is the case.
212      *
213      * @throws IllegalStateException if not currently logged-in to the server.
214      * @throws XMPPException if an error occurs when changing the password.
215      */

216     public void changePassword(String JavaDoc 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         // Stop queuing results
230
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     /**
240      * Deletes the currently logged-in account from the server. This operation can only
241      * be performed after a successful login operation has been completed. Not all servers
242      * support deleting accounts; an XMPPException will be thrown when that is the case.
243      *
244      * @throws IllegalStateException if not currently logged-in to the server.
245      * @throws XMPPException if an error occurs when deleting the account.
246      */

247     public void deleteAccount() throws XMPPException {
248         if (!connection.isAuthenticated()) {
249             throw new IllegalStateException JavaDoc("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         // To delete an account, we add a single attribute, "remove", that is blank.
256
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         // Stop queuing results
264
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     /**
274      * Gets the account registration info from the server.
275      *
276      * @throws XMPPException if an error occurs.
277      */

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         // Stop queuing results
287
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