Package com.sos.VirtualFileSystem.FTPS

Source Code of com.sos.VirtualFileSystem.FTPS.SOSVfsFtpS

/********************************************************* begin of preamble
**
** Copyright (C) 2003-2010 Software- und Organisations-Service GmbH.
** All rights reserved.
**
** This file may be used under the terms of either the
**
**   GNU General Public License version 2.0 (GPL)
**
**   as published by the Free Software Foundation
**   http://www.gnu.org/licenses/gpl-2.0.txt and appearing in the file
**   LICENSE.GPL included in the packaging of this file.
**
** or the
** 
**   Agreement for Purchase and Licensing
**
**   as offered by Software- und Organisations-Service GmbH
**   in the respective terms of supply that ship with this file.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
** IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
** THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
** BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
** POSSIBILITY OF SUCH DAMAGE.
********************************************************** end of preamble*/
package com.sos.VirtualFileSystem.FTPS;

import java.io.PrintWriter;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPSClient;
import org.apache.log4j.Logger;

import com.sos.JSHelper.Exceptions.JobSchedulerException;
import com.sos.VirtualFileSystem.FTP.SOSVfsFtpBaseClass;
import com.sos.i18n.annotation.I18NResourceBundle;

@I18NResourceBundle(baseName = "SOSVirtualFileSystem", defaultLocale = "en")
public class SOSVfsFtpS extends SOSVfsFtpBaseClass  {
  private final String          conClassName      = "SOSVfsFtpS";
  private Logger              logger          = Logger.getLogger(SOSVfsFtpS.class);
  private FTPSClient            objFTPClient      = null;

  /**
   *
   * \brief SOSVfsFtpS
   *
   * \details
   *
   */
  public SOSVfsFtpS() {
  }

  public FTPSClient getClient() {
    return Client();
  }

  @Override
  protected FTPSClient Client() {
    if (objFTPClient == null) {
      try {
        String strProtocol = objConnection2Options.FtpS_protocol.Value();
        objFTPClient = new FTPSClient(strProtocol);
      }
      catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        throw new JobSchedulerException("can not create FTPSClient");
      }
      FTPClientConfig conf = new FTPClientConfig();
//      conf.setServerLanguageCode("fr");
//      objFTPClient.configure(conf);
      /**
       * This listener is to write all commands and response from commands to system.out
       *
       */
      // TODO create a hidden debug-option to activate this listener
      if (objConnection2Options != null) {
        if (objConnection2Options.ProtocolCommandListener.value() == true) {
          listener = new PrintCommandListener(new PrintWriter(System.out));
          objFTPClient.addProtocolCommandListener(listener);
        }
      }
     
      listener = new PrintCommandListener(new PrintWriter(System.out));
      String strAddFTPProtocol = System.getenv("AddFTPProtocol");
      if (strAddFTPProtocol != null && strAddFTPProtocol.equalsIgnoreCase("true")) {
        objFTPClient.addProtocolCommandListener(listener);
      }

    }
    return objFTPClient;
  }

  @Override
  public void connect(String phost, int pport) {
    try {
      host = phost;
      port = pport;
      logger.debug(String.format("Try to connect to host '%1$s' at Port '%2$d'.", host, port));
      if (isConnected() == false) {
        Client().connect(host, port);
        logger.debug(String.format("Connected to '%1$s' at Port '%2$d'.", host, port));
        LogReply();
       
        /**
         * PBSZ (protection buffer size) command, as detailed in [RFC-2228],
         * is compulsory prior to any PROT command.
         */
        Client().execPBSZ(0);
        LogReply();
        Client().execPROT("P")// Secure Data channel, see http://www.faqs.org/rfcs/rfc2228.html
        LogReply();
        Client().enterLocalPassiveMode();
        LogReply();

      }
      else {
        logger.warn(String.format("host '%1$s' at Port '%2$d' is already connected.", host, port));
      }
    }
    catch (Exception e) {
      String strM = HostID("connect returns an exception");
      e.printStackTrace(System.err);
      logger.error(strM, e);
      // throw new RuntimeException(strM, e);
    }
  }

}
TOP

Related Classes of com.sos.VirtualFileSystem.FTPS.SOSVfsFtpS

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.