Search in sources :

Example 6 with IIncomingFileTransferReceiveDoneEvent

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

the class TorrentFileTransfer method notifyCompletion.

private void notifyCompletion(Exception exception) {
    this.exception = exception;
    torrent.removePieceProgressListener(pieceListener);
    torrent.removeTorrentStateListener(stateListener);
    listener.handleTransferEvent(new IIncomingFileTransferReceiveDoneEvent() {

        public Exception getException() {
            return TorrentFileTransfer.this.exception;
        }

        public IIncomingFileTransfer getSource() {
            return TorrentFileTransfer.this;
        }
    });
}
Also used : IIncomingFileTransferReceiveDoneEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDoneEvent) IOException(java.io.IOException) UserCancelledException(org.eclipse.ecf.filetransfer.UserCancelledException) IIncomingFileTransfer(org.eclipse.ecf.filetransfer.IIncomingFileTransfer)

Example 7 with IIncomingFileTransferReceiveDoneEvent

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

the class URLRetrievePauseResumeTest method testReceiveHttp.

protected void testReceiveHttp(String url) throws Exception {
    assertNotNull(transferInstance);
    final IFileTransferListener listener = new IFileTransferListener() {

        public void handleTransferEvent(IFileTransferEvent event) {
            if (event instanceof IIncomingFileTransferReceiveResumedEvent) {
                try {
                    IIncomingFileTransferReceiveResumedEvent rse = (IIncomingFileTransferReceiveResumedEvent) event;
                    session = rse.receive(outs);
                } catch (Exception e) {
                    fail(e.getLocalizedMessage());
                }
            } else if (event instanceof IIncomingFileTransferReceiveStartEvent) {
                IIncomingFileTransferReceiveStartEvent rse = (IIncomingFileTransferReceiveStartEvent) event;
                try {
                    incomingFile = new File(FILENAME);
                    outs = new FileOutputStream(incomingFile);
                    session = rse.receive(outs);
                    pausable = (IFileTransferPausable) session.getAdapter(IFileTransferPausable.class);
                    if (pausable == null)
                        fail("pausable is null");
                } catch (IOException e) {
                    fail(e.getLocalizedMessage());
                }
            } else if (event instanceof IIncomingFileTransferReceiveDataEvent) {
                System.out.println("data=" + event);
            } else if (event instanceof IIncomingFileTransferReceivePausedEvent) {
                System.out.println("paused=" + event);
            } else if (event instanceof IIncomingFileTransferReceiveDoneEvent) {
                closeOutputStream();
                System.out.println("done=" + event);
                synchronized (notify) {
                    notify.notify();
                }
            }
        }
    };
    transferInstance.sendRetrieveRequest(FileIDFactory.getDefault().createFileID(transferInstance.getRetrieveNamespace(), url), listener, null);
    // Now if we can do pausing, then pause, wait a while and resume
    if (pausable != null) {
        Thread.sleep(500);
        System.out.println("pausable.pause()=" + pausable.pause());
        System.out.println("Pausing " + PAUSE_TIME / 1000 + " seconds");
        Thread.sleep(PAUSE_TIME);
        final boolean success = pausable.resume();
        System.out.println("pausable.resume()=" + success);
        if (!success) {
            System.out.println("session=" + session);
            final Exception e = session.getException();
            System.out.println("  exception=" + e);
            if (e != null)
                e.printStackTrace();
            System.out.println("  isDone=" + session.isDone());
            return;
        }
        System.out.println();
    }
    synchronized (notify) {
        notify.wait();
    }
    final Exception e = session.getException();
    if (e != null)
        throw e;
}
Also used : IIncomingFileTransferReceiveResumedEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveResumedEvent) IFileTransferPausable(org.eclipse.ecf.filetransfer.IFileTransferPausable) IIncomingFileTransferReceiveStartEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveStartEvent) IFileTransferListener(org.eclipse.ecf.filetransfer.IFileTransferListener) IOException(java.io.IOException) IOException(java.io.IOException) IFileTransferEvent(org.eclipse.ecf.filetransfer.events.IFileTransferEvent) FileOutputStream(java.io.FileOutputStream) IIncomingFileTransferReceiveDoneEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDoneEvent) IIncomingFileTransferReceivePausedEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceivePausedEvent) File(java.io.File) IIncomingFileTransferReceiveDataEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDataEvent)

Example 8 with IIncomingFileTransferReceiveDoneEvent

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

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

the class AbstractRetrieveTestCase method checkGetDoneException.

protected Exception checkGetDoneException(Class cls) {
    IIncomingFileTransferReceiveDoneEvent doneEvent = getDoneEvent();
    Exception e = doneEvent.getException();
    assertNotNull(e);
    // assertTrue(e.getClass().getName(), cls.isInstance(e));
    return e;
}
Also used : IIncomingFileTransferReceiveDoneEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDoneEvent) UserCancelledException(org.eclipse.ecf.filetransfer.UserCancelledException) TimeoutException(org.eclipse.equinox.concurrent.future.TimeoutException) IncomingFileTransferException(org.eclipse.ecf.filetransfer.IncomingFileTransferException)

Example 10 with IIncomingFileTransferReceiveDoneEvent

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

the class XMPPFileTransferRequestListener method fileTransferRequest.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.jivesoftware.smackx.filetransfer.FileTransferListener#fileTransferRequest
	 * (org.jivesoftware.smackx.filetransfer.FileTransferRequest)
	 */
