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);
}
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));
}
}
}
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();
}
}
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());
}
}
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());
}
}
Aggregations