Example usage for org.eclipse.jgit.transport CredentialItem.Username CredentialItem.Username

List of usage examples for org.eclipse.jgit.transport CredentialItem.Username CredentialItem.Username

Introduction

In this page you can find the example usage for org.eclipse.jgit.transport CredentialItem.Username CredentialItem.Username.

Prototype

CredentialItem.Username

Source Link

Usage

From source file:org.eclipse.orion.server.git.GitSshSessionFactory.java

License:Open Source License

@Override
public RemoteSession getSession(URIish uri, CredentialsProvider credentialsProvider, FS fs, int tms)
        throws TransportException {
    int port = uri.getPort();
    String user = uri.getUser();//from  w w w .j a  va 2s.  c  o m
    String pass = uri.getPass();
    if (credentialsProvider instanceof GitCredentialsProvider) {
        if (port <= 0)
            port = SSH_PORT;

        GitCredentialsProvider cp = (GitCredentialsProvider) credentialsProvider;
        if (user == null) {
            CredentialItem.Username u = new CredentialItem.Username();
            if (cp.supports(u) && cp.get(cp.getUri(), u)) {
                user = u.getValue();
            }
        }
        if (pass == null) {
            CredentialItem.Password p = new CredentialItem.Password();
            if (cp.supports(p) && cp.get(cp.getUri(), p)) {
                pass = new String(p.getValue());
            }
        }

        try {
            final SessionHandler session = new SessionHandler(user, uri.getHost(), port, cp.getKnownHosts(),
                    cp.getPrivateKey(), cp.getPublicKey(), cp.getPassphrase());
            if (pass != null)
                session.setPassword(pass);
            if (credentialsProvider != null && !credentialsProvider.isInteractive()) {
                session.setUserInfo(new CredentialsProviderUserInfo(session.getSession(), credentialsProvider));
            }

            session.connect(tms);

            return new JschSession(session.getSession(), uri);
        } catch (JSchException e) {
            throw new TransportException(uri, e.getMessage(), e);
        }
    }
    return null;
}