////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2009, Suncorp Metway Limited. All rights reserved.
//
// This is unpublished proprietary source code of Suncorp Metway Limited.
// The copyright notice above does not evidence any actual or intended
// publication of such source code.
//
////////////////////////////////////////////////////////////////////////////////
package com.ettrema.ftp;

import com.bradmcevoy.common.Path;
import com.bradmcevoy.http.CollectionResource;
import com.bradmcevoy.http.Resource;
import com.bradmcevoy.http.ResourceFactory;
import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.FileSystemFactory;
import org.apache.ftpserver.ftplet.FileSystemView;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.User;
import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.impl.DefaultFtpHandler;
import org.apache.ftpserver.listener.ListenerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author u370681
 */
public class MiltonFtpAdapter implements FileSystemFactory {

    private static final Logger log = LoggerFactory.getLogger( MiltonFtpAdapter.class );
    private final ResourceFactory resourceFactory;
    private final FtpServer server;

    /**
     * Just sets dependencies. Does NOT start the server
     *
     * @param resourceFactory
     * @param server
     */
    public MiltonFtpAdapter( ResourceFactory resourceFactory, FtpServer server ) {
        this.resourceFactory = resourceFactory;
        this.server = server;
    }



    public MiltonFtpAdapter( ResourceFactory wrapped, UserManager userManager ) throws FtpException {
        this(wrapped, userManager, null );
    }

    /**
     * Creates and starts the FTP server on port 21
     *
     * @param wrapped
     * @param userManager
     * @param actionListener
     * @throws FtpException
     */
    public MiltonFtpAdapter( ResourceFactory wrapped, UserManager userManager,FtpActionListener actionListener ) throws FtpException {
        this(wrapped, userManager, actionListener, 21, true);
    }

    /**
     * Creates and starts the FTP server on the given port
     * 
     * @param wrapped
     * @param userManager
     * @param port
     * @throws FtpException
     */
    public MiltonFtpAdapter( ResourceFactory wrapped, UserManager userManager, int port ) throws FtpException {
        this(wrapped, userManager, null, port, true );
    }


    /**
     * Creates and optionally starts the server
     *
     * @param wrapped
     * @param userManager
     * @param actionListener
     * @param port
     * @param autoStart - whether or not to start the server
     * @throws FtpException
     */
    public MiltonFtpAdapter( ResourceFactory wrapped, UserManager userManager,FtpActionListener actionListener, int port, boolean autoStart ) throws FtpException {
        log.debug("creating MiltonFtpAdapter.2");
        this.resourceFactory = wrapped;
        FtpServerFactory serverFactory = new FtpServerFactory();
        ListenerFactory factory;
        if( actionListener != null ) {
            log.debug( "using customised milton listener factory");
            MiltonFtpHandler ftpHandler = new MiltonFtpHandler(new DefaultFtpHandler(),actionListener);
            factory = new MiltonListenerFactory(ftpHandler);
        } else {
            factory = new ListenerFactory();
        }
        factory.setPort( port );
        serverFactory.addListener("default", factory.createListener());

        
        serverFactory.setFileSystem( this );
        serverFactory.setUserManager( userManager);
        server = serverFactory.createServer();
        if( autoStart) {
            log.debug( "starting the FTP server on port: " + port);
            server.start();
        }
    }

    public Resource getResource( Path path, String host ) {
        return resourceFactory.getResource( host, path.toString() );
    }

    public FileSystemView createFileSystemView( User user ) throws FtpException {
        MiltonUser mu = (MiltonUser)user;
        Resource root = resourceFactory.getResource( mu.domain, "/" );
        return new MiltonFsView( Path.root, (CollectionResource) root ,resourceFactory, (MiltonUser)user);
    }

}

@@ -1662,18 +1662,16 @@
 %0A    %7D%0A%0A
-%0A%0A
     publ
@@ -1769,32 +1769,33 @@
  %7B%0A        this(
+ 
 wrapped, userMan
@@ -2065,32 +2065,33 @@
 ger userManager,
+ 
 FtpActionListene
@@ -2136,32 +2136,33 @@
  %7B%0A        this(
+ 
 wrapped, userMan
@@ -2191,16 +2191,17 @@
 21, true
+ 
 );%0A    %7D
@@ -2505,16 +2505,17 @@
    this(
+ 
 wrapped,
@@ -2555,17 +2555,16 @@
 %0A    %7D%0A%0A
-%0A
     /**%0A
@@ -2889,16 +2889,17 @@
 Manager,
+ 
 FtpActio
@@ -2994,16 +2994,17 @@
 g.debug(
+ 
 %22creatin
@@ -3024,16 +3024,17 @@
 apter.2%22
+ 
 );%0A     
@@ -3270,16 +3270,17 @@
 factory%22
+ 
 );%0A     
@@ -3337,16 +3337,17 @@
 Handler(
+ 
 new Defa
@@ -3362,16 +3362,17 @@
 ndler(),
+ 
 actionLi
@@ -3377,16 +3377,17 @@
 Listener
+ 
 );%0A     
@@ -3429,16 +3429,17 @@
 Factory(
+ 
 ftpHandl
@@ -3440,16 +3440,17 @@
 pHandler
+ 
 );%0A     
@@ -3583,16 +3583,17 @@
 istener(
+ 
 %22default
@@ -3619,16 +3619,17 @@
 stener()
+ 
 );%0A%0A    
@@ -3632,16 +3632,33 @@
         
+// VERY IMPORTANT
 %0A       
@@ -3691,24 +3691,26 @@
 em( this );%0A
+%0A%0A
         serv
@@ -3746,16 +3746,17 @@
 rManager
+ 
 );%0A     
@@ -3818,16 +3818,17 @@
 utoStart
+ 
 ) %7B%0A    
@@ -3888,16 +3888,17 @@
 %22 + port
+ 
 );%0A     
@@ -4187,24 +4187,25 @@
 (MiltonUser)
+ 
 user;%0A      
@@ -4342,10 +4342,10 @@
 root
- 
 ,
+ 
 reso
@@ -4369,20 +4369,22 @@
 tonUser)
+ 
 user
+ 
 );%0A    %7D
@@ -4384,11 +4384,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

enable simple port configuration of ftp