Search in sources :

Example 6 with FileTransferJob

use of org.eclipse.ecf.filetransfer.FileTransferJob in project ecf by eclipse.

the class URLRetrieveTestWithCustomJob method testReceive.

protected void testReceive(String url) throws Exception {
    assertNotNull(retrieveAdapter);
    final IFileTransferListener listener = createFileTransferListener();
    final IFileID fileID = createFileID(new URL(url));
    fileTransferJob = new FileTransferJob(fileID.getName());
    fileTransferJob.addJobChangeListener(new JobChangeTraceListener(startTime));
    retrieveAdapter.sendRetrieveRequest(fileID, listener, null);
    waitForDone(10000);
    final IStatus result = fileTransferJob.getResult();
    System.out.println("job=" + fileTransferJob.getName() + " result=" + result);
    assertTrue(result != null);
    assertHasEvent(startEvents, IIncomingFileTransferReceiveStartEvent.class);
    assertHasMoreThanEventCount(dataEvents, IIncomingFileTransferReceiveDataEvent.class, 0);
    assertDoneOK();
    assertTrue(tmpFile.exists());
    assertTrue(tmpFile.length() > 0);
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) IFileID(org.eclipse.ecf.filetransfer.identity.IFileID) FileTransferJob(org.eclipse.ecf.filetransfer.FileTransferJob) IFileTransferListener(org.eclipse.ecf.filetransfer.IFileTransferListener) URL(java.net.URL)

Example 7 with FileTransferJob

use of org.eclipse.ecf.filetransfer.FileTransferJob 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 8 with FileTransferJob

use of org.eclipse.ecf.filetransfer.FileTransferJob in project ecf by eclipse.

the class URLRetrieveTestCancelConnectJob method testReceiveFile_cancelTransferJob.

public void testReceiveFile_cancelTransferJob() throws Exception {
    if (!CANCEL_SUPPORTED_ON_CONNECT) {
        trace("WARNING:  Cancel not supported by this provider.  testReceiveFile_cancelTransferJob 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));
            event.connectUsingJob(connectJob);
        }

        protected void handleStartEvent(final IIncomingFileTransferReceiveStartEvent event) {
            spawnCancelThread(doCancel, new ICancelable() {

                public void cancel() {
                    waitForSocketInRead();
                    assertNotNull(socketInReadWrapper);
                    assertTrue(socketInReadWrapper.inRead);
                    event.cancel();
                }
            });
            try {
                createTempFile();
                event.receive(tmpFile);
            } catch (IOException e) {
                e.printStackTrace();
                fail(e.toString());
            }
        }
    };
    final SimpleServer server = new SimpleServer(getName());
    SimpleHttpServer simple = server.getSimpleHttpServer();
    simple.setRequestHandler(new HttpRequestHandler() {

        public boolean processRequest(SimpleHttpServerConnection conn, SimpleRequest request) throws IOException {
            trace("Responding to request but never provide full body" + request.getRequestLine());
            ResponseWriter w = conn.getWriter();
            writeLines(w, new String[] { "HTTP/1.0 200 OK", "Content-Length: 9", "Content-Type: text/plain; charset=UTF-8", "" });
            w.flush();
            synchronized (doCancel) {
                doCancel[0] = Boolean.TRUE;
            }
            conn.setKeepAlive(true);
            // 
            return stalledInRequestHandler(doCancel);
        }
    });
    try {
        // path does not matter as server does not respond.
        testReceive(server.getServerURL() + "/foo", lw);
        assertHasEvent(startConnectEvents, IFileTransferConnectStartEvent.class);
        assertHasEvent(startEvents, IIncomingFileTransferReceiveStartEvent.class);
        assertDoneCancelled();
        assertNotNull(tmpFile);
        assertTrue(tmpFile.exists());
        assertEquals(0, tmpFile.length());
        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) IIncomingFileTransferReceiveStartEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveStartEvent) SimpleHttpServerConnection(org.apache.commons.httpclient.server.SimpleHttpServerConnection) IFileTransferListener(org.eclipse.ecf.filetransfer.IFileTransferListener) IOException(java.io.IOException) SimpleRequest(org.apache.commons.httpclient.server.SimpleRequest) IFileTransferConnectStartEvent(org.eclipse.ecf.filetransfer.events.IFileTransferConnectStartEvent) ResponseWriter(org.apache.commons.httpclient.server.ResponseWriter) FileTransferJob(org.eclipse.ecf.filetransfer.FileTransferJob) SimpleHttpServer(org.apache.commons.httpclient.server.SimpleHttpServer)

Example 9 with FileTransferJob

use of org.eclipse.ecf.filetransfer.FileTransferJob in project ecf by eclipse.

the class URLRetrieveTestWithConnectJob method handleStartConnectEvent.

protected void handleStartConnectEvent(IFileTransferConnectStartEvent event) {
    super.handleStartConnectEvent(event);
    assertNotNull(event.getFileID());
    assertNotNull(event.getFileID().getFilename());
    FileTransferJob connectJob = event.prepareConnectJob(null);
    connectJob.addJobChangeListener(new JobChangeTraceListener(startTime));
    event.connectUsingJob(connectJob);
}
Also used : FileTransferJob(org.eclipse.ecf.filetransfer.FileTransferJob)

Example 10 with FileTransferJob

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

FileTransferJob (org.eclipse.ecf.filetransfer.FileTransferJob)10 IFileTransferListener (org.eclipse.ecf.filetransfer.IFileTransferListener)5 IFileTransferConnectStartEvent (org.eclipse.ecf.filetransfer.events.IFileTransferConnectStartEvent)5 IOException (java.io.IOException)4 HttpRequestHandler (org.apache.commons.httpclient.server.HttpRequestHandler)3 SimpleHttpServer (org.apache.commons.httpclient.server.SimpleHttpServer)3 SimpleHttpServerConnection (org.apache.commons.httpclient.server.SimpleHttpServerConnection)3 SimpleRequest (org.apache.commons.httpclient.server.SimpleRequest)3 IIncomingFileTransferReceiveStartEvent (org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveStartEvent)3 IFileID (org.eclipse.ecf.filetransfer.identity.IFileID)3 SimpleServer (org.eclipse.ecf.internal.tests.filetransfer.httpserver.SimpleServer)3 ResponseWriter (org.apache.commons.httpclient.server.ResponseWriter)2 UserCancelledException (org.eclipse.ecf.filetransfer.UserCancelledException)2 File (java.io.File)1 OutputStream (java.io.OutputStream)1 URL (java.net.URL)1 UnknownHostException (java.net.UnknownHostException)1 Map (java.util.Map)1 IStatus (org.eclipse.core.runtime.IStatus)1 IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)1