This project has retired. For details please refer to its
Attic page.
DefaultRepositoryCommonValidator xref
1 package org.apache.archiva.admin.repository;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import org.apache.archiva.admin.model.RepositoryAdminException;
22 import org.apache.archiva.admin.model.RepositoryCommonValidator;
23 import org.apache.archiva.admin.model.beans.AbstractRepository;
24 import org.apache.archiva.admin.model.beans.ManagedRepository;
25 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
26 import org.apache.archiva.configuration.ArchivaConfiguration;
27 import org.apache.archiva.configuration.Configuration;
28 import org.apache.archiva.components.registry.Registry;
29 import org.apache.archiva.components.scheduler.CronExpressionValidator;
30 import org.apache.commons.lang3.StringUtils;
31 import org.apache.commons.validator.GenericValidator;
32 import org.springframework.stereotype.Service;
33
34 import javax.inject.Inject;
35 import javax.inject.Named;
36
37
38
39
40
41
42
43
44 @Service
45 public class DefaultRepositoryCommonValidator
46 implements RepositoryCommonValidator
47 {
48
49 @Inject
50 private ArchivaConfiguration archivaConfiguration;
51
52 @Inject
53 @Named( value = "commons-configuration" )
54 private org.apache.archiva.components.registry.Registry registry;
55
56
57
58
59
60
61 @Override
62 public void basicValidation( AbstractRepository abstractRepository, boolean update )
63 throws RepositoryAdminException
64 {
65 Configuration config = archivaConfiguration.getConfiguration();
66
67 String repoId = abstractRepository.getId();
68
69 if ( !update )
70 {
71
72 if ( config.getManagedRepositoriesAsMap().containsKey( repoId ) )
73 {
74 throw new RepositoryAdminException( "Unable to add new repository with id [" + repoId
75 + "], that id already exists as a managed repository." );
76 }
77 else if ( config.getRepositoryGroupsAsMap().containsKey( repoId ) )
78 {
79 throw new RepositoryAdminException( "Unable to add new repository with id [" + repoId
80 + "], that id already exists as a repository group." );
81 }
82 else if ( config.getRemoteRepositoriesAsMap().containsKey( repoId ) )
83 {
84 throw new RepositoryAdminException( "Unable to add new repository with id [" + repoId
85 + "], that id already exists as a remote repository." );
86 }
87 }
88
89 if ( StringUtils.isBlank( repoId ) )
90 {
91 throw new RepositoryAdminException( "Repository ID cannot be empty." );
92 }
93
94 if ( !GenericValidator.matchRegexp( repoId, REPOSITORY_ID_VALID_EXPRESSION ) )
95 {
96 throw new RepositoryAdminException(
97 "Invalid repository ID. Identifier must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-)." );
98 }
99
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
118
119
120
121
122 @Override
123 public void validateManagedRepository( ManagedRepository managedRepository )
124 throws RepositoryAdminException
125 {
126 String cronExpression = managedRepository.getCronExpression();
127
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
156
157
158
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 }