public void fileTransferRequest(final FileTransferRequest request) {
    requestListener.handleFileTransferRequest(new IFileTransferRequestEvent() {

        boolean requestAccepted = false;

        IFileTransferInfo fileTransferInfo = new IFileTransferInfo() {

            Map props = new HashMap();

            File f = new File(request.getFileName());

            public String getDescription() {
                return request.getDescription();
            }

            public File getFile() {
                return f;
            }

            public Map getProperties() {
                return props;
            }

            public Object getAdapter(Class adapter) {
                if (adapter == null)
                    return null;
                if (adapter.isInstance(this))
                    return this;
                final IAdapterManager adapterManager = XmppPlugin.getDefault().getAdapterManager();
                return (adapterManager == null) ? null : adapterManager.loadAdapter(this, adapter.getName());
            }

            public long getFileSize() {
                return request.getFileSize();
            }

            public String getMimeType() {
                return request.getMimeType();
            }

            public String toString() {
                final StringBuffer buf = new StringBuffer("FileTransferInfo[");
                buf.append("file=").append(f);
                buf.append(";size=").append(getFileSize());
                buf.append(";description=" + getDescription());
                buf.append(";mimeType=").append(getMimeType()).append("]");
                return buf.toString();
            }
        };

        public IIncomingFileTransfer accept(File localFileToSave) throws IncomingFileTransferException {
            try {
                final OutputStream outs = new FileOutputStream(localFileToSave);
                return accept(outs, new IFileTransferListener() {

                    public void handleTransferEvent(IFileTransferEvent event) {
                        if (event instanceof IIncomingFileTransferReceiveDoneEvent) {
                            try {
                                outs.close();
                            } catch (final IOException e) {
                            }
                        }
                    }
                });
            } catch (final FileNotFoundException e) {
                throw new IncomingFileTransferException("Exception opening file for writing", e);
            }
        }

        public IFileTransferInfo getFileTransferInfo() {
            return fileTransferInfo;
        }

        public ID getRequesterID() {
            return createIDFromName(request.getRequestor());
        }

        public void reject() {
            request.reject();
        }

        public boolean requestAccepted() {
            return requestAccepted;
        }

        public String toString() {
            final StringBuffer buf = new StringBuffer("FileTransferRequestEvent[");
            buf.append("requester=").append(getRequesterID());
            buf.append(";requestAccepted=").append(requestAccepted());
            buf.append(";transferInfo=").append(getFileTransferInfo()).append("]");
            return buf.toString();
        }

        public IIncomingFileTransfer accept(OutputStream outputStream, IFileTransferListener listener) throws IncomingFileTransferException {
            if (requestAccepted)
                throw new IncomingFileTransferException("Incoming request previously accepted");
            if (outputStream == null)
                throw new IncomingFileTransferException("outputStream cannot be null");
            incoming = request.accept();
            requestAccepted = true;
            try {
                return new XMPPIncomingFileTransfer(IDFactory.getDefault().createStringID(request.getStreamID()), request.getFileName(), incoming.recieveFile(), outputStream, request.getFileSize(), listener);
            } catch (final Exception e) {
                throw new IncomingFileTransferException("Exception receiving file", e);
            }
        }
    });
}
Also used : HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IFileTransferInfo(org.eclipse.ecf.filetransfer.IFileTransferInfo) IFileTransferListener(org.eclipse.ecf.filetransfer.IFileTransferListener) IOException(java.io.IOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) IncomingFileTransferException(org.eclipse.ecf.filetransfer.IncomingFileTransferException) IIncomingFileTransfer(org.eclipse.ecf.filetransfer.IIncomingFileTransfer) IFileTransferEvent(org.eclipse.ecf.filetransfer.events.IFileTransferEvent) FileOutputStream(java.io.FileOutputStream) IIncomingFileTransferReceiveDoneEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDoneEvent) IAdapterManager(org.eclipse.core.runtime.IAdapterManager) IncomingFileTransferException(org.eclipse.ecf.filetransfer.IncomingFileTransferException) XMPPID(org.eclipse.ecf.provider.xmpp.identity.XMPPID) ID(org.eclipse.ecf.core.identity.ID) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) IFileTransferRequestEvent(org.eclipse.ecf.filetransfer.events.IFileTransferRequestEvent)

Aggregations

IIncomingFileTransferReceiveDoneEvent (org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDoneEvent)10 IOException (java.io.IOException)5 IFileTransferListener (org.eclipse.ecf.filetransfer.IFileTransferListener)4 IncomingFileTransferException (org.eclipse.ecf.filetransfer.IncomingFileTransferException)4 UserCancelledException (org.eclipse.ecf.filetransfer.UserCancelledException)4 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 IFileTransferEvent (org.eclipse.ecf.filetransfer.events.IFileTransferEvent)3 IFileTransferPausable (org.eclipse.ecf.filetransfer.IFileTransferPausable)2 IIncomingFileTransfer (org.eclipse.ecf.filetransfer.IIncomingFileTransfer)2 IIncomingFileTransferReceiveDataEvent (org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDataEvent)2 IIncomingFileTransferReceivePausedEvent (org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceivePausedEvent)2 IIncomingFileTransferReceiveResumedEvent (org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveResumedEvent)2 IIncomingFileTransferReceiveStartEvent (org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveStartEvent)2 FileNotFoundException (java.io.FileNotFoundException)1 OutputStream (java.io.OutputStream)1 MalformedURLException (java.net.MalformedURLException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 HttpRequestHandler (org.apache.commons.httpclient.server.HttpRequestHandler)1