Search in sources :

Example 1 with IFileTransferListener

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

the class URLCancelTest method testReceive.

protected void testReceive(String url) throws Exception {
    assertNotNull(retrieveAdapter);
    final IFileTransferListener listener = createFileTransferListener();
    final IFileID fileID = createFileID(new URL(url));
    retrieveAdapter.sendRetrieveRequest(fileID, listener, null);
    waitForDone(20000);
}
Also used : IFileID(org.eclipse.ecf.filetransfer.identity.IFileID) IFileTransferListener(org.eclipse.ecf.filetransfer.IFileTransferListener) URL(java.net.URL)

Example 2 with IFileTransferListener

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

the class URLPartialRetrieveTest method testReceiveHttp.

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

        public void handleTransferEvent(IFileTransferEvent event) {
            if (event instanceof IIncomingFileTransferReceiveResumedEvent) {
                try {
                    final IIncomingFileTransferReceiveResumedEvent rse = (IIncomingFileTransferReceiveResumedEvent) event;
                    session = rse.receive(outs);
                } catch (final Exception e) {
                    fail(e.getLocalizedMessage());
                }
            } else if (event instanceof IIncomingFileTransferReceiveStartEvent) {
                final IIncomingFileTransferReceiveStartEvent rse = (IIncomingFileTransferReceiveStartEvent) event;
                try {
                    outs = new FileOutputStream(FILENAME);
                    session = rse.receive(outs);
                    pausable = (IFileTransferPausable) session.getAdapter(IFileTransferPausable.class);
                    if (pausable == null)
                        fail("pausable is null");
                } catch (final 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) {
                    isDone = true;
                    notify.notify();
                }
                session = ((IIncomingFileTransferReceiveDoneEvent) event).getSource();
            }
        }
    };
    final IFileID fileID = FileIDFactory.getDefault().createFileID(transferInstance.getRetrieveNamespace(), url);
    IFileRangeSpecification rangeSpecification = null;
    if (start != -1) {
        rangeSpecification = new IFileRangeSpecification() {

            public long getEndPosition() {
                return end;
            }

            public long getStartPosition() {
                return start;
            }
        };
    }
    transferInstance.sendRetrieveRequest(fileID, rangeSpecification, listener, null);
    if (!isDone) {
        synchronized (notify) {
            notify.wait();
        }
    }
    final Exception e = session.getException();
    if (e != null)
        throw e;
    incomingFile = new File(FILENAME);
    final long fileLength = incomingFile.length();
    final long bytesReceived = session.getBytesReceived();
    System.out.println("start=" + start);
    System.out.println("end=" + end);
    System.out.println("bytes received=" + bytesReceived);
    System.out.println("fileLength=" + fileLength);
    if (start != -1) {
        assertTrue(fileLength == bytesReceived);
        if (end != -1) {
            assertTrue(fileLength == (end - start + 1));
        }
    }
}
Also used : IIncomingFileTransferReceiveResumedEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveResumedEvent) IFileRangeSpecification(org.eclipse.ecf.filetransfer.IFileRangeSpecification) 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) IncomingFileTransferException(org.eclipse.ecf.filetransfer.IncomingFileTransferException) IFileTransferEvent(org.eclipse.ecf.filetransfer.events.IFileTransferEvent) IFileID(org.eclipse.ecf.filetransfer.identity.IFileID) FileOutputStream(java.io.FileOutputStream) IIncomingFileTransferReceiveDoneEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDoneEvent) IIncomingFileTransferReceivePausedEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceivePausedEvent) IIncomingFileTransferReceiveDataEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDataEvent) File(java.io.File)

Example 3 with IFileTransferListener

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

the class URLRetrieveTestCancelConnectJob method testReceiveFile_cancelTransferJobInMiddle.

