001/**
002 *
003 * Copyright 2017 Paul Schaub
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.transports.jingle_s5b.elements;
018
019import org.jivesoftware.smack.packet.XmlEnvironment;
020import org.jivesoftware.smack.util.XmlStringBuilder;
021
022import org.jivesoftware.smackx.jingle.element.JingleContentTransportInfo;
023
024/**
025 * Class representing possible SOCKS5 TransportInfo elements.
026 * @see <a href="https://xmpp.org/extensions/xep-0260.html">XEP-0260: Jingle SOCKS5 Bytestreams Transport Method 1.0.3 (2018-05-15)</a>
027 */
028public abstract class JingleS5BTransportInfo implements JingleContentTransportInfo {
029
030    public static final String NAMESPACE = JingleS5BTransport.NAMESPACE_V1;
031
032    @Override
033    public final String getNamespace() {
034        return NAMESPACE;
035    }
036
037    public abstract static class JingleS5BCandidateTransportInfo extends JingleS5BTransportInfo {
038        public static final String ATTR_CID = "cid";
039
040        private final String candidateId;
041
042        protected JingleS5BCandidateTransportInfo(String candidateId) {
043            this.candidateId = candidateId;
044        }
045
046        public final String getCandidateId() {
047            return candidateId;
048        }
049
050        @Override
051        public final XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
052            XmlStringBuilder xml = new XmlStringBuilder(this, xmlEnvironment);
053            xml.attribute(ATTR_CID, getCandidateId());
054            xml.closeEmptyElement();
055            return xml;
056        }
057
058        @Override
059        public final boolean equals(Object other) {
060            if (!(other instanceof JingleS5BCandidateTransportInfo)) {
061                return false;
062            }
063
064            JingleS5BCandidateTransportInfo otherCandidateTransportInfo = (JingleS5BCandidateTransportInfo) other;
065            return toXML().toString().equals(otherCandidateTransportInfo.toXML().toString());
066        }
067
068        @Override
069        public final int hashCode() {
070            return getCandidateId().toString().hashCode();
071        }
072    }
073
074    public static final class CandidateActivated extends JingleS5BCandidateTransportInfo {
075        public static final String ELEMENT = "activated";
076
077        public CandidateActivated(String candidateId) {
078            super(candidateId);
079        }
080
081        @Override
082        public String getElementName() {
083            return ELEMENT;
084        }
085    }
086
087    public static final class CandidateUsed extends JingleS5BCandidateTransportInfo {
088        public static final String ELEMENT = "candidate-used";
089
090        public CandidateUsed(String candidateId) {
091            super(candidateId);
092        }
093
094        @Override
095        public String getElementName() {
096            return ELEMENT;
097        }
098    }
099
100    public static final class CandidateError extends JingleS5BTransportInfo {
101        public static final CandidateError INSTANCE = new CandidateError();
102
103        public static final String ELEMENT = "candidate-error";
104
105        private CandidateError() {
106        }
107
108        @Override
109        public String getElementName() {
110            return ELEMENT;
111        }
112
113        @Override
114        public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
115            XmlStringBuilder xml = new XmlStringBuilder();
116            xml.halfOpenElement(this);
117            xml.closeEmptyElement();
118            return xml;
119        }
120
121        @Override
122        public boolean equals(Object other) {
123            return other == INSTANCE;
124        }
125
126        @Override
127        public int hashCode() {
128            return toXML().toString().hashCode();
129        }
130    }
131
132    public static final class ProxyError extends JingleS5BTransportInfo {
133        public static final ProxyError INSTANCE = new ProxyError();
134
135        public static final String ELEMENT = "proxy-error";
136
137        private ProxyError() {
138        }
139
140        @Override
141        public String getElementName() {
142            return ELEMENT;
143        }
144
145        @Override
146        public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
147            XmlStringBuilder xml = new XmlStringBuilder();
148            xml.halfOpenElement(this);
149            xml.closeEmptyElement();
150            return xml;
151        }
152
153        @Override
154        public boolean equals(Object other) {
155            return other == INSTANCE;
156        }
157
158        @Override
159        public int hashCode() {
160            return toXML().toString().hashCode();
161        }
162    }
163}