001/**
002 *
003 * Copyright 2017-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 */
017package org.jivesoftware.smackx.jingle.element;
018
019import java.util.HashMap;
020import java.util.Map;
021
022import org.jivesoftware.smack.packet.FullyQualifiedElement;
023import org.jivesoftware.smack.packet.XmlEnvironment;
024import org.jivesoftware.smack.util.StringUtils;
025import org.jivesoftware.smack.util.XmlStringBuilder;
026
027/**
028 * The Jingle 'reason' element.
029 *
030 * @see <a href="https://xmpp.org/extensions/xep-0166.html#def-reason">XEP-0166 ยง 7.4</a>
031 *
032 */
033public class JingleReason implements FullyQualifiedElement {
034
035    public static final String ELEMENT = "reason";
036    public static final String NAMESPACE = Jingle.NAMESPACE;
037
038    public static AlternativeSession AlternativeSession(String sessionId) {
039        return new AlternativeSession(sessionId);
040    }
041
042    public static final JingleReason Busy = new JingleReason(Reason.busy);
043    public static final JingleReason Cancel = new JingleReason(Reason.cancel);
044    public static final JingleReason ConnectivityError = new JingleReason(Reason.connectivity_error);
045    public static final JingleReason Decline = new JingleReason(Reason.decline);
046    public static final JingleReason Expired = new JingleReason(Reason.expired);
047    public static final JingleReason FailedApplication = new JingleReason(Reason.failed_application);
048    public static final JingleReason FailedTransport = new JingleReason(Reason.failed_transport);
049    public static final JingleReason GeneralError = new JingleReason(Reason.general_error);
050    public static final JingleReason Gone = new JingleReason(Reason.gone);
051    public static final JingleReason IncompatibleParameters = new JingleReason(Reason.incompatible_parameters);
052    public static final JingleReason MediaError = new JingleReason(Reason.media_error);
053    public static final JingleReason SecurityError = new JingleReason(Reason.security_error);
054    public static final JingleReason Success = new JingleReason(Reason.success);
055    public static final JingleReason Timeout = new JingleReason(Reason.timeout);
056    public static final JingleReason UnsupportedApplications = new JingleReason(Reason.unsupported_applications);
057    public static final JingleReason UnsupportedTransports = new JingleReason(Reason.unsupported_transports);
058
059    public enum Reason {
060        alternative_session,
061        busy,
062        cancel,
063        connectivity_error,
064        decline,
065        expired,
066        failed_application,
067        failed_transport,
068        general_error,
069        gone,
070        incompatible_parameters,
071        media_error,
072        security_error,
073        success,
074        timeout,
075        unsupported_applications,
076        unsupported_transports,
077        ;
078
079        protected static final Map<String, Reason> LUT = new HashMap<>(Reason.values().length);
080
081        static {
082            for (Reason reason : Reason.values()) {
083                LUT.put(reason.toString(), reason);
084            }
085        }
086
087        protected final String asString;
088
089        Reason() {
090            asString = name().replace('_', '-');
091        }
092
093        @Override
094        public String toString() {
095            return asString;
096        }
097
098        public static Reason fromString(String string) {
099            Reason reason = LUT.get(string);
100            if (reason == null) {
101                throw new IllegalArgumentException("Unknown reason: " + string);
102            }
103            return reason;
104        }
105    }
106
107    protected final Reason reason;
108
109    public JingleReason(Reason reason) {
110        this.reason = reason;
111    }
112
113    @Override
114    public String getElementName() {
115        return ELEMENT;
116    }
117
118    @Override
119    public String getNamespace() {
120        return NAMESPACE;
121    }
122
123    @Override
124    public XmlStringBuilder toXML(XmlEnvironment enclosingXmlEnvironment) {
125        XmlStringBuilder xml = new XmlStringBuilder(this, enclosingXmlEnvironment);
126        xml.rightAngleBracket();
127
128        xml.emptyElement(reason);
129
130        xml.closeElement(this);
131        return xml;
132    }
133
134    public Reason asEnum() {
135        return reason;
136    }
137
138
139    public static class AlternativeSession extends JingleReason {
140
141        public static final String SID = "sid";
142        private final String sessionId;
143
144        public AlternativeSession(String sessionId) {
145            super(Reason.alternative_session);
146            if (StringUtils.isNullOrEmpty(sessionId)) {
147                throw new NullPointerException("SessionID must not be null or empty.");
148            }
149            this.sessionId = sessionId;
150        }
151
152        @Override
153        public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
154            XmlStringBuilder xml = new XmlStringBuilder(this);
155            xml.rightAngleBracket();
156
157            xml.openElement(reason.asString);
158            xml.openElement(SID);
159            xml.append(sessionId);
160            xml.closeElement(SID);
161            xml.closeElement(reason.asString);
162
163            xml.closeElement(this);
164            return xml;
165        }
166
167        public String getAlternativeSessionId() {
168            return sessionId;
169        }
170    }
171}