1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-11 01:59:38 +02:00

More ground work

This commit is contained in:
vanitasvitae 2017-06-18 16:47:49 +02:00
parent dcb5da76d6
commit b91a9c120f
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
12 changed files with 385 additions and 83 deletions

View file

@ -0,0 +1,49 @@
/**
*
* Copyright 2017 Florian Schmaus, 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;
import org.jxmpp.jid.FullJid;
/**
* Pair of jid and sessionId.
*/
public class FullJidAndSessionId {
private final FullJid fullJid;
private final String sessionId;
public FullJidAndSessionId(FullJid fullJid, String sessionId) {
this.fullJid = fullJid;
this.sessionId = sessionId;
}
@Override
public int hashCode() {
int hashCode = 31 * fullJid.hashCode();
hashCode = 31 * hashCode + sessionId.hashCode();
return hashCode;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof FullJidAndSessionId)) {
return false;
}
FullJidAndSessionId otherFullJidAndSessionId = (FullJidAndSessionId) other;
return fullJid.equals(otherFullJidAndSessionId.fullJid)
&& sessionId.equals(otherFullJidAndSessionId.sessionId);
}
}

View file

@ -27,13 +27,12 @@ import org.jivesoftware.smack.iqrequest.AbstractIqRequestHandler;
import org.jivesoftware.smack.iqrequest.IQRequestHandler.Mode;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
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.jxmpp.jid.FullJid;
import org.jxmpp.jid.Jid;
public final class JingleManager extends Manager {
@ -54,45 +53,47 @@ public final class JingleManager extends Manager {
private final Map<FullJidAndSessionId, JingleSessionHandler> jingleSessionHandlers = new ConcurrentHashMap<>();
private final JingleUtil jutil;
private JingleManager(XMPPConnection connection) {
super(connection);
jutil = new JingleUtil(connection);
connection.registerIQRequestHandler(
new AbstractIqRequestHandler(Jingle.ELEMENT, Jingle.NAMESPACE, Type.set, Mode.async) {
@Override
public IQ handleIQRequest(IQ iqRequest) {
final Jingle jingle = (Jingle) iqRequest;
new AbstractIqRequestHandler(Jingle.ELEMENT, Jingle.NAMESPACE, Type.set, Mode.async) {
@Override
public IQ handleIQRequest(IQ iqRequest) {
final Jingle jingle = (Jingle) iqRequest;
if (jingle.getContents().isEmpty()) {
Jid from = jingle.getFrom();
assert (from != null);
FullJid fullFrom = from.asFullJidOrThrow();
String sid = jingle.getSid();
FullJidAndSessionId fullJidAndSessionId = new FullJidAndSessionId(fullFrom, sid);
JingleSessionHandler jingleSessionHandler = jingleSessionHandlers.get(fullJidAndSessionId);
if (jingleSessionHandler == null) {
// TODO handle non existing jingle session handler.
return null;
}
return jingleSessionHandler.handleJingleSessionRequest(jingle, sid);
}
FullJid fullFrom = jingle.getFrom().asFullJidOrThrow();
String sid = jingle.getSid();
FullJidAndSessionId fullJidAndSessionId = new FullJidAndSessionId(fullFrom, sid);
if (jingle.getContents().size() > 1) {
LOGGER.severe("Jingle IQs with more then one content element are currently not supported by Smack");
return null;
}
JingleSessionHandler sessionHandler = jingleSessionHandlers.get(fullJidAndSessionId);
if (sessionHandler != null) {
//Handle existing session
return sessionHandler.handleJingleSessionRequest(jingle, jingle.getSid());
}
JingleContent content = jingle.getContents().get(0);
JingleContentDescription description = content.getDescription();
JingleHandler jingleDescriptionHandler = descriptionHandlers.get(
description.getNamespace());
if (jingleDescriptionHandler == null) {
// TODO handle non existing content description handler.
return null;
}
return jingleDescriptionHandler.handleJingleRequest(jingle);
if (jingle.getAction() == JingleAction.session_initiate) {
JingleContent content = jingle.getContents().get(0);
JingleContentDescription description = content.getDescription();
JingleHandler jingleDescriptionHandler = descriptionHandlers.get(
description.getNamespace());
if (jingleDescriptionHandler == null) {
//Unsupported Application
return jutil.createSessionTerminateUnsupportedApplications(fullFrom, sid);
}
});
return jingleDescriptionHandler.handleJingleRequest(jingle);
}
//Unknown session
return jutil.createErrorUnknownSession(jingle);
}
});
}
public JingleHandler registerDescriptionHandler(String namespace, JingleHandler handler) {
@ -108,31 +109,4 @@ public final class JingleManager extends Manager {
FullJidAndSessionId fullJidAndSessionId = new FullJidAndSessionId(otherJid, sessionId);
return jingleSessionHandlers.remove(fullJidAndSessionId);
}
private static final class FullJidAndSessionId {
final FullJid fullJid;
final String sessionId;
private FullJidAndSessionId(FullJid fullJid, String sessionId) {
this.fullJid = fullJid;
this.sessionId = sessionId;
}
@Override
public int hashCode() {
int hashCode = 31 * fullJid.hashCode();
hashCode = 31 * hashCode + sessionId.hashCode();
return hashCode;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof FullJidAndSessionId)) {
return false;
}
FullJidAndSessionId otherFullJidAndSessionId = (FullJidAndSessionId) other;
return fullJid.equals(otherFullJidAndSessionId.fullJid)
&& sessionId.equals(otherFullJidAndSessionId.sessionId);
}
}
}

