From 708284ca46faa366d880bc883afb1730550754c7 Mon Sep 17 00:00:00 2001 From: Jeff Williams Date: Thu, 19 Jun 2008 18:06:32 +0000 Subject: [PATCH] CS-4346 - Added method to send a stream instead of a file. Also added a way of getting the sid for a transfer. To be reviewed by Gato. Migrated from 3.0.4 git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@10556 b35dd754-fafc-0310-a699-88a17e54d16e --- .../smackx/filetransfer/FileTransfer.java | 8 ++- .../filetransfer/FileTransferManager.java | 20 +++---- .../filetransfer/OutgoingFileTransfer.java | 59 +++++++++++++++++++ 3 files changed, 75 insertions(+), 12 deletions(-) diff --git a/source/org/jivesoftware/smackx/filetransfer/FileTransfer.java b/source/org/jivesoftware/smackx/filetransfer/FileTransfer.java index 9954adf4e..f7c2ad934 100644 --- a/source/org/jivesoftware/smackx/filetransfer/FileTransfer.java +++ b/source/org/jivesoftware/smackx/filetransfer/FileTransfer.java @@ -19,12 +19,12 @@ */ package org.jivesoftware.smackx.filetransfer; +import org.jivesoftware.smack.XMPPException; + import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import org.jivesoftware.smack.XMPPException; - /** * Contains the generic file information and progress related to a particular * file transfer. @@ -175,6 +175,10 @@ public abstract class FileTransfer { return exception; } + public String getStreamID() { + return streamID; + } + /** * Cancels the file transfer. */ diff --git a/source/org/jivesoftware/smackx/filetransfer/FileTransferManager.java b/source/org/jivesoftware/smackx/filetransfer/FileTransferManager.java index 8294fbd6f..ddf30509b 100644 --- a/source/org/jivesoftware/smackx/filetransfer/FileTransferManager.java +++ b/source/org/jivesoftware/smackx/filetransfer/FileTransferManager.java @@ -19,9 +19,6 @@ */ package org.jivesoftware.smackx.filetransfer; -import java.util.ArrayList; -import java.util.List; - import org.jivesoftware.smack.PacketListener; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.filter.AndFilter; @@ -30,9 +27,11 @@ import org.jivesoftware.smack.filter.PacketTypeFilter; import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.XMPPError; -import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smackx.packet.StreamInitiation; +import java.util.ArrayList; +import java.util.List; + /** * The file transfer manager class handles the sending and recieving of files. * To send a file invoke the {@link #createOutgoingFileTransfer(String)} method. @@ -131,12 +130,13 @@ public class FileTransferManager { * @return The send file object on which the negotiated transfer can be run. */ public OutgoingFileTransfer createOutgoingFileTransfer(String userID) { - if (userID == null || StringUtils.parseName(userID).length() <= 0 - || StringUtils.parseServer(userID).length() <= 0 - || StringUtils.parseResource(userID).length() <= 0) { - throw new IllegalArgumentException( - "The provided user id was not fully qualified"); - } +// Why is this only accepting fully qualified JID? +// if (userID == null || StringUtils.parseName(userID).length() <= 0 +// || StringUtils.parseServer(userID).length() <= 0 +// || StringUtils.parseResource(userID).length() <= 0) { +// throw new IllegalArgumentException( +// "The provided user id was not fully qualified"); +// } return new OutgoingFileTransfer(connection.getUser(), userID, fileTransferNegotiator.getNextStreamID(), diff --git a/source/org/jivesoftware/smackx/filetransfer/OutgoingFileTransfer.java b/source/org/jivesoftware/smackx/filetransfer/OutgoingFileTransfer.java index f5ef15293..88822fd86 100644 --- a/source/org/jivesoftware/smackx/filetransfer/OutgoingFileTransfer.java +++ b/source/org/jivesoftware/smackx/filetransfer/OutgoingFileTransfer.java @@ -255,6 +255,65 @@ public class OutgoingFileTransfer extends FileTransfer { transferThread.start(); } + /** + * This method handles the stream negotiation process and transmits the file + * to the remote user. It returns immediatly and the progress of the file + * transfer can be monitored through several methods: + * + * + * + * @param in the stream to transfer to the remote entity. + * @param fileName the name of the file that is transferred + * @param fileSize the size of the file that is transferred + * @param description a description for the file to transfer. + */ + public synchronized void sendStream(final InputStream in, final String fileName, final long fileSize, final String description){ + checkTransferThread(); + + transferThread = new Thread(new Runnable() { + public void run() { + //Create packet filter + try { + outputStream = negotiateStream(fileName, fileSize, description); + } catch (XMPPException e) { + handleXMPPException(e); + return; + } + if (outputStream == null) { + return; + } + + if (!updateStatus(Status.negotiated, Status.in_progress)) { + return; + } + try { + writeToStream(in, outputStream); + } catch (XMPPException e) { + setStatus(FileTransfer.Status.error); + setException(e); + } finally { + try { + if (in != null) { + in.close(); + } + + outputStream.flush(); + outputStream.close(); + } catch (IOException e) { + /* Do Nothing */ + } + } + updateStatus(Status.in_progress, FileTransfer.Status.complete); + } + + }, "File Transfer " + streamID); + transferThread.start(); + } + private void handleXMPPException(XMPPException e) { XMPPError error = e.getXMPPError(); if (error != null) {