Search in sources :

Example 6 with IIncomingFileTransferReceiveStartEvent

use of org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveStartEvent in project ecf by eclipse.

the class BitTorrentConnectWizard method performFinish.

public boolean performFinish() {
    workbenchPage = workbench.getActiveWorkbenchWindow().getActivePage();
    final IRetrieveFileTransferContainerAdapter irftca = (IRetrieveFileTransferContainerAdapter) container.getAdapter(IRetrieveFileTransferContainerAdapter.class);
    try {
        targetID = FileIDFactory.getDefault().createFileID(irftca.getRetrieveNamespace(), page.getTorrentName());
    } catch (final FileCreateException e) {
        new ContainerConnectErrorDialog(workbench.getActiveWorkbenchWindow().getShell(), 1, "The target ID to connect to could not be created", page.getTorrentName(), e).open();
        return true;
    }
    try {
        irftca.sendRetrieveRequest(targetID, new IFileTransferListener() {

            public void handleTransferEvent(final IFileTransferEvent e) {
                if (e instanceof IIncomingFileTransferReceiveStartEvent) {
                    try {
                        final IFileTransfer ift = ((IIncomingFileTransferReceiveStartEvent) e).receive(new File(page.getTargetName()));
                        workbenchPage.getWorkbenchWindow().getShell().getDisplay().asyncExec(new Runnable() {

                            public void run() {
                                FileTransfersView.addTransfer(ift);
                            }
                        });
                    } catch (final IOException ioe) {
                        new ContainerConnectErrorDialog(workbench.getActiveWorkbenchWindow().getShell(), 1, "Could not write to " + page.getTargetName(), page.getTargetName(), null).open();
                    }
                } else if (e instanceof IIncomingFileTransferEvent) {
                    final FileTransfersView ftv = (FileTransfersView) workbenchPage.findView(FileTransfersView.ID);
                    if (ftv != null) {
                        workbenchPage.getWorkbenchWindow().getShell().getDisplay().asyncExec(new Runnable() {

                            public void run() {
                                ftv.update(((IIncomingFileTransferEvent) e).getSource());
                            }
                        });
                    }
                }
            }
        }, null);
    } catch (final IncomingFileTransferException e) {
        new ContainerConnectErrorDialog(workbench.getActiveWorkbenchWindow().getShell(), 1, "Could not send retrieval request.", targetID.getName(), e).open();
    }
    return true;
}
Also used : FileCreateException(org.eclipse.ecf.filetransfer.identity.FileCreateException) IRetrieveFileTransferContainerAdapter(org.eclipse.ecf.filetransfer.IRetrieveFileTransferContainerAdapter) IIncomingFileTransferReceiveStartEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveStartEvent) FileTransfersView(org.eclipse.ecf.filetransfer.ui.FileTransfersView) ContainerConnectErrorDialog(org.eclipse.ecf.ui.dialogs.ContainerConnectErrorDialog) IFileTransferListener(org.eclipse.ecf.filetransfer.IFileTransferListener) IOException(java.io.IOException) IFileTransferEvent(org.eclipse.ecf.filetransfer.events.IFileTransferEvent) IIncomingFileTransferEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferEvent) IncomingFileTransferException(org.eclipse.ecf.filetransfer.IncomingFileTransferException) IFileTransfer(org.eclipse.ecf.filetransfer.IFileTransfer) File(java.io.File)

Example 7 with IIncomingFileTransferReceiveStartEvent

use of org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveStartEvent 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

IIncomingFileTransferReceiveStartEvent (org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveStartEvent)7 IOException (java.io.IOException)6 IFileTransferListener (org.eclipse.ecf.filetransfer.IFileTransferListener)5 File (java.io.File)4 FileTransferJob (org.eclipse.ecf.filetransfer.FileTransferJob)3 IFileTransferEvent (org.eclipse.ecf.filetransfer.events.IFileTransferEvent)3 IFileID (org.eclipse.ecf.filetransfer.identity.IFileID)3 FileOutputStream (java.io.FileOutputStream)2 Map (java.util.Map)2 HttpRequestHandler (org.apache.commons.httpclient.server.HttpRequestHandler)2 ResponseWriter (org.apache.commons.httpclient.server.ResponseWriter)2 SimpleHttpServer (org.apache.commons.httpclient.server.SimpleHttpServer)2 SimpleHttpServerConnection (org.apache.commons.httpclient.server.SimpleHttpServerConnection)2 SimpleRequest (org.apache.commons.httpclient.server.SimpleRequest)2 IFileTransferPausable (org.eclipse.ecf.filetransfer.IFileTransferPausable)2 IncomingFileTransferException (org.eclipse.ecf.filetransfer.IncomingFileTransferException)2 IFileTransferConnectStartEvent (org.eclipse.ecf.filetransfer.events.IFileTransferConnectStartEvent)2 IIncomingFileTransferReceiveDataEvent (org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDataEvent)2 IIncomingFileTransferReceiveDoneEvent (org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDoneEvent)2 IIncomingFileTransferReceivePausedEvent (org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceivePausedEvent)2