From a604266336fbaf3cccc537e19d30bf419298fe3b Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Wed, 14 Jun 2017 00:00:27 +0200 Subject: [PATCH] Create alternative-session JingleReason --- .../smackx/jingle/element/JingleReason.java | 61 +++++++++++++++++-- .../smackx/jingle/JingleReasonTest.java | 50 +++++++++------ 2 files changed, 89 insertions(+), 22 deletions(-) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleReason.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleReason.java index f2726f7f0..2855ccb8f 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleReason.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleReason.java @@ -20,6 +20,7 @@ import java.util.HashMap; import java.util.Map; import org.jivesoftware.smack.packet.NamedElement; +import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.XmlStringBuilder; /** @@ -32,6 +33,27 @@ public class JingleReason implements NamedElement { public static final String ELEMENT = "reason"; + public static AlternativeSession AlternativeSession(String sessionId) { + return new AlternativeSession(sessionId); + } + + public static final JingleReason Busy = new JingleReason(Reason.busy); + public static final JingleReason Cancel = new JingleReason(Reason.cancel); + public static final JingleReason ConnectivityError = new JingleReason(Reason.connectivity_error); + public static final JingleReason Decline = new JingleReason(Reason.decline); + public static final JingleReason Expired = new JingleReason(Reason.expired); + public static final JingleReason FailedApplication = new JingleReason(Reason.failed_application); + public static final JingleReason FailedTransport = new JingleReason(Reason.failed_transport); + public static final JingleReason GeneralError = new JingleReason(Reason.general_error); + public static final JingleReason Gone = new JingleReason(Reason.gone); + public static final JingleReason IncompatibleParameters = new JingleReason(Reason.incompatible_parameters); + public static final JingleReason MediaError = new JingleReason(Reason.media_error); + public static final JingleReason SecurityError = new JingleReason(Reason.security_error); + public static final JingleReason Success = new JingleReason(Reason.success); + public static final JingleReason Timeout = new JingleReason(Reason.timeout); + public static final JingleReason UnsupportedApplications = new JingleReason(Reason.unsupported_applications); + public static final JingleReason UnsupportedTransports = new JingleReason(Reason.unsupported_transports); + public enum Reason { alternative_session, busy, @@ -52,7 +74,7 @@ public class JingleReason implements NamedElement { unsupported_transports, ; - private static final Map LUT = new HashMap<>(Reason.values().length); + protected static final Map LUT = new HashMap<>(Reason.values().length); static { for (Reason reason : Reason.values()) { @@ -60,9 +82,9 @@ public class JingleReason implements NamedElement { } } - private final String asString; + protected final String asString; - private Reason() { + Reason() { asString = name().replace('_', '-'); } @@ -80,9 +102,9 @@ public class JingleReason implements NamedElement { } } - private final Reason reason; + protected final Reason reason; - public JingleReason(Reason reason) { + protected JingleReason(Reason reason) { this.reason = reason; } @@ -102,4 +124,33 @@ public class JingleReason implements NamedElement { return xml; } + + public static class AlternativeSession extends JingleReason { + + public static final String SID = "sid"; + private final String sessionId; + + public AlternativeSession(String sessionId) { + super(Reason.alternative_session); + if (StringUtils.isNullOrEmpty(sessionId)) { + throw new NullPointerException("SessionID must not be null or empty."); + } + this.sessionId = sessionId; + } + + @Override + public XmlStringBuilder toXML() { + XmlStringBuilder xml = new XmlStringBuilder(this); + xml.rightAngleBracket(); + + xml.openElement(reason.asString); + xml.openElement(SID); + xml.append(sessionId); + xml.closeElement(SID); + xml.closeElement(reason.asString); + + xml.closeElement(this); + return xml; + } + } } diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/JingleReasonTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/JingleReasonTest.java index ffd93ab6d..8950872da 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/JingleReasonTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/JingleReasonTest.java @@ -31,38 +31,54 @@ public class JingleReasonTest extends SmackTestSuite { @Test public void parserTest() { assertEquals("", - new JingleReason(JingleReason.Reason.success).toXML().toString()); + JingleReason.Success.toXML().toString()); assertEquals("", - new JingleReason(JingleReason.Reason.busy).toXML().toString()); + JingleReason.Busy.toXML().toString()); assertEquals("", - new JingleReason(JingleReason.Reason.cancel).toXML().toString()); + JingleReason.Cancel.toXML().toString()); assertEquals("", - new JingleReason(JingleReason.Reason.connectivity_error).toXML().toString()); + JingleReason.ConnectivityError.toXML().toString()); assertEquals("", - new JingleReason(JingleReason.Reason.decline).toXML().toString()); + JingleReason.Decline.toXML().toString()); assertEquals("", - new JingleReason(JingleReason.Reason.expired).toXML().toString()); + JingleReason.Expired.toXML().toString()); assertEquals("", - new JingleReason(JingleReason.Reason.unsupported_transports).toXML().toString()); + JingleReason.UnsupportedTransports.toXML().toString()); assertEquals("", - new JingleReason(JingleReason.Reason.failed_transport).toXML().toString()); + JingleReason.FailedTransport.toXML().toString()); assertEquals("", - new JingleReason(JingleReason.Reason.general_error).toXML().toString()); + JingleReason.GeneralError.toXML().toString()); assertEquals("", - new JingleReason(JingleReason.Reason.gone).toXML().toString()); + JingleReason.Gone.toXML().toString()); assertEquals("", - new JingleReason(JingleReason.Reason.media_error).toXML().toString()); + JingleReason.MediaError.toXML().toString()); assertEquals("", - new JingleReason(JingleReason.Reason.security_error).toXML().toString()); + JingleReason.SecurityError.toXML().toString()); assertEquals("", - new JingleReason(JingleReason.Reason.unsupported_applications).toXML().toString()); + JingleReason.UnsupportedApplications.toXML().toString()); assertEquals("", - new JingleReason(JingleReason.Reason.timeout).toXML().toString()); + JingleReason.Timeout.toXML().toString()); assertEquals("", - new JingleReason(JingleReason.Reason.failed_application).toXML().toString()); + JingleReason.FailedApplication.toXML().toString()); assertEquals("", - new JingleReason(JingleReason.Reason.incompatible_parameters).toXML().toString()); - assertEquals(JingleReason.Reason.alternative_session, JingleReason.Reason.fromString("alternative-session")); + JingleReason.IncompatibleParameters.toXML().toString()); + assertEquals("1234", + JingleReason.AlternativeSession("1234").toXML().toString()); + // Alternative sessionID must not be empty + try { + JingleReason.AlternativeSession(""); + fail(); + } catch (NullPointerException e) { + // Expected + } + // Alternative sessionID must not be null + try { + JingleReason.AlternativeSession(null); + fail(); + } catch (NullPointerException e) { + // Expected + } + try { JingleReason.Reason nonExistent = JingleReason.Reason.fromString("illegal-reason"); fail();