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.iqregister.packet;
019
020import java.util.Map;
021
022import javax.xml.namespace.QName;
023
024import org.jivesoftware.smack.packet.ExtensionElement;
025import org.jivesoftware.smack.packet.IQ;
026
027/**
028 * Represents registration packets. An empty GET query will cause the server to return information
029 * about it's registration support. SET queries can be used to create accounts or update
030 * existing account information. XMPP servers may require a number of attributes to be set
031 * when creating a new account. The standard account attributes are as follows:
032 * <ul>
033 *      <li>name -- the user's name.
034 *      <li>first -- the user's first name.
035 *      <li>last -- the user's last name.
036 *      <li>email -- the user's email address.
037 *      <li>city -- the user's city.
038 *      <li>state -- the user's state.
039 *      <li>zip -- the user's ZIP code.
040 *      <li>phone -- the user's phone number.
041 *      <li>url -- the user's website.
042 *      <li>date -- the date the registration took place.
043 *      <li>misc -- other miscellaneous information to associate with the account.
044 *      <li>text -- textual information to associate with the account.
045 *      <li>remove -- empty flag to remove account.
046 * </ul>
047 *
048 * @author Matt Tucker
049 */
050public class Registration extends IQ {
051
052    public static final String ELEMENT = QUERY_ELEMENT;
053    public static final String NAMESPACE = "jabber:iq:register";
054
055    private final String instructions;
056    private final Map<String, String> attributes;
057
058    public Registration() {
059        this(null);
060    }
061
062    public Registration(Map<String, String> attributes) {
063        this(null, attributes);
064    }
065
066    public Registration(String instructions, Map<String, String> attributes) {
067        super(ELEMENT, NAMESPACE);
068        this.instructions = instructions;
069        this.attributes = attributes;
070    }
071
072    /**
073     * Returns the registration instructions, or <code>null</code> if no instructions
074     * have been set. If present, instructions should be displayed to the end-user
075     * that will complete the registration process.
076     *
077     * @return the registration instructions, or <code>null</code> if there are none.
078     */
079    public String getInstructions() {
080        return instructions;
081    }
082
083    /**
084     * Returns the map of String key/value pairs of account attributes.
085     *
086     * @return the account attributes.
087     */
088    public Map<String, String> getAttributes() {
089        return attributes;
090    }
091
092    @Override
093    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
094        xml.rightAngleBracket();
095        xml.optElement("instructions", instructions);
096        if (attributes != null && attributes.size() > 0) {
097            for (String name : attributes.keySet()) {
098                String value = attributes.get(name);
099                xml.element(name, value);
100            }
101        }
102        return xml;
103    }
104
105    public static final class Feature implements ExtensionElement {
106
107        public static final String ELEMENT = "register";
108        public static final String NAMESPACE = "http://jabber.org/features/iq-register";
109        public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
110
111        public static final Feature INSTANCE = new Registration.Feature();
112
113        private Feature() {
114        }
115
116        @Override
117        public String getElementName() {
118            return ELEMENT;
119        }
120
121        @Override
122        public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
123            return '<' + ELEMENT + " xmlns='" + NAMESPACE + "'/>";
124        }
125
126        @Override
127        public String getNamespace() {
128            return NAMESPACE;
129        }
130
131    }
132}