View file

@ -16,28 +16,59 @@
*/
package org.jivesoftware.smackx.jingle;
import org.jxmpp.jid.Jid;
import org.jxmpp.jid.FullJid;
// TODO: Is this class still required? If not, then remove it.
public class JingleSession {
private final Jid initiator;
protected final FullJid local;
private final Jid responder;
protected final FullJid remote;
private final String sid;
protected final Role role;
public JingleSession(Jid initiator, Jid responder, String sid) {
this.initiator = initiator;
this.responder = responder;
protected final String sid;
public JingleSession(FullJid initiator, FullJid responder, Role role, String sid) {
if (role == Role.initiator) {
this.local = initiator;
this.remote = responder;
} else {
this.local = responder;
this.remote = initiator;
}
this.sid = sid;
this.role = role;
}
public FullJid getInitiator() {
return isInitiator() ? local : remote;
}
public boolean isInitiator() {
return role == Role.initiator;
}
public FullJid getResponder() {
return isResponder() ? local : remote;
}
public boolean isResponder() {
return role == Role.responder;
}
public String getSessionId() {
return sid;
}
public FullJidAndSessionId getFullJidAndSessionId() {
return new FullJidAndSessionId(remote, sid);
}
@Override
public int hashCode() {
int hashCode = 31 + initiator.hashCode();
hashCode = 31 * hashCode + responder.hashCode();
hashCode = 31 * hashCode + sid.hashCode();
int hashCode = 31 + getInitiator().hashCode();
hashCode = 31 * hashCode + getResponder().hashCode();
hashCode = 31 * hashCode + getSessionId().hashCode();
return hashCode;
}
@ -48,7 +79,8 @@ public class JingleSession {
}
JingleSession otherJingleSession = (JingleSession) other;
return initiator.equals(otherJingleSession.initiator) && responder.equals(otherJingleSession.responder)
&& sid.equals(otherJingleSession.sid);
return getInitiator().equals(otherJingleSession.getInitiator())
&& getResponder().equals(otherJingleSession.getResponder())
&& sid.equals(otherJingleSession.sid);
}
}

View file

@ -345,7 +345,6 @@ public class JingleUtil {
XMPPError.Builder error = XMPPError.getBuilder();
error.setCondition(XMPPError.Condition.feature_not_implemented)
.addExtension(JingleError.UNSUPPORTED_INFO);
return IQ.createErrorResponse(request, error);
}
@ -354,7 +353,6 @@ public class JingleUtil {
connection.sendStanza(createErrorUnsupportedInfo(request));
}
public IQ createErrorTieBreak(Jingle request) {
XMPPError.Builder error = XMPPError.getBuilder();
error.setCondition(XMPPError.Condition.conflict)

View file

@ -23,7 +23,7 @@ import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smack.util.StringUtils;
import org.junit.Test;
import org.jxmpp.jid.Jid;
import org.jxmpp.jid.FullJid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
@ -34,18 +34,20 @@ public class JingleSessionTest extends SmackTestSuite {
@Test
public void sessionTest() throws XmppStringprepException {
Jid romeo = JidCreate.from("romeo@montague.lit");
Jid juliet = JidCreate.from("juliet@capulet.lit");
FullJid romeo = JidCreate.fullFrom("romeo@montague.lit/r");
FullJid juliet = JidCreate.fullFrom("juliet@capulet.lit/j");
String sid = StringUtils.randomString(24);
JingleSession s1 = new JingleSession(romeo, juliet, sid);
JingleSession s2 = new JingleSession(juliet, romeo, sid);
JingleSession s3 = new JingleSession(romeo, juliet, StringUtils.randomString(23));
JingleSession s4 = new JingleSession(juliet, romeo, sid);
JingleSession s1 = new JingleSession(romeo, juliet, Role.initiator, sid);
JingleSession s2 = new JingleSession(juliet, romeo, Role.responder, sid);
JingleSession s3 = new JingleSession(romeo, juliet, Role.responder, StringUtils.randomString(23));
JingleSession s4 = new JingleSession(juliet, romeo, Role.responder, sid);
JingleSession s5 = new JingleSession(juliet, romeo, Role.initiator, sid);
assertNotSame(s1, s2);
assertNotSame(s1, s3);
assertNotSame(s2, s3);
assertNotSame(s4, s5);
assertEquals(s2, s4);
assertEquals(s2.hashCode(), s4.hashCode());
}