View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm;
20  
21  import java.io.Serializable;
22  import java.util.regex.Matcher;
23  import java.util.regex.Pattern;
24  
25  /**
26   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
27   */
28  public class ScmResult implements Serializable {
29      private static final long serialVersionUID = 7037918334820621525L;
30  
31      private final boolean success;
32  
33      private final String providerMessage;
34  
35      private final String commandOutput;
36  
37      private final String commandLine;
38  
39      public static final String PASSWORD_PLACE_HOLDER = "********";
40  
41      // works for SVN and git
42      private Pattern patternForUserColonPasswordAtHost = Pattern.compile("^.*:(.*)@.*$", Pattern.DOTALL);
43  
44      /**
45       * Copy constructor.
46       * <p>
47       * Typically used from derived classes when wrapping a ScmResult
48       * into a specific type e.g. AddScmResult.
49       *
50       * @param scmResult not null
51       */
52      public ScmResult(ScmResult scmResult) {
53          this.commandLine = scmResult.commandLine;
54  
55          this.providerMessage = scmResult.providerMessage;
56  
57          this.commandOutput = masked(scmResult.commandOutput);
58  
59          this.success = scmResult.success;
60      }
61  
62      /**
63       * ScmResult contructor.
64       *
65       * @param commandLine     the provider specific command line used
66       * @param providerMessage the provider message
67       * @param commandOutput   the command output of the scm tool
68       * @param success         true if the command is in success
69       */
70      public ScmResult(String commandLine, String providerMessage, String commandOutput, boolean success) {
71          this.commandLine = commandLine;
72  
73          this.providerMessage = providerMessage;
74  
75          this.commandOutput = masked(commandOutput);
76  
77          this.success = success;
78      }
79  
80      /**
81       * @return true if the command was in success
82       */
83      public boolean isSuccess() {
84          return success;
85      }
86  
87      /**
88       * @return a message from the provider. On success this would typically be null or
89       *         an empty string. On failure, it would be the error message from the provider
90       */
91      public String getProviderMessage() {
92          return providerMessage;
93      }
94  
95      /**
96       * @return output from Std.Out from the provider during execution
97       *         of the command that resulted in this
98       */
99      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 }