mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-12-14 06:51:08 +01:00
Enfore spaces for indentation
Although I'm in the tabs camp, Smack uses mostly spaces. Therefore it is the logical choice for the indentation style.
This commit is contained in:
parent
ffe9397e66
commit
ef0af66b21
125 changed files with 5336 additions and 5344 deletions
|
|
@ -31,27 +31,27 @@ import org.jxmpp.jid.Jid;
|
|||
*/
|
||||
public abstract class FileTransfer {
|
||||
|
||||
private String fileName;
|
||||
private String fileName;
|
||||
|
||||
private String filePath;
|
||||
private String filePath;
|
||||
|
||||
private long fileSize;
|
||||
private long fileSize;
|
||||
|
||||
private Jid peer;
|
||||
private Jid peer;
|
||||
|
||||
private Status status = Status.initial;
|
||||
private Status status = Status.initial;
|
||||
|
||||
private final Object statusMonitor = new Object();
|
||||
|
||||
protected FileTransferNegotiator negotiator;
|
||||
protected FileTransferNegotiator negotiator;
|
||||
|
||||
protected String streamID;
|
||||
protected String streamID;
|
||||
|
||||
protected long amountWritten = -1;
|
||||
protected long amountWritten = -1;
|
||||
|
||||
private Error error;
|
||||
private Error error;
|
||||
|
||||
private Exception exception;
|
||||
private Exception exception;
|
||||
|
||||
/**
|
||||
* Buffer size between input and output
|
||||
|
|
@ -59,137 +59,137 @@ public abstract class FileTransfer {
|
|||
private static final int BUFFER_SIZE = 8192;
|
||||
|
||||
protected FileTransfer(Jid peer, String streamID,
|
||||
FileTransferNegotiator negotiator) {
|
||||
this.peer = peer;
|
||||
this.streamID = streamID;
|
||||
this.negotiator = negotiator;
|
||||
}
|
||||
FileTransferNegotiator negotiator) {
|
||||
this.peer = peer;
|
||||
this.streamID = streamID;
|
||||
this.negotiator = negotiator;
|
||||
}
|
||||
|
||||
protected void setFileInfo(String fileName, long fileSize) {
|
||||
this.fileName = fileName;
|
||||
this.fileSize = fileSize;
|
||||
}
|
||||
protected void setFileInfo(String fileName, long fileSize) {
|
||||
this.fileName = fileName;
|
||||
this.fileSize = fileSize;
|
||||
}
|
||||
|
||||
protected void setFileInfo(String path, String fileName, long fileSize) {
|
||||
this.filePath = path;
|
||||
this.fileName = fileName;
|
||||
this.fileSize = fileSize;
|
||||
}
|
||||
protected void setFileInfo(String path, String fileName, long fileSize) {
|
||||
this.filePath = path;
|
||||
this.fileName = fileName;
|
||||
this.fileSize = fileSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the size of the file being transfered.
|
||||
*
|
||||
* @return Returns the size of the file being transfered.
|
||||
*/
|
||||
public long getFileSize() {
|
||||
return fileSize;
|
||||
}
|
||||
/**
|
||||
* Returns the size of the file being transfered.
|
||||
*
|
||||
* @return Returns the size of the file being transfered.
|
||||
*/
|
||||
public long getFileSize() {
|
||||
return fileSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the file being transfered.
|
||||
*
|
||||
* @return Returns the name of the file being transfered.
|
||||
*/
|
||||
public String getFileName() {
|
||||
return fileName;
|
||||
}
|
||||
/**
|
||||
* Returns the name of the file being transfered.
|
||||
*
|
||||
* @return Returns the name of the file being transfered.
|
||||
*/
|
||||
public String getFileName() {
|
||||
return fileName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the local path of the file.
|
||||
*
|
||||
* @return Returns the local path of the file.
|
||||
*/
|
||||
public String getFilePath() {
|
||||
return filePath;
|
||||
}
|
||||
/**
|
||||
* Returns the local path of the file.
|
||||
*
|
||||
* @return Returns the local path of the file.
|
||||
*/
|
||||
public String getFilePath() {
|
||||
return filePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the JID of the peer for this file transfer.
|
||||
*
|
||||
* @return Returns the JID of the peer for this file transfer.
|
||||
*/
|
||||
public Jid getPeer() {
|
||||
return peer;
|
||||
}
|
||||
/**
|
||||
* Returns the JID of the peer for this file transfer.
|
||||
*
|
||||
* @return Returns the JID of the peer for this file transfer.
|
||||
*/
|
||||
public Jid getPeer() {
|
||||
return peer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the progress of the file transfer as a number between 0 and 1.
|
||||
*
|
||||
* @return Returns the progress of the file transfer as a number between 0
|
||||
* and 1.
|
||||
*/
|
||||
public double getProgress() {
|
||||
/**
|
||||
* Returns the progress of the file transfer as a number between 0 and 1.
|
||||
*
|
||||
* @return Returns the progress of the file transfer as a number between 0
|
||||
* and 1.
|
||||
*/
|
||||
public double getProgress() {
|
||||
if (amountWritten <= 0 || fileSize <= 0) {
|
||||
return 0;
|
||||
}
|
||||
return (double) amountWritten / (double) fileSize;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the transfer has been cancelled, if it has stopped because
|
||||
* of a an error, or the transfer completed successfully.
|
||||
*
|
||||
* @return Returns true if the transfer has been cancelled, if it has stopped
|
||||
* because of a an error, or the transfer completed successfully.
|
||||
*/
|
||||
public boolean isDone() {
|
||||
return status == Status.cancelled || status == Status.error
|
||||
|| status == Status.complete || status == Status.refused;
|
||||
}
|
||||
/**
|
||||
* Returns true if the transfer has been cancelled, if it has stopped because
|
||||
* of a an error, or the transfer completed successfully.
|
||||
*
|
||||
* @return Returns true if the transfer has been cancelled, if it has stopped
|
||||
* because of a an error, or the transfer completed successfully.
|
||||
*/
|
||||
public boolean isDone() {
|
||||
return status == Status.cancelled || status == Status.error
|
||||
|| status == Status.complete || status == Status.refused;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current status of the file transfer.
|
||||
*
|
||||
* @return Returns the current status of the file transfer.
|
||||
*/
|
||||
public Status getStatus() {
|
||||
return status;
|
||||
}
|
||||
/**
|
||||
* Returns the current status of the file transfer.
|
||||
*
|
||||
* @return Returns the current status of the file transfer.
|
||||
*/
|
||||
public Status getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
protected void setError(Error type) {
|
||||
this.error = type;
|
||||
}
|
||||
protected void setError(Error type) {
|
||||
this.error = type;
|
||||
}
|
||||
|
||||
/**
|
||||
* When {@link #getStatus()} returns that there was an {@link Status#error}
|
||||
* during the transfer, the type of error can be retrieved through this
|
||||
* method.
|
||||
*
|
||||
* @return Returns the type of error that occurred if one has occurred.
|
||||
*/
|
||||
public Error getError() {
|
||||
return error;
|
||||
}
|
||||
/**
|
||||
* When {@link #getStatus()} returns that there was an {@link Status#error}
|
||||
* during the transfer, the type of error can be retrieved through this
|
||||
* method.
|
||||
*
|
||||
* @return Returns the type of error that occurred if one has occurred.
|
||||
*/
|
||||
public Error getError() {
|
||||
return error;
|
||||
}
|
||||
|
||||
/**
|
||||
* If an exception occurs asynchronously it will be stored for later
|
||||
* retrieval. If there is an error there maybe an exception set.
|
||||
*
|
||||
* @return The exception that occurred or null if there was no exception.
|
||||
* @see #getError()
|
||||
*/
|
||||
public Exception getException() {
|
||||
return exception;
|
||||
}
|
||||
/**
|
||||
* If an exception occurs asynchronously it will be stored for later
|
||||
* retrieval. If there is an error there maybe an exception set.
|
||||
*
|
||||
* @return The exception that occurred or null if there was no exception.
|
||||
* @see #getError()
|
||||
*/
|
||||
public Exception getException() {
|
||||
return exception;
|
||||
}
|
||||
|
||||
public String getStreamID() {
|
||||
return streamID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancels the file transfer.
|
||||
*/
|
||||
public abstract void cancel();
|
||||
/**
|
||||
* Cancels the file transfer.
|
||||
*/
|
||||
public abstract void cancel();
|
||||
|
||||
protected void setException(Exception exception) {
|
||||
this.exception = exception;
|
||||
}
|
||||
protected void setException(Exception exception) {
|
||||
this.exception = exception;
|
||||
}
|
||||
|
||||
protected void setStatus(Status status) {
|
||||
protected void setStatus(Status status) {
|
||||
synchronized (statusMonitor) {
|
||||
// CHECKSTYLE:OFF
|
||||
this.status = status;
|
||||
}
|
||||
this.status = status;
|
||||
}
|
||||
// CHECKSTYLE:ON
|
||||
}
|
||||
|
||||
|
|
@ -206,91 +206,91 @@ public abstract class FileTransfer {
|
|||
protected void writeToStream(final InputStream in, final OutputStream out)
|
||||
throws IOException
|
||||
{
|
||||
final byte[] b = new byte[BUFFER_SIZE];
|
||||
int count = 0;
|
||||
amountWritten = 0;
|
||||
final byte[] b = new byte[BUFFER_SIZE];
|
||||
int count = 0;
|
||||
amountWritten = 0;
|
||||
|
||||
while ((count = in.read(b)) > 0 && !getStatus().equals(Status.cancelled)) {
|
||||
out.write(b, 0, count);
|
||||
amountWritten += count;
|
||||
}
|
||||
|
||||
// the connection was likely terminated abruptly if these are not equal
|
||||
if (!getStatus().equals(Status.cancelled) && getError() == Error.none
|
||||
&& amountWritten != fileSize) {
|
||||
// the connection was likely terminated abruptly if these are not equal
|
||||
if (!getStatus().equals(Status.cancelled) && getError() == Error.none
|
||||
&& amountWritten != fileSize) {
|
||||
setStatus(Status.error);
|
||||
this.error = Error.connection;
|
||||
}
|
||||
}
|
||||
this.error = Error.connection;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A class to represent the current status of the file transfer.
|
||||
*
|
||||
* @author Alexander Wenckus
|
||||
*
|
||||
*/
|
||||
public enum Status {
|
||||
/**
|
||||
* A class to represent the current status of the file transfer.
|
||||
*
|
||||
* @author Alexander Wenckus
|
||||
*
|
||||
*/
|
||||
public enum Status {
|
||||
|
||||
/**
|
||||
* An error occurred during the transfer.
|
||||
*
|
||||
* @see FileTransfer#getError()
|
||||
*/
|
||||
error("Error"),
|
||||
/**
|
||||
* An error occurred during the transfer.
|
||||
*
|
||||
* @see FileTransfer#getError()
|
||||
*/
|
||||
error("Error"),
|
||||
|
||||
/**
|
||||
/**
|
||||
* The initial status of the file transfer.
|
||||
*/
|
||||
initial("Initial"),
|
||||
|
||||
/**
|
||||
* The file transfer is being negotiated with the peer. The party
|
||||
* Receiving the file has the option to accept or refuse a file transfer
|
||||
* request. If they accept, then the process of stream negotiation will
|
||||
* begin. If they refuse the file will not be transfered.
|
||||
*
|
||||
* @see #negotiating_stream
|
||||
*/
|
||||
negotiating_transfer("Negotiating Transfer"),
|
||||
* The file transfer is being negotiated with the peer. The party
|
||||
* Receiving the file has the option to accept or refuse a file transfer
|
||||
* request. If they accept, then the process of stream negotiation will
|
||||
* begin. If they refuse the file will not be transfered.
|
||||
*
|
||||
* @see #negotiating_stream
|
||||
*/
|
||||
negotiating_transfer("Negotiating Transfer"),
|
||||
|
||||
/**
|
||||
* The peer has refused the file transfer request halting the file
|
||||
* transfer negotiation process.
|
||||
*/
|
||||
refused("Refused"),
|
||||
/**
|
||||
* The peer has refused the file transfer request halting the file
|
||||
* transfer negotiation process.
|
||||
*/
|
||||
refused("Refused"),
|
||||
|
||||
/**
|
||||
* The stream to transfer the file is being negotiated over the chosen
|
||||
* stream type. After the stream negotiating process is complete the
|
||||
* status becomes negotiated.
|
||||
*
|
||||
* @see #negotiated
|
||||
*/
|
||||
negotiating_stream("Negotiating Stream"),
|
||||
/**
|
||||
* The stream to transfer the file is being negotiated over the chosen
|
||||
* stream type. After the stream negotiating process is complete the
|
||||
* status becomes negotiated.
|
||||
*
|
||||
* @see #negotiated
|
||||
*/
|
||||
negotiating_stream("Negotiating Stream"),
|
||||
|
||||
/**
|
||||
* After the stream negotiation has completed the intermediate state
|
||||
* between the time when the negotiation is finished and the actual
|
||||
* transfer begins.
|
||||
*/
|
||||
negotiated("Negotiated"),
|
||||
/**
|
||||
* After the stream negotiation has completed the intermediate state
|
||||
* between the time when the negotiation is finished and the actual
|
||||
* transfer begins.
|
||||
*/
|
||||
negotiated("Negotiated"),
|
||||
|
||||
/**
|
||||
* The transfer is in progress.
|
||||
*
|
||||
* @see FileTransfer#getProgress()
|
||||
*/
|
||||
in_progress("In Progress"),
|
||||
/**
|
||||
* The transfer is in progress.
|
||||
*
|
||||
* @see FileTransfer#getProgress()
|
||||
*/
|
||||
in_progress("In Progress"),
|
||||
|
||||
/**
|
||||
* The transfer has completed successfully.
|
||||
*/
|
||||
complete("Complete"),
|
||||
/**
|
||||
* The transfer has completed successfully.
|
||||
*/
|
||||
complete("Complete"),
|
||||
|
||||
/**
|
||||
* The file transfer was cancelled.
|
||||
*/
|
||||
cancelled("Cancelled");
|
||||
/**
|
||||
* The file transfer was cancelled.
|
||||
*/
|
||||
cancelled("Cancelled");
|
||||
|
||||
private String status;
|
||||
|
||||
|
|
@ -312,55 +312,55 @@ public abstract class FileTransfer {
|
|||
}
|
||||
|
||||
public enum Error {
|
||||
/**
|
||||
* No error.
|
||||
*/
|
||||
none("No error"),
|
||||
/**
|
||||
* No error.
|
||||
*/
|
||||
none("No error"),
|
||||
|
||||
/**
|
||||
* The peer did not find any of the provided stream mechanisms
|
||||
* acceptable.
|
||||
*/
|
||||
not_acceptable("The peer did not find any of the provided stream mechanisms acceptable."),
|
||||
/**
|
||||
* The peer did not find any of the provided stream mechanisms
|
||||
* acceptable.
|
||||
*/
|
||||
not_acceptable("The peer did not find any of the provided stream mechanisms acceptable."),
|
||||
|
||||
/**
|
||||
* The provided file to transfer does not exist or could not be read.
|
||||
*/
|
||||
bad_file("The provided file to transfer does not exist or could not be read."),
|
||||
/**
|
||||
* The provided file to transfer does not exist or could not be read.
|
||||
*/
|
||||
bad_file("The provided file to transfer does not exist or could not be read."),
|
||||
|
||||
/**
|
||||
* The remote user did not respond or the connection timed out.
|
||||
*/
|
||||
no_response("The remote user did not respond or the connection timed out."),
|
||||
/**
|
||||
* The remote user did not respond or the connection timed out.
|
||||
*/
|
||||
no_response("The remote user did not respond or the connection timed out."),
|
||||
|
||||
/**
|
||||
* An error occurred over the socket connected to send the file.
|
||||
*/
|
||||
connection("An error occured over the socket connected to send the file."),
|
||||
/**
|
||||
* An error occurred over the socket connected to send the file.
|
||||
*/
|
||||
connection("An error occured over the socket connected to send the file."),
|
||||
|
||||
/**
|
||||
* An error occurred while sending or receiving the file.
|
||||
*/
|
||||
stream("An error occured while sending or recieving the file.");
|
||||
/**
|
||||
* An error occurred while sending or receiving the file.
|
||||
*/
|
||||
stream("An error occured while sending or recieving the file.");
|
||||
|
||||
private final String msg;
|
||||
private final String msg;
|
||||
|
||||
private Error(String msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
private Error(String msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a String representation of this error.
|
||||
*
|
||||
* @return Returns a String representation of this error.
|
||||
*/
|
||||
public String getMessage() {
|
||||
return msg;
|
||||
}
|
||||
/**
|
||||
* Returns a String representation of this error.
|
||||
*
|
||||
* @return Returns a String representation of this error.
|
||||
*/
|
||||
public String getMessage() {
|
||||
return msg;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
public String toString() {
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,11 +23,11 @@ package org.jivesoftware.smackx.filetransfer;
|
|||
* @author Alexander Wenckus
|
||||
*/
|
||||
public interface FileTransferListener {
|
||||
/**
|
||||
* A request to send a file has been recieved from another user.
|
||||
*
|
||||
* @param request
|
||||
* The request from the other user.
|
||||
*/
|
||||
public void fileTransferRequest(final FileTransferRequest request);
|
||||
/**
|
||||
* A request to send a file has been recieved from another user.
|
||||
*
|
||||
* @param request
|
||||
* The request from the other user.
|
||||
*/
|
||||
public void fileTransferRequest(final FileTransferRequest request);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,20 +56,20 @@ public final class FileTransferManager extends Manager {
|
|||
return fileTransferManager;
|
||||
}
|
||||
|
||||
private final FileTransferNegotiator fileTransferNegotiator;
|
||||
private final FileTransferNegotiator fileTransferNegotiator;
|
||||
|
||||
private final List<FileTransferListener> listeners = new CopyOnWriteArrayList<FileTransferListener>();
|
||||
private final List<FileTransferListener> listeners = new CopyOnWriteArrayList<FileTransferListener>();
|
||||
|
||||
/**
|
||||
* Creates a file transfer manager to initiate and receive file transfers.
|
||||
*
|
||||
* @param connection
|
||||
* The XMPPConnection that the file transfers will use.
|
||||
*/
|
||||
private FileTransferManager(XMPPConnection connection) {
|
||||
super(connection);
|
||||
this.fileTransferNegotiator = FileTransferNegotiator
|
||||
.getInstanceFor(connection);
|
||||
/**
|
||||
* Creates a file transfer manager to initiate and receive file transfers.
|
||||
*
|
||||
* @param connection
|
||||
* The XMPPConnection that the file transfers will use.
|
||||
*/
|
||||
private FileTransferManager(XMPPConnection connection) {
|
||||
super(connection);
|
||||
this.fileTransferNegotiator = FileTransferNegotiator
|
||||
.getInstanceFor(connection);
|
||||
connection.registerIQRequestHandler(new AbstractIqRequestHandler(StreamInitiation.ELEMENT,
|
||||
StreamInitiation.NAMESPACE, IQ.Type.set, Mode.async) {
|
||||
@Override
|
||||
|
|
@ -82,42 +82,42 @@ public final class FileTransferManager extends Manager {
|
|||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a file transfer listener to listen to incoming file transfer
|
||||
* requests.
|
||||
*
|
||||
* @param li
|
||||
* The listener
|
||||
* @see #removeFileTransferListener(FileTransferListener)
|
||||
* @see FileTransferListener
|
||||
*/
|
||||
public void addFileTransferListener(final FileTransferListener li) {
|
||||
listeners.add(li);
|
||||
}
|
||||
/**
|
||||
* Add a file transfer listener to listen to incoming file transfer
|
||||
* requests.
|
||||
*
|
||||
* @param li
|
||||
* The listener
|
||||
* @see #removeFileTransferListener(FileTransferListener)
|
||||
* @see FileTransferListener
|
||||
*/
|
||||
public void addFileTransferListener(final FileTransferListener li) {
|
||||
listeners.add(li);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a file transfer listener.
|
||||
*
|
||||
* @param li
|
||||
* The file transfer listener to be removed
|
||||
* @see FileTransferListener
|
||||
*/
|
||||
public void removeFileTransferListener(final FileTransferListener li) {
|
||||
listeners.remove(li);
|
||||
}
|
||||
/**
|
||||
* Removes a file transfer listener.
|
||||
*
|
||||
* @param li
|
||||
* The file transfer listener to be removed
|
||||
* @see FileTransferListener
|
||||
*/
|
||||
public void removeFileTransferListener(final FileTransferListener li) {
|
||||
listeners.remove(li);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an OutgoingFileTransfer to send a file to another user.
|
||||
*
|
||||
* @param userID
|
||||
* The fully qualified jabber ID (i.e. full JID) with resource of the user to
|
||||
* send the file to.
|
||||
* @return The send file object on which the negotiated transfer can be run.
|
||||
* @exception IllegalArgumentException if userID is null or not a full JID
|
||||
*/
|
||||
public OutgoingFileTransfer createOutgoingFileTransfer(EntityFullJid userID) {
|
||||
/**
|
||||
* Creates an OutgoingFileTransfer to send a file to another user.
|
||||
*
|
||||
* @param userID
|
||||
* The fully qualified jabber ID (i.e. full JID) with resource of the user to
|
||||
* send the file to.
|
||||
* @return The send file object on which the negotiated transfer can be run.
|
||||
* @exception IllegalArgumentException if userID is null or not a full JID
|
||||
*/
|
||||
public OutgoingFileTransfer createOutgoingFileTransfer(EntityFullJid userID) {
|
||||
// We need to create outgoing file transfers with a full JID since this method will later
|
||||
// use XEP-0095 to negotiate the stream. This is done with IQ stanzas that need to be addressed to a full JID
|
||||
// in order to reach an client entity.
|
||||
|
|
@ -125,45 +125,45 @@ public final class FileTransferManager extends Manager {
|
|||
throw new IllegalArgumentException("userID was null");
|
||||
}
|
||||
|
||||
return new OutgoingFileTransfer(connection().getUser(), userID,
|
||||
FileTransferNegotiator.getNextStreamID(),
|
||||
fileTransferNegotiator);
|
||||
}
|
||||
|
||||
/**
|
||||
* When the file transfer request is acceptable, this method should be
|
||||
* invoked. It will create an IncomingFileTransfer which allows the
|
||||
* transmission of the file to procede.
|
||||
*
|
||||
* @param request
|
||||
* The remote request that is being accepted.
|
||||
* @return The IncomingFileTransfer which manages the download of the file
|
||||
* from the transfer initiator.
|
||||
*/
|
||||
protected IncomingFileTransfer createIncomingFileTransfer(
|
||||
FileTransferRequest request) {
|
||||
if (request == null) {
|
||||
throw new NullPointerException("RecieveRequest cannot be null");
|
||||
}
|
||||
|
||||
IncomingFileTransfer transfer = new IncomingFileTransfer(request,
|
||||
return new OutgoingFileTransfer(connection().getUser(), userID,
|
||||
FileTransferNegotiator.getNextStreamID(),
|
||||
fileTransferNegotiator);
|
||||
transfer.setFileInfo(request.getFileName(), request.getFileSize());
|
||||
}
|
||||
|
||||
return transfer;
|
||||
}
|
||||
/**
|
||||
* When the file transfer request is acceptable, this method should be
|
||||
* invoked. It will create an IncomingFileTransfer which allows the
|
||||
* transmission of the file to procede.
|
||||
*
|
||||
* @param request
|
||||
* The remote request that is being accepted.
|
||||
* @return The IncomingFileTransfer which manages the download of the file
|
||||
* from the transfer initiator.
|
||||
*/
|
||||
protected IncomingFileTransfer createIncomingFileTransfer(
|
||||
FileTransferRequest request) {
|
||||
if (request == null) {
|
||||
throw new NullPointerException("RecieveRequest cannot be null");
|
||||
}
|
||||
|
||||
/**
|
||||
* Reject an incoming file transfer.
|
||||
* <p>
|
||||
* Specified in XEP-95 4.2 and 3.2 Example 8
|
||||
* </p>
|
||||
* @param request
|
||||
* @throws NotConnectedException
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
protected void rejectIncomingFileTransfer(FileTransferRequest request) throws NotConnectedException, InterruptedException {
|
||||
StreamInitiation initiation = request.getStreamInitiation();
|
||||
IncomingFileTransfer transfer = new IncomingFileTransfer(request,
|
||||
fileTransferNegotiator);
|
||||
transfer.setFileInfo(request.getFileName(), request.getFileSize());
|
||||
|
||||
return transfer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reject an incoming file transfer.
|
||||
* <p>
|
||||
* Specified in XEP-95 4.2 and 3.2 Example 8
|
||||
* </p>
|
||||
* @param request
|
||||
* @throws NotConnectedException
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
protected void rejectIncomingFileTransfer(FileTransferRequest request) throws NotConnectedException, InterruptedException {
|
||||
StreamInitiation initiation = request.getStreamInitiation();
|
||||
|
||||
// Reject as specified in XEP-95 4.2. Note that this is not to be confused with the Socks 5
|
||||
// Bytestream rejection as specified in XEP-65 5.3.1 Example 13, which says that
|
||||
|
|
@ -172,5 +172,5 @@ public final class FileTransferManager extends Manager {
|
|||
IQ rejection = IQ.createErrorResponse(initiation, XMPPError.getBuilder(
|
||||
XMPPError.Condition.forbidden));
|
||||
connection().sendStanza(rejection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,113 +27,113 @@ import org.jxmpp.jid.Jid;
|
|||
*
|
||||
*/
|
||||
public class FileTransferRequest {
|
||||
private final StreamInitiation streamInitiation;
|
||||
private final StreamInitiation streamInitiation;
|
||||
|
||||
private final FileTransferManager manager;
|
||||
private final FileTransferManager manager;
|
||||
|
||||
/**
|
||||
* A recieve request is constructed from the Stream Initiation request
|
||||
* received from the initator.
|
||||
*
|
||||
* @param manager
|
||||
* The manager handling this file transfer
|
||||
*
|
||||
* @param si
|
||||
* The Stream initiaton recieved from the initiator.
|
||||
*/
|
||||
public FileTransferRequest(FileTransferManager manager, StreamInitiation si) {
|
||||
this.streamInitiation = si;
|
||||
this.manager = manager;
|
||||
}
|
||||
/**
|
||||
* A recieve request is constructed from the Stream Initiation request
|
||||
* received from the initator.
|
||||
*
|
||||
* @param manager
|
||||
* The manager handling this file transfer
|
||||
*
|
||||
* @param si
|
||||
* The Stream initiaton recieved from the initiator.
|
||||
*/
|
||||
public FileTransferRequest(FileTransferManager manager, StreamInitiation si) {
|
||||
this.streamInitiation = si;
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the file.
|
||||
*
|
||||
* @return Returns the name of the file.
|
||||
*/
|
||||
public String getFileName() {
|
||||
return streamInitiation.getFile().getName();
|
||||
}
|
||||
/**
|
||||
* Returns the name of the file.
|
||||
*
|
||||
* @return Returns the name of the file.
|
||||
*/
|
||||
public String getFileName() {
|
||||
return streamInitiation.getFile().getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the size in bytes of the file.
|
||||
*
|
||||
* @return Returns the size in bytes of the file.
|
||||
*/
|
||||
public long getFileSize() {
|
||||
return streamInitiation.getFile().getSize();
|
||||
}
|
||||
/**
|
||||
* Returns the size in bytes of the file.
|
||||
*
|
||||
* @return Returns the size in bytes of the file.
|
||||
*/
|
||||
public long getFileSize() {
|
||||
return streamInitiation.getFile().getSize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the description of the file provided by the requestor.
|
||||
*
|
||||
* @return Returns the description of the file provided by the requestor.
|
||||
*/
|
||||
public String getDescription() {
|
||||
return streamInitiation.getFile().getDesc();
|
||||
}
|
||||
/**
|
||||
* Returns the description of the file provided by the requestor.
|
||||
*
|
||||
* @return Returns the description of the file provided by the requestor.
|
||||
*/
|
||||
public String getDescription() {
|
||||
return streamInitiation.getFile().getDesc();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the mime-type of the file.
|
||||
*
|
||||
* @return Returns the mime-type of the file.
|
||||
*/
|
||||
public String getMimeType() {
|
||||
return streamInitiation.getMimeType();
|
||||
}
|
||||
/**
|
||||
* Returns the mime-type of the file.
|
||||
*
|
||||
* @return Returns the mime-type of the file.
|
||||
*/
|
||||
public String getMimeType() {
|
||||
return streamInitiation.getMimeType();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the fully-qualified jabber ID of the user that requested this
|
||||
* file transfer.
|
||||
*
|
||||
* @return Returns the fully-qualified jabber ID of the user that requested
|
||||
* this file transfer.
|
||||
*/
|
||||
public Jid getRequestor() {
|
||||
return streamInitiation.getFrom();
|
||||
}
|
||||
/**
|
||||
* Returns the fully-qualified jabber ID of the user that requested this
|
||||
* file transfer.
|
||||
*
|
||||
* @return Returns the fully-qualified jabber ID of the user that requested
|
||||
* this file transfer.
|
||||
*/
|
||||
public Jid getRequestor() {
|
||||
return streamInitiation.getFrom();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the stream ID that uniquely identifies this file transfer.
|
||||
*
|
||||
* @return Returns the stream ID that uniquely identifies this file
|
||||
* transfer.
|
||||
*/
|
||||
public String getStreamID() {
|
||||
return streamInitiation.getSessionID();
|
||||
}
|
||||
/**
|
||||
* Returns the stream ID that uniquely identifies this file transfer.
|
||||
*
|
||||
* @return Returns the stream ID that uniquely identifies this file
|
||||
* transfer.
|
||||
*/
|
||||
public String getStreamID() {
|
||||
return streamInitiation.getSessionID();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the stream initiation stanza(/packet) that was sent by the requestor which
|
||||
* contains the parameters of the file transfer being transfer and also the
|
||||
* methods available to transfer the file.
|
||||
*
|
||||
* @return Returns the stream initiation stanza(/packet) that was sent by the
|
||||
* requestor which contains the parameters of the file transfer
|
||||
* being transfer and also the methods available to transfer the
|
||||
* file.
|
||||
*/
|
||||
protected StreamInitiation getStreamInitiation() {
|
||||
return streamInitiation;
|
||||
}
|
||||
/**
|
||||
* Returns the stream initiation stanza(/packet) that was sent by the requestor which
|
||||
* contains the parameters of the file transfer being transfer and also the
|
||||
* methods available to transfer the file.
|
||||
*
|
||||
* @return Returns the stream initiation stanza(/packet) that was sent by the
|
||||
* requestor which contains the parameters of the file transfer
|
||||
* being transfer and also the methods available to transfer the
|
||||
* file.
|
||||
*/
|
||||
protected StreamInitiation getStreamInitiation() {
|
||||
return streamInitiation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Accepts this file transfer and creates the incoming file transfer.
|
||||
*
|
||||
* @return Returns the <b><i>IncomingFileTransfer</b></i> on which the
|
||||
* file transfer can be carried out.
|
||||
*/
|
||||
public IncomingFileTransfer accept() {
|
||||
return manager.createIncomingFileTransfer(this);
|
||||
}
|
||||
/**
|
||||
* Accepts this file transfer and creates the incoming file transfer.
|
||||
*
|
||||
* @return Returns the <b><i>IncomingFileTransfer</b></i> on which the
|
||||
* file transfer can be carried out.
|
||||
*/
|
||||
public IncomingFileTransfer accept() {
|
||||
return manager.createIncomingFileTransfer(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rejects the file transfer request.
|
||||
* @throws NotConnectedException
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
public void reject() throws NotConnectedException, InterruptedException {
|
||||
manager.rejectIncomingFileTransfer(this);
|
||||
}
|
||||
/**
|
||||
* Rejects the file transfer request.
|
||||
* @throws NotConnectedException
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
public void reject() throws NotConnectedException, InterruptedException {
|
||||
manager.rejectIncomingFileTransfer(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ import org.jxmpp.jid.Jid;
|
|||
public class OutgoingFileTransfer extends FileTransfer {
|
||||
private static final Logger LOGGER = Logger.getLogger(OutgoingFileTransfer.class.getName());
|
||||
|
||||
private static int RESPONSE_TIMEOUT = 60 * 1000;
|
||||
private static int RESPONSE_TIMEOUT = 60 * 1000;
|
||||
private NegotiationProgress callback;
|
||||
|
||||
/**
|
||||
|
|
@ -58,349 +58,349 @@ public class OutgoingFileTransfer extends FileTransfer {
|
|||
return RESPONSE_TIMEOUT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the time in milliseconds after which the file transfer negotiation
|
||||
* process will timeout if the other user has not responded.
|
||||
*
|
||||
* @param responseTimeout
|
||||
* The timeout time in milliseconds.
|
||||
*/
|
||||
public static void setResponseTimeout(int responseTimeout) {
|
||||
RESPONSE_TIMEOUT = responseTimeout;
|
||||
}
|
||||
/**
|
||||
* Sets the time in milliseconds after which the file transfer negotiation
|
||||
* process will timeout if the other user has not responded.
|
||||
*
|
||||
* @param responseTimeout
|
||||
* The timeout time in milliseconds.
|
||||
*/
|
||||
public static void setResponseTimeout(int responseTimeout) {
|
||||
RESPONSE_TIMEOUT = responseTimeout;
|
||||
}
|
||||
|
||||
private OutputStream outputStream;
|
||||
private OutputStream outputStream;
|
||||
|
||||
private Jid initiator;
|
||||
private Jid initiator;
|
||||
|
||||
private Thread transferThread;
|
||||
private Thread transferThread;
|
||||
|
||||
protected OutgoingFileTransfer(Jid initiator, Jid target,
|
||||
String streamID, FileTransferNegotiator transferNegotiator) {
|
||||
super(target, streamID, transferNegotiator);
|
||||
this.initiator = initiator;
|
||||
}
|
||||
protected OutgoingFileTransfer(Jid initiator, Jid target,
|
||||
String streamID, FileTransferNegotiator transferNegotiator) {
|
||||
super(target, streamID, transferNegotiator);
|
||||
this.initiator = initiator;
|
||||
}
|
||||
|
||||
protected void setOutputStream(OutputStream stream) {
|
||||
if (outputStream == null) {
|
||||
this.outputStream = stream;
|
||||
}
|
||||
}
|
||||
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 successfully negotiated by the
|
||||
* {@link StreamNegotiator}.
|
||||
*
|
||||
* @return Returns the output stream connected to the peer to transfer the
|
||||
* file.
|
||||
*/
|
||||
protected OutputStream getOutputStream() {
|
||||
if (getStatus().equals(FileTransfer.Status.negotiated)) {
|
||||
return outputStream;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Returns the output stream connected to the peer to transfer the file. It
|
||||
* is only available after it has been successfully negotiated by the
|
||||
* {@link StreamNegotiator}.
|
||||
*
|
||||
* @return Returns the output stream connected to the peer to transfer the
|
||||
* file.
|
||||
*/
|
||||
protected OutputStream getOutputStream() {
|
||||
if (getStatus().equals(FileTransfer.Status.negotiated)) {
|
||||
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.
|
||||
*
|
||||
* @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.
|
||||
* @throws SmackException if there was no response from the server.
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
public synchronized OutputStream sendFile(String fileName, long fileSize,
|
||||
String description) throws XMPPException, SmackException, InterruptedException {
|
||||
if (isDone() || outputStream != null) {
|
||||
throw new IllegalStateException(
|
||||
"The negotation process has already"
|
||||
+ " been attempted on this file transfer");
|
||||
}
|
||||
try {
|
||||
setFileInfo(fileName, fileSize);
|
||||
this.outputStream = negotiateStream(fileName, fileSize, description);
|
||||
} catch (XMPPErrorException e) {
|
||||
handleXMPPException(e);
|
||||
throw e;
|
||||
}
|
||||
return outputStream;
|
||||
}
|
||||
/**
|
||||
* This method handles the negotiation of the file transfer and the stream,
|
||||
* it only returns the created stream after the negotiation has been completed.
|
||||
*
|
||||
* @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.
|
||||
* @throws SmackException if there was no response from the server.
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
public synchronized OutputStream sendFile(String fileName, long fileSize,
|
||||
String description) throws XMPPException, SmackException, InterruptedException {
|
||||
if (isDone() || outputStream != null) {
|
||||
throw new IllegalStateException(
|
||||
"The negotation process has already"
|
||||
+ " been attempted on this file transfer");
|
||||
}
|
||||
try {
|
||||
setFileInfo(fileName, fileSize);
|
||||
this.outputStream = negotiateStream(fileName, fileSize, description);
|
||||
} catch (XMPPErrorException e) {
|
||||
handleXMPPException(e);
|
||||
throw e;
|
||||
}
|
||||
return outputStream;
|
||||
}
|
||||
|
||||
/**
|
||||
* This methods handles the transfer and stream negotiation process. It
|
||||
* returns immediately and its progress will be updated through the
|
||||
* {@link NegotiationProgress} callback.
|
||||
*
|
||||
* @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,
|
||||
final NegotiationProgress progress)
|
||||
/**
|
||||
* This methods handles the transfer and stream negotiation process. It
|
||||
* returns immediately and its progress will be updated through the
|
||||
* {@link NegotiationProgress} callback.
|
||||
*
|
||||
* @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,
|
||||
final NegotiationProgress progress)
|
||||
{
|
||||
if(progress == null) {
|
||||
throw new IllegalArgumentException("Callback progress cannot be null.");
|
||||
}
|
||||
checkTransferThread();
|
||||
if (isDone() || outputStream != null) {
|
||||
throw new IllegalStateException(
|
||||
"The negotation process has already"
|
||||
+ " been attempted for this file transfer");
|
||||
}
|
||||
if (isDone() || outputStream != null) {
|
||||
throw new IllegalStateException(
|
||||
"The negotation process has already"
|
||||
+ " been attempted for this file transfer");
|
||||
}
|
||||
setFileInfo(fileName, fileSize);
|
||||
this.callback = progress;
|
||||
transferThread = new Thread(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
OutgoingFileTransfer.this.outputStream = negotiateStream(
|
||||
fileName, fileSize, description);
|
||||
public void run() {
|
||||
try {
|
||||
OutgoingFileTransfer.this.outputStream = negotiateStream(
|
||||
fileName, fileSize, description);
|
||||
progress.outputStreamEstablished(OutgoingFileTransfer.this.outputStream);
|
||||
}
|
||||
catch (XMPPErrorException e) {
|
||||
handleXMPPException(e);
|
||||
}
|
||||
handleXMPPException(e);
|
||||
}
|
||||
catch (Exception e) {
|
||||
setException(e);
|
||||
}
|
||||
}
|
||||
}, "File Transfer Negotiation " + streamID);
|
||||
transferThread.start();
|
||||
}
|
||||
}
|
||||
}, "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.");
|
||||
}
|
||||
}
|
||||
private void checkTransferThread() {
|
||||
if (transferThread != null && transferThread.isAlive() || isDone()) {
|
||||
throw new IllegalStateException(
|
||||
"File transfer in progress or has already completed.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method handles the stream negotiation process and transmits the file
|
||||
* to the remote user. It returns immediately 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>
|
||||
*
|
||||
* This method handles the stream negotiation process and transmits the file
|
||||
* to the remote user. It returns immediately 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 file the file to transfer to the remote entity.
|
||||
* @param description a description for the file to transfer.
|
||||
* @throws SmackException
|
||||
* 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 SmackException {
|
||||
checkTransferThread();
|
||||
if (file == null || !file.exists() || !file.canRead()) {
|
||||
throw new IllegalArgumentException("Could not read file");
|
||||
} else {
|
||||
setFileInfo(file.getAbsolutePath(), file.getName(), file.length());
|
||||
}
|
||||
* @throws SmackException
|
||||
* 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 SmackException {
|
||||
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 (XMPPErrorException e) {
|
||||
handleXMPPException(e);
|
||||
return;
|
||||
}
|
||||
transferThread = new Thread(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
outputStream = negotiateStream(file.getName(), file
|
||||
.length(), description);
|
||||
} catch (XMPPErrorException e) {
|
||||
handleXMPPException(e);
|
||||
return;
|
||||
}
|
||||
catch (Exception e) {
|
||||
setException(e);
|
||||
}
|
||||
if (outputStream == null) {
|
||||
return;
|
||||
}
|
||||
if (outputStream == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!updateStatus(Status.negotiated, Status.in_progress)) {
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
inputStream = new FileInputStream(file);
|
||||
writeToStream(inputStream, outputStream);
|
||||
} catch (FileNotFoundException e) {
|
||||
setStatus(FileTransfer.Status.error);
|
||||
setError(Error.bad_file);
|
||||
setException(e);
|
||||
} catch (IOException e) {
|
||||
setStatus(FileTransfer.Status.error);
|
||||
setException(e);
|
||||
} finally {
|
||||
if (inputStream != null) {
|
||||
try {
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
inputStream = new FileInputStream(file);
|
||||
writeToStream(inputStream, outputStream);
|
||||
} catch (FileNotFoundException e) {
|
||||
setStatus(FileTransfer.Status.error);
|
||||
setError(Error.bad_file);
|
||||
setException(e);
|
||||
} catch (IOException e) {
|
||||
setStatus(FileTransfer.Status.error);
|
||||
setException(e);
|
||||
} finally {
|
||||
if (inputStream != null) {
|
||||
try {
|
||||
inputStream.close();
|
||||
} catch (IOException e) {
|
||||
LOGGER.log(Level.WARNING, "Closing input stream", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
try {
|
||||
outputStream.close();
|
||||
} catch (IOException e) {
|
||||
LOGGER.log(Level.WARNING, "Closing output stream", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
updateStatus(Status.in_progress, FileTransfer.Status.complete);
|
||||
}
|
||||
}
|
||||
|
||||
}, "File Transfer " + streamID);
|
||||
transferThread.start();
|
||||
}
|
||||
}, "File Transfer " + streamID);
|
||||
transferThread.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method handles the stream negotiation process and transmits the file
|
||||
* to the remote user. It returns immediately 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>
|
||||
*
|
||||
* This method handles the stream negotiation process and transmits the file
|
||||
* to the remote user. It returns immediately 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();
|
||||
*/
|
||||
public synchronized void sendStream(final InputStream in, final String fileName, final long fileSize, final String description){
|
||||
checkTransferThread();
|
||||
|
||||
setFileInfo(fileName, fileSize);
|
||||
transferThread = new Thread(new Runnable() {
|
||||
public void run() {
|
||||
setFileInfo(fileName, fileSize);
|
||||
transferThread = new Thread(new Runnable() {
|
||||
public void run() {
|
||||
//Create packet filter
|
||||
try {
|
||||
outputStream = negotiateStream(fileName, fileSize, description);
|
||||
} catch (XMPPErrorException e) {
|
||||
handleXMPPException(e);
|
||||
return;
|
||||
}
|
||||
outputStream = negotiateStream(fileName, fileSize, description);
|
||||
} catch (XMPPErrorException e) {
|
||||
handleXMPPException(e);
|
||||
return;
|
||||
}
|
||||
catch (Exception e) {
|
||||
setException(e);
|
||||
}
|
||||
if (outputStream == null) {
|
||||
return;
|
||||
}
|
||||
if (outputStream == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!updateStatus(Status.negotiated, Status.in_progress)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
writeToStream(in, outputStream);
|
||||
} catch (IOException e) {
|
||||
setStatus(FileTransfer.Status.error);
|
||||
setException(e);
|
||||
} finally {
|
||||
try {
|
||||
if (in != null) {
|
||||
in.close();
|
||||
}
|
||||
return;
|
||||
}
|
||||
try {
|
||||
writeToStream(in, outputStream);
|
||||
} catch (IOException e) {
|
||||
setStatus(FileTransfer.Status.error);
|
||||
setException(e);
|
||||
} finally {
|
||||
try {
|
||||
if (in != null) {
|
||||
in.close();
|
||||
}
|
||||
|
||||
outputStream.flush();
|
||||
outputStream.close();
|
||||
} catch (IOException e) {
|
||||
outputStream.flush();
|
||||
outputStream.close();
|
||||
} catch (IOException e) {
|
||||
/* Do Nothing */
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
updateStatus(Status.in_progress, FileTransfer.Status.complete);
|
||||
}
|
||||
}
|
||||
|
||||
}, "File Transfer " + streamID);
|
||||
transferThread.start();
|
||||
}
|
||||
}, "File Transfer " + streamID);
|
||||
transferThread.start();
|
||||
}
|
||||
|
||||
private void handleXMPPException(XMPPErrorException e) {
|
||||
XMPPError error = e.getXMPPError();
|
||||
if (error != null) {
|
||||
switch (error.getCondition()) {
|
||||
case forbidden:
|
||||
setStatus(Status.refused);
|
||||
return;
|
||||
case bad_request:
|
||||
setStatus(Status.error);
|
||||
setError(Error.not_acceptable);
|
||||
break;
|
||||
private void handleXMPPException(XMPPErrorException e) {
|
||||
XMPPError error = e.getXMPPError();
|
||||
if (error != null) {
|
||||
switch (error.getCondition()) {
|
||||
case forbidden:
|
||||
setStatus(Status.refused);
|
||||
return;
|
||||
case bad_request:
|
||||
setStatus(Status.error);
|
||||
setError(Error.not_acceptable);
|
||||
break;
|
||||
default:
|
||||
setStatus(FileTransfer.Status.error);
|
||||
}
|
||||
}
|
||||
|
||||
setException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 actually transmits the
|
||||
* file.
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
/**
|
||||
* 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 actually transmits the
|
||||
* file.
|
||||
*
|
||||
* @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 SmackException, XMPPException, InterruptedException {
|
||||
// Negotiate the file transfer profile
|
||||
private OutputStream negotiateStream(String fileName, long fileSize,
|
||||
String description) throws SmackException, XMPPException, InterruptedException {
|
||||
// Negotiate the file transfer profile
|
||||
|
||||
if (!updateStatus(Status.initial, Status.negotiating_transfer)) {
|
||||
throw new IllegalStateChangeException();
|
||||
}
|
||||
StreamNegotiator streamNegotiator = negotiator.negotiateOutgoingTransfer(
|
||||
getPeer(), streamID, fileName, fileSize, description,
|
||||
RESPONSE_TIMEOUT);
|
||||
StreamNegotiator streamNegotiator = negotiator.negotiateOutgoingTransfer(
|
||||
getPeer(), streamID, fileName, fileSize, description,
|
||||
RESPONSE_TIMEOUT);
|
||||
|
||||
// Negotiate the stream
|
||||
if (!updateStatus(Status.negotiating_transfer, Status.negotiating_stream)) {
|
||||
throw new IllegalStateChangeException();
|
||||
}
|
||||
outputStream = streamNegotiator.createOutgoingStream(streamID,
|
||||
outputStream = streamNegotiator.createOutgoingStream(streamID,
|
||||
initiator, getPeer());
|
||||
|
||||
if (!updateStatus(Status.negotiating_stream, Status.negotiated)) {
|
||||
throw new IllegalStateChangeException();
|
||||
}
|
||||
return outputStream;
|
||||
}
|
||||
}
|
||||
return outputStream;
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
setStatus(Status.cancelled);
|
||||
}
|
||||
public void cancel() {
|
||||
setStatus(Status.cancelled);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean updateStatus(Status oldStatus, Status newStatus) {
|
||||
|
|
@ -429,30 +429,30 @@ public class OutgoingFileTransfer extends FileTransfer {
|
|||
}
|
||||
|
||||
/**
|
||||
* A callback class to retrieve the status of an outgoing transfer
|
||||
* negotiation process.
|
||||
*
|
||||
* @author Alexander Wenckus
|
||||
*
|
||||
*/
|
||||
public interface NegotiationProgress {
|
||||
* A callback class to retrieve the status of an outgoing transfer
|
||||
* negotiation process.
|
||||
*
|
||||
* @author Alexander Wenckus
|
||||
*
|
||||
*/
|
||||
public interface NegotiationProgress {
|
||||
|
||||
/**
|
||||
* Called when the status changes.
|
||||
/**
|
||||
* 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);
|
||||
void statusUpdated(Status oldStatus, Status newStatus);
|
||||
|
||||
/**
|
||||
* Once the negotiation process is completed the output stream can be
|
||||
* retrieved.
|
||||
/**
|
||||
* Once the negotiation process is completed the output stream can be
|
||||
* retrieved.
|
||||
*
|
||||
* @param stream the established stream which can be used to transfer the file to the remote
|
||||
* entity
|
||||
*/
|
||||
void outputStreamEstablished(OutputStream stream);
|
||||
*/
|
||||
void outputStreamEstablished(OutputStream stream);
|
||||
|
||||
/**
|
||||
* Called when an exception occurs during the negotiation progress.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue