1 package org.apache.archiva.admin.repository;
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
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
21 import org.apache.commons.lang.StringUtils;
22 import org.apache.commons.validator.GenericValidator;
23 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
24 import org.apache.maven.archiva.configuration.Configuration;
25 import org.codehaus.plexus.registry.Registry;
26 import org.springframework.stereotype.Service;
28 import javax.inject.Inject;
29 import javax.inject.Named;
32 * apply basic repository validation : id and name.
33 * Check if already exists.
35 * @author Olivier Lamy
39 public class RepositoryCommonValidator
42 public static final String REPOSITORY_ID_VALID_EXPRESSION = "^[a-zA-Z0-9._-]+$";
44 public static final String REPOSITORY_NAME_VALID_EXPRESSION = "^([a-zA-Z0-9.)/_(-]|\\s)+$";
48 private ArchivaConfiguration archivaConfiguration;
51 @Named( value = "commons-configuration" )
52 private Registry registry;
55 * @param abstractRepository
56 * @param update in update mode if yes already exists won't be check
57 * @throws RepositoryAdminException
59 public void basicValidation( AbstractRepository abstractRepository, boolean update )
60 throws RepositoryAdminException
62 Configuration config = archivaConfiguration.getConfiguration();
64 String repoId = abstractRepository.getId();
69 if ( config.getManagedRepositoriesAsMap().containsKey( repoId ) )
71 throw new RepositoryAdminException( "Unable to add new repository with id [" + repoId
72 + "], that id already exists as a managed repository." );
74 else if ( config.getRepositoryGroupsAsMap().containsKey( repoId ) )
76 throw new RepositoryAdminException( "Unable to add new repository with id [" + repoId
77 + "], that id already exists as a repository group." );
79 else if ( config.getRemoteRepositoriesAsMap().containsKey( repoId ) )
81 throw new RepositoryAdminException( "Unable to add new repository with id [" + repoId
82 + "], that id already exists as a remote repository." );
86 if ( StringUtils.isBlank( repoId ) )
88 throw new RepositoryAdminException( "Repository ID cannot be empty." );
91 if ( !GenericValidator.matchRegexp( repoId, REPOSITORY_ID_VALID_EXPRESSION ) )
93 throw new RepositoryAdminException(
94 "Invalid repository ID. Identifier must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-)." );
97 String name = abstractRepository.getName();
99 if ( StringUtils.isBlank( name ) )
101 throw new RepositoryAdminException( "repository name cannot be empty" );
104 if ( !GenericValidator.matchRegexp( name, REPOSITORY_NAME_VALID_EXPRESSION ) )
106 throw new RepositoryAdminException(
107 "Invalid repository name. Repository Name must only contain alphanumeric characters, white-spaces(' '), "
108 + "forward-slashes(/), open-parenthesis('('), close-parenthesis(')'), underscores(_), dots(.), and dashes(-)." );
115 * replace some interpolations ${appserver.base} with correct values
120 public String removeExpressions( String directory )
122 String value = StringUtils.replace( directory, "${appserver.base}",
123 getRegistry().getString( "appserver.base", "${appserver.base}" ) );
124 value = StringUtils.replace( value, "${appserver.home}",
125 getRegistry().getString( "appserver.home", "${appserver.home}" ) );
129 public ArchivaConfiguration getArchivaConfiguration()
131 return archivaConfiguration;
134 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
136 this.archivaConfiguration = archivaConfiguration;
139 public Registry getRegistry()
144 public void setRegistry( Registry registry )
146 this.registry = registry;