Search in sources :

Example 1 with SshEnvironment

use of org.eclipse.che.plugin.svn.server.utils.SshEnvironment in project che by eclipse.

the class SubversionApi method runCommand.

private CommandLineResult runCommand(@Nullable Map<String, String> env, List<String> args, File projectPath, List<String> paths, @Nullable String username, @Nullable String password, String repoUrl) throws SubversionException, UnauthorizedException {
    final List<String> lines = new ArrayList<>();
    final CommandLineResult result;
    final StringBuffer buffer;
    boolean isWarning = false;
    // Add paths to the end of the list of arguments
    for (final String path : paths) {
        args.add(path);
    }
    String[] credentialsArgs;
    if (!isNullOrEmpty(username) && !isNullOrEmpty(password)) {
        credentialsArgs = new String[] { "--username", username, "--password", password };
    } else {
        credentialsArgs = null;
    }
    SshEnvironment sshEnvironment = null;
    if (SshEnvironment.isSSH(repoUrl)) {
        sshEnvironment = new SshEnvironment(sshScriptProvider, repoUrl);
        if (env == null) {
            env = new HashMap<>();
        }
        env.putAll(sshEnvironment.get());
    }
    try {
        result = UpstreamUtils.executeCommandLine(env, "svn", args.toArray(new String[args.size()]), credentialsArgs, -1, projectPath, svnOutputPublisherFactory);
    } catch (IOException e) {
        throw new SubversionException(e);
    } finally {
        if (sshEnvironment != null) {
            sshEnvironment.cleanUp();
        }
    }
    if (result.getExitCode() != 0) {
        buffer = new StringBuffer();
        lines.addAll(result.getStdout());
        lines.addAll(result.getStderr());
        for (final String line : lines) {
            // Subversion returns an error code of 1 even when the "error" is just a warning
            if (line.startsWith("svn: warning: ")) {
                isWarning = true;
            }
            buffer.append(line);
            buffer.append("\n");
        }
        if (!isWarning) {
            String errorMessage = buffer.toString();
            if (errorMessage.endsWith("Authentication failed\n")) {
                throw new UnauthorizedException("Authentication failed", ErrorCodes.UNAUTHORIZED_SVN_OPERATION);
            } else {
                throw new SubversionException(errorMessage);
            }
        }
    }
    return result;
}
Also used : SshEnvironment(org.eclipse.che.plugin.svn.server.utils.SshEnvironment) CommandLineResult(org.eclipse.che.plugin.svn.server.upstream.CommandLineResult) ArrayList(java.util.ArrayList) UnauthorizedException(org.eclipse.che.api.core.UnauthorizedException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 UnauthorizedException (org.eclipse.che.api.core.UnauthorizedException)1 CommandLineResult (org.eclipse.che.plugin.svn.server.upstream.CommandLineResult)1 SshEnvironment (org.eclipse.che.plugin.svn.server.utils.SshEnvironment)1