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.

BasicRepositoryGroupValidator.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package org.apache.archiva.repository.base.group;
  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.repository.RepositoryGroup;
  20. import org.apache.archiva.repository.RepositoryRegistry;
  21. import org.apache.archiva.repository.base.ConfigurationHandler;
  22. import org.apache.archiva.repository.validation.AbstractRepositoryValidator;
  23. import org.apache.archiva.repository.validation.RepositoryValidator;
  24. import org.apache.archiva.repository.validation.ValidationError;
  25. import org.apache.archiva.repository.validation.ValidationResponse;
  26. import org.apache.commons.lang3.StringUtils;
  27. import org.springframework.stereotype.Service;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.regex.Matcher;
  31. import java.util.regex.Pattern;
  32. import static org.apache.archiva.repository.validation.ErrorKeys.*;
  33. /**
  34. *
  35. * A validator for repository groups. All validation errors are prefixed with category 'repository_group'.
  36. *
  37. * @author Martin Stockhammer <martin_s@apache.org>
  38. */
  39. @Service( "repositoryValidator#common#group" )
  40. public class BasicRepositoryGroupValidator extends AbstractRepositoryValidator<RepositoryGroup> implements RepositoryValidator<RepositoryGroup>
  41. {
  42. private static final String CATEGORY = "repository_group";
  43. private static final Pattern REPO_GROUP_ID_PATTERN = Pattern.compile( "[A-Za-z0-9._\\-]+" );
  44. private final ConfigurationHandler configurationHandler;
  45. private RepositoryRegistry repositoryRegistry;
  46. public BasicRepositoryGroupValidator( ConfigurationHandler configurationHandler )
  47. {
  48. super( CATEGORY );
  49. this.configurationHandler = configurationHandler;
  50. }
  51. @Override
  52. public ValidationResponse<RepositoryGroup> apply( RepositoryGroup repositoryGroup, boolean updateMode ) throws IllegalArgumentException
  53. {
  54. final String repoGroupId = repositoryGroup.getId( );
  55. Map<String, List<ValidationError>> errors = null;
  56. if ( StringUtils.isBlank( repoGroupId ) )
  57. {
  58. errors = appendError( null, "id", ISEMPTY );
  59. }
  60. if ( repoGroupId.length( ) > 100 )
  61. {
  62. errors = appendError( errors, "id", MAX_LENGTH_EXCEEDED, repoGroupId, Integer.toString( 100 ) );
  63. }
  64. Matcher matcher = REPO_GROUP_ID_PATTERN.matcher( repoGroupId );
  65. if ( !matcher.matches( ) )
  66. {
  67. errors = appendError( errors, "id", INVALID_CHARS, repoGroupId, new String[]{"alphanumeric, '.', '-','_'"} );
  68. }
  69. if ( repositoryGroup.getMergedIndexTTL( ) <= 0 )
  70. {
  71. errors = appendError( errors, "merged_index_ttl",BELOW_MIN, "0" );
  72. }
  73. if ( repositoryRegistry != null && !updateMode )
  74. {
  75. if ( repositoryRegistry.hasRepositoryGroup( repoGroupId ) )
  76. {
  77. errors = appendError( errors, "id", REPOSITORY_GROUP_EXISTS, repoGroupId );
  78. }
  79. else if ( repositoryRegistry.hasManagedRepository( repoGroupId ) )
  80. {
  81. errors = appendError( errors, "id", MANAGED_REPOSITORY_EXISTS );
  82. }
  83. else if ( repositoryRegistry.hasRemoteRepository( repoGroupId ) )
  84. {
  85. errors = appendError( errors, "id", REMOTE_REPOSITORY_EXISTS );
  86. }
  87. }
  88. return new ValidationResponse<>(repositoryGroup, errors );
  89. }
  90. public ConfigurationHandler getConfigurationHandler( )
  91. {
  92. return configurationHandler;
  93. }
  94. public RepositoryRegistry getRepositoryRegistry( )
  95. {
  96. return repositoryRegistry;
  97. }
  98. @Override
  99. public void setRepositoryRegistry( RepositoryRegistry repositoryRegistry )
  100. {
  101. this.repositoryRegistry = repositoryRegistry;
  102. }
  103. @Override
  104. public Class<RepositoryGroup> getFlavour( )
  105. {
  106. return RepositoryGroup.class;
  107. }
  108. }