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.

BasicManagedRepositoryValidator.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package org.apache.archiva.repository.base.managed;
  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. * 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. import org.apache.archiva.components.registry.Registry;
  20. import org.apache.archiva.repository.base.ConfigurationHandler;
  21. import org.apache.archiva.repository.validation.AbstractRepositoryValidator;
  22. import org.apache.archiva.repository.ManagedRepository;
  23. import org.apache.archiva.repository.RepositoryRegistry;
  24. import org.apache.archiva.repository.validation.RepositoryValidator;
  25. import org.apache.archiva.repository.validation.ValidationError;
  26. import org.apache.archiva.repository.validation.ValidationResponse;
  27. import org.apache.commons.lang3.StringUtils;
  28. import org.springframework.stereotype.Service;
  29. import org.apache.archiva.components.scheduler.CronExpressionValidator;
  30. import java.util.List;
  31. import java.util.Map;
  32. import java.util.regex.Pattern;
  33. import static org.apache.archiva.repository.validation.ErrorKeys.*;
  34. /**
  35. * Validator for managed repository data.
  36. *
  37. * @author Martin Stockhammer <martin_s@apache.org>
  38. */
  39. @Service( "repositoryValidator#common#managed" )
  40. public class BasicManagedRepositoryValidator extends AbstractRepositoryValidator<ManagedRepository> implements RepositoryValidator<ManagedRepository>
  41. {
  42. RepositoryRegistry repositoryRegistry;
  43. private static final String CATEGORY = "managed_repository";
  44. private static final Pattern REPOSITORY_ID_VALID_EXPRESSION_PATTERN = Pattern.compile( REPOSITORY_ID_VALID_EXPRESSION );
  45. private static final Pattern REPOSITORY_NAME_VALID_EXPRESSION_PATTERN = Pattern.compile( REPOSITORY_NAME_VALID_EXPRESSION );
  46. private static final Pattern REPOSITORY_LOCATION_VALID_EXPRESSION_PATTERN = Pattern.compile( REPOSITORY_LOCATION_VALID_EXPRESSION );
  47. private final ConfigurationHandler configurationHandler;
  48. public BasicManagedRepositoryValidator( ConfigurationHandler configurationHandler)
  49. {
  50. super( CATEGORY );
  51. this.configurationHandler = configurationHandler;
  52. }
  53. @Override
  54. public ValidationResponse<ManagedRepository> apply( ManagedRepository managedRepository, boolean update )
  55. {
  56. Map<String, List<ValidationError>> errors = null;
  57. if (managedRepository==null) {
  58. errors = appendError( errors, "id", ISNULL );
  59. }
  60. final String repoId = managedRepository.getId( );
  61. if ( StringUtils.isBlank( repoId ) ) {
  62. errors = appendError( errors, "id", ISEMPTY );
  63. }
  64. if (!update)
  65. {
  66. if ( repositoryRegistry.hasManagedRepository( managedRepository.getId( ) ) )
  67. {
  68. errors = appendError( errors, "id", MANAGED_REPOSITORY_EXISTS, repoId );
  69. }
  70. else if ( repositoryRegistry.hasRemoteRepository( repoId ) )
  71. {
  72. errors = appendError( errors, "id", REMOTE_REPOSITORY_EXISTS, repoId );
  73. }
  74. else if ( repositoryRegistry.hasRepositoryGroup( repoId ) )
  75. {
  76. errors = appendError( errors, "id", REPOSITORY_GROUP_EXISTS, repoId );
  77. }
  78. }
  79. if ( !REPOSITORY_ID_VALID_EXPRESSION_PATTERN.matcher( repoId ).matches( ) )
  80. {
  81. errors = appendError( errors, "id", INVALID_CHARS, repoId, new String[]{"alphanumeric", "_", ".", "-"} );
  82. }
  83. if ( StringUtils.isBlank( managedRepository.getName() ) )
  84. {
  85. errors = appendError( errors, "name", ISEMPTY );
  86. }
  87. if ( !REPOSITORY_NAME_VALID_EXPRESSION_PATTERN.matcher( managedRepository.getName() ).matches( ) )
  88. {
  89. errors = appendError( errors, "name", INVALID_CHARS, managedRepository.getName( ), new String[]{"alphanumeric", "whitespace", "/", "(", ")", "_", ".", "-"} );
  90. }
  91. String cronExpression = managedRepository.getSchedulingDefinition( );
  92. if ( StringUtils.isNotBlank( cronExpression ) )
  93. {
  94. CronExpressionValidator validator = new CronExpressionValidator( );
  95. if ( !validator.validate( cronExpression ) )
  96. {
  97. errors = appendError( errors, "scheduling_definition", INVALID_SCHEDULING_EXPRESSION, cronExpression );
  98. }
  99. }
  100. // Cron expression may be empty
  101. String repoLocation = interpolateVars( managedRepository.getLocation( ).toString() );
  102. if ( !REPOSITORY_LOCATION_VALID_EXPRESSION_PATTERN.matcher( repoLocation ).matches() )
  103. {
  104. errors = appendError( errors, "location", INVALID_LOCATION, repoLocation, new String[]{"alphanumeric", "=", "?", "!", "&", "/", "\\", "_", ".", ":", "~", "-"} );
  105. }
  106. return new ValidationResponse<>( managedRepository, errors );
  107. }
  108. public String interpolateVars( String directory )
  109. {
  110. Registry registry = configurationHandler.getArchivaConfiguration( ).getRegistry( );
  111. String value = StringUtils.replace( directory, "${appserver.base}",
  112. registry.getString( "appserver.base", "${appserver.base}" ) );
  113. value = StringUtils.replace( value, "${appserver.home}",
  114. registry.getString( "appserver.home", "${appserver.home}" ) );
  115. return value;
  116. }
  117. @Override
  118. public void setRepositoryRegistry( RepositoryRegistry repositoryRegistry )
  119. {
  120. this.repositoryRegistry = repositoryRegistry;
  121. }
  122. @Override
  123. public Class<ManagedRepository> getFlavour( )
  124. {
  125. return ManagedRepository.class;
  126. }
  127. }