Skip to content

Commit

Permalink
FTP Homogenize model (fixes #395) (#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan José Vázquez authored and raboof committed Jul 21, 2017
1 parent 06ac78d commit 95eb459
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
package akka.stream.alpakka.ftp.impl

import akka.stream.alpakka.ftp.FtpCredentials.{AnonFtpCredentials, NonAnonFtpCredentials}
import akka.stream.alpakka.ftp.{FtpFileSettings, RemoteFileSettings, SftpSettings}
import akka.stream.alpakka.ftp.RemoteFileSettings._
import akka.stream.alpakka.ftp.{FtpFileSettings, FtpSettings, FtpsSettings, RemoteFileSettings, SftpSettings}
import net.schmizz.sshj.SSHClient
import org.apache.commons.net.ftp.FTPClient
import java.net.InetAddress
Expand Down Expand Up @@ -109,7 +108,7 @@ private[ftp] trait FtpDefaultSettings {
): FtpSettings =
FtpSettings(
InetAddress.getByName(hostname),
DefaultFtpPort,
FtpSettings.DefaultFtpPort,
if (username.isDefined)
NonAnonFtpCredentials(username.get, password.getOrElse(""))
else
Expand All @@ -125,7 +124,7 @@ private[ftp] trait FtpsDefaultSettings {
): FtpsSettings =
FtpsSettings(
InetAddress.getByName(hostname),
DefaultFtpsPort,
FtpsSettings.DefaultFtpsPort,
if (username.isDefined)
NonAnonFtpCredentials(username.get, password.getOrElse(""))
else
Expand All @@ -141,7 +140,7 @@ private[ftp] trait SftpDefaultSettings {
): SftpSettings =
SftpSettings(
InetAddress.getByName(hostname),
DefaultSftpPort,
SftpSettings.DefaultSftpPort,
if (username.isDefined)
NonAnonFtpCredentials(username.get, password.getOrElse(""))
else
Expand Down
116 changes: 74 additions & 42 deletions ftp/src/main/scala/akka/stream/alpakka/ftp/model.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,51 +46,82 @@ sealed abstract class FtpFileSettings extends RemoteFileSettings {
def passiveMode: Boolean
}

object RemoteFileSettings {
/**
* FTP settings
*
* @param host host
* @param port port
* @param credentials credentials (username and password)
* @param binary specifies the file transfer mode, BINARY or ASCII. Default is ASCII (false)
* @param passiveMode specifies whether to use passive mode connections. Default is active mode (false)
*/
final case class FtpSettings(
host: InetAddress,
port: Int = FtpSettings.DefaultFtpPort,
credentials: FtpCredentials = AnonFtpCredentials,
binary: Boolean = false,
passiveMode: Boolean = false
) extends FtpFileSettings {
def withPort(port: Int): FtpSettings =
copy(port = port)

def withCredentials(credentials: FtpCredentials): FtpSettings =
copy(credentials = credentials)

def withBinary(binary: Boolean): FtpSettings =
copy(binary = binary)

def withPassiveMode(passiveMode: Boolean): FtpSettings =
copy(passiveMode = passiveMode)
}

object FtpSettings {

/** Default FTP port */
final val DefaultFtpPort = 21

/** Default FTPs port */
final val DefaultFtpsPort = 2222
/** Java API */
def create(host: InetAddress): FtpSettings =
FtpSettings(host)
}

/** Default SFTP port */
final val DefaultSftpPort = 22
/**
* FTPs settings
*
* @param host host
* @param port port
* @param credentials credentials (username and password)
* @param binary specifies the file transfer mode, BINARY or ASCII. Default is ASCII (false)
* @param passiveMode specifies whether to use passive mode connections. Default is active mode (false)
*/
final case class FtpsSettings(
host: InetAddress,
port: Int = FtpsSettings.DefaultFtpsPort,
credentials: FtpCredentials = AnonFtpCredentials,
binary: Boolean = false,
passiveMode: Boolean = false
) extends FtpFileSettings {
def withPort(port: Int): FtpsSettings =
copy(port = port)

/**
* FTP settings
*
* @param host host
* @param port port
* @param credentials credentials (username and password)
* @param binary specifies the file transfer mode, BINARY or ASCII. Default is ASCII (false)
* @param passiveMode specifies whether to use passive mode connections. Default is active mode (false)
*/
final case class FtpSettings(
host: InetAddress,
port: Int = DefaultFtpPort,
credentials: FtpCredentials = AnonFtpCredentials,
binary: Boolean = false,
passiveMode: Boolean = false
) extends FtpFileSettings
def withCredentials(credentials: FtpCredentials): FtpsSettings =
copy(credentials = credentials)

/**
* FTPs settings
*
* @param host host
* @param port port
* @param credentials credentials (username and password)
* @param binary specifies the file transfer mode, BINARY or ASCII. Default is ASCII (false)
* @param passiveMode specifies whether to use passive mode connections. Default is active mode (false)
*/
final case class FtpsSettings(
host: InetAddress,
port: Int = DefaultFtpsPort,
credentials: FtpCredentials = AnonFtpCredentials,
binary: Boolean = false,
passiveMode: Boolean = false
) extends FtpFileSettings
def withBinary(binary: Boolean): FtpsSettings =
copy(binary = binary)

def withPassiveMode(passiveMode: Boolean): FtpsSettings =
copy(passiveMode = passiveMode)
}

object FtpsSettings {

/** Default FTPs port */
final val DefaultFtpsPort = 2222

/** Java API */
def create(host: InetAddress): FtpsSettings =
FtpsSettings(host)
}

/**
Expand All @@ -104,7 +135,7 @@ object RemoteFileSettings {
*/
final case class SftpSettings(
host: InetAddress,
port: Int = RemoteFileSettings.DefaultSftpPort,
port: Int = SftpSettings.DefaultSftpPort,
credentials: FtpCredentials = AnonFtpCredentials,
strictHostKeyChecking: Boolean = true,
knownHosts: Option[String] = None,
Expand All @@ -127,12 +158,13 @@ final case class SftpSettings(
}

object SftpSettings {
def create(host: InetAddress): SftpSettings =
SftpSettings(host)

def createEmptyIdentity(): Option[SftpIdentity] = None
/** Default SFTP port */
final val DefaultSftpPort = 22

def createEmptyKnownHosts(): Option[String] = None
/** Java API */
def create(host: InetAddress): SftpSettings =
SftpSettings(host)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import akka.NotUsed;
import akka.stream.IOResult;
import akka.stream.alpakka.ftp.RemoteFileSettings.FtpSettings;
import akka.stream.alpakka.ftp.javadsl.Ftp;
import akka.stream.javadsl.Sink;
import akka.stream.javadsl.Source;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import akka.NotUsed;
import akka.stream.IOResult;
import akka.stream.alpakka.ftp.RemoteFileSettings.FtpsSettings;
import akka.stream.alpakka.ftp.javadsl.Ftps;
import akka.stream.javadsl.Sink;
import akka.stream.javadsl.Source;
Expand Down
2 changes: 0 additions & 2 deletions ftp/src/test/scala/akka/stream/alpakka/ftp/BaseFtpSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ package akka.stream.alpakka.ftp
import akka.NotUsed
import akka.stream.IOResult
import akka.stream.scaladsl.{Sink, Source}
import akka.stream.alpakka.ftp.RemoteFileSettings.FtpSettings
import akka.stream.alpakka.ftp.FtpCredentials.AnonFtpCredentials
import akka.stream.alpakka.ftp.scaladsl.Ftp
import akka.util.ByteString

import scala.concurrent.Future
import java.net.InetAddress

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package akka.stream.alpakka.ftp

import akka.NotUsed
import akka.stream.alpakka.ftp.RemoteFileSettings.FtpsSettings
import akka.stream.alpakka.ftp.FtpCredentials.AnonFtpCredentials
import akka.stream.alpakka.ftp.scaladsl.Ftps
import akka.stream.IOResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ trait CommonFtpStageSpec extends BaseSpec with Eventually {
implicit val system = getSystem
implicit val mat = getMaterializer
implicit val defaultPatience =
PatienceConfig(timeout = Span(10, Seconds), interval = Span(300, Millis))
PatienceConfig(timeout = Span(30, Seconds), interval = Span(600, Millis))

"FtpBrowserSource" should {
"list all files from root" in {
Expand Down

0 comments on commit 95eb459

Please sign in to comment.