use of org.eclipse.ecf.filetransfer.UserCancelledException in project ecf by eclipse.
the class TorrentFileTransfer method cancel.
public void cancel() {
try {
torrent.stop();
notifyCompletion(new UserCancelledException());
} catch (final IOException e) {
notifyCompletion(e);
}
}
use of org.eclipse.ecf.filetransfer.UserCancelledException in project ecf by eclipse.
the class AbstractRetrieveTestCase method assertDoneCancelled.
protected void assertDoneCancelled() {
IIncomingFileTransferReceiveDoneEvent doneEvent = getDoneEvent();
assertTrue(doneEvent.getException().getClass().getName(), doneEvent.getException() instanceof UserCancelledException);
}
use of org.eclipse.ecf.filetransfer.UserCancelledException in project ecf by eclipse.
the class URLRetrieveTestCancelConnectJob method testReceiveFile_cancelConnectJob.
// TODO: add test that cancel without connect job, when server does not
// respond
public void testReceiveFile_cancelConnectJob() throws Exception {
if (!CANCEL_SUPPORTED_ON_CONNECT) {
trace("WARNING: Cancel not supported by this provider. testReceiveFile_cancelConnectJob cannot be used");
return;
}
final Object[] doCancel = new Object[1];
final IFileTransferListener listener = createFileTransferListener();
final FileTransferListenerWrapper lw = new FileTransferListenerWrapper(listener) {
protected void handleStartConnectEvent(final IFileTransferConnectStartEvent event) {
assertNotNull(event.getFileID());
assertNotNull(event.getFileID().getFilename());
FileTransferJob connectJob = event.prepareConnectJob(null);
connectJob.addJobChangeListener(new JobChangeTraceListener(startTime) {
public void running(IJobChangeEvent jobEvent) {
super.running(jobEvent);
spawnCancelThread(doCancel, new ICancelable() {
public void cancel() {
assertNotNull(socketInReadWrapper);
assertTrue(socketInReadWrapper.inRead);
event.cancel();
}
});
}
});
event.connectUsingJob(connectJob);
}
};
final SimpleServer server = new SimpleServer(getName());
SimpleHttpServer simple = server.getSimpleHttpServer();
simple.setRequestHandler(new HttpRequestHandler() {
public boolean processRequest(SimpleHttpServerConnection conn, SimpleRequest request) throws IOException {
trace("Not responding to request " + request.getRequestLine());
return stalledInRequestHandler(doCancel);
}
});
try {
// path does not matter as server does not respond.
testReceive(server.getServerURL() + "/foo", lw);
assertHasEvent(startConnectEvents, IFileTransferConnectStartEvent.class);
assertHasNoEvent(startEvents, IIncomingFileTransferReceiveStartEvent.class);
assertHasNoEvent(dataEvents, IIncomingFileTransferReceiveDataEvent.class);
IIncomingFileTransferReceiveDoneEvent doneEvent = getDoneEvent();
assertTrue(doneEvent.getException().toString(), doneEvent.getException() instanceof UserCancelledException);
assertTrue(doneEvent.getSource().isDone());
assertSame(doneEvent.getException(), doneEvent.getSource().getException());
assertNull(tmpFile);
assertFalse(socketInReadWrapper.inRead);
socketEvents.validateOneSocketCreatedAndClosed();
} finally {
server.shutdown();
}
}
use of org.eclipse.ecf.filetransfer.UserCancelledException in project ecf by eclipse.
the class BitTorrentContainer method sendRetrieveRequest.
public void sendRetrieveRequest(final IFileID remoteFileReference, final IFileTransferListener transferListener, Map options) throws IncomingFileTransferException {
// $NON-NLS-1$
Assert.isNotNull(remoteFileReference, "remoteFileReference cannot be null");
// $NON-NLS-1$
Assert.isLegal(remoteFileReference instanceof TorrentID, "remoteFileReference must be instanceof TorrentID");
// $NON-NLS-1$
Assert.isNotNull(transferListener, "transferListener cannot be null");
transferListener.handleTransferEvent(new IIncomingFileTransferReceiveStartEvent() {
private IIncomingFileTransfer transfer;
private boolean cancelled = false;
public IIncomingFileTransfer receive(File localFileToSave) throws IOException {
if (cancelled) {
throw new RuntimeException(new UserCancelledException());
}
// $NON-NLS-1$
Assert.isNotNull(localFileToSave, "localFileToSave cannot be null");
if (localFileToSave.exists() && !localFileToSave.canWrite()) {
// $NON-NLS-1$ //$NON-NLS-2$
throw new IOException("file=" + localFileToSave.getAbsolutePath() + " does not exist or cannot be written to");
}
final TorrentFile file = new TorrentFile(((TorrentID) remoteFileReference).getFile());
file.setTargetFile(localFileToSave);
final Torrent torrent = TorrentFactory.createTorrent(file);
transfer = new TorrentFileTransfer(remoteFileReference, transferListener, torrent);
return transfer;
}
public IIncomingFileTransfer receive(File localFileToSave, FileTransferJob fileTransferJob) throws IOException {
if (cancelled) {
throw new RuntimeException(new UserCancelledException());
}
// $NON-NLS-1$
Assert.isNotNull(localFileToSave, "localFileToSave must not be null");
if (localFileToSave.exists() && !localFileToSave.canWrite()) {
// $NON-NLS-1$ //$NON-NLS-2$
throw new IOException("file=" + localFileToSave.getAbsolutePath() + " does not exist or cannot be written to");
}
final TorrentFile file = new TorrentFile(((TorrentID) remoteFileReference).getFile());
file.setTargetFile(localFileToSave);
final Torrent torrent = TorrentFactory.createTorrent(file);
transfer = new TorrentFileTransfer(remoteFileReference, transferListener, torrent);
return transfer;
}
public void cancel() {
if (transfer != null) {
transfer.cancel();
cancelled = true;
}
}
public IFileID getFileID() {
return remoteFileReference;
}
public IIncomingFileTransfer receive(OutputStream streamToStore) throws IOException {
throw new UnsupportedOperationException("receive(OutputStream) not supported by this provider");
}
public IIncomingFileTransfer receive(OutputStream streamToStore, FileTransferJob fileTransferJob) throws IOException {
throw new UnsupportedOperationException("receive(OutputStream,FileTransferJob) not supported by this provider");
}
public IIncomingFileTransfer getSource() {
return transfer;
}
public Map getResponseHeaders() {
return null;
}
});
}
Aggregations