From 186dfd224ea9671c9f72ceefca7c49ffc445bde2 Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Wed, 26 Jul 2017 16:17:33 +0200 Subject: [PATCH] Work on JingleFileTransfer --- .../JingleFileTransferChildElement.java | 12 +++---- .../element/JingleFileTransferElement.java | 4 +++ .../smackx/jft/internal/JingleFileOffer.java | 31 ++++------------- .../jft/internal/JingleFileRequest.java | 6 ++++ .../jft/internal/JingleFileTransfer.java | 4 --- .../jft/internal/JingleFileTransferFile.java | 30 +++++++++++++++++ .../jft/internal/JingleIncomingFileOffer.java | 32 ++++++++++++++++++ .../jft/internal/JingleOutgoingFileOffer.java | 20 +++++++++++ .../smackx/jft/internal/LocalFile.java | 33 +++++++++++++++++++ .../smackx/jft/internal/RemoteFile.java | 33 +++++++++++++++++++ 10 files changed, 171 insertions(+), 34 deletions(-) create mode 100644 smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleFileTransferFile.java create mode 100644 smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleIncomingFileOffer.java create mode 100644 smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleOutgoingFileOffer.java create mode 100644 smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/LocalFile.java create mode 100644 smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/RemoteFile.java diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/element/JingleFileTransferChildElement.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/element/JingleFileTransferChildElement.java index f9aed7017..2cbf107e4 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/element/JingleFileTransferChildElement.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/element/JingleFileTransferChildElement.java @@ -39,10 +39,10 @@ public class JingleFileTransferChildElement extends JingleContentDescriptionChil private final HashElement hash; private final String mediaType; private final String name; - private final int size; + private final long size; private final Range range; - public JingleFileTransferChildElement(Date date, String desc, HashElement hash, String mediaType, String name, int size, Range range) { + public JingleFileTransferChildElement(Date date, String desc, HashElement hash, String mediaType, String name, long size, Range range) { this.date = date; this.desc = desc; this.hash = hash; @@ -72,7 +72,7 @@ public class JingleFileTransferChildElement extends JingleContentDescriptionChil return name; } - public int getSize() { + public long getSize() { return size; } @@ -96,7 +96,7 @@ public class JingleFileTransferChildElement extends JingleContentDescriptionChil sb.optElement(ELEM_NAME, name); sb.optElement(range); if (size > 0) { - sb.element(ELEM_SIZE, Integer.toString(size)); + sb.element(ELEM_SIZE, Long.toString(size)); } sb.optElement(hash); sb.closeElement(this); @@ -113,7 +113,7 @@ public class JingleFileTransferChildElement extends JingleContentDescriptionChil private HashElement hash; private String mediaType; private String name; - private int size; + private long size; private Range range; private Builder() { @@ -152,7 +152,7 @@ public class JingleFileTransferChildElement extends JingleContentDescriptionChil return this; } - public Builder setSize(int size) { + public Builder setSize(long size) { this.size = size; return this; } diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/element/JingleFileTransferElement.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/element/JingleFileTransferElement.java index 3cb671e98..f09106310 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/element/JingleFileTransferElement.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/element/JingleFileTransferElement.java @@ -16,6 +16,7 @@ */ package org.jivesoftware.smackx.jft.element; +import java.util.Collections; import java.util.List; import org.jivesoftware.smackx.jft.internal.JingleFileTransfer; @@ -27,6 +28,9 @@ import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionChildEleme */ public class JingleFileTransferElement extends JingleContentDescriptionElement { + public JingleFileTransferElement(JingleContentDescriptionChildElement payload) { + this(Collections.singletonList(payload)); + } public JingleFileTransferElement(List payloads) { super(payloads); diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleFileOffer.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleFileOffer.java index 68427a13e..797e9ec9e 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleFileOffer.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleFileOffer.java @@ -1,38 +1,21 @@ package org.jivesoftware.smackx.jft.internal; -import java.io.File; -import java.io.IOException; -import java.io.OutputStream; -import java.util.logging.Level; -import java.util.logging.Logger; - -import org.jivesoftware.smackx.bytestreams.BytestreamSession; +import org.jivesoftware.smackx.jft.element.JingleFileTransferElement; /** * Created by vanitas on 22.07.17. */ -public class JingleFileOffer extends JingleFileTransfer { +public abstract class JingleFileOffer extends JingleFileTransfer { - private static final Logger LOGGER = Logger.getLogger(JingleFileOffer.class.getName()); + protected D jingleFile; - private File file; - - public JingleFileOffer(File file) { + public JingleFileOffer(D fileTransferFile) { super(); - this.file = file; + this.jingleFile = fileTransferFile; } @Override - public void onTransportReady(BytestreamSession bytestreamSession) { - OutputStream outputStream; - - try { - outputStream = bytestreamSession.getOutputStream(); - } catch (IOException e) { - LOGGER.log(Level.SEVERE, "Error retrieving outputStream: " + e, e); - return; - } - - + public JingleFileTransferElement getElement() { + return new JingleFileTransferElement(jingleFile.getElement()); } } diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleFileRequest.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleFileRequest.java index 75465d757..3c672e940 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleFileRequest.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleFileRequest.java @@ -1,12 +1,18 @@ package org.jivesoftware.smackx.jft.internal; import org.jivesoftware.smackx.bytestreams.BytestreamSession; +import org.jivesoftware.smackx.jft.element.JingleFileTransferElement; /** * Created by vanitas on 22.07.17. */ public class JingleFileRequest extends JingleFileTransfer { + @Override + public JingleFileTransferElement getElement() { + return null; + } + @Override public void onTransportReady(BytestreamSession bytestreamSession) { diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleFileTransfer.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleFileTransfer.java index f52efe9f0..2d2e218a5 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleFileTransfer.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleFileTransfer.java @@ -11,8 +11,4 @@ public abstract class JingleFileTransfer extends JingleDescription { + + private static final Logger LOGGER = Logger.getLogger(JingleIncomingFileOffer.class.getName()); + + public JingleIncomingFileOffer(JingleFileTransferChildElement offer) { + super(new RemoteFile(offer)); + } + + @Override + public void onTransportReady(BytestreamSession bytestreamSession) { + OutputStream outputStream; + try { + outputStream = bytestreamSession.getOutputStream(); + } catch (IOException e) { + LOGGER.log(Level.SEVERE, "Cannot get OutputStream from BytestreamSession: " + e, e); + return; + } + } +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleOutgoingFileOffer.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleOutgoingFileOffer.java new file mode 100644 index 000000000..b69c97731 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/JingleOutgoingFileOffer.java @@ -0,0 +1,20 @@ +package org.jivesoftware.smackx.jft.internal; + +import java.io.File; + +import org.jivesoftware.smackx.bytestreams.BytestreamSession; + +/** + * Created by vanitas on 26.07.17. + */ +public class JingleOutgoingFileOffer extends JingleFileOffer { + + public JingleOutgoingFileOffer(File file) { + super(new LocalFile(file)); + } + + @Override + public void onTransportReady(BytestreamSession bytestreamSession) { + + } +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/LocalFile.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/LocalFile.java new file mode 100644 index 000000000..dbd194671 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/LocalFile.java @@ -0,0 +1,33 @@ +package org.jivesoftware.smackx.jft.internal; + +import java.io.File; +import java.util.Date; + +/** + * Created by vanitas on 26.07.17. + */ +public class LocalFile extends JingleFileTransferFile { + + private File file; + + public LocalFile(File file) { + super(); + this.file = file; + } + + @Override + public Date getDate() { + return new Date(file.lastModified()); + } + + @Override + public long getSize() { + return file.length(); + } + + @Override + public String getName() { + String path = file.getAbsolutePath(); + return path.substring(path.lastIndexOf(File.pathSeparatorChar)+1); + } +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/RemoteFile.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/RemoteFile.java new file mode 100644 index 000000000..b92848466 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jft/internal/RemoteFile.java @@ -0,0 +1,33 @@ +package org.jivesoftware.smackx.jft.internal; + +import java.util.Date; + +import org.jivesoftware.smackx.jft.element.JingleFileTransferChildElement; + +/** + * Created by vanitas on 26.07.17. + */ +public class RemoteFile extends JingleFileTransferFile { + + private JingleFileTransferChildElement file; + + public RemoteFile(JingleFileTransferChildElement file) { + super(); + this.file = file; + } + + @Override + public Date getDate() { + return file.getDate(); + } + + @Override + public long getSize() { + return file.getSize(); + } + + @Override + public String getName() { + return file.getName(); + } +}