Example usage for java.net PasswordAuthentication getUserName

List of usage examples for java.net PasswordAuthentication getUserName

Introduction

In this page you can find the example usage for java.net PasswordAuthentication getUserName.

Prototype

public String getUserName() 

Source Link

Document

Returns the user name.

Usage

From source file:com.adaptris.core.http.auth.ThreadLocalCredentials.java

@Override
public PasswordAuthentication authenticate(ResourceTarget target) {
    if (matcher().matches(target)) {
        PasswordAuthentication auth = getThreadCredentials();
        if (auth != null) {
            log.trace("Using user={} to login", auth.getUserName());
        }//from   www. j  a va 2  s .  co m
        return auth;
    }
    return null;
}

From source file:net.sf.eclipsecs.ui.config.configtypes.RemoteConfigurationEditor.java

/**
 * {@inheritDoc}//from  w  w  w .j av  a 2s. c o m
 */
public Control createEditorControl(Composite parent, final Shell shell) {

    Composite contents = new Composite(parent, SWT.NULL);
    contents.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    GridLayout layout = new GridLayout(2, false);
    layout.marginWidth = 0;
    layout.marginHeight = 0;
    contents.setLayout(layout);

    Label lblConfigName = new Label(contents, SWT.NULL);
    lblConfigName.setText(Messages.CheckConfigurationPropertiesDialog_lblName);
    GridData gd = new GridData();
    lblConfigName.setLayoutData(gd);

    mConfigName = new Text(contents, SWT.LEFT | SWT.SINGLE | SWT.BORDER);
    gd = new GridData(GridData.FILL_HORIZONTAL);
    mConfigName.setLayoutData(gd);

    Label lblConfigLocation = new Label(contents, SWT.NULL);
    lblConfigLocation.setText(Messages.CheckConfigurationPropertiesDialog_lblLocation);
    gd = new GridData();
    gd.verticalAlignment = GridData.VERTICAL_ALIGN_BEGINNING;
    lblConfigLocation.setLayoutData(gd);

    mLocation = new Text(contents, SWT.LEFT | SWT.SINGLE | SWT.BORDER);
    gd = new GridData(GridData.FILL_HORIZONTAL);
    mLocation.setLayoutData(gd);

    Label lblDescription = new Label(contents, SWT.NULL);
    lblDescription.setText(Messages.CheckConfigurationPropertiesDialog_lblDescription);
    gd = new GridData();
    gd.horizontalSpan = 2;
    lblDescription.setLayoutData(gd);

    mDescription = new Text(contents, SWT.LEFT | SWT.WRAP | SWT.MULTI | SWT.BORDER | SWT.VERTICAL);
    gd = new GridData(GridData.FILL_BOTH);
    gd.horizontalSpan = 2;
    gd.widthHint = 300;
    gd.heightHint = 100;
    gd.grabExcessHorizontalSpace = true;
    gd.grabExcessVerticalSpace = true;
    mDescription.setLayoutData(gd);

    Group credentialsGroup = new Group(contents, SWT.NULL);
    credentialsGroup.setText(Messages.RemoteConfigurationEditor_titleCredentialsGroup);
    gd = new GridData(GridData.FILL_HORIZONTAL);
    gd.horizontalSpan = 2;
    credentialsGroup.setLayoutData(gd);
    credentialsGroup.setLayout(new GridLayout(2, false));

    Label lblUserName = new Label(credentialsGroup, SWT.NULL);
    lblUserName.setText(Messages.RemoteConfigurationEditor_lblUserName);
    gd = new GridData();
    lblUserName.setLayoutData(gd);

    mUserName = new Text(credentialsGroup, SWT.SINGLE | SWT.BORDER);
    gd = new GridData();
    gd.widthHint = 100;
    mUserName.setLayoutData(gd);

    Label lblPassword = new Label(credentialsGroup, SWT.NULL);
    lblPassword.setText(Messages.RemoteConfigurationEditor_lblPassword);
    gd = new GridData();
    lblPassword.setLayoutData(gd);

    mPassword = new Text(credentialsGroup, SWT.SINGLE | SWT.BORDER | SWT.PASSWORD);
    gd = new GridData();
    gd.widthHint = 100;
    mPassword.setLayoutData(gd);

    Group advancedGroup = new Group(contents, SWT.NULL);
    advancedGroup.setText(Messages.RemoteConfigurationEditor_titleAdvancedOptions);
    gd = new GridData(GridData.FILL_HORIZONTAL);
    gd.horizontalSpan = 2;
    advancedGroup.setLayoutData(gd);
    advancedGroup.setLayout(new GridLayout(2, false));

    mChkCacheConfig = new Button(advancedGroup, SWT.CHECK);
    mChkCacheConfig.setText(Messages.RemoteConfigurationEditor_btnCacheRemoteConfig);
    gd = new GridData(GridData.FILL_HORIZONTAL);
    gd.horizontalSpan = 2;
    mChkCacheConfig.setLayoutData(gd);

    if (mWorkingCopy.getName() != null) {
        mConfigName.setText(mWorkingCopy.getName());
    }
    if (mWorkingCopy.getLocation() != null) {
        mLocation.setText(mWorkingCopy.getLocation());
    }
    if (mWorkingCopy.getDescription() != null) {
        mDescription.setText(mWorkingCopy.getDescription());
    }

    mChkCacheConfig.setSelection(
            Boolean.valueOf(mWorkingCopy.getAdditionalData().get(RemoteConfigurationType.KEY_CACHE_CONFIG))
                    .booleanValue());

    if (mWorkingCopy.getLocation() != null) {
        try {

            PasswordAuthentication auth = RemoteConfigurationType.RemoteConfigAuthenticator
                    .getPasswordAuthentication(mWorkingCopy.getResolvedConfigurationFileURL());

            if (auth != null) {
                mUserName.setText(auth.getUserName());
                mPassword.setText(new String(auth.getPassword()));
            }
        } catch (CheckstylePluginException e) {
            CheckstyleUIPlugin.errorDialog(shell, e, true);
        }
    }

    return contents;
}

