示例#1
0
    public Credentials getCredentials(final AuthScope authScope) {
      if (authScope == null) {
        throw new IllegalArgumentException("Authentication scope may not be null");
      }

      //	if( cache.containsKey( authScope ) )
      //	{
      //	return cache.get( authScope );
      //	}

      String pw = getPassword();
      if (pw == null) pw = "";

      if (AuthPolicy.NTLM.equalsIgnoreCase(authScope.getScheme())
          || AuthPolicy.SPNEGO.equalsIgnoreCase(authScope.getScheme())) {
        String workstation = "";
        try {
          workstation = InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
        }

        if (hasCredentials()) {
          log.info("Returning url credentials");
          return new NTCredentials(getUsername(), pw, workstation, null);
        }

        log.info(
            authScope.getHost() + ":" + authScope.getPort() + " requires Windows authentication");
        if (ntDialog == null) {
          buildNtDialog();
        }

        StringToStringMap values = new StringToStringMap();
        values.put(
            "Info",
            "Authentication required for ["
                + authScope.getHost()
                + ":"
                + authScope.getPort()
                + "]");
        ntDialog.setValues(values);

        if (ntDialog.show()) {
          values = ntDialog.getValues();
          NTCredentials credentials =
              new NTCredentials(
                  values.get("Username"),
                  values.get("Password"),
                  workstation,
                  values.get("Domain"));

          cache.put(authScope, credentials);
          return credentials;
        }
      } else if (AuthPolicy.BASIC.equalsIgnoreCase(authScope.getScheme())
          || AuthPolicy.DIGEST.equalsIgnoreCase(authScope.getScheme())) {
        if (hasCredentials()) {
          log.info("Returning url credentials");
          UsernamePasswordCredentials credentials =
              new UsernamePasswordCredentials(getUsername(), pw);
          cache.put(authScope, credentials);
          return credentials;
        }

        log.info(
            authScope.getHost()
                + ":"
                + authScope.getPort()
                + " requires authentication with the realm '"
                + authScope.getRealm()
                + "'");
        ShowDialog showDialog = new ShowDialog();
        showDialog.values.put(
            "Info",
            "Authentication required for ["
                + authScope.getHost()
                + ":"
                + authScope.getPort()
                + "]");

        UISupport.getUIUtils().runInUIThreadIfSWT(showDialog);
        if (showDialog.result) {
          UsernamePasswordCredentials credentials =
              new UsernamePasswordCredentials(
                  showDialog.values.get("Username"), showDialog.values.get("Password"));
          cache.put(authScope, credentials);
          return credentials;
        }
      }

      return null;
    }