Scout/HowTo/3.7/Use RemoteFileService

From Eclipsepedia

< Scout‎ | HowTo‎ | 3.7
Jump to: navigation, search


Scout
Wiki Home
Website
DownloadGit
Community
ForumsBlogTwitter
Bugzilla
Bugzilla


With RemoteFileService you can load files from the server to the client.

Contents

Set rootPath

Before using the remote file service, for security reasons, the server has to know what the root directory is where to load the files from. You can configure this setting in the config.ini of the server. If this parameter has not been set, you will get a SecurityException("invalid path for file service: path may not be null");

org.eclipse.scout.rt.server.services.common.file.RemoteFileService#rootPath=D:/

Get one file

 RemoteFile file = SERVICES.getService(IRemoteFileService.class).getRemoteFile(new RemoteFile("mydocuments", "document1.txt", 0L));
 try {
    System.out.println(file.getName());
    System.out.println(new String(file.extractData()));
 }
    catch (IOException e) {
    e.printStackTrace(
 }

It will print the name and the contents of the file.

Get all the files in a directory

 RemoteFile[] files = SERVICES.getService(IRemoteFileService.class).getRemoteFiles("mydocuments", new NoFilenameFilter(), null);
 for (int i = 0; i < files.length; i++) {
   System.out.println(files[i].getName());
   try {
     System.out.println(new String(files[i].extractData()));
   }
   catch (IOException e) {
     e.printStackTrace();
   }
 }

the second parameter in the call is a instance of java.io.FilenameFilter

 import java.io.File;
 import java.io.FilenameFilter;
 import java.io.Serializable; 
 
 public class NoFilenameFilter implements FilenameFilter, Serializable {
   @Override
   public boolean accept(File dir, String name) {
     return true;
   }
 }

Create such a class and put it in the shared plug-in. Because this instance of FilenameFilter does not filter at all, the code above will print all the filenames. Be aware that it also loads all the contents of the files, so getting a lot of files will result in an out of memory soon.

Get particular files in a directory

If you create a better filter you can get only the files that you need.

 import java.io.File;
 import java.io.FilenameFilter;
 import java.io.Serializable;
 
 public class OnlyTxtFilenameFilter implements Serializable, FilenameFilter {
   @Override
   public boolean accept(File dir, String name) {
     return name.endsWith(".txt");
   }
 }

Of course you could make your filter more flexible by adding a parameter to the constructor or create some setters.

Getting large files

If you need to get a large file, the RemoteFileService offers methods to only get a header (getRemoteFileHeader()) and to get the contents of the file in parts (getRemoteFilePart). See I obj.pngIRemoteFileService for detailed information.