You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultRepositoryCommonValidator.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. import org.apache.archiva.admin.model.RepositoryAdminException;
  21. import org.apache.archiva.admin.model.RepositoryCommonValidator;
  22. import org.apache.archiva.admin.model.beans.AbstractRepository;
  23. import org.apache.archiva.admin.model.beans.ManagedRepository;
  24. import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
  25. import org.apache.archiva.configuration.ArchivaConfiguration;
  26. import org.apache.archiva.configuration.Configuration;
  27. import org.apache.archiva.components.registry.Registry;
  28. import org.apache.archiva.components.scheduler.CronExpressionValidator;
  29. import org.apache.commons.lang3.StringUtils;
  30. import org.apache.commons.validator.GenericValidator;
  31. import org.springframework.stereotype.Service;
  32. import javax.inject.Inject;
  33. import javax.inject.Named;
  34. /**
  35. * apply basic repository validation : id and name.
  36. * Check if already exists.
  37. *
  38. * @author Olivier Lamy
  39. * @since 1.4-M1
  40. */
  41. @Service
  42. public class DefaultRepositoryCommonValidator
  43. implements RepositoryCommonValidator
  44. {
  45. @Inject
  46. private ArchivaConfiguration archivaConfiguration;
  47. @Inject
  48. @Named( value = "commons-configuration" )
  49. private org.apache.archiva.components.registry.Registry registry;
  50. /**
  51. * @param abstractRepository
  52. * @param update in update mode if yes already exists won't be check
  53. * @throws RepositoryAdminException
  54. */
  55. @Override
  56. public void basicValidation( AbstractRepository abstractRepository, boolean update )
  57. throws RepositoryAdminException
  58. {
  59. Configuration config = archivaConfiguration.getConfiguration();
  60. String repoId = abstractRepository.getId();
  61. if ( !update )
  62. {
  63. if ( config.getManagedRepositoriesAsMap().containsKey( repoId ) )
  64. {
  65. throw new RepositoryAdminException( "Unable to add new repository with id [" + repoId
  66. + "], that id already exists as a managed repository." );
  67. }
  68. else if ( config.getRepositoryGroupsAsMap().containsKey( repoId ) )
  69. {
  70. throw new RepositoryAdminException( "Unable to add new repository with id [" + repoId
  71. + "], that id already exists as a repository group." );
  72. }
  73. else if ( config.getRemoteRepositoriesAsMap().containsKey( repoId ) )
  74. {
  75. throw new RepositoryAdminException( "Unable to add new repository with id [" + repoId
  76. + "], that id already exists as a remote repository." );
  77. }
  78. }
  79. if ( StringUtils.isBlank( repoId ) )
  80. {
  81. throw new RepositoryAdminException( "Repository ID cannot be empty." );
  82. }
  83. if ( !GenericValidator.matchRegexp( repoId, REPOSITORY_ID_VALID_EXPRESSION ) )
  84. {
  85. throw new RepositoryAdminException(
  86. "Invalid repository ID. Identifier must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-)." );
  87. }
  88. String name = abstractRepository.getName();
  89. if ( StringUtils.isBlank( name ) )
  90. {
  91. throw new RepositoryAdminException( "repository name cannot be empty" );
  92. }
  93. if ( !GenericValidator.matchRegexp( name, REPOSITORY_NAME_VALID_EXPRESSION ) )
  94. {
  95. throw new RepositoryAdminException(
  96. "Invalid repository name. Repository Name must only contain alphanumeric characters, white-spaces(' '), "
  97. + "forward-slashes(/), open-parenthesis('('), close-parenthesis(')'), underscores(_), dots(.), and dashes(-)." );
  98. }
  99. }
  100. /**
  101. * validate cronExpression and location format
  102. *
  103. * @param managedRepository
  104. * @since 1.4-M2
  105. */
  106. @Override
  107. public void validateManagedRepository( ManagedRepository managedRepository )
  108. throws RepositoryAdminException
  109. {
  110. String cronExpression = managedRepository.getCronExpression();
  111. // FIXME : olamy can be empty to avoid scheduled scan ?
  112. if ( StringUtils.isNotBlank( cronExpression ) )
  113. {
  114. CronExpressionValidator validator = new CronExpressionValidator();
  115. if ( !validator.validate( cronExpression ) )
  116. {
  117. throw new RepositoryAdminException( "Invalid cron expression.", "cronExpression" );
  118. }
  119. }
  120. else
  121. {
  122. throw new RepositoryAdminException( "Cron expression cannot be empty." );
  123. }
  124. String repoLocation = removeExpressions( managedRepository.getLocation() );
  125. if ( !GenericValidator.matchRegexp( repoLocation,
  126. ManagedRepositoryAdmin.REPOSITORY_LOCATION_VALID_EXPRESSION ) )
  127. {
  128. throw new RepositoryAdminException(
  129. "Invalid repository location. Directory must only contain alphanumeric characters, equals(=), question-marks(?), "
  130. + "exclamation-points(!), ampersands(&), forward-slashes(/), back-slashes(\\), underscores(_), dots(.), colons(:), tildes(~), and dashes(-).",
  131. "location" );
  132. }
  133. }
  134. /**
  135. * replace some interpolations ${appserver.base} with correct values
  136. *
  137. * @param directory
  138. * @return
  139. */
  140. @Override
  141. public String removeExpressions( String directory )
  142. {
  143. String value = StringUtils.replace( directory, "${appserver.base}",
  144. getRegistry().getString( "appserver.base", "${appserver.base}" ) );
  145. value = StringUtils.replace( value, "${appserver.home}",
  146. getRegistry().getString( "appserver.home", "${appserver.home}" ) );
  147. return value;
  148. }
  149. public ArchivaConfiguration getArchivaConfiguration()
  150. {
  151. return archivaConfiguration;
  152. }
  153. public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
  154. {
  155. this.archivaConfiguration = archivaConfiguration;
  156. }
  157. public Registry getRegistry()
  158. {
  159. return registry;
  160. }
  161. public void setRegistry( org.apache.archiva.components.registry.Registry registry )
  162. {
  163. this.registry = registry;
  164. }
  165. }