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