Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2014 Philip Helger (www.helger.com) philip[at]helger[dot]com Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package com.helger.webbasics.ssh.generic.ftp;
 
 
 
 
 
 public final class FftpConnectionDestination implements IFtpConnectionDestination
 {
   private static final boolean DEFAULT_ENTER_LOCAL_PASSIVE_MODE = false;
   private static final Logger s_aLogger = LoggerFactory.getLogger (FftpConnectionDestination.class);
 
   private final int m_nConnectTimeoutMilliSeconds;
   private final String m_sHostname;
   private final int m_nPort;
   private final boolean m_bEnterLocalPassiveMode;
 
   public FftpConnectionDestination (@Nonnull final String sHostname)
   {
     this (sHostname);
   }
 
   public FftpConnectionDestination (@Nonnull final String sHostnamefinal boolean bEnterLocalPassiveMode)
   {
     this (sHostnamebEnterLocalPassiveMode);
   }
 
   public FftpConnectionDestination (@Nonnull final String sHostname, @Nonnegative final int nPort)
   {
     this (sHostnamenPort);
   }
 
   public FftpConnectionDestination (@Nonnull final String sHostname,
                                     @Nonnegative final int nPort,
                                     final boolean bEnterLocalPassiveMode)
   {
     this (sHostnamenPort, 2000, bEnterLocalPassiveMode);
   }
 
   public FftpConnectionDestination (@Nonnull final String sHostname,
                                     @Nonnegative final int nPort,
                                     @Nonnegative final int nConnectTimeoutMilliSeconds,
                                     final boolean bEnterLocalPassiveMode)
   {
     if (StringHelper.hasNoText (sHostname))
       throw new IllegalArgumentException ("hostname");
     if (nPort <= 0 || nPort > 65535)
       throw new IllegalArgumentException ("port");
     if (nConnectTimeoutMilliSeconds < 1)
       throw new IllegalArgumentException ("Illegal connection timeout specified");
      = sHostname;
      = nPort;
      = nConnectTimeoutMilliSeconds;
      = bEnterLocalPassiveMode;
   }
 
   @Nonnull
   public String getHostname ()
   {
     return ;
   }
 
   public int getPort ()
   {
     return ;
   }
 
   @Nullable
   public FTPClient openConnection (@Nonnull final IAuthCredentials aCredentials)
   {
    if (!(aCredentials instanceof IUserNamePasswordCredentials))
      throw new IllegalArgumentException ("Needs to be username/password credentials");
    final IUserNamePasswordCredentials aUPC = (IUserNamePasswordCredentialsaCredentials;
    final FTPClient aFtpClient = new FTPClient ();
    try
    {
      aFtpClient.connect ();
      if ()
        aFtpClient.enterLocalPassiveMode ();
      if (!aFtpClient.login (aUPC.getUserName (), aUPC.getPassword ()))
        throw new IOException ("Failed to log into FTP server " +
                                +
                               ":" +
                                +
                               ": " +
                               aFtpClient.getReplyString ());
      aFtpClient.setFileType (.);
      return aFtpClient;
    }
    catch (final SocketTimeoutException ex)
    {
      .error ("Failed to connect to " +  + ":" +  + ": " + ex.getMessage ());
    }
    catch (final IOException ex)
    {
      .error ("Connection exception to " +  + ":" +  + ": " + aFtpClient.getReplyString (),
                       ex);
    }
    // In case of an error, disconnect (if connected)
    if (aFtpClient.isConnected ())
      try
      {
        aFtpClient.disconnect ();
      }
      catch (final IOException ex2)
      {
        .error ("Error disconnecting from FTP while in connection phase: " + aFtpClient.getReplyString (), ex2);
      }
    return null;
  }
  public EChange closeConnection (@Nullable final FTPClient aFtpClient)
  {
    if (aFtpClient == null)
      return .;
    try
    {
      // end FTP session
      if (!aFtpClient.logout ())
        .warn ("Failed to log out from FTP connection: " + aFtpClient.getReplyString ());
      // close channel
      if (aFtpClient.isConnected ())
        aFtpClient.disconnect ();
    }
    catch (final IOException ex)
    {
      .error ("Error closing FTP connection: " + aFtpClient.getReplyString (), ex);
    }
    return .;
  }
New to GrepCode? Check out our FAQ X