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.smack.packet;
019
020import javax.xml.namespace.QName;
021
022import org.jivesoftware.smack.util.XmlStringBuilder;
023
024/**
025 * IQ stanza that will be sent to the server to establish a session.<p>
026 *
027 * If a server supports sessions, it MUST include a <i>session</i> element in the
028 * stream features it advertises to a client after the completion of stream authentication.
029 * Upon being informed that session establishment is required by the server the client MUST
030 * establish a session if it desires to engage in instant messaging and presence functionality.<p>
031 *
032 * For more information refer to the following
033 * <a href=http://www.xmpp.org/specs/rfc3921.html#session>link</a>.
034 *
035 * @author Gaston Dombiak
036 */
037public class Session extends SimpleIQ {
038
039    public static final String ELEMENT = "session";
040    public static final String NAMESPACE = "urn:ietf:params:xml:ns:xmpp-session";
041
042    public Session() {
043        super(ELEMENT, NAMESPACE);
044        setType(IQ.Type.set);
045    }
046
047    public static class Feature implements ExtensionElement {
048
049        public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
050
051        public static final String OPTIONAL_ELEMENT = "optional";
052
053        private final boolean optional;
054
055        public Feature(boolean optional) {
056            this.optional = optional;
057        }
058
059        public boolean isOptional() {
060            return optional;
061        }
062
063        @Override
064        public String getElementName() {
065            return ELEMENT;
066        }
067
068        @Override
069        public String getNamespace() {
070            return NAMESPACE;
071        }
072
073        @Override
074        public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
075            XmlStringBuilder xml = new XmlStringBuilder(this);
076            if (optional) {
077                xml.rightAngleBracket();
078                xml.emptyElement(OPTIONAL_ELEMENT);
079                xml.closeElement(this);
080            } else {
081                xml.closeEmptyElement();
082            }
083            return xml;
084        }
085    }
086}