Search in sources :

Example 16 with CommandLineResult

use of org.eclipse.che.plugin.svn.server.upstream.CommandLineResult in project che by eclipse.

the class SubversionApi method update.

/**
     * Perform an "svn checkout" based on the request.
     *
     * @param request
     *         the request
     * @return the response
     * @throws IOException
     *         if there is a problem executing the command
     * @throws SubversionException
     *         if there is a Subversion issue
     */
public CLIOutputWithRevisionResponse update(final UpdateRequest request) throws IOException, SubversionException, UnauthorizedException {
    final File projectPath = new File(request.getProjectPath());
    final List<String> uArgs = defaultArgs();
    // Flags
    addFlag(uArgs, "--ignore-externals", request.isIgnoreExternals());
    // Options
    addOption(uArgs, "--depth", request.getDepth());
    addOption(uArgs, "--revision", request.getRevision());
    // Command Name
    uArgs.add("update");
    final CommandLineResult result = runCommand(null, uArgs, projectPath, addWorkingCopyPathIfNecessary(request.getPaths()), request.getUsername(), request.getPassword());
    return DtoFactory.getInstance().createDto(CLIOutputWithRevisionResponse.class).withCommand(result.getCommandLine().toString()).withRevision(SubversionUtils.getUpdateRevision(result.getStdout())).withOutput(result.getStdout()).withErrOutput(result.getStderr());
}
Also used : CommandLineResult(org.eclipse.che.plugin.svn.server.upstream.CommandLineResult) File(java.io.File)

Example 17 with CommandLineResult

use of org.eclipse.che.plugin.svn.server.upstream.CommandLineResult in project che by eclipse.

the class SubversionApi method propset.

/**
     * Perform an "svn propset" based on the request.
     *
     * @param request
     *         the request
     * @return the response
     * @throws IOException
     *         if there is a problem executing the command
     * @throws ServerException
     *         if there is a Subversion issue
     */
public CLIOutputResponse propset(final PropertySetRequest request) throws IOException, ServerException, UnauthorizedException {
    final File projectPath = new File(request.getProjectPath());
    final List<String> uArgs = defaultArgs();
    if (request.isForce()) {
        uArgs.add("--force");
    }
    addDepth(uArgs, request.getDepth().getValue());
    uArgs.add("propset");
    uArgs.add(request.getName());
    String value = request.getValue();
    Path valueFile = null;
    if (value.contains("\n")) {
        try {
            valueFile = java.nio.file.Files.createTempFile("svn-propset-value-", null);
            java.nio.file.Files.write(valueFile, value.getBytes());
            uArgs.add("-F");
            uArgs.add(valueFile.toString());
        } catch (IOException e) {
            uArgs.add(value);
        }
    } else {
        uArgs.add(value);
    }
    final CommandLineResult result = runCommand(null, uArgs, projectPath, Arrays.asList(request.getPath()));
    return DtoFactory.getInstance().createDto(CLIOutputResponse.class).withCommand(result.getCommandLine().toString()).withOutput(result.getStdout()).withErrOutput(result.getStderr());
}
Also used : Path(java.nio.file.Path) CommandLineResult(org.eclipse.che.plugin.svn.server.upstream.CommandLineResult) IOException(java.io.IOException) CLIOutputResponse(org.eclipse.che.plugin.svn.shared.CLIOutputResponse) File(java.io.File)

Example 18 with CommandLineResult

use of org.eclipse.che.plugin.svn.server.upstream.CommandLineResult in project che by eclipse.

the class SubversionApi method lockUnlock.

public CLIOutputResponse lockUnlock(final LockRequest request, final boolean lock) throws IOException, SubversionException, UnauthorizedException {
    final File projectPath = new File(request.getProjectPath());
    final List<String> args = defaultArgs();
    addFlag(args, "--force", request.isForce());
    // command
    if (lock) {
        args.add("lock");
    } else {
        args.add("unlock");
    }
    final CommandLineResult result = runCommand(null, args, projectPath, request.getTargets(), request.getUsername(), request.getPassword());
    return DtoFactory.getInstance().createDto(CLIOutputResponse.class).withCommand(result.getCommandLine().toString()).withOutput(result.getStdout()).withErrOutput(result.getStderr());
}
Also used : CommandLineResult(org.eclipse.che.plugin.svn.server.upstream.CommandLineResult) CLIOutputResponse(org.eclipse.che.plugin.svn.shared.CLIOutputResponse) File(java.io.File)

Example 19 with CommandLineResult

use of org.eclipse.che.plugin.svn.server.upstream.CommandLineResult in project che by eclipse.

the class SubversionApi method showLog.

/**
     * Perform an "svn log" based on the request.
     *
     * @param request
     *         the request
     * @return the response
     * @throws IOException
     *         if there is a problem executing the command
     * @throws SubversionException
     *         if there is a Subversion issue
     */
public CLIOutputResponse showLog(final ShowLogRequest request) throws IOException, SubversionException, UnauthorizedException {
    final File projectPath = new File(request.getProjectPath());
    final List<String> uArgs = defaultArgs();
    addOption(uArgs, "--revision", request.getRevision());
    uArgs.add("log");
    final CommandLineResult result = runCommand(null, uArgs, projectPath, request.getPaths());
    return DtoFactory.getInstance().createDto(CLIOutputResponse.class).withCommand(result.getCommandLine().toString()).withOutput(result.getStdout()).withErrOutput(result.getStderr());
}
Also used : CommandLineResult(org.eclipse.che.plugin.svn.server.upstream.CommandLineResult) CLIOutputResponse(org.eclipse.che.plugin.svn.shared.CLIOutputResponse) File(java.io.File)

Example 20 with CommandLineResult

use of org.eclipse.che.plugin.svn.server.upstream.CommandLineResult 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

CommandLineResult (org.eclipse.che.plugin.svn.server.upstream.CommandLineResult)28 File (java.io.File)27 CLIOutputResponse (org.eclipse.che.plugin.svn.shared.CLIOutputResponse)18 ArrayList (java.util.ArrayList)9 IOException (java.io.IOException)5 InfoResponse (org.eclipse.che.plugin.svn.shared.InfoResponse)5 ListResponse (org.eclipse.che.plugin.svn.shared.ListResponse)5 Predicate (com.google.common.base.Predicate)4 Path (java.nio.file.Path)4 Date (java.util.Date)4 Response (javax.ws.rs.core.Response)4 ServerException (org.eclipse.che.api.core.ServerException)4 UnauthorizedException (org.eclipse.che.api.core.UnauthorizedException)4 DeleteOnCloseFileInputStream (org.eclipse.che.api.vfs.util.DeleteOnCloseFileInputStream)4 SubversionItem (org.eclipse.che.plugin.svn.shared.SubversionItem)4 Strings.isNullOrEmpty (com.google.common.base.Strings.isNullOrEmpty)3 Iterables (com.google.common.collect.Iterables)3 Files (com.google.common.io.Files)3 MediaType (com.google.common.net.MediaType)3 Singleton (com.google.inject.Singleton)3