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   */
29  public class ScmResult implements Serializable {
30      private static final long serialVersionUID = 7037918334820621525L;
31  
32      private final boolean success;
33  
34      private final String providerMessage;
35  
36      private final String commandOutput;
37  
38      private final String commandLine;
39  
40      public static final String PASSWORD_PLACE_HOLDER = "********";
41  
42      // works for SVN and git
43      private Pattern patternForUserColonPasswordAtHost = Pattern.compile("^.*:(.*)@.*$", Pattern.DOTALL);
44  
45      /**
46       * Copy constructor.
47       * <p>
48       * Typically used from derived classes when wrapping a ScmResult
49       * into a specific type eg. AddScmResult
50       *
51       * @param scmResult not null
52       */
53      public ScmResult(ScmResult scmResult) {
54          this.commandLine = scmResult.commandLine;
55  
56          this.providerMessage = scmResult.providerMessage;
57  
58          this.commandOutput = masked(scmResult.commandOutput);
59  
60          this.success = scmResult.success;
61      }
62  
63      /**
64       * ScmResult contructor.
65       *
66       * @param commandLine     The provider specific command line used
67       * @param providerMessage The provider message
68       * @param commandOutput   The command output of the scm tool
69       * @param success         True if the command is in success
70       */
71      public ScmResult(String commandLine, String providerMessage, String commandOutput, boolean success) {
72          this.commandLine = commandLine;
73  
74          this.providerMessage = providerMessage;
75  
76          this.commandOutput = masked(commandOutput);
77  
78          this.success = success;
79      }
80  
81      /**
82       * @return True if the command was in success
83       */
84      public boolean isSuccess() {
85          return success;
86      }
87  
88      /**
89       * @return A message from the provider. On success this would typically be null or
90       *         an empty string. On failure it would be the error message from the provider
91       */
92      public String getProviderMessage() {
93          return providerMessage;
94      }
95  
96      /**
97       * @return Output from Std.Out from the provider during execution
98       *         of the command that resulted in this
99       */
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 }