You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by im...@apache.org on 2004/05/26 10:13:35 UTC

cvs commit: jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/ftp FtpClientFactory.java

imario      2004/05/26 01:13:35

  Added:       vfs/src/java/org/apache/commons/vfs/provider/ftp
                        FtpClientFactory.java
  Log:
  create ftpClient
  
  Revision  Changes    Path
  1.1                  jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/ftp/FtpClientFactory.java
  
  Index: FtpClientFactory.java
  ===================================================================
  /*
   * Copyright 2002, 2003,2004 The Apache Software Foundation.
   *
   * 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 org.apache.commons.vfs.provider.ftp;
  
  import org.apache.commons.net.ftp.FTP;
  import org.apache.commons.net.ftp.FTPClient;
  import org.apache.commons.net.ftp.FTPReply;
  import org.apache.commons.vfs.FileSystemException;
  
  import java.io.IOException;
  
  /**
   * Create a FtpClient instance
   *
   * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
   * @version $Revision: 1.1 $ $Date: 2004/05/26 08:13:35 $
   */
  public class FtpClientFactory
  {
      private FtpClientFactory()
      {
      }
  
      /**
       * Creates a new connection to the server.
       */
      public static FTPClient createConnection(String hostname, int port, String username, String password, String workingDirectory) throws FileSystemException
      {
          // Determine the username and password to use
          if (username == null)
          {
              username = "anonymous";
          }
  
          if (password == null)
          {
              password = "anonymous";
          }
  
          try
          {
              final FTPClient client = new FTPClient();
  
              /* as soon as commons-1.2 will be released
              FTPFileEntryParserFactory myFactory = FtpFileSystemConfigBuilder.getInstance().getFTPFileEntryParserFactory(getFileSystemOptions());
              if (myFactory != null)
              {
              client.setParserFactory(myFactory);
              }
              */
  
              try
              {
                  client.connect(hostname, port);
  
                  int reply = client.getReplyCode();
                  if (!FTPReply.isPositiveCompletion(reply))
                  {
                      throw new FileSystemException("vfs.provider.ftp/connect-rejected.error", hostname);
                  }
  
                  // Login
                  if (!client.login(username, password))
                  {
                      throw new FileSystemException("vfs.provider.ftp/login.error", new Object[]{hostname, username}, null);
                  }
  
                  // Set binary mode
                  if (!client.setFileType(FTP.BINARY_FILE_TYPE))
                  {
                      throw new FileSystemException("vfs.provider.ftp/set-binary.error", hostname);
                  }
  
                  // Change to root by default
                  // All file operations a relative to the filesystem-root
                  // String root = getRoot().getName().getPath();
                  if (workingDirectory != null)
                  {
                      if (!client.changeWorkingDirectory(workingDirectory))
                      {
                          throw new FileSystemException("vfs.provider/get-attributes-no-exist.error", "/");
                      }
                  }
              }
              catch (final IOException e)
              {
                  if (client.isConnected())
                  {
                      client.disconnect();
                  }
                  throw e;
              }
  
              return client;
          }
          catch (final Exception exc)
          {
              throw new FileSystemException("vfs.provider.ftp/connect.error", new Object[]{hostname}, exc);
          }
      }
  }
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org