001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm;
020
021import java.io.Serializable;
022import java.util.regex.Matcher;
023import java.util.regex.Pattern;
024
025/**
026 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
027 */
028public class ScmResult implements Serializable {
029    private static final long serialVersionUID = 7037918334820621525L;
030
031    private final boolean success;
032
033    private final String providerMessage;
034
035    private final String commandOutput;
036
037    private final String commandLine;
038
039    public static final String PASSWORD_PLACE_HOLDER = "********";
040
041    // works for SVN and git
042    private Pattern patternForUserColonPasswordAtHost = Pattern.compile("^.*:(.*)@.*$", Pattern.DOTALL);
043
044    /**
045     * Copy constructor.
046     * <p>
047     * Typically used from derived classes when wrapping a ScmResult
048     * into a specific type e.g. AddScmResult.
049     *
050     * @param scmResult not null
051     */
052    public ScmResult(ScmResult scmResult) {
053        this.commandLine = scmResult.commandLine;
054
055        this.providerMessage = scmResult.providerMessage;
056
057        this.commandOutput = masked(scmResult.commandOutput);
058
059        this.success = scmResult.success;
060    }
061
062    /**
063     * ScmResult contructor.
064     *
065     * @param commandLine     the provider specific command line used
066     * @param providerMessage the provider message
067     * @param commandOutput   the command output of the scm tool
068     * @param success         true if the command is in success
069     */
070    public ScmResult(String commandLine, String providerMessage, String commandOutput, boolean success) {
071        this.commandLine = commandLine;
072
073        this.providerMessage = providerMessage;
074
075        this.commandOutput = masked(commandOutput);
076
077        this.success = success;
078    }
079
080    /**
081     * @return true if the command was in success
082     */
083    public boolean isSuccess() {
084        return success;
085    }
086
087    /**
088     * @return a message from the provider. On success this would typically be null or
089     *         an empty string. On failure, it would be the error message from the provider
090     */
091    public String getProviderMessage() {
092        return providerMessage;
093    }
094
095    /**
096     * @return output from Std.Out from the provider during execution
097     *         of the command that resulted in this
098     */
099    public String getCommandOutput() {
100        return commandOutput;
101    }
102
103    /**
104     * @return the actual provider specific command that resulted in this
105     */
106    public String getCommandLine() {
107        return commandLine;
108    }
109
110    private String masked(String commandOutput) {
111        if (null != commandOutput) {
112            final Matcher passwordMatcher = patternForUserColonPasswordAtHost.matcher(commandOutput);
113            if (passwordMatcher.find()) {
114                // clear password
115                final String clearPassword = passwordMatcher.group(1);
116                // to be replaced in output by stars
117                commandOutput = commandOutput.replace(clearPassword, PASSWORD_PLACE_HOLDER);
118            }
119        }
120        return commandOutput;
121    }
122}