From source file:com.intellij.util.net.HttpConfigurable.java

public void putGenericPassword(final String host, final int port, final PasswordAuthentication authentication,
        final boolean remember) {
    final PasswordAuthentication coded = new PasswordAuthentication(authentication.getUserName(),
            encode(String.valueOf(authentication.getPassword())).toCharArray());
    synchronized (myLock) {
        myGenericPasswords.put(new CommonProxy.HostInfo("", host, port),
                new ProxyInfo(remember, coded.getUserName(), String.valueOf(coded.getPassword())));
    }/*from   w  w w  . ja  v a  2  s .com*/
}

From source file:org.callimachusproject.test.WebResource.java

public void patch(String type, byte[] body) throws IOException {
    ByteArrayEntity entity = new ByteArrayEntity(body);
    entity.setContentType(type);/*w  w  w.ja  v a 2  s. co  m*/
    HttpPatch req = new HttpPatch(uri);
    req.setEntity(entity);
    DefaultHttpClient client = new DefaultHttpClient();
    URL url = req.getURI().toURL();
    int port = url.getPort();
    String host = url.getHost();
    PasswordAuthentication passAuth = Authenticator.requestPasswordAuthentication(url.getHost(),
            InetAddress.getByName(url.getHost()), port, url.getProtocol(), "", "digest", url,
            RequestorType.SERVER);
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(passAuth.getUserName(),
            new String(passAuth.getPassword()));
    client.getCredentialsProvider().setCredentials(new AuthScope(host, port), credentials);
    client.execute(req, new ResponseHandler<Void>() {
        public Void handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
            StatusLine status = response.getStatusLine();
            int code = status.getStatusCode();
            if (code != 200 && code != 204) {
                Assert.assertEquals(status.getReasonPhrase(), 204, code);
            }
            return null;
        }
    });
}

