1 package org.apache.archiva.repository.base.managed;
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.components.registry.Registry;
21 import org.apache.archiva.repository.base.ConfigurationHandler;
22 import org.apache.archiva.repository.validation.AbstractRepositoryValidator;
23 import org.apache.archiva.repository.ManagedRepository;
24 import org.apache.archiva.repository.RepositoryRegistry;
25 import org.apache.archiva.repository.validation.RepositoryValidator;
26 import org.apache.archiva.repository.validation.ValidationError;
27 import org.apache.archiva.repository.validation.ValidationResponse;
28 import org.apache.commons.lang3.StringUtils;
29 import org.springframework.stereotype.Service;
30 import org.apache.archiva.components.scheduler.CronExpressionValidator;
33 import java.util.List;
35 import java.util.regex.Pattern;
37 import static org.apache.archiva.repository.validation.ErrorKeys.*;
40 * Validator for managed repository data.
42 * @author Martin Stockhammer <martin_s@apache.org>
44 @Service( "repositoryValidator#common#managed" )
45 public class BasicManagedRepositoryValidator extends AbstractRepositoryValidator<ManagedRepository> implements RepositoryValidator<ManagedRepository>
47 RepositoryRegistry repositoryRegistry;
48 private static final String CATEGORY = "managed_repository";
49 private static final Pattern REPOSITORY_ID_VALID_EXPRESSION_PATTERN = Pattern.compile( REPOSITORY_ID_VALID_EXPRESSION );
50 private static final Pattern REPOSITORY_NAME_VALID_EXPRESSION_PATTERN = Pattern.compile( REPOSITORY_NAME_VALID_EXPRESSION );
51 private static final Pattern REPOSITORY_LOCATION_VALID_EXPRESSION_PATTERN = Pattern.compile( REPOSITORY_LOCATION_VALID_EXPRESSION );
53 private final ConfigurationHandler configurationHandler;
56 public BasicManagedRepositoryValidator( ConfigurationHandler configurationHandler)
59 this.configurationHandler = configurationHandler;
63 public ValidationResponse<ManagedRepository> apply( ManagedRepository managedRepository, boolean update )
65 Map<String, List<ValidationError>> errors = null;
66 if (managedRepository==null) {
67 errors = appendError( errors, "id", ISNULL );
69 final String repoId = managedRepository.getId( );
70 if ( StringUtils.isBlank( repoId ) ) {
71 errors = appendError( errors, "id", ISEMPTY );
76 if ( repositoryRegistry.hasManagedRepository( managedRepository.getId( ) ) )
78 errors = appendError( errors, "id", MANAGED_REPOSITORY_EXISTS, repoId );
80 else if ( repositoryRegistry.hasRemoteRepository( repoId ) )
82 errors = appendError( errors, "id", REMOTE_REPOSITORY_EXISTS, repoId );
84 else if ( repositoryRegistry.hasRepositoryGroup( repoId ) )
86 errors = appendError( errors, "id", REPOSITORY_GROUP_EXISTS, repoId );
90 if ( !REPOSITORY_ID_VALID_EXPRESSION_PATTERN.matcher( repoId ).matches( ) )
92 errors = appendError( errors, "id", INVALID_CHARS, repoId, new String[]{"alphanumeric", "_", ".", "-"} );
94 if ( StringUtils.isBlank( managedRepository.getName() ) )
96 errors = appendError( errors, "name", ISEMPTY );
99 if ( !REPOSITORY_NAME_VALID_EXPRESSION_PATTERN.matcher( managedRepository.getName() ).matches( ) )
101 errors = appendError( errors, "name", INVALID_CHARS, managedRepository.getName( ), new String[]{"alphanumeric", "whitespace", "/", "(", ")", "_", ".", "-"} );
104 String cronExpression = managedRepository.getSchedulingDefinition( );
105 if ( StringUtils.isNotBlank( cronExpression ) )
107 CronExpressionValidator validator = new CronExpressionValidator( );
109 if ( !validator.validate( cronExpression ) )
111 errors = appendError( errors, "scheduling_definition", INVALID_SCHEDULING_EXPRESSION, cronExpression );
114 // Cron expression may be empty
116 String repoLocation = interpolateVars( managedRepository.getLocation( ).toString() );
118 if ( !REPOSITORY_LOCATION_VALID_EXPRESSION_PATTERN.matcher( repoLocation ).matches() )
120 errors = appendError( errors, "location", INVALID_LOCATION, repoLocation, new String[]{"alphanumeric", "=", "?", "!", "&", "/", "\\", "_", ".", ":", "~", "-"} );
123 return new ValidationResponse<>( managedRepository, errors );
126 public String interpolateVars( String directory )
128 Registry registry = configurationHandler.getArchivaConfiguration( ).getRegistry( );
129 String value = StringUtils.replace( directory, "${appserver.base}",
130 registry.getString( "appserver.base", "${appserver.base}" ) );
131 value = StringUtils.replace( value, "${appserver.home}",
132 registry.getString( "appserver.home", "${appserver.home}" ) );
138 public void setRepositoryRegistry( RepositoryRegistry repositoryRegistry )
140 this.repositoryRegistry = repositoryRegistry;
144 public Class<ManagedRepository> getFlavour( )
146 return ManagedRepository.class;