View Javadoc
1   package org.apache.maven.settings.validation;
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.util.HashSet;
23  import java.util.List;
24  import java.util.Set;
25  
26  import org.apache.maven.settings.Mirror;
27  import org.apache.maven.settings.Profile;
28  import org.apache.maven.settings.Repository;
29  import org.apache.maven.settings.Server;
30  import org.apache.maven.settings.Settings;
31  import org.apache.maven.settings.building.SettingsProblem.Severity;
32  import org.apache.maven.settings.building.SettingsProblemCollector;
33  import org.codehaus.plexus.component.annotations.Component;
34  import org.codehaus.plexus.util.StringUtils;
35  
36  /**
37   * @author Milos Kleint
38   */
39  @Component( role = SettingsValidator.class )
40  public class DefaultSettingsValidator
41      implements SettingsValidator
42  {
43  
44      private static final String ID_REGEX = "[A-Za-z0-9_\\-.]+";
45  
46      private static final String ILLEGAL_FS_CHARS = "\\/:\"<>|?*";
47  
48      private static final String ILLEGAL_REPO_ID_CHARS = ILLEGAL_FS_CHARS;
49  
50      public void validate( Settings settings, SettingsProblemCollector problems )
51      {
52          if ( settings.isUsePluginRegistry() )
53          {
54              addViolation( problems, Severity.WARNING, "usePluginRegistry", null, "is deprecated and has no effect." );
55          }
56  
57          List<String> pluginGroups = settings.getPluginGroups();
58  
59          if ( pluginGroups != null )
60          {
61              for ( int i = 0; i < pluginGroups.size(); i++ )
62              {
63                  String pluginGroup = pluginGroups.get( i ).trim();
64  
65                  if ( StringUtils.isBlank( pluginGroup ) )
66                  {
67                      addViolation( problems, Severity.ERROR, "pluginGroups.pluginGroup[" + i + "]", null,
68                                    "must not be empty" );
69                  }
70                  else if ( !pluginGroup.matches( ID_REGEX ) )
71                  {
72                      addViolation( problems, Severity.ERROR, "pluginGroups.pluginGroup[" + i + "]", null,
73                                    "must denote a valid group id and match the pattern " + ID_REGEX );
74                  }
75              }
76          }
77  
78          List<Server> servers = settings.getServers();
79  
80          if ( servers != null )
81          {
82              Set<String> serverIds = new HashSet<String>();
83  
84              for ( int i = 0; i < servers.size(); i++ )
85              {
86                  Server server = servers.get( i );
87  
88                  validateStringNotEmpty( problems, "servers.server[" + i + "].id", server.getId(), null );
89  
90                  if ( !serverIds.add( server.getId() ) )
91                  {
92                      addViolation( problems, Severity.WARNING, "servers.server.id", null,
93                                    "must be unique but found duplicate server with id " + server.getId() );
94                  }
95              }
96          }
97  
98          List<Mirror> mirrors = settings.getMirrors();
99  
100         if ( mirrors != null )
101         {
102             for ( Mirror mirror : mirrors )
103             {
104                 validateStringNotEmpty( problems, "mirrors.mirror.id", mirror.getId(), mirror.getUrl() );
105 
106                 validateBannedCharacters( problems, "mirrors.mirror.id", Severity.WARNING, mirror.getId(), null,
107                                           ILLEGAL_REPO_ID_CHARS );
108 
109                 if ( "local".equals( mirror.getId() ) )
110                 {
111                     addViolation( problems, Severity.WARNING, "mirrors.mirror.id", null, "must not be 'local'"
112                         + ", this identifier is reserved for the local repository"
113                         + ", using it for other repositories will corrupt your repository metadata." );
114                 }
115 
116                 validateStringNotEmpty( problems, "mirrors.mirror.url", mirror.getUrl(), mirror.getId() );
117 
118                 validateStringNotEmpty( problems, "mirrors.mirror.mirrorOf", mirror.getMirrorOf(), mirror.getId() );
119             }
120         }
121 
122         List<Profile> profiles = settings.getProfiles();
123 
124         if ( profiles != null )
125         {
126             Set<String> profileIds = new HashSet<String>();
127 
128             for ( Profile profile : profiles )
129             {
130                 if ( !profileIds.add( profile.getId() ) )
131                 {
132                     addViolation( problems, Severity.WARNING, "profiles.profile.id", null,
133                                   "must be unique but found duplicate profile with id " + profile.getId() );
134                 }
135 
136                 String prefix = "profiles.profile[" + profile.getId() + "].";
137 
138                 validateRepositories( problems, profile.getRepositories(), prefix + "repositories.repository" );
139                 validateRepositories( problems, profile.getPluginRepositories(), prefix
140                     + "pluginRepositories.pluginRepository" );
141             }
142         }
143     }
144 
145     private void validateRepositories( SettingsProblemCollector problems, List<Repository> repositories, String prefix )
146     {
147         Set<String> repoIds = new HashSet<String>();
148 
149         for ( Repository repository : repositories )
150         {
151             validateStringNotEmpty( problems, prefix + ".id", repository.getId(), repository.getUrl() );
152 
153             validateBannedCharacters( problems, prefix + ".id", Severity.WARNING, repository.getId(), null,
154                                       ILLEGAL_REPO_ID_CHARS );
155 
156             if ( "local".equals( repository.getId() ) )
157             {
158                 addViolation( problems, Severity.WARNING, prefix + ".id", null, "must not be 'local'"
159                     + ", this identifier is reserved for the local repository"
160                     + ", using it for other repositories will corrupt your repository metadata." );
161             }
162 
163             if ( !repoIds.add( repository.getId() ) )
164             {
165                 addViolation( problems, Severity.WARNING, prefix + ".id", null,
166                               "must be unique but found duplicate repository with id " + repository.getId() );
167             }
168 
169             validateStringNotEmpty( problems, prefix + ".url", repository.getUrl(), repository.getId() );
170 
171             if ( "legacy".equals( repository.getLayout() ) )
172             {
173                 addViolation( problems, Severity.WARNING, prefix + ".layout", repository.getId(),
174                               "uses the unsupported value 'legacy', artifact resolution might fail." );
175             }
176         }
177     }
178 
179     // ----------------------------------------------------------------------
180     // Field validation
181     // ----------------------------------------------------------------------
182 
183     /**
184      * Asserts:
185      * <p/>
186      * <ul>
187      * <li><code>string.length != null</code>
188      * <li><code>string.length > 0</code>
189      * </ul>
190      */
191     private boolean validateStringNotEmpty( SettingsProblemCollector problems, String fieldName, String string,
192                                             String sourceHint )
193     {
194         if ( !validateNotNull( problems, fieldName, string, sourceHint ) )
195         {
196             return false;
197         }
198 
199         if ( string.length() > 0 )
200         {
201             return true;
202         }
203 
204         addViolation( problems, Severity.ERROR, fieldName, sourceHint, "is missing" );
205 
206         return false;
207     }
208 
209     /**
210      * Asserts:
211      * <p/>
212      * <ul>
213      * <li><code>string != null</code>
214      * </ul>
215      */
216     private boolean validateNotNull( SettingsProblemCollector problems, String fieldName, Object object,
217                                      String sourceHint )
218     {
219         if ( object != null )
220         {
221             return true;
222         }
223 
224         addViolation( problems, Severity.ERROR, fieldName, sourceHint, "is missing" );
225 
226         return false;
227     }
228 
229     private boolean validateBannedCharacters( SettingsProblemCollector problems, String fieldName, Severity severity,
230                                               String string, String sourceHint, String banned )
231     {
232         if ( string != null )
233         {
234             for ( int i = string.length() - 1; i >= 0; i-- )
235             {
236                 if ( banned.indexOf( string.charAt( i ) ) >= 0 )
237                 {
238                     addViolation( problems, severity, fieldName, sourceHint,
239                                   "must not contain any of these characters " + banned + " but found "
240                                       + string.charAt( i ) );
241                     return false;
242                 }
243             }
244         }
245 
246         return true;
247     }
248 
249     private void addViolation( SettingsProblemCollector problems, Severity severity, String fieldName,
250                                String sourceHint, String message )
251     {
252         StringBuilder buffer = new StringBuilder( 256 );
253         buffer.append( '\'' ).append( fieldName ).append( '\'' );
254 
255         if ( sourceHint != null )
256         {
257             buffer.append( " for " ).append( sourceHint );
258         }
259 
260         buffer.append( ' ' ).append( message );
261 
262         problems.add( severity, buffer.toString(), -1, -1, null );
263     }
264 
265 }