1 package org.apache.archiva.repository.base.group;
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
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
20 import org.apache.archiva.repository.RepositoryGroup;
21 import org.apache.archiva.repository.RepositoryRegistry;
22 import org.apache.archiva.repository.base.ConfigurationHandler;
23 import org.apache.archiva.repository.validation.AbstractRepositoryValidator;
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;
30 import java.util.List;
32 import java.util.regex.Matcher;
33 import java.util.regex.Pattern;
35 import static org.apache.archiva.repository.validation.ErrorKeys.*;
39 * A validator for repository groups. All validation errors are prefixed with category 'repository_group'.
41 * @author Martin Stockhammer <martin_s@apache.org>
43 @Service( "repositoryValidator#common#group" )
44 public class BasicRepositoryGroupValidator extends AbstractRepositoryValidator<RepositoryGroup> implements RepositoryValidator<RepositoryGroup>
47 private static final String CATEGORY = "repository_group";
48 private static final Pattern REPO_GROUP_ID_PATTERN = Pattern.compile( "[A-Za-z0-9._\\-]+" );
49 private final ConfigurationHandler configurationHandler;
51 private RepositoryRegistry repositoryRegistry;
53 public BasicRepositoryGroupValidator( ConfigurationHandler configurationHandler )
56 this.configurationHandler = configurationHandler;
61 public ValidationResponse<RepositoryGroup> apply( RepositoryGroup repositoryGroup, boolean updateMode ) throws IllegalArgumentException
63 final String repoGroupId = repositoryGroup.getId( );
64 Map<String, List<ValidationError>> errors = null;
65 if ( StringUtils.isBlank( repoGroupId ) )
67 errors = appendError( null, "id", ISEMPTY );
70 if ( repoGroupId.length( ) > 100 )
72 errors = appendError( errors, "id", MAX_LENGTH_EXCEEDED, repoGroupId, Integer.toString( 100 ) );
76 Matcher matcher = REPO_GROUP_ID_PATTERN.matcher( repoGroupId );
77 if ( !matcher.matches( ) )
79 errors = appendError( errors, "id", INVALID_CHARS, repoGroupId, new String[]{"alphanumeric, '.', '-','_'"} );
82 if ( repositoryGroup.getMergedIndexTTL( ) <= 0 )
84 errors = appendError( errors, "merged_index_ttl",BELOW_MIN, "0" );
88 if ( repositoryRegistry != null && !updateMode )
90 if ( repositoryRegistry.hasRepositoryGroup( repoGroupId ) )
92 errors = appendError( errors, "id", REPOSITORY_GROUP_EXISTS, repoGroupId );
94 else if ( repositoryRegistry.hasManagedRepository( repoGroupId ) )
96 errors = appendError( errors, "id", MANAGED_REPOSITORY_EXISTS );
98 else if ( repositoryRegistry.hasRemoteRepository( repoGroupId ) )
100 errors = appendError( errors, "id", REMOTE_REPOSITORY_EXISTS );
103 return new ValidationResponse<>(repositoryGroup, errors );
109 public ConfigurationHandler getConfigurationHandler( )
111 return configurationHandler;
114 public RepositoryRegistry getRepositoryRegistry( )
116 return repositoryRegistry;
120 public void setRepositoryRegistry( RepositoryRegistry repositoryRegistry )
122 this.repositoryRegistry = repositoryRegistry;
126 public Class<RepositoryGroup> getFlavour( )
128 return RepositoryGroup.class;