From source file:org.apache.tools.ant.taskdefs.optional.net2.FTP2.java

/**
 * Runs the task./*from   w  w  w. j  av  a2s  .co m*/
 *
 * @throws BuildException if the task fails or is not configured
 *         correctly.
 */
@Override
public void execute() throws BuildException {
    checkAttributes();

    FTPClient ftp = null;

    try {
        log("Opening FTP connection to " + server, Project.MSG_VERBOSE);

        ftp = new FTPClient();
        if (this.isConfigurationSet) {
            ftp = FTPConfigurator.configure(ftp, this);
        }

        ftp.setRemoteVerificationEnabled(enableRemoteVerification);

        if (this.proxyServer != null) {

            // Connect through an FTP proxy.

            // Get FTP proxy credentials (optional).
            String proxyUserId;
            char[] proxyPassword;
            {
                PasswordAuthentication pa = FTP2.getPasswordAuthentication(this.proxyServer, this.proxyPort,
                        this.proxyUserid, this.proxyPassword, RequestorType.PROXY);
                if (pa == null) {
                    proxyUserId = null;
                    proxyPassword = null;
                } else {
                    proxyUserId = pa.getUserName();
                    if (proxyUserId == null)
                        throw new BuildException("Proxy user ID missing");
                    proxyPassword = pa.getPassword();
                    if (proxyPassword == null)
                        throw new BuildException("Proxy password missing");
                }
            }

            // Get remote FTP server credentials (mandatory).
            String serverUserId;
            char[] serverPassword;
            {
                PasswordAuthentication pa = FTP2.getPasswordAuthentication(this.server, this.port, this.userid,
                        this.password, RequestorType.SERVER);
                if (pa == null)
                    throw new BuildException("User ID and password missing");
                serverUserId = pa.getUserName();
                if (serverUserId == null)
                    throw new BuildException("User ID missing");
                serverPassword = pa.getPassword();
                if (serverPassword == null)
                    throw new BuildException("Password missing");
            }

            // Connect to the FTP proxy.
            this.log("Opening FTP connection to proxy server \"" + this.proxyServer + "\" on port "
                    + this.proxyPort, Project.MSG_VERBOSE);
            ftp.connect(this.proxyServer, this.proxyPort);
            if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                throw new BuildException("FTP connection to proxy server failed: " + ftp.getReplyString());
            }
            this.log("connected to proxy server", Project.MSG_VERBOSE);

            // Authenticate with the FTP proxy (optional).
            if (proxyUserId != null) {
                this.log("logging in to FTP proxy server", Project.MSG_VERBOSE);
                if (!ftp.login(proxyUserId, new String(proxyPassword))) {
                    throw new BuildException("Could not login to FTP proxy server");
                }
            }

            // Log in to the remote FTP server.
            this.log("logging in to FTP server", Project.MSG_VERBOSE);
            String userid2 = serverUserId + '@' + this.server;
            if (this.port != FTP2.DEFAULT_FTP_PORT)
                userid2 += ":" + this.port;
            if (this.account == null ? !ftp.login(userid2, new String(serverPassword))
                    : !ftp.login(userid2, new String(serverPassword), this.account))
                throw new BuildException("Could not login to FTP server");
            this.log("login succeeded", Project.MSG_VERBOSE);
        } else {

            // Direct connection to remote FTP server.

            // Get remote FTP server credentials (mandatory).
            String serverUserId;
            char[] serverPassword;
            {
                PasswordAuthentication pa = FTP2.getPasswordAuthentication(this.server, this.port, this.userid,
                        this.password, RequestorType.SERVER);
                if (pa == null)
                    throw new BuildException("User ID and password missing");
                serverUserId = pa.getUserName();
                if (serverUserId == null)
                    throw new BuildException("User ID missing");
                serverPassword = pa.getPassword();
                if (serverPassword == null)
                    throw new BuildException("Password missing");
            }

            // Connect to the remote FTP server.
            this.log("Opening FTP connection to " + this.server, Project.MSG_VERBOSE);
            ftp.connect(this.server, this.port);
            if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                throw new BuildException("FTP connection failed: " + ftp.getReplyString());
            }
            this.log("connected", Project.MSG_VERBOSE);

            // Log in to the remote FTP server.
            this.log("logging in to FTP server", Project.MSG_VERBOSE);
            if (this.account == null ? !ftp.login(serverUserId, new String(serverPassword))
                    : !ftp.login(serverUserId, new String(serverPassword), this.account))
                throw new BuildException("Could not login to FTP server");
            this.log("login succeeded", Project.MSG_VERBOSE);
        }

        if (binary) {
            ftp.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
            if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                throw new BuildException("could not set transfer type: " + ftp.getReplyString());
            }
        } else {
            ftp.setFileType(org.apache.commons.net.ftp.FTP.ASCII_FILE_TYPE);
            if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                throw new BuildException("could not set transfer type: " + ftp.getReplyString());
            }
        }

        if (passive) {
            log("entering passive mode", Project.MSG_VERBOSE);
            ftp.enterLocalPassiveMode();
            if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                throw new BuildException("could not enter into passive " + "mode: " + ftp.getReplyString());
            }
        }

        // If an initial command was configured then send it.
        // Some FTP servers offer different modes of operation,
        // E.G. switching between a UNIX file system mode and
        // a legacy file system.
        if (this.initialSiteCommand != null) {
            RetryHandler h = new RetryHandler(this.retriesAllowed, this);
            final FTPClient lftp = ftp;
            executeRetryable(h, new Retryable() {
                @Override
                public void execute() throws IOException {
                    doSiteCommand(lftp, FTP2.this.initialSiteCommand);
                }
            }, "initial site command: " + this.initialSiteCommand);
        }

        // For a unix ftp server you can set the default mask for all files
        // created.

        if (umask != null) {
            RetryHandler h = new RetryHandler(this.retriesAllowed, this);
            final FTPClient lftp = ftp;
            executeRetryable(h, new Retryable() {
                @Override
                public void execute() throws IOException {
                    doSiteCommand(lftp, "umask " + umask);
                }
            }, "umask " + umask);
        }

        // If the action is MK_DIR, then the specified remote
        // directory is the directory to create.

        if (action == MK_DIR) {
            RetryHandler h = new RetryHandler(this.retriesAllowed, this);
            final FTPClient lftp = ftp;
            executeRetryable(h, new Retryable() {
                @Override
                public void execute() throws IOException {
                    makeRemoteDir(lftp, remotedir);
                }
            }, remotedir);
        } else if (action == SITE_CMD) {
            RetryHandler h = new RetryHandler(this.retriesAllowed, this);
            final FTPClient lftp = ftp;
            executeRetryable(h, new Retryable() {
                @Override
                public void execute() throws IOException {
                    doSiteCommand(lftp, FTP2.this.siteCommand);
                }
            }, "Site Command: " + this.siteCommand);
        } else {
            if (remotedir != null) {
                log("changing the remote directory to " + remotedir, Project.MSG_VERBOSE);
                ftp.changeWorkingDirectory(remotedir);
                if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                    throw new BuildException("could not change remote " + "directory: " + ftp.getReplyString());
                }
            }
            if (newerOnly && timeDiffAuto) {
                // in this case we want to find how much time span there is between local
                // and remote
                timeDiffMillis = getTimeDiff(ftp);
            }
            log(ACTION_STRS[action] + " " + ACTION_TARGET_STRS[action]);
            transferFiles(ftp);
        }

    } catch (IOException ex) {
        throw new BuildException("error during FTP transfer: " + ex, ex);
    } finally {
        if (ftp != null && ftp.isConnected()) {
            try {
                log("disconnecting", Project.MSG_VERBOSE);
                ftp.logout();
                ftp.disconnect();
            } catch (IOException ex) {
                // ignore it
            }
        }
    }
}

