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