rana_b      2002/10/29 09:02:55

  Modified:    ftpserver/src/java/org/apache/avalon/ftpserver
                        SiteCommandHandler.java
  Log:
  SITE STAT added to get server statistics
  
  Revision  Changes    Path
  1.6       +169 -140  
jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/SiteCommandHandler.java
  
  Index: SiteCommandHandler.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/SiteCommandHandler.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- SiteCommandHandler.java   10 Oct 2002 16:01:01 -0000      1.5
  +++ SiteCommandHandler.java   29 Oct 2002 17:02:55 -0000      1.6
  @@ -1,3 +1,4 @@
  +//$Id$
   /*
    * Copyright (C) The Apache Software Foundation. All rights reserved.
    *
  @@ -5,7 +6,6 @@
    * version 1.1, a copy of which has been included with this distribution in
    * the LICENSE file.
    */
  -
   package org.apache.avalon.ftpserver;
   
   import java.util.Date;
  @@ -28,20 +28,20 @@
    */
   public
   class SiteCommandHandler {
  -
  +    
       // as SimpleDateFormat is not thread-safe we have to use ThreadLocal
  -    private final static ThreadLocal DATE_FMT = new ThreadLocal() {
  +    private final static ThreadLocal DATE_FMT = new ThreadLocal()  {
           protected Object initialValue() {
  -            return new SimpleDateFormat("MM/dd HH:mm:ss"); 
  +            return new SimpleDateFormat("MM/dd HH:mm:ss");
           }
       };
       
       protected final static Class[] INPUT_SIG = new Class[] {String[].class, 
FtpRequest.class};
  -
  +    
       private FtpConfig mConfig;
       private FtpUser mUser;
  -
  -
  +    
  +    
       /**
        * Constructor - set the configuration object
        */
  @@ -49,17 +49,17 @@
           mConfig = cfg;
           mUser = user;
       }
  -
  -
  +    
  +    
       /**
        * Handle site.
        */
       public String getResponse(FtpRequest request) {
           String argArray[] = parseArg(request.getArgument());
  -
  +        
           String response = "";
  -        if (hasPermission(argArray)) {
  -            if ((argArray != null) && (argArray.length != 0)) {
  +        if(hasPermission(argArray)) {
  +            if((argArray != null) && (argArray.length != 0)) {
                   try {
                       String metName = "do" + argArray[0].toUpperCase();
                       Method actionMet = getClass().getDeclaredMethod(metName, 
INPUT_SIG);
  @@ -77,128 +77,132 @@
           else {
               response = mConfig.getStatus().getResponse(530, request, mUser, null);
           }
  -
  +        
           return response;
       }
  -
  -
  +    
  +    
       /**
        * Parse all the tokens.
        */
       private String[] parseArg(String arg) {
  -        if (arg == null) {
  +        if(arg == null) {
               return null;
           }
  -
  +        
           StringTokenizer st = new StringTokenizer(arg, " ");
           String[] args = new String[st.countTokens()];
  -        for(int i=0; i<args.length; i++) {
  +        for(int i = 0;i < args.length;i++) {
               args[i] = st.nextToken();
           }
  -
  +        
           return args;
       }
  -
  -
  +    
  +    
       /**
        * Has permission
        */
       private boolean hasPermission(String args[]) {
           UserManagerInterface userManager = mConfig.getUserManager();
           String adminName = userManager.getAdminName();
  -        if ( args == null || mUser.getName().equals(adminName) ) {
  +        if(args == null || mUser.getName().equals(adminName)) {
               return true;
           }
  -        return ( (args.length > 0) && "HELP".equalsIgnoreCase(args[0]) );
  +        return ((args.length > 0) && "HELP".equalsIgnoreCase(args[0]));
       }
  -
  -
  +    
  +    
       ////////////////////////////////////////////////////////////////////////////
       ////////////////////////  All site command handlers ////////////////////////
       /**
        * Add banned IP
        */
  -     public String doADDIP(String[] args, FtpRequest cmd) {
  -         if (args.length != 2) {
  -             return mConfig.getStatus().getResponse(501, cmd, mUser, null);
  -         }
  -
  -         String response = "";
  -         try {
  -             IpRestrictorInterface ipRestrictor = mConfig.getIpRestrictor();
  -             ipRestrictor.addEntry(args[1]);
  -             ipRestrictor.save();
  -             response = mConfig.getStatus().getResponse(200, cmd, mUser, null);
  -         }
  -         catch(Exception ex) {
  -             mConfig.getLogger().warn("SiteCommandHandler.doADDIP()", ex);
  -             response = mConfig.getStatus().getResponse(451, cmd, mUser, null);
  -         }
  -         return response;
  -     }
  -
  -
  +    public String doADDIP(String[] args, FtpRequest cmd) {
  +        if(args.length != 2) {
  +            return mConfig.getStatus().getResponse(501, cmd, mUser, null);
  +        }
  +         
  +        String response = "";
  +        try {
  +            IpRestrictorInterface ipRestrictor = mConfig.getIpRestrictor();
  +            ipRestrictor.addEntry(args[1]);
  +            ipRestrictor.save();
  +            response = mConfig.getStatus().getResponse(200, cmd, mUser, null);
  +        }
  +        catch(Exception ex) {
  +            mConfig.getLogger().warn("SiteCommandHandler.doADDIP()", ex);
  +            response = mConfig.getStatus().getResponse(451, cmd, mUser, null);
  +        }
  +        return response;
  +    }
  +    
  +    
       /**
        * Add user
        */
  -     public String doADDUSER(String[] args, FtpRequest cmd) {
  -         if (args.length != 2) {
  -             return mConfig.getStatus().getResponse(501, cmd, mUser, null);
  -         }
  -
  -         String response = "";
  -         try {
  -             User user = new User();
  -             user.setName(args[1]);
  -             user.setPassword("");
  -             user.setEnabled(false);
  -             user.getVirtualDirectory().setWritePermission(false);
  -             user.setMaxUploadRate(0);
  -             user.setMaxDownloadRate(0);
  -             user.getVirtualDirectory().setRootDirectory(mConfig.getDefaultRoot());
  -             user.setMaxIdleTime(mConfig.getDefaultIdleTime());
  -             mConfig.getUserManager().save(user);
  -             response = mConfig.getStatus().getResponse(200, cmd, mUser, null);
  -         }
  -         catch(Exception ex) {
  -             mConfig.getLogger().warn("SiteCommandHandler.doADDUSER()", ex);
  -             response = mConfig.getStatus().getResponse(451, cmd, mUser, null);
  -         }
  -         return response;
  -     }
  -
  -
  -     /**
  -      * Add banned IP
  -      */
  -     public String doDELIP(String[] args, FtpRequest cmd) {
  -          if (args.length != 2) {
  -              return mConfig.getStatus().getResponse(501, cmd, mUser, null);
  -          }
  -
  -          String response = "";
  -          try {
  -              IpRestrictorInterface ipRestrictor = mConfig.getIpRestrictor();
  -              ipRestrictor.removeEntry(args[1]);
  -              ipRestrictor.save();
  -              response = mConfig.getStatus().getResponse(200, cmd, mUser, null);
  -          }
  -          catch(Exception ex) {
  -              mConfig.getLogger().warn("SiteCommandHandler.doDELIP()", ex);
  -              response = mConfig.getStatus().getResponse(451, cmd, mUser, null);
  -          }
  -          return response;
  +    public String doADDUSER(String[] args, FtpRequest cmd) {
  +        if(args.length != 2) {
  +            return mConfig.getStatus().getResponse(501, cmd, mUser, null);
  +        }
  +         
  +        String response = "";
  +        try {
  +            String userName = args[1];
  +            UserManagerInterface userManager = mConfig.getUserManager();
  +            if(!userManager.doesExist(userName)) {
  +                User user = new User();
  +                user.setName(userName);
  +                user.setPassword("");
  +                user.setEnabled(false);
  +                user.getVirtualDirectory().setWritePermission(false);
  +                user.setMaxUploadRate(0);
  +                user.setMaxDownloadRate(0);
  +                
user.getVirtualDirectory().setRootDirectory(mConfig.getDefaultRoot());
  +                user.setMaxIdleTime(mConfig.getDefaultIdleTime());
  +                mConfig.getUserManager().save(user);
  +            }
  +            response = mConfig.getStatus().getResponse(200, cmd, mUser, null);
  +        }
  +        catch(Exception ex) {
  +            mConfig.getLogger().warn("SiteCommandHandler.doADDUSER()", ex);
  +            response = mConfig.getStatus().getResponse(451, cmd, mUser, null);
  +        }
  +        return response;
       }
  -
  -
  +    
  +    
  +    /**
  +     * Add banned IP
  +     */
  +    public String doDELIP(String[] args, FtpRequest cmd) {
  +        if(args.length != 2) {
  +            return mConfig.getStatus().getResponse(501, cmd, mUser, null);
  +        }
  +          
  +        String response = "";
  +        try {
  +            IpRestrictorInterface ipRestrictor = mConfig.getIpRestrictor();
  +            ipRestrictor.removeEntry(args[1]);
  +            ipRestrictor.save();
  +            response = mConfig.getStatus().getResponse(200, cmd, mUser, null);
  +        }
  +        catch(Exception ex) {
  +            mConfig.getLogger().warn("SiteCommandHandler.doDELIP()", ex);
  +            response = mConfig.getStatus().getResponse(451, cmd, mUser, null);
  +        }
  +        return response;
  +    }
  +    
  +    
       /**
        * Delete user from repository.
        */
       public String doDELUSER(String[] args, FtpRequest cmd) {
  -        if (args.length != 2) {
  +        if(args.length != 2) {
               return mConfig.getStatus().getResponse(501, cmd, mUser, null);
           }
  -
  +        
           String response = "";
           try {
               mConfig.getUserManager().delete(args[1]);
  @@ -210,19 +214,19 @@
           }
           return response;
       }
  -
  +    
       /**
        * Describe user.
        */
       public String doDESCUSER(String[] args, FtpRequest cmd) {
  -        if (args.length != 2) {
  +        if(args.length != 2) {
               return mConfig.getStatus().getResponse(501, cmd, mUser, null);
           }
  -
  +        
           StringBuffer sb = new StringBuffer();
           sb.append('\n');
           User user = mConfig.getUserManager().getUserByName(args[1]);
  -        if (user != null) {
  +        if(user != null) {
               sb.append("login : ").append(user.getName()).append('\n');
               sb.append("password : ").append("******").append('\n');
               sb.append("home : 
").append(user.getVirtualDirectory().getRootDirectory()).append('\n');
  @@ -235,8 +239,8 @@
           sb.append('\n');
           return mConfig.getStatus().processNewLine(sb.toString(), 200);
       }
  -
  -
  +    
  +    
       /**
        * Display site help.
        */
  @@ -253,44 +257,45 @@
           sb.append("LISTIP : display all banned IPs").append('\n');
           sb.append("LISTUSER : display all user names").append('\n');
           sb.append("SETATTR <userName> <attrName> <attrValue> : set user 
attributes").append('\n');
  +        sb.append("STAT : show statistics").append('\n');
           sb.append("WHO : display all connected users").append('\n');
           sb.append('\n');
           return mConfig.getStatus().processNewLine(sb.toString(), 200);
       }
  -
  +    
       /**
        * Disconnect ftp connections
        */
       public String doKICK(String[] args, FtpRequest cmd) {
  -        if (args.length != 2) {
  +        if(args.length != 2) {
               return mConfig.getStatus().getResponse(501, cmd, mUser, null);
           }
           String userName = args[1];
           List allUsers = mConfig.getConnectionService().getAllUsers();
  -        for(Iterator userIt=allUsers.iterator(); userIt.hasNext(); ) {
  +        for(Iterator userIt = allUsers.iterator();userIt.hasNext();) {
               FtpUser user = (FtpUser)userIt.next();
  -            if ( userName.equals(user.getName()) ) {
  +            if(userName.equals(user.getName())) {
                   mConfig.getConnectionService().closeConnection(user.getSessionId());
               }
           }
           return mConfig.getStatus().getResponse(200, cmd, mUser, null);
       }
  -
  -
  +    
  +    
       /**
        * List all banned IPs.
        */
       public String doLISTIP(String[] args, FtpRequest cmd) {
           StringBuffer sb = new StringBuffer();
           sb.append('\n');
  -        for(Iterator ipIt=mConfig.getIpRestrictor().getAllEntries().iterator(); 
ipIt.hasNext(); ) {
  +        for(Iterator ipIt = 
mConfig.getIpRestrictor().getAllEntries().iterator();ipIt.hasNext();) {
               sb.append(ipIt.next()).append('\n');
           }
           sb.append('\n');
           return mConfig.getStatus().processNewLine(sb.toString(), 200);
       }
  -
  -
  +    
  +    
       /**
        * List all the users.
        */
  @@ -298,53 +303,53 @@
           Collection userNames = mConfig.getUserManager().getAllUserNames();
           StringBuffer sb = new StringBuffer();
           sb.append('\n');
  -        for(Iterator userIt=userNames.iterator(); userIt.hasNext(); ) {
  +        for(Iterator userIt = userNames.iterator();userIt.hasNext();) {
               sb.append(userIt.next()).append('\n');
           }
           sb.append('\n');
           return mConfig.getStatus().processNewLine(sb.toString(), 200);
       }
  -
  -
  +    
  +    
       /**
        * Delete user from repository.
        */
       public String doSETATTR(String[] args, FtpRequest cmd) {
  -        if (args.length != 4) {
  +        if(args.length != 4) {
               return mConfig.getStatus().getResponse(501, cmd, mUser, null);
           }
  -
  +        
           boolean bSuccess = true;
           try {
               User user = mConfig.getUserManager().getUserByName(args[1]);
  -            if (user != null) {
  -
  -                if ( "password".equals(args[2]) ) {
  +            if(user != null) {
  +            
  +                if("password".equals(args[2])) {
                       user.setPassword(args[3]);
                   }
  -                else if ( "home".equals(args[2]) ) {
  +                else if("home".equals(args[2])) {
                       user.getVirtualDirectory().setRootDirectory(args[3]);
                   }
  -                else if ( "writepermission".equals(args[2]) ) {
  +                else if("writepermission".equals(args[2])) {
                       
user.getVirtualDirectory().setWritePermission("true".equals(args[3]));
                   }
  -                else if ( "enable".equals(args[2]) ) {
  +                else if("enable".equals(args[2])) {
                       user.setEnabled("true".equals(args[3]));
                   }
  -                else if ( "maxidletime".equals(args[2]) ) {
  +                else if("maxidletime".equals(args[2])) {
                       user.setMaxIdleTime(Integer.parseInt(args[3]));
                   }
  -                else if ( "maxuploadrate".equals(args[2]) ) {
  +                else if("maxuploadrate".equals(args[2])) {
                       user.setMaxUploadRate(Integer.parseInt(args[3]));
                   }
  -                else if ( "maxdownloadrate".equals(args[2]) ) {
  +                else if("maxdownloadrate".equals(args[2])) {
                       user.setMaxDownloadRate(Integer.parseInt(args[3]));
                   }
                   else {
                       bSuccess = false;
                   }
  -
  -                if (bSuccess) {
  +            
  +                if(bSuccess) {
                       mConfig.getUserManager().save(user);
                   }
               }
  @@ -356,43 +361,67 @@
               mConfig.getLogger().warn("SiteCommandHandler.doSETATTR()", ex);
               bSuccess = false;
           }
  -
  +        
           String response = "";
  -        if (bSuccess) {
  +        if(bSuccess) {
               response = mConfig.getStatus().getResponse(200, cmd, mUser, null);
           }
           else {
               response = mConfig.getStatus().getResponse(451, cmd, mUser, null);
           }
  -
  +        
           return response;
       }
  -
  -
  +    
  +    
  +    /**
  +     * Delete user from repository.
  +     */
  +    public String doSTAT(String[] args, FtpRequest cmd) {
  +        FtpStatistics stat = mConfig.getStatistics();
  +        StringBuffer sb = new StringBuffer();
  +        sb.append('\n');
  +        sb.append("Start Time               : 
").append(((SimpleDateFormat)DATE_FMT.get()).format(stat.getStartTime())).append('\n');
  +        sb.append("Upload Number            : 
").append(stat.getFileUploadNbr()).append('\n');
  +        sb.append("Download Number          : 
").append(stat.getFileDownloadNbr()).append('\n');
  +        sb.append("Delete Number            : 
").append(stat.getFileDeleteNbr()).append('\n');
  +        sb.append("Uploade Bytes            : 
").append(stat.getFileUploadSize()).append('\n');
  +        sb.append("Downloaded Bytes         : 
").append(stat.getFileDownloadSize()).append('\n');
  +        sb.append("Current Logins           : 
").append(stat.getLoginNbr()).append('\n');
  +        sb.append("Total Logins             : 
").append(stat.getTotalLoginNbr()).append('\n');
  +        sb.append("Current Anonymous Logins : 
").append(stat.getAnonLoginNbr()).append('\n');
  +        sb.append("Total Anonymous Logins   : 
").append(stat.getTotalAnonLoginNbr()).append('\n');
  +        sb.append("Current Connections      : 
").append(stat.getConnectionNbr()).append('\n');
  +        sb.append("Total Connections        : 
").append(stat.getTotalConnectionNbr()).append('\n');
  +        sb.append('\n');
  +        return mConfig.getStatus().processNewLine(sb.toString(), 200);
  +    }
  +    
  +    
       /**
        * Display all connected users.
        */
       public String doWHO(String[] args, FtpRequest cmd) {
           StringBuffer sb = new StringBuffer();
           List allUsers = mConfig.getConnectionService().getAllUsers();
  -
  +        
           sb.append('\n');
  -        for(Iterator userIt = allUsers.iterator(); userIt.hasNext(); ) {
  +        for(Iterator userIt = allUsers.iterator();userIt.hasNext();) {
               FtpUser user = (FtpUser)userIt.next();
  -            if (!user.hasLoggedIn()) {
  +            if(!user.hasLoggedIn()) {
                   continue;
               }
               
               SimpleDateFormat fmt = (SimpleDateFormat)DATE_FMT.get();
  -            sb.append( StringUtils.pad(user.getName(), ' ', true, 16) );
  -            sb.append( StringUtils.pad(user.getClientAddress().getHostAddress(), ' 
', true, 16) );
  -            sb.append( StringUtils.pad(fmt.format(new Date(user.getLoginTime())), ' 
', true, 16) );
  -            sb.append( StringUtils.pad(fmt.format(new 
Date(user.getLastAccessTime())), ' ', true, 16) );
  +            sb.append(StringUtils.pad(user.getName(), ' ', true, 16));
  +            sb.append(StringUtils.pad(user.getClientAddress().getHostAddress(), ' 
', true, 16));
  +            sb.append(StringUtils.pad(fmt.format(new Date(user.getLoginTime())), ' 
', true, 16));
  +            sb.append(StringUtils.pad(fmt.format(new 
Date(user.getLastAccessTime())), ' ', true, 16));
               sb.append('\n');
           }
           sb.append('\n');
           return mConfig.getStatus().processNewLine(sb.toString(), 200);
       }
  -
  +      
   }
   
  
  
  

--
To unsubscribe, e-mail:   <mailto:avalon-cvs-unsubscribe@;jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-cvs-help@;jakarta.apache.org>

Reply via email to