package com.github._1element.service;

import com.github._1element.domain.Camera;
import com.github._1element.repository.CameraRepository;
import org.apache.ftpserver.ftplet.Authority;
import org.apache.ftpserver.ftplet.Ftplet;
import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
import org.apache.ftpserver.usermanager.impl.BaseUser;
import org.apache.ftpserver.usermanager.impl.WritePermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.listener.ListenerFactory;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Ftp service class. Will start ftp server.
 */
@Service
public class FtpService {

  @Autowired
  private CameraRepository cameraRepository;

  @Autowired
  private UploadFtplet uploadFtplet;

  @Value("${sc.ftp.port:2221}")
  private int ftpServerPort;

  private FtpServer server;

  private PropertiesUserManagerFactory userManagerFactory;

  /**
   * Start ftp server.
   *
   * @throws FtpException
   */
  @PostConstruct
  public void start() throws FtpException {
    FtpServerFactory ftpServerFactory = new FtpServerFactory();
    ListenerFactory listenerFactory = new ListenerFactory();

    listenerFactory.setPort(ftpServerPort);
    ftpServerFactory.addListener("default", listenerFactory.createListener());

    // user manager
    userManagerFactory = new PropertiesUserManagerFactory();
    ftpServerFactory.setUserManager(populateUserManager());

    // ftplet
    Map<String, Ftplet> ftpletMap = new HashMap<String, Ftplet>();
    ftpletMap.put("uploadFtplet", uploadFtplet);
    ftpServerFactory.setFtplets(ftpletMap);

    // start server
    server = ftpServerFactory.createServer();
    server.start();
  }

  /**
   * Stop ftp server.
   */
  @PreDestroy
  public void stop() {
    server.stop();
  }

  /**
   * Populate ftp credentials and home directories.
   *
   * @return user manager
   * @throws FtpException
   */
  private UserManager populateUserManager() throws FtpException {
    UserManager userManager = userManagerFactory.createUserManager();

    List<Authority> authorities = new ArrayList<Authority>();
    authorities.add(new WritePermission());

    for (Camera camera : cameraRepository.findAll()) {
      BaseUser user = new BaseUser();
      user.setName(camera.getFtpUsername());
      user.setPassword(camera.getFtpPassword());
      user.setHomeDirectory(camera.getFtpIncomingDirectory());
      user.setAuthorities(authorities);

      userManager.save(user);
    }

    return userManager;
  }

}

@@ -1189,16 +1189,80 @@
 tplet;%0A%0A
+  @Value(%22$%7Bsc.ftp.enabled:false%7D%22)%0A  private boolean enabled;%0A%0A
   @Value
@@ -1527,24 +1527,65 @@
 Exception %7B%0A
+    if (!enabled) %7B%0A      return;%0A    %7D%0A%0A
     FtpServe
@@ -2297,24 +2297,52 @@
 id stop() %7B%0A
+    if (server != null) %7B%0A  
     server.s
@@ -2344,24 +2344,30 @@
 ver.stop();%0A
+    %7D%0A
   %7D%0A%0A  /**%0A 

Make ftp server start configurable