1 package org.apache.maven.archiva.web.action.admin.repositories;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import com.opensymphony.xwork2.Preparable;
23 import org.apache.archiva.audit.AuditEvent;
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.maven.archiva.configuration.Configuration;
26 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
27 import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
28 import org.apache.archiva.web.util.ContextUtils;
29 import org.apache.struts2.interceptor.ServletRequestAware;
30 import org.springframework.context.annotation.Scope;
31 import org.springframework.stereotype.Controller;
33 import javax.servlet.http.HttpServletRequest;
34 import java.util.List;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
40 * RepositoryGroupsAction
42 * @version plexus.component role="com.opensymphony.xwork2.Action" role-hint="repositoryGroupsAction" instantiation-strategy="per-lookup"
44 @Controller( "repositoryGroupsAction" )
46 public class RepositoryGroupsAction
47 extends AbstractRepositoriesAdminAction
48 implements ServletRequestAware, Preparable
50 private RepositoryGroupConfiguration repositoryGroup;
52 private Map<String, RepositoryGroupConfiguration> repositoryGroups;
54 private Map<String, ManagedRepositoryConfiguration> managedRepositories;
56 private Map<String, List<String>> groupToRepositoryMap;
58 private String repoGroupId;
60 private String repoId;
63 * Used to construct the repository WebDAV URL in the repository action.
65 private String baseUrl;
67 private static final Pattern REPO_GROUP_ID_PATTERN = Pattern.compile( "[A-Za-z0-9\\._\\-]+" );
69 public void setServletRequest( HttpServletRequest request )
71 this.baseUrl = ContextUtils.getBaseURL( request, "repository" );
76 Configuration config = archivaConfiguration.getConfiguration();
78 repositoryGroup = new RepositoryGroupConfiguration();
79 repositoryGroups = config.getRepositoryGroupsAsMap();
80 managedRepositories = config.getManagedRepositoriesAsMap();
81 groupToRepositoryMap = config.getGroupToRepositoryMap();
84 public String addRepositoryGroup()
86 Configuration configuration = archivaConfiguration.getConfiguration();
88 String repoGroupId = repositoryGroup.getId();
90 if ( repoGroupId == null || "".equals( repoGroupId.trim() ) )
92 addActionError( "Identifier field is required." );
96 if ( repoGroupId.length() > 100 )
98 addActionError( "Identifier [" + repoGroupId + "] is over the maximum limit of 100 characters" );
102 Matcher matcher = REPO_GROUP_ID_PATTERN.matcher( repoGroupId );
103 if ( !matcher.matches() )
106 "Invalid character(s) found in identifier. Only the following characters are allowed: alphanumeric, '.', '-' and '_'" );
110 if ( StringUtils.isBlank( repoGroupId ) )
112 addActionError( "You must enter a repository group id." );
116 if ( configuration.getRepositoryGroupsAsMap().containsKey( repoGroupId ) )
118 addActionError( "Unable to add new repository group with id [" + repoGroupId
119 + "], that id already exists as a repository group." );
122 else if ( configuration.getManagedRepositoriesAsMap().containsKey( repoGroupId ) )
124 addActionError( "Unable to add new repository group with id [" + repoGroupId
125 + "], that id already exists as a managed repository." );
128 else if ( configuration.getRemoteRepositoriesAsMap().containsKey( repoGroupId ) )
130 addActionError( "Unable to add new repository group with id [" + repoGroupId
131 + "], that id already exists as a remote repository." );
135 configuration.addRepositoryGroup( repositoryGroup );
136 triggerAuditEvent( AuditEvent.ADD_REPO_GROUP + " " + repoGroupId );
137 return saveConfiguration( configuration );
140 public String addRepositoryToGroup()
142 Configuration config = archivaConfiguration.getConfiguration();
143 RepositoryGroupConfiguration group = config.findRepositoryGroupById( repoGroupId );
145 validateRepository();
152 if ( group.getRepositories().contains( repoId ) )
154 addActionError( "Repository with id [" + repoId + "] is already in the group" );
158 // remove the old repository group configuration
159 config.removeRepositoryGroup( group );
161 // save repository group configuration
162 group.addRepository( repoId );
163 config.addRepositoryGroup( group );
165 triggerAuditEvent( repoId, null, AuditEvent.ADD_REPO_TO_GROUP + " " + repoGroupId );
167 return saveConfiguration( config );
170 public String removeRepositoryFromGroup()
172 Configuration config = archivaConfiguration.getConfiguration();
173 RepositoryGroupConfiguration group = config.findRepositoryGroupById( repoGroupId );
175 validateRepository();
182 if ( !group.getRepositories().contains( repoId ) )
184 addActionError( "No repository with id[" + repoId + "] found in the group" );
188 // remove the old repository group configuration
189 config.removeRepositoryGroup( group );
191 // save repository group configuration
192 group.removeRepository( repoId );
193 config.addRepositoryGroup( group );
195 triggerAuditEvent( repoId, null, AuditEvent.DELETE_REPO_FROM_GROUP + " " + repoGroupId );
197 return saveConfiguration( config );
200 public void validateRepository()
202 Configuration config = archivaConfiguration.getConfiguration();
203 RepositoryGroupConfiguration group = config.findRepositoryGroupById( repoGroupId );
204 ManagedRepositoryConfiguration repo = config.findManagedRepositoryById( repoId );
208 addActionError( "A repository group with that id does not exist." );
213 addActionError( "A repository with that id does not exist." );
217 public RepositoryGroupConfiguration getRepositoryGroup()
219 return repositoryGroup;
222 public void setRepositoryGroup( RepositoryGroupConfiguration repositoryGroup )
224 this.repositoryGroup = repositoryGroup;
227 public Map<String, RepositoryGroupConfiguration> getRepositoryGroups()
229 return repositoryGroups;
232 public void setRepositoryGroups( Map<String, RepositoryGroupConfiguration> repositoryGroups )
234 this.repositoryGroups = repositoryGroups;
237 public Map<String, ManagedRepositoryConfiguration> getManagedRepositories()
239 return managedRepositories;
242 public Map<String, List<String>> getGroupToRepositoryMap()
244 return this.groupToRepositoryMap;
247 public String getRepoGroupId()
252 public void setRepoGroupId( String repoGroupId )
254 this.repoGroupId = repoGroupId;
257 public String getRepoId()
262 public void setRepoId( String repoId )
264 this.repoId = repoId;
267 public String getBaseUrl()