diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/JingleFileTransferManager.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/JingleFileTransferManager.java index 94c662f48..7a3fa6bd0 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/JingleFileTransferManager.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/JingleFileTransferManager.java @@ -26,6 +26,7 @@ import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamSession; import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; import org.jivesoftware.smackx.hash.HashManager; import org.jivesoftware.smackx.hash.element.HashElement; +import org.jivesoftware.smackx.jingle.JingleHandler; import org.jivesoftware.smackx.jingle.JingleManager; import org.jivesoftware.smackx.jingle.JingleSessionHandler; import org.jivesoftware.smackx.jingle.element.Jingle; @@ -35,6 +36,7 @@ import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionPayloadTyp import org.jivesoftware.smackx.jingle.provider.JingleContentProviderManager; import org.jivesoftware.smackx.jingle_filetransfer.element.JingleContentDescriptionFileTransfer; import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferPayload; +import org.jivesoftware.smackx.jingle_filetransfer.listener.IncomingJingleFileTransferListener; import org.jivesoftware.smackx.jingle_filetransfer.provider.JingleContentDescriptionFileTransferProvider; import org.jivesoftware.smackx.jingle_ibb.JingleInBandByteStreamManager; import org.jivesoftware.smackx.jingle_ibb.element.JingleInBandByteStreamTransport; @@ -44,6 +46,7 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Date; +import java.util.HashSet; import java.util.WeakHashMap; import java.util.logging.Level; import java.util.logging.Logger; @@ -53,13 +56,14 @@ import java.util.logging.Logger; * * @author Paul Schaub */ -public final class JingleFileTransferManager extends Manager { +public final class JingleFileTransferManager extends Manager implements JingleHandler { private static final Logger LOGGER = Logger.getLogger(JingleFileTransferManager.class.getName()); public static final String NAMESPACE_V5 = "urn:xmpp:jingle:apps:file-transfer:5"; private static final WeakHashMap INSTANCES = new WeakHashMap<>(); + private final HashSet incomingJingleFileTransferListeners = new HashSet<>(); /** * Private constructor. This registers a JingleContentDescriptionFileTransferProvider with the @@ -70,8 +74,13 @@ public final class JingleFileTransferManager extends Manager { super(connection); ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection); sdm.addFeature(NAMESPACE_V5); + + JingleManager.getInstanceFor(connection).registerDescriptionHandler( + NAMESPACE_V5, this); + JingleContentProviderManager.addJingleContentDescriptionProvider( NAMESPACE_V5, new JingleContentDescriptionFileTransferProvider()); + } /** @@ -89,6 +98,14 @@ public final class JingleFileTransferManager extends Manager { return manager; } + public void addIncomingFileTransferListener(IncomingJingleFileTransferListener listener) { + incomingJingleFileTransferListeners.add(listener); + } + + public void removeIncomingFileTransferListener(IncomingJingleFileTransferListener listener) { + incomingJingleFileTransferListeners.remove(listener); + } + public JingleFileTransferPayload createPayloadFromFile(File file) { JingleFileTransferPayload.Builder payloadBuilder = JingleFileTransferPayload.getBuilder(); @@ -157,4 +174,42 @@ public final class JingleFileTransferManager extends Manager { connection().sendStanza(jingle); } + + @Override + public IQ handleRequest(Jingle jingle) { + + for (int i = 0; i < jingle.getContents().size() && i < 1; i++) { //TODO: Remove && i<1 later + JingleContent content = jingle.getContents().get(i); + switch (jingle.getAction()) { + case content_accept: + case content_add: + case content_modify: + case content_reject: + case content_remove: + case description_info: + case session_accept: + case session_info: + case session_initiate: + // File Offer + if (content.getSenders() == JingleContent.Senders.initiator) { + + } + //File Request + else if (content.getSenders() == JingleContent.Senders.responder) { + + } + //Both or none + else { + throw new AssertionError("Undefined (see XEP-0234 ยง4.1)"); + } + break; + case session_terminate: + case transport_accept: + case transport_info: + case transport_reject: + case transport_replace: + } + } + return null; + } } diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/JingleFileTransferSession.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/JingleFileTransferSession.java index 600d0043e..51b5be6d7 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/JingleFileTransferSession.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/JingleFileTransferSession.java @@ -20,7 +20,7 @@ import org.jivesoftware.smackx.jingle.JingleSession; import org.jxmpp.jid.Jid; /** - * Class that represents a jingle session in the context of Jingle File Transfer + * Class that represents a jingle session in the context of Jingle File Transfer. */ public class JingleFileTransferSession extends JingleSession { diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/callback/IncomingJingleFileTransferCallback.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/callback/IncomingJingleFileTransferCallback.java index 9620f1e54..7039ae3a0 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/callback/IncomingJingleFileTransferCallback.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/callback/IncomingJingleFileTransferCallback.java @@ -16,12 +16,14 @@ */ package org.jivesoftware.smackx.jingle_filetransfer.callback; +import java.io.File; + /** * Callback that allows the user to accept or cancel file transfers. */ public interface IncomingJingleFileTransferCallback { - void acceptFileTransfer(); + void acceptFileTransfer(File target); void cancelFileTransfer(); } diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/Checksum.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/Checksum.java new file mode 100644 index 000000000..3a144101a --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/element/Checksum.java @@ -0,0 +1,64 @@ +/** + * + * 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.element; + +import org.jivesoftware.smack.packet.ExtensionElement; +import org.jivesoftware.smack.util.Objects; +import org.jivesoftware.smack.util.XmlStringBuilder; +import org.jivesoftware.smackx.jingle.element.JingleContent; +import org.jivesoftware.smackx.jingle_filetransfer.JingleFileTransferManager; + +/** + * Checksum element. + */ +public class Checksum implements ExtensionElement { + public static final String ELEMENT = "checksum"; + public static final String ATTR_CREATOR = "creator"; + public static final String ATTR_NAME = "name"; + + private final JingleContent.Creator creator; + private final String name; + private JingleFileTransferPayload file; + + public Checksum(JingleContent.Creator creator, String name, JingleFileTransferPayload file) { + this.creator = creator; + this.name = name; + Objects.requireNonNull(file.getHash(), "file MUST contain at least one hash element."); + this.file = Objects.requireNonNull(file, "file MUST NOT be null."); + } + + @Override + public String getElementName() { + return ELEMENT; + } + + @Override + public CharSequence toXML() { + XmlStringBuilder sb = new XmlStringBuilder(this); + sb.optAttribute(ATTR_CREATOR, creator); + sb.optAttribute(ATTR_NAME, name); + sb.rightAngleBracket(); + sb.element(file); + sb.closeElement(this); + return sb; + } + + @Override + public String getNamespace() { + return JingleFileTransferManager.NAMESPACE_V5; + } +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/listener/IncomingJingleFileTransferListener.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/listener/IncomingJingleFileTransferListener.java index 7ec44f0ed..e80ff6b4a 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/listener/IncomingJingleFileTransferListener.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/listener/IncomingJingleFileTransferListener.java @@ -16,13 +16,13 @@ */ package org.jivesoftware.smackx.jingle_filetransfer.listener; +import org.jivesoftware.smackx.jingle.element.Jingle; import org.jivesoftware.smackx.jingle_filetransfer.callback.IncomingJingleFileTransferCallback; -import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferPayload; /** * Listener for incoming file transfers. */ public interface IncomingJingleFileTransferListener { - void onIncomingJingleFileTransfer(JingleFileTransferPayload file, IncomingJingleFileTransferCallback callback); + void onIncomingJingleFileTransfer(Jingle jingle, IncomingJingleFileTransferCallback callback); } diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/provider/ChecksumProvider.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/provider/ChecksumProvider.java new file mode 100644 index 000000000..dfafda608 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/provider/ChecksumProvider.java @@ -0,0 +1,40 @@ +/** + * + * 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.provider; + +import org.jivesoftware.smack.provider.ExtensionElementProvider; +import org.jivesoftware.smackx.jingle.element.JingleContent; +import org.jivesoftware.smackx.jingle_filetransfer.element.Checksum; +import org.xmlpull.v1.XmlPullParser; + +/** + * Provider for the Checksum element. + */ +public class ChecksumProvider extends ExtensionElementProvider { + @Override + public Checksum parse(XmlPullParser parser, int initialDepth) throws Exception { + JingleContent.Creator creator = null; + String creatorString = parser.getAttributeValue(null, Checksum.ATTR_CREATOR); + if (creatorString != null) { + creator = JingleContent.Creator.valueOf(creatorString); + } + String name = parser.getAttributeValue(null, Checksum.ATTR_NAME); + //TODO JingleFileTransferPayload file = new JingleFileTransferPayloadProvider().parse(parser); + + return new Checksum(creator, name, null); + } +}