use of org.eclipse.ecf.filetransfer.IIncomingFileTransfer 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;
}
});
}
use of org.eclipse.ecf.filetransfer.IIncomingFileTransfer in project ecf by eclipse.
the class BitTorrentContainer method sendRetrieveRequest.
public void sendRetrieveRequest(final IFileID remoteFileReference, final IFileTransferListener transferListener, Map options) throws IncomingFileTransferException {
// $NON-NLS-1$
Assert.isNotNull(remoteFileReference, "remoteFileReference cannot be null");
// $NON-NLS-1$
Assert.isLegal(remoteFileReference instanceof TorrentID, "remoteFileReference must be instanceof TorrentID");
// $NON-NLS-1$
Assert.isNotNull(transferListener, "transferListener cannot be null");
transferListener.handleTransferEvent(new IIncomingFileTransferReceiveStartEvent() {
private IIncomingFileTransfer transfer;
private boolean cancelled = false;
public IIncomingFileTransfer receive(File localFileToSave) throws IOException {
if (cancelled) {
throw new RuntimeException(new UserCancelledException());
}
// $NON-NLS-1$
Assert.isNotNull(localFileToSave, "localFileToSave cannot be null");
if (localFileToSave.exists() && !localFileToSave.canWrite()) {
// $NON-NLS-1$ //$NON-NLS-2$
throw new IOException("file=" + localFileToSave.getAbsolutePath() + " does not exist or cannot be written to");
}
final TorrentFile file = new TorrentFile(((TorrentID) remoteFileReference).getFile());
file.setTargetFile(localFileToSave);
final Torrent torrent = TorrentFactory.createTorrent(file);
transfer = new TorrentFileTransfer(remoteFileReference, transferListener, torrent);
return transfer;
}
public IIncomingFileTransfer receive(File localFileToSave, FileTransferJob fileTransferJob) throws IOException {
if (cancelled) {
throw new RuntimeException(new UserCancelledException());
}
// $NON-NLS-1$
Assert.isNotNull(localFileToSave, "localFileToSave must not be null");
if (localFileToSave.exists() && !localFileToSave.canWrite()) {
// $NON-NLS-1$ //$NON-NLS-2$
throw new IOException("file=" + localFileToSave.getAbsolutePath() + " does not exist or cannot be written to");
}
final TorrentFile file = new TorrentFile(((TorrentID) remoteFileReference).getFile());
file.setTargetFile(localFileToSave);
final Torrent torrent = TorrentFactory.createTorrent(file);
transfer = new TorrentFileTransfer(remoteFileReference, transferListener, torrent);
return transfer;
}
public void cancel() {
if (transfer != null) {
transfer.cancel();
cancelled = true;
}
}
public IFileID getFileID() {
return remoteFileReference;
}
public IIncomingFileTransfer receive(OutputStream streamToStore) throws IOException {
throw new UnsupportedOperationException("receive(OutputStream) not supported by this provider");
}
public IIncomingFileTransfer receive(OutputStream streamToStore, FileTransferJob fileTransferJob) throws IOException {
throw new UnsupportedOperationException("receive(OutputStream,FileTransferJob) not supported by this provider");
}
public IIncomingFileTransfer getSource() {
return transfer;
}
public Map getResponseHeaders() {
return null;
}
});
}
use of org.eclipse.ecf.filetransfer.IIncomingFileTransfer 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);
}
}
});
}
Aggregations