View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package org.apache.commons.vfs.provider.sftp;
    18
    19  import com.jcraft.jsch.UserInfo;
    20  import org.apache.commons.vfs.FileSystemConfigBuilder;
    21  import org.apache.commons.vfs.FileSystemException;
    22  import org.apache.commons.vfs.FileSystemOptions;
    23
    24  import java.io.File;
    25  import java.io.Serializable;
    26
    27  /***
    28   * The config builder for various sftp configuration options
    29   *
    30   * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
    31   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
    32   */
    33  public class SftpFileSystemConfigBuilder extends FileSystemConfigBuilder
    34  {
    35      private final static SftpFileSystemConfigBuilder builder = new SftpFileSystemConfigBuilder();
    36
    37      private final static String USER_DIR_IS_ROOT = SftpFileSystemConfigBuilder.class.getName() + ".USER_DIR_IS_ROOT";
    38      private final static String TIMEOUT = SftpFileSystemConfigBuilder.class.getName() + ".TIMEOUT";
    39
    40      public final static ProxyType PROXY_HTTP = new ProxyType("http");
    41      public final static ProxyType PROXY_SOCKS5 = new ProxyType("socks");
    42
    43      public static class ProxyType implements Serializable, Comparable
    44      {
    45          private final String proxyType;
    46
    47          private ProxyType(final String proxyType)
    48          {
    49              this.proxyType = proxyType;
    50          }
    51
    52          public int compareTo(Object o)
    53          {
    54              return proxyType.compareTo(((ProxyType) o).proxyType);
    55          }
    56
    57
    58          public boolean equals(Object o)
    59          {
    60              if (this == o)
    61              {
    62                  return true;
    63              }
    64              if (o == null || getClass() != o.getClass())
    65              {
    66                  return false;
    67              }
    68
    69              ProxyType proxyType1 = (ProxyType) o;
    70
    71              if (proxyType != null ? !proxyType.equals(proxyType1.proxyType) : proxyType1.proxyType != null)
    72              {
    73                  return false;
    74              }
    75
    76              return true;
    77          }
    78      }
    79
    80      public static SftpFileSystemConfigBuilder getInstance()
    81      {
    82          return builder;
    83      }
    84
    85      private SftpFileSystemConfigBuilder()
    86      {
    87      }
    88
    89      /***
    90       * Set the userinfo class to use if e.g. a password or a not known host
    91       * will be contacted
    92       *
    93       * @param opts
    94       * @param info
    95       */
    96      public void setUserInfo(FileSystemOptions opts, UserInfo info)
    97      {
    98          setParam(opts, UserInfo.class.getName(), info);
    99      }
    100
    101     /***
    102      * @param opts
    103      * @see #setUserInfo
    104      */
    105     public UserInfo getUserInfo(FileSystemOptions opts)
    106     {
    107         return (UserInfo) getParam(opts, UserInfo.class.getName());
    108     }
    109
    110     /***
    111      * Set the known_hosts file. e.g. /home/user/.ssh/known_hosts2<br>
    112      * Need to use a java.io.File as JSch cant deal with vfs FileObjects ;-)
    113      *
    114      * @param opts
    115      * @param sshdir
    116      */
    117     public void setKnownHosts(FileSystemOptions opts, File sshdir) throws FileSystemException
    118     {
    119         setParam(opts, "knownHosts", sshdir);
    120     }
    121
    122     /***
    123      * @param opts
    124      * @see #setKnownHosts
    125      */
    126     public File getKnownHosts(FileSystemOptions opts)
    127     {
    128         return (File) getParam(opts, "knownHosts");
    129     }
    130
    131     /***
    132      * Set the identity files (your private key files).<br>
    133      * Need to use a java.io.File as JSch cant deal with vfs FileObjects ;-)
    134      *
    135      * @param opts
    136      * @param identities
    137      */
    138     public void setIdentities(FileSystemOptions opts, File[] identities) throws FileSystemException
    139     {
    140         setParam(opts, "identities", identities);
    141     }
    142
    143     /***
    144      * configure the compression to use.<br>
    145      * e.g. pass "zlib,none" to enable the compression.<br>
    146      * See the jsch documentation for details.
    147      *
    148      * @param opts
    149      * @param compression
    150      * @throws FileSystemException
    151      */
    152     public void setCompression(FileSystemOptions opts, String compression) throws FileSystemException
    153     {
    154         setParam(opts, "compression", compression);
    155     }
    156
    157     /***
    158      * @param opts
    159      * @see #setCompression
    160      */
    161     public String getCompression(FileSystemOptions opts)
    162     {
    163         return (String) getParam(opts, "compression");
    164     }
    165
    166     /***
    167      * @param opts
    168      * @see #setIdentities
    169      */
    170     public File[] getIdentities(FileSystemOptions opts)
    171     {
    172         return (File[]) getParam(opts, "identities");
    173     }
    174
    175     /***
    176      * configure the host key checking to use.<br>
    177      * valid arguments are only yes, no and ask.<br>
    178      * See the jsch documentation for details.
    179      *
    180      * @param opts
    181      * @param hostKeyChecking
    182      * @throws FileSystemException
    183      */
    184     public void setStrictHostKeyChecking(FileSystemOptions opts, String hostKeyChecking) throws FileSystemException
    185     {
    186         if (hostKeyChecking == null || (!hostKeyChecking.equals("ask") && !hostKeyChecking.equals("no") && !hostKeyChecking.equals("yes")))
    187         {
    188             throw new FileSystemException("vfs.provider.sftp/StrictHostKeyChecking-arg.error", hostKeyChecking);
    189         }
    190
    191         setParam(opts, "StrictHostKeyChecking", hostKeyChecking);
    192     }
    193
    194     /***
    195      * @param opts
    196      * @return the option value
    197      * @see #setStrictHostKeyChecking(FileSystemOptions, String)
    198      */
    199     public String getStrictHostKeyChecking(FileSystemOptions opts)
    200     {
    201         return (String) getParam(opts, "StrictHostKeyChecking");
    202     }
    203
    204     /***
    205      * use user directory as root (do not change to fs root)
    206      *
    207      * @param opts
    208      * @param userDirIsRoot
    209      */
    210     public void setUserDirIsRoot(FileSystemOptions opts, boolean userDirIsRoot)
    211     {
    212         setParam(opts, USER_DIR_IS_ROOT, userDirIsRoot ? Boolean.TRUE : Boolean.FALSE);
    213     }
    214
    215     /***
    216      * @param opts
    217      * @see #setUserDirIsRoot
    218      */
    219     public Boolean getUserDirIsRoot(FileSystemOptions opts)
    220     {
    221         return (Boolean) getParam(opts, USER_DIR_IS_ROOT);
    222     }
    223
    224     /***
    225      * set the timeout value on jsch session
    226      *
    227      * @param opts
    228      * @param timeout
    229      */
    230     public void setTimeout(FileSystemOptions opts, Integer timeout)
    231     {
    232         setParam(opts, TIMEOUT, timeout);
    233     }
    234
    235     /***
    236      * @param opts
    237      * @see #setTimeout
    238      */
    239     public Integer getTimeout(FileSystemOptions opts)
    240     {
    241         return (Integer) getParam(opts, TIMEOUT);
    242     }
    243
    244     protected Class getConfigClass()
    245     {
    246         return SftpFileSystem.class;
    247     }
    248
    249     /***
    250      * Set the proxy to use for sftp connection.<br>
    251      * You have to set the ProxyPort too if you would like to have the proxy relly used.
    252      *
    253      * @param proxyHost the host
    254      * @see #setProxyPort
    255      */
    256     public void setProxyHost(FileSystemOptions opts, String proxyHost)
    257     {
    258         setParam(opts, "proxyHost", proxyHost);
    259     }
    260
    261     /***
    262      * Set the proxy-port to use for sftp connection
    263      * You have to set the ProxyHost too if you would like to have the proxy relly used.
    264      *
    265      * @param proxyPort the port
    266      * @see #setProxyHost
    267      */
    268     public void setProxyPort(FileSystemOptions opts, int proxyPort)
    269     {
    270         setParam(opts, "proxyPort", new Integer(proxyPort));
    271     }
    272
    273     /***
    274      * Get the proxy to use for sftp connection
    275      * You have to set the ProxyPort too if you would like to have the proxy relly used.
    276      *
    277      * @return proxyHost
    278      * @see #setProxyPort
    279      */
    280     public String getProxyHost(FileSystemOptions opts)
    281     {
    282         return (String) getParam(opts, "proxyHost");
    283     }
    284
    285     /***
    286      * Get the proxy-port to use for sftp the connection
    287      * You have to set the ProxyHost too if you would like to have the proxy relly used.
    288      *
    289      * @return proxyPort: the port number or 0 if it is not set
    290      * @see #setProxyHost
    291      */
    292     public int getProxyPort(FileSystemOptions opts)
    293     {
    294         if (!hasParam(opts, "proxyPort"))
    295         {
    296             return 0;
    297         }
    298
    299         return ((Number) getParam(opts, "proxyPort")).intValue();
    300     }
    301
    302     /***
    303      * Set the proxy type to use for sftp connection.
    304      */
    305     public void setProxyType(FileSystemOptions opts, ProxyType proxyType)
    306     {
    307         setParam(opts, "proxyType", proxyType);
    308     }
    309
    310     /***
    311      * Get the proxy type to use for sftp connection.
    312      */
    313     public ProxyType getProxyType(FileSystemOptions opts)
    314     {
    315         return (ProxyType) getParam(opts, "proxyType");
    316     }
    317 }