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 *
028 */
029public class ScmResult implements Serializable {
030    private static final long serialVersionUID = 7037918334820621525L;
031
032    private final boolean success;
033
034    private final String providerMessage;
035
036    private final String commandOutput;
037
038    private final String commandLine;
039
040    public static final String PASSWORD_PLACE_HOLDER = "********";
041
042    // works for SVN and git
043    private Pattern patternForUserColonPasswordAtHost = Pattern.compile("^.*:(.*)@.*$", Pattern.DOTALL);
044
045    /**
046     * Copy constructor.
047     * <p>
048     * Typically used from derived classes when wrapping a ScmResult
049     * into a specific type eg. AddScmResult
050     *
051     * @param scmResult not null
052     */
053    public ScmResult(ScmResult scmResult) {
054        this.commandLine = scmResult.commandLine;
055
056        this.providerMessage = scmResult.providerMessage;
057
058        this.commandOutput = masked(scmResult.commandOutput);
059
060        this.success = scmResult.success;
061    }
062
063    /**
064     * ScmResult contructor.
065     *
066     * @param commandLine     The provider specific command line used
067     * @param providerMessage The provider message
068     * @param commandOutput   The command output of the scm tool
069     * @param success         True if the command is in success
070     */
071    public ScmResult(String commandLine, String providerMessage, String commandOutput, boolean success) {
072        this.commandLine = commandLine;
073
074        this.providerMessage = providerMessage;
075
076        this.commandOutput = masked(commandOutput);
077
078        this.success = success;
079    }
080
081    /**
082     * @return True if the command was in success
083     */
084    public boolean isSuccess() {
085        return success;
086    }
087
088    /**
089     * @return A message from the provider. On success this would typically be null or
090     *         an empty string. On failure it would be the error message from the provider
091     */
092    public String getProviderMessage() {
093        return providerMessage;
094    }
095
096    /**
097     * @return Output from Std.Out from the provider during execution
098     *         of the command that resulted in this
099     */
100    public String getCommandOutput() {
101        return commandOutput;
102    }
103
104    /**
105     * @return The actual provider specific command that resulted in this
106     */
107    public String getCommandLine() {
108        return commandLine;
109    }
110
111    private String masked(String commandOutput) {
112        if (null != commandOutput) {
113            final Matcher passwordMatcher = patternForUserColonPasswordAtHost.matcher(commandOutput);
114            if (passwordMatcher.find()) {
115                // clear password
116                final String clearPassword = passwordMatcher.group(1);
117                // to be replaced in output by stars
118                commandOutput = commandOutput.replace(clearPassword, PASSWORD_PLACE_HOLDER);
119            }
120        }
121        return commandOutput;
122    }
123}