001/**
002 *
003 * Copyright © 2014-2019 Florian Schmaus
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 org.jivesoftware.smack.util.StringUtils;
021import org.jivesoftware.smack.util.XmlStringBuilder;
022
023/**
024 * The stream open <b>tag</b>.
025 */
026public class StreamOpen implements Nonza {
027
028    public static final String ELEMENT = "stream:stream";
029
030    public static final String CLIENT_NAMESPACE = "jabber:client";
031    public static final String SERVER_NAMESPACE = "jabber:server";
032
033    /**
034     * RFC 6120 § 4.7.5.
035     */
036    public static final String VERSION = "1.0";
037
038    /**
039     * RFC 6120 § 4.7.1.
040     */
041    private final String from;
042
043    /**
044     * RFC 6120 § 4.7.2.
045     */
046    private final String to;
047
048    /**
049     * RFC 6120 § 4.7.3.
050     */
051    private final String id;
052
053    /**
054     * RFC 6120 § 4.7.4.
055     */
056    private final String lang;
057
058    /**
059     * RFC 6120 § 4.8.2.
060     */
061    private final String contentNamespace;
062
063    public StreamOpen(CharSequence to) {
064       this(to, null, null, null, StreamContentNamespace.client);
065    }
066
067    public StreamOpen(CharSequence to, CharSequence from, String id) {
068        this(to, from, id, "en", StreamContentNamespace.client);
069    }
070
071    public StreamOpen(CharSequence to, CharSequence from, String id, String lang, StreamContentNamespace ns) {
072        this.to = StringUtils.maybeToString(to);
073        this.from = StringUtils.maybeToString(from);
074        this.id = id;
075        this.lang = lang;
076        switch (ns) {
077        case client:
078            this.contentNamespace = CLIENT_NAMESPACE;
079            break;
080        case server:
081            this.contentNamespace = SERVER_NAMESPACE;
082            break;
083        default:
084            throw new IllegalStateException();
085        }
086    }
087
088    @Override
089    public String getNamespace() {
090        return contentNamespace;
091    }
092
093    @Override
094    public String getElementName() {
095        return ELEMENT;
096    }
097
098    @Override
099    public XmlStringBuilder toXML(XmlEnvironment enclosingXmlEnvironment) {
100        XmlStringBuilder xml = new XmlStringBuilder();
101        xml.halfOpenElement(getElementName());
102
103        String namespace = CLIENT_NAMESPACE;
104        // We always want to state 'xmlns' for stream open tags.
105        if (enclosingXmlEnvironment != null) {
106            namespace = enclosingXmlEnvironment.getEffectiveNamespaceOrUse(CLIENT_NAMESPACE);
107        }
108        xml.attribute("xmlns", namespace);
109
110        xml.attribute("to", to);
111        xml.attribute("xmlns:stream", "http://etherx.jabber.org/streams");
112        xml.attribute("version", VERSION);
113        xml.optAttribute("from", from);
114        xml.optAttribute("id", id);
115        xml.xmllangAttribute(lang);
116        xml.rightAngleBracket();
117        return xml;
118    }
119
120    public enum StreamContentNamespace {
121        client,
122        server,
123    }
124}