This project has retired. For details please refer to its Attic page.
Source code
001package org.apache.archiva.admin.repository;
002/*
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 */
020
021import org.apache.archiva.admin.model.RepositoryAdminException;
022import org.apache.archiva.admin.model.RepositoryCommonValidator;
023import org.apache.archiva.admin.model.beans.AbstractRepository;
024import org.apache.archiva.admin.model.beans.ManagedRepository;
025import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
026import org.apache.archiva.configuration.ArchivaConfiguration;
027import org.apache.archiva.configuration.Configuration;
028import org.apache.archiva.components.registry.Registry;
029import org.apache.archiva.components.scheduler.CronExpressionValidator;
030import org.apache.commons.lang3.StringUtils;
031import org.apache.commons.validator.GenericValidator;
032import org.springframework.stereotype.Service;
033
034import javax.inject.Inject;
035import javax.inject.Named;
036
037/**
038 * apply basic repository validation : id and name.
039 * Check if already exists.
040 *
041 * @author Olivier Lamy
042 * @since 1.4-M1
043 */
044@Service
045public class DefaultRepositoryCommonValidator
046    implements RepositoryCommonValidator
047{
048
049    @Inject
050    private ArchivaConfiguration archivaConfiguration;
051
052    @Inject
053    @Named( value = "commons-configuration" )
054    private org.apache.archiva.components.registry.Registry registry;
055
056    /**
057     * @param abstractRepository
058     * @param update             in update mode if yes already exists won't be check
059     * @throws RepositoryAdminException
060     */
061    @Override
062    public void basicValidation( AbstractRepository abstractRepository, boolean update )
063        throws RepositoryAdminException
064    {
065        Configuration config = archivaConfiguration.getConfiguration();
066
067        String repoId = abstractRepository.getId();
068
069        if ( !update )
070        {
071
072            if ( config.getManagedRepositoriesAsMap().containsKey( repoId ) )
073            {
074                throw new RepositoryAdminException( "Unable to add new repository with id [" + repoId
075                                                        + "], that id already exists as a managed repository." );
076            }
077            else if ( config.getRepositoryGroupsAsMap().containsKey( repoId ) )
078            {
079                throw new RepositoryAdminException( "Unable to add new repository with id [" + repoId
080                                                        + "], that id already exists as a repository group." );
081            }
082            else if ( config.getRemoteRepositoriesAsMap().containsKey( repoId ) )
083            {
084                throw new RepositoryAdminException( "Unable to add new repository with id [" + repoId
085                                                        + "], that id already exists as a remote repository." );
086            }
087        }
088
089        if ( StringUtils.isBlank( repoId ) )
090        {
091            throw new RepositoryAdminException( "Repository ID cannot be empty." );
092        }
093
094        if ( !GenericValidator.matchRegexp( repoId, REPOSITORY_ID_VALID_EXPRESSION ) )
095        {
096            throw new RepositoryAdminException(
097                "Invalid repository ID. Identifier must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-)." );
098        }
099
100        String name = abstractRepository.getName();
101
102        if ( StringUtils.isBlank( name ) )
103        {
104            throw new RepositoryAdminException( "repository name cannot be empty" );
105        }
106
107        if ( !GenericValidator.matchRegexp( name, REPOSITORY_NAME_VALID_EXPRESSION ) )
108        {
109            throw new RepositoryAdminException(
110                "Invalid repository name. Repository Name must only contain alphanumeric characters, white-spaces(' '), "
111                    + "forward-slashes(/), open-parenthesis('('), close-parenthesis(')'),  underscores(_), dots(.), and dashes(-)." );
112        }
113
114    }
115
116    /**
117     * validate cronExpression and location format
118     *
119     * @param managedRepository
120     * @since 1.4-M2
121     */
122    @Override
123    public void validateManagedRepository( ManagedRepository managedRepository )
124        throws RepositoryAdminException
125    {
126        String cronExpression = managedRepository.getCronExpression();
127        // FIXME : olamy can be empty to avoid scheduled scan ?
128        if ( StringUtils.isNotBlank( cronExpression ) )
129        {
130            CronExpressionValidator validator = new CronExpressionValidator();
131
132            if ( !validator.validate( cronExpression ) )
133            {
134                throw new RepositoryAdminException( "Invalid cron expression.", "cronExpression" );
135            }
136        }
137        else
138        {
139            throw new RepositoryAdminException( "Cron expression cannot be empty." );
140        }
141
142        String repoLocation = removeExpressions( managedRepository.getLocation() );
143
144        if ( !GenericValidator.matchRegexp( repoLocation,
145                                            ManagedRepositoryAdmin.REPOSITORY_LOCATION_VALID_EXPRESSION ) )
146        {
147            throw new RepositoryAdminException(
148                "Invalid repository location. Directory must only contain alphanumeric characters, equals(=), question-marks(?), "
149                    + "exclamation-points(!), ampersands(&), forward-slashes(/), back-slashes(\\), underscores(_), dots(.), colons(:), tildes(~), and dashes(-).",
150                "location" );
151        }
152    }
153
154    /**
155     * replace some interpolations ${appserver.base} with correct values
156     *
157     * @param directory
158     * @return
159     */
160    @Override
161    public String removeExpressions( String directory )
162    {
163        String value = StringUtils.replace( directory, "${appserver.base}",
164                                            getRegistry().getString( "appserver.base", "${appserver.base}" ) );
165        value = StringUtils.replace( value, "${appserver.home}",
166                                     getRegistry().getString( "appserver.home", "${appserver.home}" ) );
167        return value;
168    }
169
170    public ArchivaConfiguration getArchivaConfiguration()
171    {
172        return archivaConfiguration;
173    }
174
175    public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
176    {
177        this.archivaConfiguration = archivaConfiguration;
178    }
179
180    public Registry getRegistry()
181    {
182        return registry;
183    }
184
185    public void setRegistry( org.apache.archiva.components.registry.Registry registry )
186    {
187        this.registry = registry;
188    }
189}