Search in sources :

Example 1 with IFileTransferInfo

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

the class LocalFileOutgoingFileTransfer method openStreams.

/* (non-Javadoc)
	 * @see org.eclipse.ecf.provider.filetransfer.outgoing.AbstractOutgoingFileTransfer#openStreams()
	 */
protected void openStreams() throws SendFileTransferException {
    IFileTransferInfo localFileTransferInfo = getFileTransferInfo();
    Assert.isNotNull(localFileTransferInfo);
    // Setup input file
    File inputFile = localFileTransferInfo.getFile();
    try {
        setInputStream(new BufferedInputStream(new FileInputStream(inputFile)));
    } catch (Exception e) {
        hardClose();
        throw new SendFileTransferException(NLS.bind(Messages.LocalFileOutgoingFileTransfer_EXCEPTION_OPENING_FOR_INPUT, inputFile));
    }
    URL url = getRemoteFileURL();
    Assert.isNotNull(url);
    try {
        File outputFile = new File(url.getPath());
        setOutputStream(new BufferedOutputStream(new FileOutputStream(outputFile)));
    } catch (Exception e) {
        hardClose();
        throw new SendFileTransferException(NLS.bind(Messages.LocalFileOutgoingFileTransfer_EXCEPTION_OPENING_FOR_OUTPUT, url));
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) IFileTransferInfo(org.eclipse.ecf.filetransfer.IFileTransferInfo) SendFileTransferException(org.eclipse.ecf.filetransfer.SendFileTransferException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) FileInputStream(java.io.FileInputStream) SendFileTransferException(org.eclipse.ecf.filetransfer.SendFileTransferException) URL(java.net.URL)

Example 2 with IFileTransferInfo

use of org.eclipse.ecf.filetransfer.IFileTransferInfo 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);
            }
        }
    });
}
Also used : HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IFileTransferInfo(org.eclipse.ecf.filetransfer.IFileTransferInfo) IFileTransferListener(org.eclipse.ecf.filetransfer.IFileTransferListener) IOException(java.io.IOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) IncomingFileTransferException(org.eclipse.ecf.filetransfer.IncomingFileTransferException) IIncomingFileTransfer(org.eclipse.ecf.filetransfer.IIncomingFileTransfer) IFileTransferEvent(org.eclipse.ecf.filetransfer.events.IFileTransferEvent) FileOutputStream(java.io.FileOutputStream) IIncomingFileTransferReceiveDoneEvent(org.eclipse.ecf.filetransfer.events.IIncomingFileTransferReceiveDoneEvent) IAdapterManager(org.eclipse.core.runtime.IAdapterManager) IncomingFileTransferException(org.eclipse.ecf.filetransfer.IncomingFileTransferException) XMPPID(org.eclipse.ecf.provider.xmpp.identity.XMPPID) ID(org.eclipse.ecf.core.identity.ID) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) IFileTransferRequestEvent(org.eclipse.ecf.filetransfer.events.IFileTransferRequestEvent)

Aggregations

File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 IFileTransferInfo (org.eclipse.ecf.filetransfer.IFileTransferInfo)2 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 IAdapterManager (org.eclipse.core.runtime.IAdapterManager)1 ID (org.eclipse.ecf.core.identity.ID)1 IFileTransferListener (org.eclipse.ecf.filetransfer.IFileTransferListener)1 IIncomingFileTransfer (org.eclipse.ecf.filetransfer.IIncomingFileTransfer)1 IncomingFileTransferException (org.eclipse.ecf.filetransfer.IncomingFileTransferException)1 SendFileTransferException (org.eclipse.ecf.filetransfer.SendFileTransferException)1 IFileTransferEvent (org.eclipse.ecf.filetransfer.events.IFileTransferEvent)1 IFileTransferRequestEvent (org.eclipse.ecf.filetransfer.events.IFileTransferRequestEvent)1