From source file:org.apache.ws.scout.registry.BusinessLifeCycleManagerImpl.java

private String getUsernameFromCredentials(Set credentials) {
    String username = "", pwd = "";

    if (credentials != null) {
        Iterator it = credentials.iterator();
        while (it.hasNext()) {
            PasswordAuthentication pass = (PasswordAuthentication) it.next();
            username = pass.getUserName();
        }/*from  w  w w  .  j  a v  a 2 s  .  c om*/
    }
    return username;
}

From source file:org.apache.ws.scout.registry.BusinessLifeCycleManagerImpl.java

/**
 * Get the Auth Token from the registry//w  w w.  ja v  a2  s .  co  m
 *
 * @param connection
 * @param ireg
 * @return auth token
 * @throws JAXRException
 */
private AuthToken getAuthToken(ConnectionImpl connection, IRegistry ireg) throws JAXRException {
    Set creds = connection.getCredentials();
    String username = "", pwd = "";
    if (creds != null) {
        Iterator it = creds.iterator();
        while (it.hasNext()) {
            PasswordAuthentication pass = (PasswordAuthentication) it.next();
            username = pass.getUserName();
            pwd = new String(pass.getPassword());
        }
    }

    if (AuthTokenSingleton.getToken(username) != null) {
        return (AuthToken) AuthTokenSingleton.getToken(username);
    }

    AuthToken token = null;
    try {
        token = ireg.getAuthToken(username, pwd);
    } catch (Exception e) {
        throw new JAXRException(e);
    }
    AuthTokenSingleton.addAuthToken(username, token);
    return token;
}

