Search in sources :

Example 1 with UserCancelledException

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);
    }
}
Also used : UserCancelledException(org.eclipse.ecf.filetransfer.UserCancelledException) IOException(java.io.IOException)

Example 2 with UserCancelledException

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);
}
Also used : IIncomingFileTransferReceiveDoneEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDoneEvent) UserCancelledException(org.eclipse.ecf.filetransfer.UserCancelledException)

Example 3 with 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();
    }
}
Also used : SimpleServer(org.eclipse.ecf.internal.tests.filetransfer.httpserver.SimpleServer) HttpRequestHandler(org.apache.commons.httpclient.server.HttpRequestHandler) UserCancelledException(org.eclipse.ecf.filetransfer.UserCancelledException) SimpleHttpServerConnection(org.apache.commons.httpclient.server.SimpleHttpServerConnection) IFileTransferListener(org.eclipse.ecf.filetransfer.IFileTransferListener) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) SimpleRequest(org.apache.commons.httpclient.server.SimpleRequest) IOException(java.io.IOException) IFileTransferConnectStartEvent(org.eclipse.ecf.filetransfer.events.IFileTransferConnectStartEvent) FileTransferJob(org.eclipse.ecf.filetransfer.FileTransferJob) SimpleHttpServer(org.apache.commons.httpclient.server.SimpleHttpServer) IIncomingFileTransferReceiveDoneEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDoneEvent)

Example 4 with UserCancelledException

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;
        }
    });
}
Also used : Torrent(org.eclipse.ecf.protocol.bittorrent.Torrent) UserCancelledException(org.eclipse.ecf.filetransfer.UserCancelledException) OutputStream(java.io.OutputStream) IIncomingFileTransferReceiveStartEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveStartEvent) IOException(java.io.IOException) IIncomingFileTransfer(org.eclipse.ecf.filetransfer.IIncomingFileTransfer) IFileID(org.eclipse.ecf.filetransfer.identity.IFileID) FileTransferJob(org.eclipse.ecf.filetransfer.FileTransferJob) TorrentFile(org.eclipse.ecf.protocol.bittorrent.TorrentFile) TorrentFile(org.eclipse.ecf.protocol.bittorrent.TorrentFile) File(java.io.File) Map(java.util.Map)

Aggregations

UserCancelledException (org.eclipse.ecf.filetransfer.UserCancelledException)4 IOException (java.io.IOException)3 FileTransferJob (org.eclipse.ecf.filetransfer.FileTransferJob)2 IIncomingFileTransferReceiveDoneEvent (org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDoneEvent)2 File (java.io.File)1 OutputStream (java.io.OutputStream)1 Map (java.util.Map)1 HttpRequestHandler (org.apache.commons.httpclient.server.HttpRequestHandler)1 SimpleHttpServer (org.apache.commons.httpclient.server.SimpleHttpServer)1 SimpleHttpServerConnection (org.apache.commons.httpclient.server.SimpleHttpServerConnection)1 SimpleRequest (org.apache.commons.httpclient.server.SimpleRequest)1 IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)1 IFileTransferListener (org.eclipse.ecf.filetransfer.IFileTransferListener)1 IIncomingFileTransfer (org.eclipse.ecf.filetransfer.IIncomingFileTransfer)1 IFileTransferConnectStartEvent (org.eclipse.ecf.filetransfer.events.IFileTransferConnectStartEvent)1 IIncomingFileTransferReceiveStartEvent (org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveStartEvent)1 IFileID (org.eclipse.ecf.filetransfer.identity.IFileID)1 SimpleServer (org.eclipse.ecf.internal.tests.filetransfer.httpserver.SimpleServer)1 Torrent (org.eclipse.ecf.protocol.bittorrent.Torrent)1 TorrentFile (org.eclipse.ecf.protocol.bittorrent.TorrentFile)1