2006-02-03 19:44:22 +01:00
|
|
|
/**
|
2006-02-03 20:13:23 +01:00
|
|
|
* $RCSfile$
|
|
|
|
* $Revision: $
|
|
|
|
* $Date: $
|
|
|
|
*
|
|
|
|
* Copyright 2003-2006 Jive Software.
|
|
|
|
*
|
|
|
|
* All rights reserved. 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.
|
2006-02-03 19:44:22 +01:00
|
|
|
*/
|
|
|
|
package org.jivesoftware.smackx.filetransfer;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.XMPPException;
|
|
|
|
import org.jivesoftware.smack.packet.XMPPError;
|
|
|
|
|
2006-03-20 18:59:01 +01:00
|
|
|
import java.io.*;
|
|
|
|
|
2006-02-03 19:44:22 +01:00
|
|
|
/**
|
|
|
|
* Handles the sending of a file to another user. File transfer's in jabber have
|
|
|
|
* several steps and there are several methods in this class that handle these
|
|
|
|
* steps differently.
|
2006-03-20 18:59:01 +01:00
|
|
|
*
|
2006-02-03 19:44:22 +01:00
|
|
|
* @author Alexander Wenckus
|
2006-03-20 18:59:01 +01:00
|
|
|
*
|
2006-02-03 19:44:22 +01:00
|
|
|
*/
|
|
|
|
public class OutgoingFileTransfer extends FileTransfer {
|
|
|
|
|
|
|
|
private static int RESPONSE_TIMEOUT = 60 * 1000;
|
2006-12-12 20:47:59 +01:00
|
|
|
private NegotiationProgress callback;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the time in milliseconds after which the file transfer
|
|
|
|
* negotiation process will timeout if the other user has not responded.
|
|
|
|
*
|
|
|
|
* @return Returns the time in milliseconds after which the file transfer
|
|
|
|
* negotiation process will timeout if the remote user has not
|
|
|
|
* responded.
|
|
|
|
*/
|
|
|
|
public static int getResponseTimeout() {
|
|
|
|
return RESPONSE_TIMEOUT;
|
|
|
|
}
|
2006-02-03 19:44:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the time in milliseconds after which the file transfer negotiation
|
|
|
|
* process will timeout if the other user has not responded.
|
2006-03-20 18:59:01 +01:00
|
|
|
*
|
2006-02-03 19:44:22 +01:00
|
|
|
* @param responseTimeout
|
|
|
|
* The timeout time in milliseconds.
|
|
|
|
*/
|
2006-04-15 00:36:42 +02:00
|
|
|
public static void setResponseTimeout(int responseTimeout) {
|
2006-02-03 19:44:22 +01:00
|
|
|
RESPONSE_TIMEOUT = responseTimeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
private OutputStream outputStream;
|
|
|
|
|
|
|
|
private String initiator;
|
|
|
|
|
|
|
|
private Thread transferThread;
|
|
|
|
|
|
|
|
protected OutgoingFileTransfer(String initiator, String target,
|
|
|
|
String streamID, FileTransferNegotiator transferNegotiator) {
|
|
|
|
super(target, streamID, transferNegotiator);
|
|
|
|
this.initiator = initiator;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void setOutputStream(OutputStream stream) {
|
|
|
|
if (outputStream == null) {
|
|
|
|
this.outputStream = stream;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the output stream connected to the peer to transfer the file. It
|
|
|
|
* is only available after it has been succesfully negotiated by the
|
|
|
|
* {@link StreamNegotiator}.
|
2006-03-20 18:59:01 +01:00
|
|
|
*
|
2006-02-03 19:44:22 +01:00
|
|
|
* @return Returns the output stream connected to the peer to transfer the
|
|
|
|
* file.
|
|
|
|
*/
|
|
|
|
protected OutputStream getOutputStream() {
|
2006-09-01 19:38:53 +02:00
|
|
|
if (getStatus().equals(FileTransfer.Status.negotiated)) {
|
2006-02-03 19:44:22 +01:00
|
|
|
return outputStream;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method handles the negotiation of the file transfer and the stream,
|
|
|
|
* it only returns the created stream after the negotiation has been completed.
|
2006-03-20 18:59:01 +01:00
|
|
|
*
|
2006-02-03 19:44:22 +01:00
|
|
|
* @param fileName
|
|
|
|
* The name of the file that will be transmitted. It is
|
|
|
|
* preferable for this name to have an extension as it will be
|
|
|
|
* used to determine the type of file it is.
|
|
|
|
* @param fileSize
|
|
|
|
* The size in bytes of the file that will be transmitted.
|
|
|
|
* @param description
|
|
|
|
* A description of the file that will be transmitted.
|
|
|
|
* @return The OutputStream that is connected to the peer to transmit the
|
|
|
|
* file.
|
|
|
|
* @throws XMPPException
|
|
|
|
* Thrown if an error occurs during the file transfer
|
|
|
|
* negotiation process.
|
|
|
|
*/
|
|
|
|
public synchronized OutputStream sendFile(String fileName, long fileSize,
|
|
|
|
String description) throws XMPPException {
|
|
|
|
if (isDone() || outputStream != null) {
|
|
|
|
throw new IllegalStateException(
|
|
|
|
"The negotation process has already"
|
|
|
|
+ " been attempted on this file transfer");
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
this.outputStream = negotiateStream(fileName, fileSize, description);
|
|
|
|
} catch (XMPPException e) {
|
|
|
|
handleXMPPException(e);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
return outputStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This methods handles the transfer and stream negotiation process. It
|
2006-12-12 20:47:59 +01:00
|
|
|
* returns immediately and its progress will be updated through the
|
|
|
|
* {@link NegotiationProgress} callback.
|
2006-03-20 18:59:01 +01:00
|
|
|
*
|
2006-02-03 19:44:22 +01:00
|
|
|
* @param fileName
|
|
|
|
* The name of the file that will be transmitted. It is
|
|
|
|
* preferable for this name to have an extension as it will be
|
|
|
|
* used to determine the type of file it is.
|
|
|
|
* @param fileSize
|
|
|
|
* The size in bytes of the file that will be transmitted.
|
|
|
|
* @param description
|
|
|
|
* A description of the file that will be transmitted.
|
|
|
|
* @param progress
|
|
|
|
* A callback to monitor the progress of the file transfer
|
|
|
|
* negotiation process and to retrieve the OutputStream when it
|
|
|
|
* is complete.
|
|
|
|
*/
|
|
|
|
public synchronized void sendFile(final String fileName,
|
|
|
|
final long fileSize, final String description,
|
2006-12-12 20:47:59 +01:00
|
|
|
final NegotiationProgress progress)
|
|
|
|
{
|
|
|
|
if(progress == null) {
|
|
|
|
throw new IllegalArgumentException("Callback progress cannot be null.");
|
|
|
|
}
|
|
|
|
checkTransferThread();
|
2006-02-03 19:44:22 +01:00
|
|
|
if (isDone() || outputStream != null) {
|
|
|
|
throw new IllegalStateException(
|
|
|
|
"The negotation process has already"
|
|
|
|
+ " been attempted for this file transfer");
|
|
|
|
}
|
2006-12-12 20:47:59 +01:00
|
|
|
this.callback = progress;
|
|
|
|
transferThread = new Thread(new Runnable() {
|
2006-02-03 19:44:22 +01:00
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
OutgoingFileTransfer.this.outputStream = negotiateStream(
|
|
|
|
fileName, fileSize, description);
|
2006-12-12 20:47:59 +01:00
|
|
|
progress.outputStreamEstablished(OutgoingFileTransfer.this.outputStream);
|
|
|
|
}
|
|
|
|
catch (XMPPException e) {
|
2006-02-03 19:44:22 +01:00
|
|
|
handleXMPPException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, "File Transfer Negotiation " + streamID);
|
|
|
|
transferThread.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void checkTransferThread() {
|
|
|
|
if (transferThread != null && transferThread.isAlive() || isDone()) {
|
|
|
|
throw new IllegalStateException(
|
|
|
|
"File transfer in progress or has already completed.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-12 20:47:59 +01:00
|
|
|
/**
|
2006-02-03 19:44:22 +01:00
|
|
|
* 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:
|
2006-03-20 18:59:01 +01:00
|
|
|
*
|
2006-02-03 19:44:22 +01:00
|
|
|
* <UL>
|
|
|
|
* <LI>{@link FileTransfer#getStatus()}
|
|
|
|
* <LI>{@link FileTransfer#getProgress()}
|
|
|
|
* <LI>{@link FileTransfer#isDone()}
|
|
|
|
* </UL>
|
2006-03-20 18:59:01 +01:00
|
|
|
*
|
2006-12-12 20:47:59 +01:00
|
|
|
* @param file the file to transfer to the remote entity.
|
|
|
|
* @param description a description for the file to transfer.
|
2006-02-03 19:44:22 +01:00
|
|
|
* @throws XMPPException
|
|
|
|
* If there is an error during the negotiation process or the
|
|
|
|
* sending of the file.
|
|
|
|
*/
|
|
|
|
public synchronized void sendFile(final File file, final String description)
|
|
|
|
throws XMPPException {
|
|
|
|
checkTransferThread();
|
|
|
|
if (file == null || !file.exists() || !file.canRead()) {
|
|
|
|
throw new IllegalArgumentException("Could not read file");
|
|
|
|
} else {
|
|
|
|
setFileInfo(file.getAbsolutePath(), file.getName(), file.length());
|
|
|
|
}
|
|
|
|
|
|
|
|
transferThread = new Thread(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
outputStream = negotiateStream(file.getName(), file
|
|
|
|
.length(), description);
|
|
|
|
} catch (XMPPException e) {
|
|
|
|
handleXMPPException(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (outputStream == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-09-01 19:38:53 +02:00
|
|
|
if (!updateStatus(Status.negotiated, Status.in_progress)) {
|
2006-02-03 19:44:22 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
InputStream inputStream = null;
|
|
|
|
try {
|
|
|
|
inputStream = new FileInputStream(file);
|
|
|
|
writeToStream(inputStream, outputStream);
|
|
|
|
} catch (FileNotFoundException e) {
|
2006-09-01 19:38:53 +02:00
|
|
|
setStatus(FileTransfer.Status.error);
|
|
|
|
setError(Error.bad_file);
|
2006-02-03 19:44:22 +01:00
|
|
|
setException(e);
|
|
|
|
} catch (XMPPException e) {
|
2006-09-01 19:38:53 +02:00
|
|
|
setStatus(FileTransfer.Status.error);
|
2006-02-03 19:44:22 +01:00
|
|
|
setException(e);
|
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
if (inputStream != null) {
|
|
|
|
inputStream.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
outputStream.flush();
|
|
|
|
outputStream.close();
|
|
|
|
} catch (IOException e) {
|
2006-03-20 18:59:01 +01:00
|
|
|
/* Do Nothing */
|
2008-06-19 20:06:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
updateStatus(Status.in_progress, FileTransfer.Status.complete);
|
|
|
|
}
|
|
|
|
|
|
|
|
}, "File Transfer " + streamID);
|
|
|
|
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:
|
|
|
|
*
|
|
|
|
* <UL>
|
|
|
|
* <LI>{@link FileTransfer#getStatus()}
|
|
|
|
* <LI>{@link FileTransfer#getProgress()}
|
|
|
|
* <LI>{@link FileTransfer#isDone()}
|
|
|
|
* </UL>
|
|
|
|
*
|
|
|
|
* @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 */
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
|
|
|
}
|
2006-09-01 19:38:53 +02:00
|
|
|
updateStatus(Status.in_progress, FileTransfer.Status.complete);
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}, "File Transfer " + streamID);
|
|
|
|
transferThread.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleXMPPException(XMPPException e) {
|
|
|
|
XMPPError error = e.getXMPPError();
|
|
|
|
if (error != null) {
|
|
|
|
int code = error.getCode();
|
|
|
|
if (code == 403) {
|
2006-09-01 19:38:53 +02:00
|
|
|
setStatus(Status.refused);
|
2006-02-03 19:44:22 +01:00
|
|
|
return;
|
2006-12-12 20:47:59 +01:00
|
|
|
}
|
|
|
|
else if (code == 400) {
|
2006-09-01 19:38:53 +02:00
|
|
|
setStatus(Status.error);
|
|
|
|
setError(Error.not_acceptable);
|
2006-12-12 20:47:59 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
setStatus(FileTransfer.Status.error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setException(e);
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the amount of bytes that have been sent for the file transfer. Or
|
|
|
|
* -1 if the file transfer has not started.
|
|
|
|
* <p>
|
|
|
|
* Note: This method is only useful when the {@link #sendFile(File, String)}
|
|
|
|
* method is called, as it is the only method that actualy transmits the
|
|
|
|
* file.
|
2006-03-20 18:59:01 +01:00
|
|
|
*
|
2006-02-03 19:44:22 +01:00
|
|
|
* @return Returns the amount of bytes that have been sent for the file
|
|
|
|
* transfer. Or -1 if the file transfer has not started.
|
|
|
|
*/
|
|
|
|
public long getBytesSent() {
|
|
|
|
return amountWritten;
|
|
|
|
}
|
|
|
|
|
|
|
|
private OutputStream negotiateStream(String fileName, long fileSize,
|
|
|
|
String description) throws XMPPException {
|
|
|
|
// Negotiate the file transfer profile
|
|
|
|
|
2006-09-01 19:38:53 +02:00
|
|
|
if (!updateStatus(Status.initial, Status.negotiating_transfer)) {
|
2006-03-20 18:59:01 +01:00
|
|
|
throw new XMPPException("Illegal state change");
|
|
|
|
}
|
2006-02-03 19:44:22 +01:00
|
|
|
StreamNegotiator streamNegotiator = negotiator.negotiateOutgoingTransfer(
|
|
|
|
getPeer(), streamID, fileName, fileSize, description,
|
|
|
|
RESPONSE_TIMEOUT);
|
|
|
|
|
|
|
|
if (streamNegotiator == null) {
|
2006-09-01 19:38:53 +02:00
|
|
|
setStatus(Status.error);
|
|
|
|
setError(Error.no_response);
|
2006-02-03 19:44:22 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2006-03-20 18:59:01 +01:00
|
|
|
// Negotiate the stream
|
2006-09-01 19:38:53 +02:00
|
|
|
if (!updateStatus(Status.negotiating_transfer, Status.negotiating_stream)) {
|
2006-03-20 18:59:01 +01:00
|
|
|
throw new XMPPException("Illegal state change");
|
|
|
|
}
|
2006-02-08 01:31:17 +01:00
|
|
|
outputStream = streamNegotiator.createOutgoingStream(streamID,
|
|
|
|
initiator, getPeer());
|
2006-03-20 18:59:01 +01:00
|
|
|
|
2006-09-01 19:38:53 +02:00
|
|
|
if (!updateStatus(Status.negotiating_stream, Status.negotiated)) {
|
2006-03-20 18:59:01 +01:00
|
|
|
throw new XMPPException("Illegal state change");
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
|
|
|
return outputStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void cancel() {
|
2006-09-01 19:38:53 +02:00
|
|
|
setStatus(Status.cancelled);
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
|
|
|
|
2006-12-12 20:47:59 +01:00
|
|
|
@Override
|
|
|
|
protected boolean updateStatus(Status oldStatus, Status newStatus) {
|
|
|
|
boolean isUpdated = super.updateStatus(oldStatus, newStatus);
|
|
|
|
if(callback != null && isUpdated) {
|
|
|
|
callback.statusUpdated(oldStatus, newStatus);
|
|
|
|
}
|
|
|
|
return isUpdated;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void setStatus(Status status) {
|
|
|
|
Status oldStatus = getStatus();
|
|
|
|
super.setStatus(status);
|
|
|
|
if(callback != null) {
|
|
|
|
callback.statusUpdated(oldStatus, status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void setException(Exception exception) {
|
|
|
|
super.setException(exception);
|
|
|
|
if(callback != null) {
|
|
|
|
callback.errorEstablishingStream(exception);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-02-03 19:44:22 +01:00
|
|
|
* A callback class to retrive the status of an outgoing transfer
|
|
|
|
* negotiation process.
|
2006-03-20 18:59:01 +01:00
|
|
|
*
|
2006-02-03 19:44:22 +01:00
|
|
|
* @author Alexander Wenckus
|
2006-03-20 18:59:01 +01:00
|
|
|
*
|
2006-02-03 19:44:22 +01:00
|
|
|
*/
|
2006-12-12 20:47:59 +01:00
|
|
|
public interface NegotiationProgress {
|
2006-02-03 19:44:22 +01:00
|
|
|
|
|
|
|
/**
|
2006-12-12 20:47:59 +01:00
|
|
|
* Called when the status changes
|
|
|
|
*
|
|
|
|
* @param oldStatus the previous status of the file transfer.
|
|
|
|
* @param newStatus the new status of the file transfer.
|
|
|
|
*/
|
|
|
|
void statusUpdated(Status oldStatus, Status newStatus);
|
2006-02-03 19:44:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Once the negotiation process is completed the output stream can be
|
|
|
|
* retrieved.
|
2006-12-12 20:47:59 +01:00
|
|
|
*
|
|
|
|
* @param stream the established stream which can be used to transfer the file to the remote
|
|
|
|
* entity
|
2006-02-03 19:44:22 +01:00
|
|
|
*/
|
2006-12-12 20:47:59 +01:00
|
|
|
void outputStreamEstablished(OutputStream stream);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when an exception occurs during the negotiation progress.
|
|
|
|
*
|
|
|
|
* @param e the exception that occured.
|
|
|
|
*/
|
|
|
|
void errorEstablishingStream(Exception e);
|
|
|
|
}
|
2006-02-03 19:44:22 +01:00
|
|
|
|
|
|
|
}
|