use of org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveStartEvent in project ecf by eclipse.
the class RetrieveFileTransfer method openStreams.
/*
* (non-Javadoc)
*
* @see
* org.eclipse.ecf.provider.filetransfer.retrieve.AbstractRetrieveFileTransfer
* #openStreams()
*/
protected void openStreams() throws IncomingFileTransferException {
try {
final IFileStore fileStore = EFS.getStore(new URI(getRemoteFileURL().getPath()));
final IFileInfo info = fileStore.fetchInfo();
setFileLength(info.getLength());
setInputStream(fileStore.openInputStream(0, null));
setLastModifiedTime(info.getLastModified());
fileName = info.getName();
listener.handleTransferEvent(new IIncomingFileTransferReceiveStartEvent() {
public IIncomingFileTransfer getSource() {
return RetrieveFileTransfer.this;
}
public IFileID getFileID() {
return remoteFileID;
}
public IIncomingFileTransfer receive(File localFileToSave) throws IOException {
return receive(localFileToSave, null);
}
public IIncomingFileTransfer receive(File localFileToSave, FileTransferJob fileTransferJob) throws IOException {
setOutputStream(new BufferedOutputStream(new FileOutputStream(localFileToSave)));
setupAndScheduleJob(fileTransferJob);
return RetrieveFileTransfer.this;
}
public String toString() {
final StringBuffer sb = new StringBuffer(// $NON-NLS-1$
"IIncomingFileTransferReceiveStartEvent[");
// $NON-NLS-1$ //$NON-NLS-2$
sb.append("isdone=").append(done).append(";");
// $NON-NLS-1$
sb.append("bytesReceived=").append(bytesReceived).append(// $NON-NLS-1$
"]");
return sb.toString();
}
public void cancel() {
hardClose();
}
/**
* @param streamToStore
* @return incoming file transfer instance.
* @throws IOException
* not thrown in this implementation.
*/
public IIncomingFileTransfer receive(OutputStream streamToStore) throws IOException {
return receive(streamToStore, null);
}
/**
* @throws IOException
* not actually thrown by this implementation.
*/
public IIncomingFileTransfer receive(OutputStream streamToStore, FileTransferJob fileTransferJob) throws IOException {
setOutputStream(streamToStore);
setCloseOutputStream(false);
setupAndScheduleJob(fileTransferJob);
return RetrieveFileTransfer.this;
}
public Map getResponseHeaders() {
return null;
}
});
} catch (final Exception e) {
throw new IncomingFileTransferException(e);
}
}
use of org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveStartEvent 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.events.IIncomingFileTransferReceiveStartEvent 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.events.IIncomingFileTransferReceiveStartEvent 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;
}
use of org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveStartEvent 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();
}
}
Aggregations