From ae03da8d1c1010c722642a4d36c68154ef370fcf Mon Sep 17 00:00:00 2001 From: Kushal Munir < kmunir@ca.ibm.com> Date: Tue, 20 Feb 2007 04:17:18 +0000 Subject: [PATCH] [162954] [api] Provide a way to get InputStream and OutputStream from file services. Added a getInputStream() and getOutputStream() to IFileService and implemented for SSH, FTP and Local. Dstore needs to be implemented. --- .../dstore/files/DStoreFileService.java | 17 ++++ .../services/files/ftp/FTPService.java | 81 ++++++++++++++++++- .../local/files/LocalFileService.java | 42 +++++++++- .../services/ssh/files/SftpFileService.java | 72 ++++++++++++++++- .../services/files/AbstractFileService.java | 19 +++++ .../rse/services/files/IFileService.java | 26 ++++++ 6 files changed, 252 insertions(+), 5 deletions(-) diff --git a/rse/plugins/org.eclipse.rse.services.dstore/src/org/eclipse/rse/services/dstore/files/DStoreFileService.java b/rse/plugins/org.eclipse.rse.services.dstore/src/org/eclipse/rse/services/dstore/files/DStoreFileService.java index 047868c3a63..6fb1ee9d284 100644 --- a/rse/plugins/org.eclipse.rse.services.dstore/src/org/eclipse/rse/services/dstore/files/DStoreFileService.java +++ b/rse/plugins/org.eclipse.rse.services.dstore/src/org/eclipse/rse/services/dstore/files/DStoreFileService.java @@ -22,6 +22,7 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.text.MessageFormat; import java.util.ArrayList; @@ -1313,4 +1314,20 @@ public class DStoreFileService extends AbstractDStoreService implements IFileSer return remoteEncoding; } + + /* (non-Javadoc) + * @see org.eclipse.rse.services.files.IFileService#getInputStream(org.eclipse.core.runtime.IProgressMonitor, java.lang.String, java.lang.String, boolean) + */ + public InputStream getInputStream(IProgressMonitor monitor, String remoteParent, String remoteFile, boolean isBinary) throws SystemMessageException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.rse.services.files.IFileService#getOutputStream(org.eclipse.core.runtime.IProgressMonitor, java.lang.String, java.lang.String, boolean) + */ + public OutputStream getOutputStream(IProgressMonitor monitor, String remoteParent, String remoteFile, boolean isBinary) throws SystemMessageException { + // TODO Auto-generated method stub + return null; + } } \ No newline at end of file diff --git a/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/internal/services/files/ftp/FTPService.java b/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/internal/services/files/ftp/FTPService.java index f173478ebcf..a70560fb286 100644 --- a/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/internal/services/files/ftp/FTPService.java +++ b/rse/plugins/org.eclipse.rse.services.files.ftp/src/org/eclipse/rse/internal/services/files/ftp/FTPService.java @@ -1070,6 +1070,83 @@ public class FTPService extends AbstractFileService implements IFileService, IFT return result; } - - + + /** + * Gets the input stream to access the contents of a remote file. + * @since 2.0 + * @see org.eclipse.rse.services.files.AbstractFileService#getInputStream(org.eclipse.core.runtime.IProgressMonitor, java.lang.String, java.lang.String, boolean) + */ + public InputStream getInputStream(IProgressMonitor monitor, String remoteParent, String remoteFile, boolean isBinary) throws SystemMessageException { + + if (monitor != null){ + + if (monitor.isCanceled()) { + return null; + } + } + + FTPClient ftpClient = getFTPClient(); + + InputStream stream = null; + + try { + remoteParent = adaptPath(remoteParent); + + ftpClient.changeWorkingDirectory(remoteParent); + + if (isBinary) { + ftpClient.setFileType(FTP.BINARY_FILE_TYPE); + } + else { + ftpClient.setFileType(FTP.ASCII_FILE_TYPE); + } + + stream = ftpClient.retrieveFileStream(remoteFile); + } + catch (Exception e) { + throw new RemoteFileIOException(e); + } + + return stream; + } + + /** + * Gets the output stream to write to a remote file. + * @since 2.0 + * @see org.eclipse.rse.services.files.AbstractFileService#getOutputStream(org.eclipse.core.runtime.IProgressMonitor, java.lang.String, java.lang.String, boolean) + */ + public OutputStream getOutputStream(IProgressMonitor monitor, String remoteParent, String remoteFile, boolean isBinary) throws SystemMessageException { + + if (monitor != null){ + + if (monitor.isCanceled()) { + return null; + } + } + + FTPClient ftpClient = getFTPClient(); + + OutputStream stream = null; + + try { + + remoteParent = adaptPath(remoteParent); + + ftpClient.changeWorkingDirectory(remoteParent); + + if (isBinary) { + ftpClient.setFileType(FTP.BINARY_FILE_TYPE); + } + else { + ftpClient.setFileType(FTP.ASCII_FILE_TYPE); + } + + stream = ftpClient.storeFileStream(remoteFile); + } + catch (Exception e) { + throw new RemoteFileIOException(e); + } + + return stream; + } } \ No newline at end of file diff --git a/rse/plugins/org.eclipse.rse.services.local/src/org/eclipse/rse/internal/services/local/files/LocalFileService.java b/rse/plugins/org.eclipse.rse.services.local/src/org/eclipse/rse/internal/services/local/files/LocalFileService.java index 2225cb2d769..cdb9669172a 100644 --- a/rse/plugins/org.eclipse.rse.services.local/src/org/eclipse/rse/internal/services/local/files/LocalFileService.java +++ b/rse/plugins/org.eclipse.rse.services.local/src/org/eclipse/rse/internal/services/local/files/LocalFileService.java @@ -27,6 +27,7 @@ import java.io.FileOutputStream; import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.util.ArrayList; @@ -50,6 +51,7 @@ import org.eclipse.rse.services.files.AbstractFileService; import org.eclipse.rse.services.files.IFileService; import org.eclipse.rse.services.files.IHostFile; import org.eclipse.rse.services.files.RemoteFileException; +import org.eclipse.rse.services.files.RemoteFileIOException; public class LocalFileService extends AbstractFileService implements IFileService, ILocalService { @@ -1364,5 +1366,43 @@ public class LocalFileService extends AbstractFileService implements IFileServic } } - + /** + * Gets the input stream to access the contents of a remote file. + * @since 2.0 + * @see org.eclipse.rse.services.files.AbstractFileService#getInputStream(IProgressMonitor, String, String, boolean) + */ + public InputStream getInputStream(IProgressMonitor monitor, String remoteParent, String remoteFile, boolean isBinary) throws SystemMessageException { + + File file = new File(remoteParent, remoteFile); + InputStream stream = null; + + try { + stream = new FileInputStream(file); + } + catch (Exception e) { + throw new RemoteFileIOException(e); + } + + return stream; + } + + /** + * Gets the output stream to write to a remote file. + * @since 2.0 + * @see org.eclipse.rse.services.files.AbstractFileService#getOutputStream(IProgressMonitor, String, String, boolean) + */ + public OutputStream getOutputStream(IProgressMonitor monitor, String remoteParent, String remoteFile, boolean isBinary) throws SystemMessageException { + + File file = new File(remoteParent, remoteFile); + OutputStream stream = null; + + try { + stream = new FileOutputStream(file); + } + catch (Exception e) { + throw new RemoteFileIOException(e); + } + + return stream; + } } \ No newline at end of file diff --git a/rse/plugins/org.eclipse.rse.services.ssh/src/org/eclipse/rse/internal/services/ssh/files/SftpFileService.java b/rse/plugins/org.eclipse.rse.services.ssh/src/org/eclipse/rse/internal/services/ssh/files/SftpFileService.java index 5ee5e87b7ae..fccf9ec5887 100644 --- a/rse/plugins/org.eclipse.rse.services.ssh/src/org/eclipse/rse/internal/services/ssh/files/SftpFileService.java +++ b/rse/plugins/org.eclipse.rse.services.ssh/src/org/eclipse/rse/internal/services/ssh/files/SftpFileService.java @@ -7,7 +7,8 @@ * * Contributors: * Martin Oberhuber (Wind River) - initial API and implementation - * Dave Dykstal (IBM) - fixing bug 162510: correctly process filter strings + * Dave Dykstal (IBM) - fixing bug 162510: correctly process filter strings + * Kushal Munir (IBM) - for API bug *******************************************************************************/ package org.eclipse.rse.internal.services.ssh.files; @@ -765,4 +766,71 @@ public class SftpFileService extends AbstractFileService implements IFileService return false; } -} + /** + * Gets the input stream to access the contents of a remote file. Clients should call {@link #disconnect()} after closing the input stream. + * @since 2.0 + * @see org.eclipse.rse.services.files.AbstractFileService#getInputStream(IProgressMonitor, String, String, boolean) + */ + public InputStream getInputStream(IProgressMonitor monitor, String remoteParent, String remoteFile, boolean isBinary) throws SystemMessageException { + + InputStream stream = null; + + try { + String remotePath = remoteParent + '/' + remoteFile; + int mode = ChannelSftp.OVERWRITE; + MyProgressMonitor sftpMonitor = new MyProgressMonitor(monitor); + getChannel("SftpFileService.getInputStream " + remoteFile); //check the session is healthy //$NON-NLS-1$ + ChannelSftp channel = (ChannelSftp)fSessionProvider.getSession().openChannel("sftp"); //$NON-NLS-1$ + channel.connect(); + stream = channel.get(remotePath, sftpMonitor, mode); + Activator.trace("SftpFileService.getInputStream " + remoteFile + " ok"); //$NON-NLS-1$ //$NON-NLS-2$ + } + catch (Exception e) { + Activator.trace("SftpFileService.getInputStream " + remoteFile + " failed: " + e.toString()); //$NON-NLS-1$ //$NON-NLS-2$ + throw makeSystemMessageException(e); + } + + return stream; + } + + /** + * Gets the output stream to write to a remote file. Clients should call {@link #disconnect()} after closing the output stream. + * @since 2.0 + * @see org.eclipse.rse.services.files.AbstractFileService#getOutputStream(IProgressMonitor, String, String, boolean) + */ + public OutputStream getOutputStream(IProgressMonitor monitor, String remoteParent, String remoteFile, boolean isBinary) throws SystemMessageException { + + if (monitor == null) { + monitor = new NullProgressMonitor(); + } + + OutputStream stream = null; + + try { + SftpProgressMonitor sftpMonitor = new MyProgressMonitor(monitor); + int mode = ChannelSftp.OVERWRITE; + String dst = remoteParent; + + if (remoteFile != null) { + + if (!dst.endsWith("/")) { //$NON-NLS-1$ + dst += '/'; + } + + dst += remoteFile; + } + + getChannel("SftpFileService.getOutputStream " + remoteFile); //check the session is healthy //$NON-NLS-1$ + ChannelSftp channel = (ChannelSftp)fSessionProvider.getSession().openChannel("sftp"); //$NON-NLS-1$ + channel.connect(); + stream = channel.put(dst, sftpMonitor, mode); + Activator.trace("SftpFileService.getOutputStream " + remoteFile + " ok"); //$NON-NLS-1$ //$NON-NLS-2$ + } + catch (Exception e) { + Activator.trace("SftpFileService.getOutputStream " + remoteFile + " failed: " + e.toString()); //$NON-NLS-1$ //$NON-NLS-2$ + throw makeSystemMessageException(e); + } + + return stream; + } +} \ No newline at end of file diff --git a/rse/plugins/org.eclipse.rse.services/src/org/eclipse/rse/services/files/AbstractFileService.java b/rse/plugins/org.eclipse.rse.services/src/org/eclipse/rse/services/files/AbstractFileService.java index 348e4329532..2ef06e09cd0 100644 --- a/rse/plugins/org.eclipse.rse.services/src/org/eclipse/rse/services/files/AbstractFileService.java +++ b/rse/plugins/org.eclipse.rse.services/src/org/eclipse/rse/services/files/AbstractFileService.java @@ -16,6 +16,9 @@ package org.eclipse.rse.services.files; +import java.io.InputStream; +import java.io.OutputStream; + import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.rse.services.clientserver.messages.SystemMessage; @@ -102,4 +105,20 @@ public abstract class AbstractFileService implements IFileService public String getEncoding(IProgressMonitor monitor) throws SystemMessageException { return System.getProperty("file.encoding"); //$NON-NLS-1$ } + + /** + * The default implementation returns null. Clients can override to return an input stream to the file. + * @see org.eclipse.rse.services.files.IFileService#getInputStream(IProgressMonitor, String, String, boolean) + */ + public InputStream getInputStream(IProgressMonitor monitor, String remoteParent, String remoteFile, boolean isBinary) throws SystemMessageException { + return null; + } + + /** + * The default implementation returns null. Clients can override to return an output stream to the file. + * @see org.eclipse.rse.services.files.IFileService#getOutputStream(IProgressMonitor, String, String, boolean) + */ + public OutputStream getOutputStream(IProgressMonitor monitor, String remoteParent, String remoteFile, boolean isBinary) throws SystemMessageException { + return null; + } } \ No newline at end of file diff --git a/rse/plugins/org.eclipse.rse.services/src/org/eclipse/rse/services/files/IFileService.java b/rse/plugins/org.eclipse.rse.services/src/org/eclipse/rse/services/files/IFileService.java index 5da6a8e5c8a..a104ffd8898 100644 --- a/rse/plugins/org.eclipse.rse.services/src/org/eclipse/rse/services/files/IFileService.java +++ b/rse/plugins/org.eclipse.rse.services/src/org/eclipse/rse/services/files/IFileService.java @@ -18,8 +18,10 @@ package org.eclipse.rse.services.files; import java.io.File; import java.io.InputStream; +import java.io.OutputStream; import org.eclipse.core.runtime.IProgressMonitor; + import org.eclipse.rse.services.IService; import org.eclipse.rse.services.clientserver.messages.SystemMessageException; @@ -293,4 +295,28 @@ public interface IFileService extends IService * @since 2.0 */ public String getEncoding(IProgressMonitor monitor) throws SystemMessageException; + + /** + * Gets the input stream to access the contents a remote file. Clients should close the input stream when done. + * @param monitor the progress monitor. + * @param remoteParent the absolute path of the parent. + * @param remoteFile the name of the remote file. + * @param isBinary true if the file is a binary file, false otherwise. + * @return the input stream to access the contents of the remote file. + * @throws SystemMessageException if an error occurs. + * @since 2.0 + */ + public InputStream getInputStream(IProgressMonitor monitor, String remoteParent, String remoteFile, boolean isBinary) throws SystemMessageException; + + /** + * Gets the output stream to write to a remote file. Clients should close the output stream when done. + * @param monitor the progress monitor. + * @param remoteParent the absolute path of the parent. + * @param remoteFile the name of the remote file. + * @param isBinary true if the file is a binary file, false otherwise. + * @return the input stream to access the contents of the remote file. + * @throws SystemMessageException if an error occurs. + * @since 2.0 + */ + public OutputStream getOutputStream(IProgressMonitor monitor, String remoteParent, String remoteFile, boolean isBinary) throws SystemMessageException; } \ No newline at end of file