public void testReceiveFile_cancelTransferJobInMiddle(final long len, final boolean expectedSocketInRead) throws Exception {
    if (!CANCEL_SUPPORTED_ON_CONNECT) {
        trace("WARNING:  Cancel not supported by this provider.  testReceiveFile_cancelTransferJobInMiddle 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() {
                    if (expectedSocketInRead) {
                        waitForSocketInRead();
                    }
                    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 only 50% of body" + request.getRequestLine());
            ResponseWriter w = conn.getWriter();
            writeLines(w, new String[] { "HTTP/1.0 200 OK", "Content-Length: " + len, "Content-Type: text/plain; charset=UTF-8", "" });
            w.flush();
            for (int i = 0; i < len / 2; i++) {
                w.write("x");
            }
            w.flush();
            conn.setKeepAlive(true);
            try {
                // give it a bit of time to receive the data
                Thread.sleep(200);
            } catch (InterruptedException e) {
            }
            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);
        assertHasMoreThanEventCount(dataEvents, IIncomingFileTransferReceiveDataEvent.class, 0);
        assertDoneCancelled();
        assertNotNull(tmpFile);
        assertTrue(tmpFile.exists());
        assertEquals(len / 2, 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 4 with IFileTransferListener

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

the class URLRetrieveTestUnknownHost method testReceiveFile_unknownHostWithConnectJob.

public void testReceiveFile_unknownHostWithConnectJob() throws Exception {
    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);
        }
    };
    try {
        // path does not matter as server does not respond.
        testReceive(HTTP_UNKNOWN_HOST_URL, lw);
        if (CANCEL_SUPPORTED_ON_CONNECT) {
            assertHasEvent(startConnectEvents, IFileTransferConnectStartEvent.class);
        }
        assertHasNoEvent(startEvents, IIncomingFileTransferReceiveStartEvent.class);
        assertHasNoEvent(dataEvents, IIncomingFileTransferReceiveDataEvent.class);
        assertDoneExceptionBeforeServerResponse(UnknownHostException.class);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.toString());
    }
}
Also used : FileTransferJob(org.eclipse.ecf.filetransfer.FileTransferJob) IFileTransferListener(org.eclipse.ecf.filetransfer.IFileTransferListener) UnknownHostException(java.net.UnknownHostException) IFileTransferConnectStartEvent(org.eclipse.ecf.filetransfer.events.IFileTransferConnectStartEvent)

Example 5 with IFileTransferListener

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

the class URLRetrieveTestUnknownHost method testReceiveFile_unknownHostWithoutConnectJob.

public void testReceiveFile_unknownHostWithoutConnectJob() throws Exception {
    final IFileTransferListener listener = createFileTransferListener();
    try {
        // path does not matter as server does not respond.
        testReceive(HTTP_UNKNOWN_HOST_URL, listener);
        if (CANCEL_SUPPORTED_ON_CONNECT) {
            assertHasEvent(startConnectEvents, IFileTransferConnectStartEvent.class);
        }
        assertHasNoEvent(startEvents, IIncomingFileTransferReceiveStartEvent.class);
        assertHasNoEvent(dataEvents, IIncomingFileTransferReceiveDataEvent.class);
        assertHasEvent(doneEvents, IIncomingFileTransferReceiveDoneEvent.class);
        assertDoneExceptionBeforeServerResponse(UnknownHostException.class);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.toString());
    }
}
Also used : IFileTransferListener(org.eclipse.ecf.filetransfer.IFileTransferListener) UnknownHostException(java.net.UnknownHostException)

Aggregations

IFileTransferListener (org.eclipse.ecf.filetransfer.IFileTransferListener)18 IOException (java.io.IOException)9 IFileID (org.eclipse.ecf.filetransfer.identity.IFileID)7 File (java.io.File)5 URL (java.net.URL)5 FileTransferJob (org.eclipse.ecf.filetransfer.FileTransferJob)5 IFileTransferConnectStartEvent (org.eclipse.ecf.filetransfer.events.IFileTransferConnectStartEvent)5 IFileTransferEvent (org.eclipse.ecf.filetransfer.events.IFileTransferEvent)5 IIncomingFileTransferReceiveStartEvent (org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveStartEvent)5 IIncomingFileTransferReceiveDoneEvent (org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDoneEvent)4 FileOutputStream (java.io.FileOutputStream)3 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 IncomingFileTransferException (org.eclipse.ecf.filetransfer.IncomingFileTransferException)3 SimpleServer (org.eclipse.ecf.internal.tests.filetransfer.httpserver.SimpleServer)3 UnknownHostException (java.net.UnknownHostException)2 ResponseWriter (org.apache.commons.httpclient.server.ResponseWriter)2 IFileTransferPausable (org.eclipse.ecf.filetransfer.IFileTransferPausable)2