Search in sources :

Example 1 with IFileTransferConnectStartEvent

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

the class HttpClientRetrieveFileTransfer method fireConnectStartEvent.

protected void fireConnectStartEvent() {
    // $NON-NLS-1$
    Trace.entering(Activator.PLUGIN_ID, DebugOptions.METHODS_ENTERING, this.getClass(), "fireConnectStartEvent");
    // TODO: should the following be in super.fireReceiveStartEvent();
    listener.handleTransferEvent(new IFileTransferConnectStartEvent() {

        public IFileID getFileID() {
            return remoteFileID;
        }

        public void cancel() {
            HttpClientRetrieveFileTransfer.this.cancel();
        }

        public FileTransferJob prepareConnectJob(FileTransferJob j) {
            return HttpClientRetrieveFileTransfer.this.prepareConnectJob(j);
        }

        public void connectUsingJob(FileTransferJob j) {
            HttpClientRetrieveFileTransfer.this.connectUsingJob(j);
        }

        public String toString() {
            // $NON-NLS-1$
            final StringBuffer sb = new StringBuffer("IFileTransferConnectStartEvent[");
            sb.append(getFileID());
            // $NON-NLS-1$
            sb.append("]");
            return sb.toString();
        }

        public Object getAdapter(Class adapter) {
            return HttpClientRetrieveFileTransfer.this.getAdapter(adapter);
        }
    });
}
Also used : IFileID(org.eclipse.ecf.filetransfer.identity.IFileID) FileTransferJob(org.eclipse.ecf.filetransfer.FileTransferJob) IFileTransferConnectStartEvent(org.eclipse.ecf.filetransfer.events.IFileTransferConnectStartEvent)

Example 2 with IFileTransferConnectStartEvent

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

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

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

the class URLRetrieveTestCancelConnectJob method testReceiveFile_cancelOnConnectEvent.

public void testReceiveFile_cancelOnConnectEvent() throws Exception {
    if (!CANCEL_SUPPORTED_ON_CONNECT) {
        trace("WARNING:  Cancel not supported by this provider.  testReceiveFile_cancelOnConnectEvent cannot be used");
        return;
    }
    final IFileTransferListener listener = createFileTransferListener();
    final FileTransferListenerWrapper lw = new FileTransferListenerWrapper(listener) {

        protected void handleStartConnectEvent(IFileTransferConnectStartEvent event) {
            assertNotNull(event.getFileID());
            assertNotNull(event.getFileID().getFilename());
            assertNull(socketInReadWrapper);
            setDone(true);
            event.cancel();
        }
    };
    testReceive(URLRetrieveTest.HTTP_RETRIEVE, lw);
    assertHasEvent(startConnectEvents, IFileTransferConnectStartEvent.class);
    assertHasNoEvent(startEvents, IIncomingFileTransferReceiveStartEvent.class);
    assertHasNoEvent(dataEvents, IIncomingFileTransferReceiveDataEvent.class);
    assertDoneCancelled();
    assertNull(tmpFile);
    socketEvents.validateNoSocketCreated();
}
Also used : IFileTransferListener(org.eclipse.ecf.filetransfer.IFileTransferListener) IFileTransferConnectStartEvent(org.eclipse.ecf.filetransfer.events.IFileTransferConnectStartEvent)

Example 5 with IFileTransferConnectStartEvent

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

Aggregations

IFileTransferConnectStartEvent (org.eclipse.ecf.filetransfer.events.IFileTransferConnectStartEvent)6 FileTransferJob (org.eclipse.ecf.filetransfer.FileTransferJob)5 IFileTransferListener (org.eclipse.ecf.filetransfer.IFileTransferListener)5 IOException (java.io.IOException)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 SimpleServer (org.eclipse.ecf.internal.tests.filetransfer.httpserver.SimpleServer)3 ResponseWriter (org.apache.commons.httpclient.server.ResponseWriter)2 IIncomingFileTransferReceiveStartEvent (org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveStartEvent)2 UnknownHostException (java.net.UnknownHostException)1 IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)1 UserCancelledException (org.eclipse.ecf.filetransfer.UserCancelledException)1 IIncomingFileTransferReceiveDoneEvent (org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDoneEvent)1 IFileID (org.eclipse.ecf.filetransfer.identity.IFileID)1