1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-23 20:42:06 +01: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,30 @@
/**
*
* 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_filetransfer;
import org.jivesoftware.smackx.jingle.Role;
import org.jxmpp.jid.FullJid;
/**
* Offer.
*/
public class JingleFileOffer extends JingleFileTransferSession {
public JingleFileOffer(FullJid initiator, FullJid responder, Role role, String sid) {
super(initiator, responder, role, sid, Type.offer);
}
}

View file

@ -0,0 +1,31 @@
/**
*
* 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_filetransfer;
import org.jivesoftware.smackx.jingle.Role;
import org.jxmpp.jid.FullJid;
/**
* Request.
*/
public class JingleFileRequest extends JingleFileTransferSession {
public JingleFileRequest(FullJid initiator, FullJid responder, Role role, String sid) {
super(initiator, responder, role, sid, Type.request);
}
}

View file

@ -20,16 +20,27 @@ import java.util.WeakHashMap;
import org.jivesoftware.smack.Manager; import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.jingle.JingleHandler;
import org.jivesoftware.smackx.jingle.JingleManager;
import org.jivesoftware.smackx.jingle.element.Jingle;
import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransfer;
import org.jxmpp.jid.FullJid;
/** /**
* Manager for JingleFileTransfer (XEP-0234). * Manager for JingleFileTransfer (XEP-0234).
*/ */
public final class JingleFileTransferManager extends Manager { public final class JingleFileTransferManager extends Manager implements JingleHandler {
private static final WeakHashMap<XMPPConnection, JingleFileTransferManager> INSTANCES = new WeakHashMap<>(); private static final WeakHashMap<XMPPConnection, JingleFileTransferManager> INSTANCES = new WeakHashMap<>();
private JingleFileTransferManager(XMPPConnection connection) { private JingleFileTransferManager(XMPPConnection connection) {
super(connection); super(connection);
ServiceDiscoveryManager.getInstanceFor(connection).addFeature(JingleFileTransfer.NAMESPACE_V5);
JingleManager jingleManager = JingleManager.getInstanceFor(connection);
jingleManager.registerDescriptionHandler(JingleFileTransfer.NAMESPACE_V5, this);
} }
public static JingleFileTransferManager getInstanceFor(XMPPConnection connection) { public static JingleFileTransferManager getInstanceFor(XMPPConnection connection) {
@ -40,4 +51,13 @@ public final class JingleFileTransferManager extends Manager {
} }
return manager; return manager;
} }
@Override
public IQ handleJingleRequest(Jingle jingle) {
FullJid fullJid = jingle.getFrom().asFullJidOrThrow();
String sid = jingle.getSid();
JingleFileTransferSessionHandler handler = JingleFileTransferSessionHandler.getInstanceFor(connection());
JingleManager.getInstanceFor(connection()).registerJingleSessionHandler(fullJid, sid, handler);
return handler.handleJingleSessionRequest(jingle, jingle.getSid());
}
} }

View file

@ -0,0 +1,61 @@
/**
*
* 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_filetransfer;
import org.jivesoftware.smackx.jingle.JingleSession;
import org.jivesoftware.smackx.jingle.Role;
import org.jxmpp.jid.FullJid;
/**
* Class representing a Jingle session in the context of Jingle File Transfer (XEP-0234).
*/
public abstract class JingleFileTransferSession extends JingleSession {
public enum Type {
offer,
request,
;
}
private final Type type;
public JingleFileTransferSession(FullJid initiator, FullJid responder, Role role, String sid, Type type) {
super(initiator, responder, role, sid);
this.type = type;
}
public Type getType() {
return type;
}
public boolean isOffer() {
return this.type == Type.offer;
}
public boolean isRequest() {
return this.type == Type.request;
}
public boolean isSender() {
return (isOffer() && isInitiator()) || (isRequest() && isResponder());
}
public boolean isReceiver() {
return (isRequest() && isInitiator()) || (isOffer() && isResponder());
}
}

View file

