This project has retired. For details please refer to its Attic page.
DefaultRepositoryCommonValidator xref
View Javadoc
1   package org.apache.archiva.admin.repository;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  import org.apache.archiva.admin.model.RepositoryAdminException;
22  import org.apache.archiva.admin.model.RepositoryCommonValidator;
23  import org.apache.archiva.admin.model.beans.AbstractRepository;
24  import org.apache.archiva.admin.model.beans.ManagedRepository;
25  import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
26  import org.apache.archiva.configuration.ArchivaConfiguration;
27  import org.apache.archiva.configuration.Configuration;
28  import org.apache.archiva.components.registry.Registry;
29  import org.apache.archiva.components.scheduler.CronExpressionValidator;
30  import org.apache.commons.lang3.StringUtils;
31  import org.apache.commons.validator.GenericValidator;
32  import org.springframework.stereotype.Service;
33  
34  import javax.inject.Inject;
35  import javax.inject.Named;
36  
37  /**
38   * apply basic repository validation : id and name.
39   * Check if already exists.
40   *
41   * @author Olivier Lamy
42   * @since 1.4-M1
43   */
44  @Service
45  public class DefaultRepositoryCommonValidator
46      implements RepositoryCommonValidator
47  {
48  
49      @Inject
50      private ArchivaConfiguration archivaConfiguration;
51  
52      @Inject
53      @Named( value = "commons-configuration" )
54      private org.apache.archiva.components.registry.Registry registry;
55  
56      /**
57       * @param abstractRepository
58       * @param update             in update mode if yes already exists won't be check
59       * @throws RepositoryAdminException
60       */
61      @Override
62      public void basicValidation( AbstractRepository abstractRepository, boolean update )
63          throws RepositoryAdminException
64      {
65          Configuration config = archivaConfiguration.getConfiguration();
66  
67          String repoId = abstractRepository.getId();
68  
69          if ( !update )
70          {
71  
72              if ( config.getManagedRepositoriesAsMap().containsKey( repoId ) )
73              {
74                  throw new RepositoryAdminException( "Unable to add new repository with id [" + repoId
75                                                          + "], that id already exists as a managed repository." );
76              }
77              else if ( config.getRepositoryGroupsAsMap().containsKey( repoId ) )
78              {
79                  throw new RepositoryAdminException( "Unable to add new repository with id [" + repoId
80                                                          + "], that id already exists as a repository group." );
81              }
82              else if ( config.getRemoteRepositoriesAsMap().containsKey( repoId ) )
83              {
84                  throw new RepositoryAdminException( "Unable to add new repository with id [" + repoId
85                                                          + "], that id already exists as a remote repository." );
86              }
87          }
88  
89          if ( StringUtils.isBlank( repoId ) )
90          {
91              throw new RepositoryAdminException( "Repository ID cannot be empty." );
92          }
93  
94          if ( !GenericValidator.matchRegexp( repoId, REPOSITORY_ID_VALID_EXPRESSION ) )
95          {
96              throw new RepositoryAdminException(
97                  "Invalid repository ID. Identifier must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-)." );
98          }
99  
100         String name = abstractRepository.getName();
101 
102         if ( StringUtils.isBlank( name ) )
103         {
104             throw new RepositoryAdminException( "repository name cannot be empty" );
105         }
106 
107         if ( !GenericValidator.matchRegexp( name, REPOSITORY_NAME_VALID_EXPRESSION ) )
108         {
109             throw new RepositoryAdminException(
110                 "Invalid repository name. Repository Name must only contain alphanumeric characters, white-spaces(' '), "
111                     + "forward-slashes(/), open-parenthesis('('), close-parenthesis(')'),  underscores(_), dots(.), and dashes(-)." );
112         }
113 
114     }
115 
116     /**
117      * validate cronExpression and location format
118      *
119      * @param managedRepository
120      * @since 1.4-M2
121      */
122     @Override
123     public void validateManagedRepository( ManagedRepository managedRepository )
124         throws RepositoryAdminException
125     {
126         String cronExpression = managedRepository.getCronExpression();
127         // FIXME : olamy can be empty to avoid scheduled scan ?
128         if ( StringUtils.isNotBlank( cronExpression ) )
129         {
130             CronExpressionValidator validator = new CronExpressionValidator();
131 
132             if ( !validator.validate( cronExpression ) )
133             {
134                 throw new RepositoryAdminException( "Invalid cron expression.", "cronExpression" );
135             }
136         }
137         else
138         {
139             throw new RepositoryAdminException( "Cron expression cannot be empty." );
140         }
141 
142         String repoLocation = removeExpressions( managedRepository.getLocation() );
143 
144         if ( !GenericValidator.matchRegexp( repoLocation,
145                                             ManagedRepositoryAdmin.REPOSITORY_LOCATION_VALID_EXPRESSION ) )
146         {
147             throw new RepositoryAdminException(
148                 "Invalid repository location. Directory must only contain alphanumeric characters, equals(=), question-marks(?), "
149                     + "exclamation-points(!), ampersands(&), forward-slashes(/), back-slashes(\\), underscores(_), dots(.), colons(:), tildes(~), and dashes(-).",
150                 "location" );
151         }
152     }
153 
154     /**
155      * replace some interpolations ${appserver.base} with correct values
156      *
157      * @param directory
158      * @return
159      */
160     @Override
161     public String removeExpressions( String directory )
162     {
163         String value = StringUtils.replace( directory, "${appserver.base}",
164                                             getRegistry().getString( "appserver.base", "${appserver.base}" ) );
165         value = StringUtils.replace( value, "${appserver.home}",
166                                      getRegistry().getString( "appserver.home", "${appserver.home}" ) );
167         return value;
168     }
169 
170     public ArchivaConfiguration getArchivaConfiguration()
171     {
172         return archivaConfiguration;
173     }
174 
175     public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
176     {
177         this.archivaConfiguration = archivaConfiguration;
178     }
179 
180     public Registry getRegistry()
181     {
182         return registry;
183     }
184 
185     public void setRegistry( org.apache.archiva.components.registry.Registry registry )
186     {
187         this.registry = registry;
188     }
189 }