View Javadoc
1   package com.codehaus.mojo.vfs;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.commons.vfs2.FileSystemException;
23  import org.apache.commons.vfs2.FileSystemOptions;
24  import org.apache.commons.vfs2.auth.StaticUserAuthenticator;
25  import org.apache.commons.vfs2.impl.DefaultFileSystemConfigBuilder;
26  import org.apache.commons.vfs2.provider.ftp.FtpFileSystemConfigBuilder;
27  import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;
28  import org.codehaus.plexus.util.StringUtils;
29  
30  public class FileSystemOptionsFactory
31  {
32  
33      private FtpSettings ftpSettings = new FtpSettings();
34  
35      public void setFtpSettings( FtpSettings ftpSettings )
36      {
37          this.ftpSettings = ftpSettings;
38      }
39  
40      private SftpSettings sftpSettings = new SftpSettings();
41  
42      public void setSftpSettings( SftpSettings sftpSettings )
43      {
44          this.sftpSettings = sftpSettings;
45      }
46  
47      public FileSystemOptions getFileSystemOptions( String url, String username, String password )
48          throws FileSystemException
49      {
50  
51          FileSystemOptions opts = new FileSystemOptions();
52  
53          String[] tokens = StringUtils.split( url, ":" );
54  
55          String protocol = tokens[0];
56  
57          if ( "ftp".equals( protocol ) )
58          {
59              FtpFileSystemConfigBuilder builder = FtpFileSystemConfigBuilder.getInstance();
60              builder.setPassiveMode( opts, ftpSettings.isPassiveMode() );
61              builder.setUserDirIsRoot( opts, ftpSettings.isUserDirIsRoot() );
62          }
63          if ( "sftp".equals( protocol ) )
64          {
65              SftpFileSystemConfigBuilder builder = SftpFileSystemConfigBuilder.getInstance();
66              builder.setUserDirIsRoot( opts, sftpSettings.isUserDirIsRoot() );
67          }
68  
69          String domain = null;
70          tokens = StringUtils.split( "\\" );
71          if ( tokens.length == 2 )
72          {
73              domain = tokens[0];
74              username = tokens[1];
75          }
76  
77          StaticUserAuthenticator auth = new StaticUserAuthenticator( domain, username, password );
78  
79          DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator( opts, auth );
80  
81          return opts;
82      }
83  
84  }