@ -0,0 +1,56 @@
/**
*
* 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_filetransfer;
import java.util.WeakHashMap;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smackx.jingle.JingleSessionHandler;
import org.jivesoftware.smackx.jingle.JingleUtil;
import org.jivesoftware.smackx.jingle.element.Jingle;
/**
* Handler for JingleFileTransfer sessions.
*/
public final class JingleFileTransferSessionHandler implements JingleSessionHandler {
private static final WeakHashMap<XMPPConnection, JingleFileTransferSessionHandler> INSTANCES = new WeakHashMap<>();
private final XMPPConnection connection;
private final JingleUtil jutil;
private JingleFileTransferSessionHandler(XMPPConnection connection) {
this.connection = connection;
jutil = new JingleUtil(connection);
}
public static JingleFileTransferSessionHandler getInstanceFor(XMPPConnection connection) {
JingleFileTransferSessionHandler handler = INSTANCES.get(connection);
if (handler == null) {
handler = new JingleFileTransferSessionHandler(connection);
INSTANCES.put(connection, handler);
}
return handler;
}
@Override
public IQ handleJingleSessionRequest(Jingle jingle, String sessionId) {
return null;
}
}

View file

@ -0,0 +1,27 @@
/**
*
* 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_filetransfer.listener;
import org.jivesoftware.smackx.jingle.element.Jingle;
/**
* Created by vanitas on 18.06.17.
*/
public interface IncomingFileTransferListener {
void onIncomingJingleFileTransfer(Jingle request);
}

View file

@ -0,0 +1,22 @@
/**
*
* 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.
*/
/**
* Smack's API for <a href="https://xmpp.org/extensions/xep-0234.html">XEP-0234: Jingle File Transfer</a>.
* Listeners.
*/
package org.jivesoftware.smackx.jingle_filetransfer.listener;

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

View file

@ -345,7 +345,6 @@ public class JingleUtil {
XMPPError.Builder error = XMPPError.getBuilder(); XMPPError.Builder error = XMPPError.getBuilder();
error.setCondition(XMPPError.Condition.feature_not_implemented) error.setCondition(XMPPError.Condition.feature_not_implemented)
.addExtension(JingleError.UNSUPPORTED_INFO); .addExtension(JingleError.UNSUPPORTED_INFO);
return IQ.createErrorResponse(request, error); return IQ.createErrorResponse(request, error);
} }
@ -354,7 +353,6 @@ public class JingleUtil {
connection.sendStanza(createErrorUnsupportedInfo(request)); connection.sendStanza(createErrorUnsupportedInfo(request));
} }
public IQ createErrorTieBreak(Jingle request) { public IQ createErrorTieBreak(Jingle request) {
XMPPError.Builder error = XMPPError.getBuilder(); XMPPError.Builder error = XMPPError.getBuilder();
error.setCondition(XMPPError.Condition.conflict) 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.jivesoftware.smack.util.StringUtils;
import org.junit.Test; import org.junit.Test;
import org.jxmpp.jid.Jid; import org.jxmpp.jid.FullJid;
import org.jxmpp.jid.impl.JidCreate; import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException; import org.jxmpp.stringprep.XmppStringprepException;
@ -34,18 +34,20 @@ public class JingleSessionTest extends SmackTestSuite {
@Test @Test
public void sessionTest() throws XmppStringprepException { public void sessionTest() throws XmppStringprepException {
Jid romeo = JidCreate.from("romeo@montague.lit"); FullJid romeo = JidCreate.fullFrom("romeo@montague.lit/r");
Jid juliet = JidCreate.from("juliet@capulet.lit"); FullJid juliet = JidCreate.fullFrom("juliet@capulet.lit/j");
String sid = StringUtils.randomString(24); String sid = StringUtils.randomString(24);
JingleSession s1 = new JingleSession(romeo, juliet, sid); JingleSession s1 = new JingleSession(romeo, juliet, Role.initiator, sid);
JingleSession s2 = new JingleSession(juliet, romeo, sid); JingleSession s2 = new JingleSession(juliet, romeo, Role.responder, sid);
JingleSession s3 = new JingleSession(romeo, juliet, StringUtils.randomString(23)); JingleSession s3 = new JingleSession(romeo, juliet, Role.responder, StringUtils.randomString(23));
JingleSession s4 = new JingleSession(juliet, romeo, sid); JingleSession s4 = new JingleSession(juliet, romeo, Role.responder, sid);
JingleSession s5 = new JingleSession(juliet, romeo, Role.initiator, sid);
assertNotSame(s1, s2); assertNotSame(s1, s2);
assertNotSame(s1, s3); assertNotSame(s1, s3);
assertNotSame(s2, s3); assertNotSame(s2, s3);
assertNotSame(s4, s5);
assertEquals(s2, s4); assertEquals(s2, s4);
assertEquals(s2.hashCode(), s4.hashCode()); assertEquals(s2.hashCode(), s4.hashCode());
} }