From source file:org.apache.ws.scout.registry.BusinessLifeCycleManagerV3Impl.java

/**
 * Get the Auth Token from the registry//from   w w  w  .j  av  a2s. c om
 *
 * @param connection
 * @param ireg
 * @return auth token
 * @throws JAXRException
 */
private AuthToken getAuthToken(ConnectionImpl connection, IRegistryV3 ireg) throws JAXRException {

    Set creds = connection.getCredentials();
    String username = "", pwd = "";

    if (creds != null) {
        Iterator it = creds.iterator();
        while (it.hasNext()) {
            PasswordAuthentication pass = (PasswordAuthentication) it.next();
            username = pass.getUserName();
            pwd = new String(pass.getPassword());
        }
    }

    if (AuthTokenV3Singleton.getToken(username) != null) {
        return (AuthToken) AuthTokenV3Singleton.getToken(username);
    }

    AuthToken token = null;
    try {
        token = ireg.getAuthToken(username, pwd);
    } catch (Exception e) {
        throw new JAXRException(e);
    }
    AuthTokenV3Singleton.addAuthToken(username, token);
    return token;
}

From source file:org.apache.ws.scout.registry.BusinessQueryManagerImpl.java

/**
  * Get the Auth Token from the registry
  */*  w w  w.j a v  a  2s  . c o m*/
  * @param connection
  * @param ireg
  * @return auth token
  * @throws JAXRException
  */
private AuthToken getAuthToken(ConnectionImpl connection, IRegistry ireg) throws JAXRException {
    Set creds = connection.getCredentials();
    Iterator it = creds.iterator();
    String username = "", pwd = "";
    while (it.hasNext()) {
        PasswordAuthentication pass = (PasswordAuthentication) it.next();
        username = pass.getUserName();
        pwd = new String(pass.getPassword());
    }

    if (AuthTokenSingleton.getToken(username) != null) {
        return (AuthToken) AuthTokenSingleton.getToken(username);
    }

    AuthToken token = null;
    try {
        token = ireg.getAuthToken(username, pwd);
    } catch (Exception e) {
        throw new JAXRException(e);
    }
    AuthTokenSingleton.addAuthToken(username, token);

    return token;
}