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