Exemplo n.º 1
0
  /**
   * Creates a {@link FileSystem}. If you're looking at this method and wondering how to get a
   * FileSystemOptions object bearing the proxy host and credentials configuration through to this
   * method so it's used for resolving a {@link FileObject} in the FileSystem, then be sure to use
   * correct signature of the {@link FileSystemManager} resolveFile method.
   *
   * @see org.apache.commons.vfs2.impl.DefaultFileSystemManager#resolveFile(FileObject, String,
   *     FileSystemOptions)
   */
  @Override
  protected FileSystem doCreateFileSystem(
      final FileName name, final FileSystemOptions fileSystemOptions) throws FileSystemException {
    // Create the file system
    final GenericFileName rootName = (GenericFileName) name;
    FileSystemOptions fsOpts =
        fileSystemOptions == null ? new FileSystemOptions() : fileSystemOptions;

    UserAuthenticationData authData = null;
    HttpClient httpClient;
    try {
      authData = UserAuthenticatorUtils.authenticate(fsOpts, AUTHENTICATOR_TYPES);

      httpClient =
          HttpClientFactory.createConnection(
              WebdavFileSystemConfigBuilder.getInstance(),
              "http",
              rootName.getHostName(),
              rootName.getPort(),
              UserAuthenticatorUtils.toString(
                  UserAuthenticatorUtils.getData(
                      authData,
                      UserAuthenticationData.USERNAME,
                      UserAuthenticatorUtils.toChar(rootName.getUserName()))),
              UserAuthenticatorUtils.toString(
                  UserAuthenticatorUtils.getData(
                      authData,
                      UserAuthenticationData.PASSWORD,
                      UserAuthenticatorUtils.toChar(rootName.getPassword()))),
              fsOpts);
    } finally {
      UserAuthenticatorUtils.cleanup(authData);
    }

    return new WebdavFileSystem(rootName, httpClient, fsOpts);
  }
  /** Tests parsing a URI into its parts. */
  public void testParseUri() throws Exception {
    URLFileNameParser urlParser = new URLFileNameParser(21);
    // Simple name
    GenericFileName name = (GenericFileName) urlParser.parseUri(null, null, "ftp://hostname/file");
    assertEquals("ftp", name.getScheme());
    assertNull(name.getUserName());
    assertNull(name.getPassword());
    assertEquals("hostname", name.getHostName());
    assertEquals(21, name.getPort());
    assertEquals(name.getDefaultPort(), name.getPort());
    assertEquals("/file", name.getPath());
    assertEquals("ftp://hostname/", name.getRootURI());
    assertEquals("ftp://hostname/file", name.getURI());

    // Name with port
    name = (GenericFileName) urlParser.parseUri(null, null, "ftp://*****:*****@hostname/file");
    assertEquals("ftp", name.getScheme());
    assertEquals("user", name.getUserName());
    assertNull(name.getPassword());
    assertEquals("hostname", name.getHostName());
    assertEquals(21, name.getPort());
    assertEquals("/file", name.getPath());
    assertEquals("ftp://user@hostname/", name.getRootURI());
    assertEquals("ftp://user@hostname/file", name.getURI());

    // Name with username and password
    name = (GenericFileName) urlParser.parseUri(null, null, "ftp://*****:*****@hostname/file");
    assertEquals("ftp", name.getScheme());
    assertEquals("user", name.getUserName());
    assertEquals("password", name.getPassword());
    assertEquals("hostname", name.getHostName());
    assertEquals(21, name.getPort());
    assertEquals("/file", name.getPath());
    assertEquals("ftp://*****:*****@hostname/", name.getRootURI());
    assertEquals("ftp://*****:*****@hostname/file", name.getURI());

    // Encoded username and password
    name = (GenericFileName) urlParser.parseUri(null, null, "ftp://%75ser%3A:%40@hostname");
    assertEquals("ftp", name.getScheme());
    assertEquals("user:"******"@", name.getPassword());
    assertEquals("hostname", name.getHostName());
    assertEquals(21, name.getPort());
    assertEquals("/", name.getPath());
    assertEquals("ftp://user%3a:%40@hostname/", name.getRootURI());
    assertEquals("ftp://user%3a:%40@hostname/", name.getURI());
  }