Example #1
0
  /**
   * Establishes a secure and authenticated context with the server.
   *
   * @param factory factory for creating the DssContext of the secure session
   * @param username specific username to authenticate as.
   * @throws IOException on i/o error
   * @throws ServerException on server refusal or faulty server behavior
   */
  public void authenticate(DssContextFactory factory, String username)
      throws IOException, ServerException {
    try {
      // authenticate
      GridFTPControlChannel gridFTPControlChannel =
          new GridFTPControlChannel(controlChannel, factory, expectedHostName);

      // from now on, the commands and replies
      // are protected and pass through gsi wrapped socket

      // login
      try {
        Reply reply =
            gridFTPControlChannel.exchange(
                new Command("USER", (username == null) ? ":globus-mapping:" : username));
        // wu-gsiftp sends intermediate code while
        // gssftp send completion reply code
        if (!Reply.isPositiveCompletion(reply) && !Reply.isPositiveIntermediate(reply)) {
          throw ServerException.embedUnexpectedReplyCodeException(
              new UnexpectedReplyCodeException(reply), "User authorization failed.");
        }
      } catch (FTPReplyParseException rpe) {
        throw ServerException.embedFTPReplyParseException(
            rpe, "Received faulty reply to USER command.");
      }

      try {
        Reply reply = gridFTPControlChannel.exchange(new Command("PASS", "dummy"));
        if (!Reply.isPositiveCompletion(reply)) {
          throw ServerException.embedUnexpectedReplyCodeException(
              new UnexpectedReplyCodeException(reply), "Bad password.");
        }
      } catch (FTPReplyParseException rpe) {
        throw ServerException.embedFTPReplyParseException(
            rpe, "Received faulty reply to PASS command.");
      }

      this.gSession.authorized = true;
      this.username = username;
      this.controlChannel = gridFTPControlChannel;

      // quietly send version information to the server.
      // ignore errors
      try {
        this.site(usageString);
      } catch (Exception ex) {
      }

    } catch (ServerException | IOException e) {
      close();
      throw e;
    }
  }