Groovy Embedded Apache FTP Server Example


/ Published in: Groovy
Save to your folder(s)

http://mina.apache.org/ftpserver/


Copy this code and paste it in your HTML
  1. #!/usr/bin/env groovy
  2.  
  3. // file: FtpSvr.groovy
  4.  
  5. /*
  6. Laurence Toenjes Jan 12, 2013
  7.  
  8. Standalone Groovy ftp server script (no jars to download if Groovy is
  9. properly installed).
  10.  
  11. The default user is guest with password of guest
  12. and the ftp home/base directory is the home folder of the account
  13. running this script.
  14. A root account is also created with the ftp home/base dir set to /
  15. and the password is root (for root to fully work you would obviously
  16. have to run it under an account with expanded file permissions).
  17.  
  18. Optional command line args:
  19. arg0 defaults to port 8021.
  20. arg1 (ftp base dir) defaults to the home directory of the user running this script.
  21. */
  22.  
  23. // See http://mina.apache.org/ftpserver/ for more details.
  24.  
  25. // for OS X/*nix
  26. // don't forget to do: chmod +x FtpServer.groovy
  27. // also run as sudo ./FtpSvr.groovy for when using a port number < 1024
  28.  
  29. @Grapes([
  30. @Grab(group='org.apache.ftpserver', module='ftpserver-core', version='1.0.6')
  31. , @Grab(group='ch.qos.logback', module='logback-classic', version='1.0.9')
  32. ])
  33.  
  34. import org.apache.ftpserver.FtpServerFactory;
  35. import org.apache.ftpserver.FtpServer;
  36.  
  37. import org.apache.ftpserver.listener.ListenerFactory;
  38.  
  39. import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
  40. import org.apache.ftpserver.usermanager.SaltedPasswordEncryptor;
  41. import org.apache.ftpserver.usermanager.impl.BaseUser;
  42.  
  43. import org.apache.ftpserver.ftplet.UserManager;
  44. import org.apache.ftpserver.ftplet.Authority;
  45.  
  46. import org.apache.ftpserver.usermanager.impl.WritePermission;
  47.  
  48. import java.util.Properties;
  49.  
  50. public class FtpSvr {
  51.  
  52. def go(String[] args) {
  53. String ftpUsersPropFileName = "FtpSvrUsers.properties"
  54. int defaultPort = 8021
  55.  
  56. String userHome = System.properties.'user.home';
  57. String homeDir = args.size()>=2 ? args[1].replace('~', userHome) : userHome ;
  58. assert new File(homeDir).exists();
  59.  
  60. def gi = groovy.inspect.swingui.ObjectBrowser.&inspect; // for debug
  61.  
  62. int port = args.size()>=1 ? args[0].toInteger() : defaultPort ;
  63. println "### ftp port : $port"
  64. println "### ftp homeDir: $homeDir"
  65.  
  66. // the basics
  67. // FtpServerFactory serverFactory = new FtpServerFactory();
  68. // FtpServer server = serverFactory.createServer();
  69. // server.start();
  70.  
  71. // setup users
  72.  
  73. PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
  74.  
  75. def myusersPropsFile = new File( ftpUsersPropFileName );
  76. if ( !myusersPropsFile.exists() ) {
  77. myusersPropsFile.createNewFile();
  78. }
  79.  
  80. userManagerFactory.setFile( myusersPropsFile );
  81. userManagerFactory.setPasswordEncryptor(new SaltedPasswordEncryptor());
  82.  
  83. UserManager um = userManagerFactory.createUserManager();
  84.  
  85. def users = []
  86. users << [ uid:'guest', pwd:'guest', home:homeDir ]
  87. users << [ uid:'root' , pwd:'root' , home:'/' ]
  88.  
  89. def addUser = { map ->
  90. BaseUser user = new BaseUser();
  91. user.setName( map.uid );
  92. user.setPassword( map.pwd );
  93. user.setHomeDirectory( map.home );
  94.  
  95. List<Authority> auths = new ArrayList<Authority>();
  96. Authority auth = new WritePermission();
  97. auths.add(auth);
  98. user.setAuthorities(auths);
  99. // gi user;
  100. // gi user.getAuthorities();
  101. um.save(user);
  102. }
  103.  
  104. users.each { itUserMap -> addUser( itUserMap ) }
  105.  
  106. FtpServerFactory serverFactory = new FtpServerFactory();
  107.  
  108. serverFactory.setUserManager( um );
  109.  
  110. ListenerFactory factory = new ListenerFactory();
  111.  
  112. // set the port of the listener
  113. factory.setPort( port );
  114.  
  115. // replace the default listener
  116. serverFactory.addListener("default", factory.createListener());
  117.  
  118. // start the server
  119. FtpServer server = serverFactory.createServer();
  120.  
  121. // groovy.inspect.swingui.ObjectBrowser.inspect( server );
  122. // groovy.inspect.swingui.ObjectBrowser.inspect( server.userManager );
  123. // gi server.userManager.getAllUserNames() as List;
  124.  
  125. server.start();
  126. }
  127.  
  128. public static void main(String[] args) {
  129. def mo = new FtpSvr();
  130. mo.go(args)
  131. }
  132. }

URL: http://mina.apache.org/ftpserver/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.