From 98cfa2231cb0931fd1d3f6d6582becfd359881f9 Mon Sep 17 00:00:00 2001 From: Nils Schwabe Date: Thu, 5 Jun 2014 12:34:12 +0200 Subject: Started to implement download of images --- .../openslx/imagemaster/ftp/MasterFtpServer.java | 129 ++++++++++++++++++--- 1 file changed, 114 insertions(+), 15 deletions(-) (limited to 'src/main/java/org/openslx/imagemaster/ftp/MasterFtpServer.java') diff --git a/src/main/java/org/openslx/imagemaster/ftp/MasterFtpServer.java b/src/main/java/org/openslx/imagemaster/ftp/MasterFtpServer.java index 9dae088..f8b8ca1 100644 --- a/src/main/java/org/openslx/imagemaster/ftp/MasterFtpServer.java +++ b/src/main/java/org/openslx/imagemaster/ftp/MasterFtpServer.java @@ -29,14 +29,55 @@ import org.openslx.imagemaster.util.Util; public class MasterFtpServer implements Runnable { + private static Logger log = Logger.getLogger( MasterFtpServer.class ); private FtpServer server; private UserManager userManager; private Listener listener; - // key: ftpUsername, value: createTime - public final HashMap users = new HashMap<>(); + // key: ftpUsername, value: infos + public final HashMap users = new HashMap<>(); private boolean ini = false; + public enum Mode + { + UPLOADING, DOWNLOADING + } + + /** + * Class to hold infos of a ftp user. + * + * @author nils + * + */ + public class Infos + { + private final Long createTime; + private final Mode mode; + private final String fileName; + + public Infos(Long createTime, Mode mode, String fileName) + { + this.createTime = createTime; + this.mode = mode; + this.fileName = fileName; + } + + public Long getCreateTime() + { + return this.createTime; + } + + public Mode getMode() + { + return this.mode; + } + + public String getFileName() + { + return this.fileName; + } + } + public void init( int port ) { if ( ini ) @@ -77,8 +118,53 @@ public class MasterFtpServer implements Runnable server = serverFactory.createServer(); ini = true; } + + /** + * Add a user with username and password. + * @param username + * @param password + * @param mode + * @param fileName the filename of the file, that is allowed to access (if downloading) + * @return + */ + public boolean addUser( String username, String password, Mode mode, String fileName) + { + String dir = Globals.getPropertyString( Globals.PropString.FTPBASEDIR ) + "/" + username + "/"; + + BaseUser user = new BaseUser(); + user.setName( username ); + user.setPassword( password ); + List authorities = new ArrayList(); + + String file = ""; + + if (mode == Mode.UPLOADING) { + user.setHomeDirectory( dir ); + // uploading satellite is allowed to write + authorities.add( new WritePermission() ); + } else if (mode == Mode.DOWNLOADING) { + // the downloading satellite may access the whole dir, but this is restricted in MasterFtplet + user.setHomeDirectory( Globals.getPropertyString( Globals.PropString.FTPBASEDIR ) ); + // downloading satellite is only allowed to read + file = fileName; + } + + user.setAuthorities( authorities ); + + try { + userManager.save( user ); + synchronized ( users ) { + users.put( username, new Infos( System.currentTimeMillis(), mode, file) ); + + } + } catch ( FtpException e ) { + return false; + } + + return true; + } - public FtpCredentials addUser( final String serverSessionId ) + public FtpCredentials addUser( final String serverSessionId, Mode mode, String fileName ) { FtpCredentials ftpCredentials = null; @@ -88,26 +174,38 @@ public class MasterFtpServer implements Runnable String dir = Globals.getPropertyString( Globals.PropString.FTPBASEDIR ) + "/" + generatedUser + "/"; - if ( !new File( dir ).mkdir() ) { - return ftpCredentials; - } - BaseUser user = new BaseUser(); user.setName( generatedUser ); user.setPassword( generatedPass ); - user.setHomeDirectory( dir ); - List authorities = new ArrayList(); - authorities.add( new WritePermission() ); + + String file = ""; + + if (mode == Mode.UPLOADING) { + // uploading satellite needs a folder + if ( !new File( dir ).mkdir() ) { + return null; + } + user.setHomeDirectory( dir ); + // uploading satellite is allowed to write + authorities.add( new WritePermission() ); + } else if (mode == Mode.DOWNLOADING) { + // the downloading satellite may access the whole dir, but this is restricted in MasterFtplet + user.setHomeDirectory( Globals.getPropertyString( Globals.PropString.FTPBASEDIR ) ); + // downloading satellite is only allowed to read + file = fileName; + } + user.setAuthorities( authorities ); try { userManager.save( user ); ftpCredentials = new FtpCredentials( generatedUser, generatedPass ); synchronized ( users ) { - users.put( ftpCredentials.username, System.currentTimeMillis()); + users.put( ftpCredentials.username, new Infos( System.currentTimeMillis(), mode, file) ); } } catch ( FtpException e ) { + // TODO: handle this } log.info( "Generated user/pass: " + generatedUser + "\t" @@ -124,10 +222,11 @@ public class MasterFtpServer implements Runnable try { // first find active session and close it Iterator iter = listener.getActiveSessions().iterator(); - while (iter.hasNext()) { - FtpIoSession session = (FtpIoSession) iter.next(); - if (session.getUser() == null) continue; - if (session.getUser().getName() == username) { + while ( iter.hasNext() ) { + FtpIoSession session = (FtpIoSession)iter.next(); + if ( session.getUser() == null ) + continue; + if ( session.getUser().getName() == username ) { session.close(); } } -- cgit v1.2.3-55-g7522