001/**
002 *
003 * Copyright 2020-2021 Florian Schmaus, 2020 Aditya Borikar
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 */
017package org.jivesoftware.smack.packet;
018
019import org.jivesoftware.smack.XMPPConnection;
020import org.jivesoftware.smack.packet.StreamOpen.StreamContentNamespace;
021import org.jivesoftware.smack.util.StringUtils;
022import org.jivesoftware.smack.util.XmlStringBuilder;
023
024/**
025 * AbstractStreamOpen is actually a {@link TopLevelStreamElement}, however we
026 * implement {@link Nonza} here. This is because, {@link XMPPConnection} doesn't
027 * yet support sending {@link TopLevelStreamElement} directly and the same can only
028 * be achieved through {@link XMPPConnection#sendNonza(Nonza)}.
029 */
030public abstract class AbstractStreamOpen implements Nonza {
031    public static final String ETHERX_JABBER_STREAMS_NAMESPACE = "http://etherx.jabber.org/streams";
032    public static final String CLIENT_NAMESPACE = "jabber:client";
033    public static final String SERVER_NAMESPACE = "jabber:server";
034
035    /**
036     * RFC 6120 § 4.7.5.
037     */
038    public static final String VERSION = "1.0";
039
040    /**
041     * RFC 6120 § 4.7.1.
042     */
043    protected final String from;
044
045    /**
046     * RFC 6120 § 4.7.2.
047     */
048    protected final String to;
049
050    /**
051     * RFC 6120 § 4.7.3.
052     */
053    protected final String id;
054
055    /**
056     * RFC 6120 § 4.7.4.
057     */
058    protected final String lang;
059
060    /**
061     * RFC 6120 § 4.8.2.
062     */
063    protected final String contentNamespace;
064
065    public AbstractStreamOpen(CharSequence to, CharSequence from, String id, String lang) {
066        this(to, from, id, lang, StreamContentNamespace.client);
067    }
068
069    public AbstractStreamOpen(CharSequence to, CharSequence from, String id, String lang, StreamContentNamespace ns) {
070        this.to = StringUtils.maybeToString(to);
071        this.from = StringUtils.maybeToString(from);
072        this.id = id;
073        this.lang = lang;
074        switch (ns) {
075        case client:
076            this.contentNamespace = CLIENT_NAMESPACE;
077            break;
078        case server:
079            this.contentNamespace = SERVER_NAMESPACE;
080            break;
081        default:
082            throw new IllegalStateException();
083        }
084    }
085
086    protected final void addCommonAttributes(XmlStringBuilder xml) {
087        xml.optAttribute("to", to);
088        xml.optAttribute("version", VERSION);
089    }
090}