From 6b472d0ccd3caede2dd7921e9d9e748bcf8a88af Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Fri, 16 Jun 2017 21:55:42 +0200 Subject: [PATCH] Add JingleUtil --- .../smackx/jingle/JingleUtil.java | 163 ++++++++++++++++++ .../org/jivesoftware/smackx/jingle/Role.java | 23 +++ .../smackx/jingle/element/Jingle.java | 5 + .../smackx/jingle/element/JingleReason.java | 2 +- 4 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/JingleUtil.java create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/Role.java diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/JingleUtil.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/JingleUtil.java new file mode 100644 index 000000000..65ec0ba84 --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/JingleUtil.java @@ -0,0 +1,163 @@ +package org.jivesoftware.smackx.jingle; + +import org.jivesoftware.smack.SmackException; +import org.jivesoftware.smack.XMPPConnection; +import org.jivesoftware.smack.XMPPException; +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smackx.jingle.element.Jingle; +import org.jivesoftware.smackx.jingle.element.JingleAction; +import org.jivesoftware.smackx.jingle.element.JingleContent; +import org.jivesoftware.smackx.jingle.element.JingleContentDescription; +import org.jivesoftware.smackx.jingle.element.JingleContentTransport; +import org.jivesoftware.smackx.jingle.element.JingleReason; +import org.jxmpp.jid.FullJid; + +/** + * Util to quickly send jingle stanzas. + */ +public class JingleUtil { + + private final XMPPConnection connection; + + public JingleUtil(XMPPConnection connection) { + this.connection = connection; + } + + public IQ sendSessionInitiate(FullJid recipient, + String sessionId, + JingleContent.Creator contentCreator, + String contentName, + JingleContent.Senders contentSenders, + JingleContentDescription description, + JingleContentTransport transport) + throws SmackException.NotConnectedException, InterruptedException, + XMPPException.XMPPErrorException, SmackException.NoResponseException { + + Jingle.Builder jb = Jingle.getBuilder(); + jb.setAction(JingleAction.session_initiate) + .setSessionId(sessionId) + .setInitiator(connection.getUser()); + + JingleContent.Builder cb = JingleContent.getBuilder(); + cb.setCreator(contentCreator) + .setName(contentName) + .setSenders(contentSenders) + .setDescription(description) + .addTransport(transport); + + Jingle jingle = jb.addJingleContent(cb.build()).build(); + jingle.setFrom(connection.getUser()); + jingle.setTo(recipient); + + return connection.createStanzaCollectorAndSend(jingle).nextResultOrThrow(); + } + + public IQ sendSessionAccept(FullJid recipient, + String sessionId, + JingleContent.Creator contentCreator, + String contentName, + JingleContent.Senders contentSenders, + JingleContentDescription description, + JingleContentTransport transport) + throws SmackException.NotConnectedException, InterruptedException, + XMPPException.XMPPErrorException, SmackException.NoResponseException { + + Jingle.Builder jb = Jingle.getBuilder(); + jb.setResponder(connection.getUser()) + .setAction(JingleAction.session_accept) + .setSessionId(sessionId); + + JingleContent.Builder cb = JingleContent.getBuilder(); + cb.setCreator(contentCreator) + .setName(contentName) + .setSenders(contentSenders) + .setDescription(description) + .addTransport(transport); + + Jingle jingle = jb.addJingleContent(cb.build()).build(); + jingle.setTo(recipient); + jingle.setFrom(connection.getUser()); + + return connection.createStanzaCollectorAndSend(jingle).nextResultOrThrow(); + } + + private IQ sendSessionTerminate(FullJid recipient, String sessionId, JingleReason.Reason reason) + throws SmackException.NotConnectedException, InterruptedException, + XMPPException.XMPPErrorException, SmackException.NoResponseException { + + return sendSessionTerminate(recipient, sessionId, new JingleReason(reason)); + } + + private IQ sendSessionTerminate(FullJid recipient, String sessionId, JingleReason reason) + throws SmackException.NotConnectedException, InterruptedException, + XMPPException.XMPPErrorException, SmackException.NoResponseException { + + Jingle.Builder jb = Jingle.getBuilder(); + jb.setAction(JingleAction.session_terminate) + .setSessionId(sessionId) + .setReason(reason); + + Jingle jingle = jb.build(); + jingle.setFrom(connection.getUser()); + jingle.setTo(recipient); + + return connection.createStanzaCollectorAndSend(jingle).nextResultOrThrow(); + } + + public IQ sendSessionTerminateDecline(FullJid recipient, + String sessionId) + throws SmackException.NotConnectedException, InterruptedException, + XMPPException.XMPPErrorException, SmackException.NoResponseException { + return sendSessionTerminate(recipient, sessionId, JingleReason.Reason.decline); + } + + public IQ sendSessionTerminateSuccess(FullJid recipient, + String sessionId) + throws InterruptedException, XMPPException.XMPPErrorException, + SmackException.NotConnectedException, SmackException.NoResponseException { + return sendSessionTerminate(recipient, sessionId, JingleReason.Reason.success); + } + + public IQ sendSessionTerminateBusy(FullJid recipient, + String sessionId) + throws InterruptedException, XMPPException.XMPPErrorException, + SmackException.NotConnectedException, SmackException.NoResponseException { + return sendSessionTerminate(recipient, sessionId, JingleReason.Reason.busy); + } + + public IQ sendSessionTerminateAlternativeSession(FullJid recipient, + String sessionId, + String altSessionId) + throws InterruptedException, XMPPException.XMPPErrorException, + SmackException.NotConnectedException, SmackException.NoResponseException { + return sendSessionTerminate(recipient, sessionId, JingleReason.AlternativeSession(altSessionId)); + } + + public IQ sendSessionTerminateCancel(FullJid recipient, + String sessionId) + throws InterruptedException, XMPPException.XMPPErrorException, + SmackException.NotConnectedException, SmackException.NoResponseException { + return sendSessionTerminate(recipient, sessionId, JingleReason.Reason.cancel); + } + + public IQ sendSessionTerminateContentCancel(FullJid recipient, + String sessionId, + JingleContent.Creator contentCreator, + String contentName) + throws SmackException.NotConnectedException, InterruptedException, + XMPPException.XMPPErrorException, SmackException.NoResponseException { + + Jingle.Builder jb = Jingle.getBuilder(); + jb.setAction(JingleAction.session_terminate) + .setSessionId(sessionId); + + JingleContent.Builder cb = JingleContent.getBuilder(); + cb.setCreator(contentCreator).setName(contentName); + + Jingle jingle = jb.addJingleContent(cb.build()).build(); + jingle.setFrom(connection.getUser()); + jingle.setTo(recipient); + + return connection.createStanzaCollectorAndSend(jingle).nextResultOrThrow(); + } +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/Role.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/Role.java new file mode 100644 index 000000000..0473b0894 --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/Role.java @@ -0,0 +1,23 @@ +/** + * + * Copyright 2017 Paul Schaub + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.jingle; + +public enum Role { + initiator, + responder, + ; +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/Jingle.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/Jingle.java index 210874ce3..1738901d8 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/Jingle.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/Jingle.java @@ -198,6 +198,11 @@ public final class Jingle extends IQ { return this; } + public Builder setReason(JingleReason reason) { + this.reason = reason; + return this; + } + public Jingle build() { return new Jingle(sid, action, initiator, responder, reason, contents); } 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 2855ccb8f..a975968b2 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 @@ -104,7 +104,7 @@ public class JingleReason implements NamedElement { protected final Reason reason; - protected JingleReason(Reason reason) { + public JingleReason(Reason reason) { this.reason = reason; }