1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-12 15:22:05 +01:00

Progress. Add Checksum element + provider (wip)

This commit is contained in:
vanitasvitae 2017-06-01 16:49:58 +02:00
parent 1a16565697
commit cf9dc49bee
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
6 changed files with 166 additions and 5 deletions

View file

@ -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<XMPPConnection, JingleFileTransferManager> INSTANCES = new WeakHashMap<>();
private final HashSet<IncomingJingleFileTransferListener> 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;
}
}

View file

@ -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 {

View file

@ -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();
}

View file

@ -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;
}
}

View file

@ -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);
}

View file

@ -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<Checksum> {
@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);
}
}