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 java.io.*;
|
2007-03-21 05:09:52 +01:00
|
|
|
import java.util.concurrent.*;
|
2006-02-03 19:44:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An incoming file transfer is created when the
|
|
|
|
* {@link FileTransferManager#createIncomingFileTransfer(FileTransferRequest)}
|
|
|
|
* method is invoked. It is a file being sent to the local user from another
|
|
|
|
* user on the jabber network. There are two stages of the file transfer to be
|
|
|
|
* concerned with and they can be handled in different ways depending upon the
|
|
|
|
* method that is invoked on this class.
|
|
|
|
* <p/>
|
|
|
|
* The first way that a file is recieved is by calling the
|
|
|
|
* {@link #recieveFile()} method. This method, negotiates the appropriate stream
|
|
|
|
* method and then returns the <b><i>InputStream</b></i> to read the file
|
|
|
|
* data from.
|
|
|
|
* <p/>
|
|
|
|
* The second way that a file can be recieved through this class is by invoking
|
|
|
|
* the {@link #recieveFile(File)} method. This method returns immediatly and
|
|
|
|
* takes as its parameter a file on the local file system where the file
|
|
|
|
* recieved from the transfer will be put.
|
|
|
|
*
|
|
|
|
* @author Alexander Wenckus
|
|
|
|
*/
|
|
|
|
public class IncomingFileTransfer extends FileTransfer {
|
|
|
|
|
|
|
|
private FileTransferRequest recieveRequest;
|
|
|
|
|
|
|
|
private InputStream inputStream;
|
|
|
|
|
|
|
|
protected IncomingFileTransfer(FileTransferRequest request,
|
|
|
|
FileTransferNegotiator transferNegotiator) {
|
|
|
|
super(request.getRequestor(), request.getStreamID(), transferNegotiator);
|
|
|
|
this.recieveRequest = request;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Negotiates the stream method to transfer the file over and then returns
|
|
|
|
* the negotiated stream.
|
|
|
|
*
|
|
|
|
* @return The negotiated InputStream from which to read the data.
|
|
|
|
* @throws XMPPException If there is an error in the negotiation process an exception
|
|
|
|
* is thrown.
|
|
|
|
*/
|
|
|
|
public InputStream recieveFile() throws XMPPException {
|
|
|
|
if (inputStream != null) {
|
|
|
|
throw new IllegalStateException("Transfer already negotiated!");
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
inputStream = negotiateStream();
|
|
|
|
}
|
|
|
|
catch (XMPPException e) {
|
|
|
|
setException(e);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
|
|
|
return inputStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method negotitates the stream and then transfer's the file over the
|
|
|
|
* negotiated stream. The transfered file will be saved at the provided
|
|
|
|
* location.
|
|
|
|
* <p/>
|
|
|
|
* This method will return immedialtly, file transfer progress can be
|
|
|
|
* monitored through several methods:
|
|
|
|
* <p/>
|
|
|
|
* <UL>
|
|
|
|
* <LI>{@link FileTransfer#getStatus()}
|
|
|
|
* <LI>{@link FileTransfer#getProgress()}
|
|
|
|
* <LI>{@link FileTransfer#isDone()}
|
|
|
|
* </UL>
|
|
|
|
*
|
|
|
|
* @param file The location to save the file.
|
2007-03-21 05:09:52 +01:00
|
|
|
* @throws XMPPException when the file transfer fails
|
2006-02-03 19:44:22 +01:00
|
|
|
* @throws IllegalArgumentException This exception is thrown when the the provided file is
|
|
|
|
* either null, or cannot be written to.
|
|
|
|
*/
|
|
|
|
public void recieveFile(final File file) throws XMPPException {
|
|
|
|
if (file != null) {
|
|
|
|
if (!file.exists()) {
|
|
|
|
try {
|
|
|
|
file.createNewFile();
|
|
|
|
}
|
|
|
|
catch (IOException e) {
|
|
|
|
throw new XMPPException(
|
|
|
|
"Could not create file to write too", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!file.canWrite()) {
|
|
|
|
throw new IllegalArgumentException("Cannot write to provided file");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new IllegalArgumentException("File cannot be null");
|
|
|
|
}
|
|
|
|
|
2006-09-01 19:38:53 +02:00
|
|
|
Thread transferThread = new Thread(new Runnable() {
|
2006-02-03 19:44:22 +01:00
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
inputStream = negotiateStream();
|
|
|
|
}
|
|
|
|
catch (XMPPException e) {
|
|
|
|
handleXMPPException(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
OutputStream outputStream = null;
|
|
|
|
try {
|
|
|
|
outputStream = new FileOutputStream(file);
|
2006-09-01 19:38:53 +02:00
|
|
|
setStatus(Status.in_progress);
|
2006-02-03 19:44:22 +01:00
|
|
|
writeToStream(inputStream, outputStream);
|
|
|
|
}
|
|
|
|
catch (XMPPException e) {
|
2006-09-01 19:38:53 +02:00
|
|
|
setStatus(Status.error);
|
|
|
|
setError(Error.stream);
|
2006-02-03 19:44:22 +01:00
|
|
|
setException(e);
|
|
|
|
}
|
|
|
|
catch (FileNotFoundException e) {
|
2006-09-01 19:38:53 +02:00
|
|
|
setStatus(Status.error);
|
|
|
|
setError(Error.bad_file);
|
2006-02-03 19:44:22 +01:00
|
|
|
setException(e);
|
|
|
|
}
|
|
|
|
|
2007-02-15 18:33:44 +01:00
|
|
|
if (getStatus().equals(Status.in_progress)) {
|
2006-09-01 19:38:53 +02:00
|
|
|
setStatus(Status.complete);
|
2007-02-15 18:33:44 +01:00
|
|
|
}
|
|
|
|
if (inputStream != null) {
|
|
|
|
try {
|
2006-02-03 19:44:22 +01:00
|
|
|
inputStream.close();
|
|
|
|
}
|
2007-03-21 05:09:52 +01:00
|
|
|
catch (Throwable io) {
|
2007-02-15 18:33:44 +01:00
|
|
|
/* Ignore */
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
|
|
|
}
|
2007-02-15 18:33:44 +01:00
|
|
|
if (outputStream != null) {
|
|
|
|
try {
|
2007-03-21 05:09:52 +01:00
|
|
|
outputStream.close();
|
2007-02-15 18:33:44 +01:00
|
|
|
}
|
|
|
|
catch (Throwable io) {
|
|
|
|
/* Ignore */
|
|
|
|
}
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}, "File Transfer " + streamID);
|
|
|
|
transferThread.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleXMPPException(XMPPException e) {
|
2006-09-01 19:38:53 +02:00
|
|
|
setStatus(FileTransfer.Status.error);
|
2006-02-03 19:44:22 +01:00
|
|
|
setException(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
private InputStream negotiateStream() throws XMPPException {
|
2006-09-01 19:38:53 +02:00
|
|
|
setStatus(Status.negotiating_transfer);
|
2007-03-21 05:09:52 +01:00
|
|
|
final StreamNegotiator streamNegotiator = negotiator
|
2006-02-03 19:44:22 +01:00
|
|
|
.selectStreamNegotiator(recieveRequest);
|
2006-09-01 19:38:53 +02:00
|
|
|
setStatus(Status.negotiating_stream);
|
2007-03-21 05:09:52 +01:00
|
|
|
FutureTask<InputStream> streamNegotiatorTask = new FutureTask<InputStream>(
|
|
|
|
new Callable<InputStream>() {
|
|
|
|
|
|
|
|
public InputStream call() throws Exception {
|
|
|
|
return streamNegotiator
|
|
|
|
.createIncomingStream(recieveRequest.getStreamInitiation());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
streamNegotiatorTask.run();
|
|
|
|
InputStream inputStream;
|
|
|
|
try {
|
|
|
|
inputStream = streamNegotiatorTask.get(15, TimeUnit.SECONDS);
|
|
|
|
}
|
|
|
|
catch (InterruptedException e) {
|
|
|
|
throw new XMPPException("Interruption while executing", e);
|
|
|
|
}
|
|
|
|
catch (ExecutionException e) {
|
|
|
|
throw new XMPPException("Error in execution", e);
|
|
|
|
}
|
|
|
|
catch (TimeoutException e) {
|
|
|
|
throw new XMPPException("Request timed out", e);
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
streamNegotiatorTask.cancel(true);
|
|
|
|
}
|
2006-09-01 19:38:53 +02:00
|
|
|
setStatus(Status.negotiated);
|
2006-02-03 19:44:22 +01:00
|
|
|
return inputStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void cancel() {
|
2006-09-01 19:38:53 +02:00
|
|
|
setStatus(Status.cancelled);
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|