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.sftp;
 
 
 
 
 
 public final class SftpConnectionDestination implements ISftpConnectionDestination
 {
   private static final Logger s_aLogger = LoggerFactory.getLogger (SftpConnectionDestination.class);
   private final int m_nConnectTimeoutMilliSeconds;
   private final String m_sHostname;
   private final int m_nPort;
 
   public SftpConnectionDestination (@Nonnull final String sHostname)
   {
     this (sHostname);
   }
 
   public SftpConnectionDestination (@Nonnull final String sHostname, @Nonnegative final int nPort)
   {
     this (sHostnamenPort, 2000);
   }
 
   public SftpConnectionDestination (@Nonnull final String sHostname,
                                     @Nonnegative final int nPort,
                                     @Nonnegative final int nConnectTimeoutMilliSeconds)
   {
     if (StringHelper.hasNoText (sHostname))
       throw new IllegalArgumentException ("hostname");
     if (nPort <= . || nPort > .)
       throw new IllegalArgumentException ("port");
     if (nConnectTimeoutMilliSeconds < 1)
       throw new IllegalArgumentException ("Illegal connection timeout specified");
      = sHostname;
      = nPort;
      = nConnectTimeoutMilliSeconds;
   }
 
   @Nonnull
   public String getHostname ()
   {
     return ;
   }
 
   public int getPort ()
   {
     return ;
   }
 
   @Nullable
   public ChannelSftp openConnection (@Nonnull final IAuthCredentials aCredentials)
   {
     if (!(aCredentials instanceof IUserNamePasswordCredentials))
       throw new IllegalArgumentException ("Needs to be username/password credentials");
     final IUserNamePasswordCredentials aUPC = (IUserNamePasswordCredentialsaCredentials;
 
     Session aSession = null;
     Channel aChannel = null;
     try
     {
       final JSch jsch = new JSch ();
 
       aSession = jsch.getSession (aUPC.getUserName (), );
       aSession.setPassword (aUPC.getPassword ());
 
       /*
       * Setup Strict HostKeyChecking to no so we don't get the unknown host key
       * exception
       */
      final Properties config = new Properties ();
      config.put ("StrictHostKeyChecking""no");
      aSession.setConfig (config);
      // do connect
      aSession.connect ();
      // Open the SFTP channel
      aChannel = aSession.openChannel ("sftp");
      aChannel.connect ();
      return (ChannelSftpaChannel;
    }
    catch (final JSchException ex)
    {
      .error ("Connection exception to " +  + ":" + ex);
      if (aChannel != null && aChannel.isConnected ())
        aChannel.disconnect ();
      if (aSession != null && aSession.isConnected ())
        aSession.disconnect ();
    }
    return null;
  }
  public EChange closeConnection (@Nullable final ChannelSftp aChannel)
  {
    if (aChannel == null)
      return .;
    // end SFTP session
    aChannel.quit ();
    // close channel
    if (aChannel.isConnected ())
      aChannel.disconnect ();
    // close session
    try
    {
      aChannel.getSession ().disconnect ();
    }
    catch (final JSchException ex)
    {
      .error ("Failed to retrieve session for closing"ex);
    }
    return .;
  }
New to GrepCode? Check out our FAQ X