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 java.util.List;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
27 import javax.servlet.http.HttpServletRequest;
29 import org.apache.struts2.interceptor.ServletRequestAware;
30 import com.opensymphony.xwork2.Preparable;
32 import org.apache.commons.lang.StringUtils;
33 import org.apache.maven.archiva.configuration.Configuration;
34 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
35 import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
36 import org.apache.maven.archiva.repository.audit.AuditEvent;
37 import org.apache.maven.archiva.web.util.ContextUtils;
40 * RepositoryGroupsAction
43 * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="repositoryGroupsAction" instantiation-strategy="per-lookup"
45 public class RepositoryGroupsAction
46 extends AbstractRepositoriesAdminAction
47 implements ServletRequestAware, Preparable
49 private RepositoryGroupConfiguration repositoryGroup;
51 private Map<String, RepositoryGroupConfiguration> repositoryGroups;
53 private Map<String, ManagedRepositoryConfiguration> managedRepositories;
55 private Map<String, List<String>> groupToRepositoryMap;
57 private String repoGroupId;
59 private String repoId;
62 * Used to construct the repository WebDAV URL in the repository action.
64 private String baseUrl;
66 private static final Pattern REPO_GROUP_ID_PATTERN = Pattern.compile( "[A-Za-z0-9\\._\\-]+" );
68 public void setServletRequest( HttpServletRequest request )
70 this.baseUrl = ContextUtils.getBaseURL( request, "repository" );
75 Configuration config = archivaConfiguration.getConfiguration();
77 repositoryGroup = new RepositoryGroupConfiguration();
78 repositoryGroups = config.getRepositoryGroupsAsMap();
79 managedRepositories = config.getManagedRepositoriesAsMap();
80 groupToRepositoryMap = config.getGroupToRepositoryMap();
83 public String addRepositoryGroup()
85 Configuration configuration = archivaConfiguration.getConfiguration();
87 String repoGroupId = repositoryGroup.getId();
89 if( repoGroupId == null || "".equals( repoGroupId.trim() ) )
91 addActionError( "Identifier field is required." );
95 if( repoGroupId.length() > 100 )
97 addActionError( "Identifier [" + repoGroupId + "] is over the maximum limit of 100 characters" );
101 Matcher matcher = REPO_GROUP_ID_PATTERN.matcher( repoGroupId );
102 if( !matcher.matches() )
104 addActionError( "Invalid character(s) found in identifier. Only the following characters are allowed: alphanumeric, '.', '-' and '_'" );
108 if ( StringUtils.isBlank( repoGroupId ) )
110 addActionError( "You must enter a repository group id." );
114 if ( configuration.getRepositoryGroupsAsMap().containsKey( repoGroupId ) )
116 addActionError( "Unable to add new repository group with id [" + repoGroupId
117 + "], that id already exists as a repository group." );
120 else if ( configuration.getManagedRepositoriesAsMap().containsKey( repoGroupId ) )
122 addActionError( "Unable to add new repository group with id [" + repoGroupId
123 + "], that id already exists as a managed repository." );
126 else if ( configuration.getRemoteRepositoriesAsMap().containsKey( repoGroupId ) )
128 addActionError( "Unable to add new repository group with id [" + repoGroupId
129 + "], that id already exists as a remote repository." );
133 configuration.addRepositoryGroup( repositoryGroup );
134 triggerAuditEvent( AuditEvent.ADD_REPO_GROUP + " " + repoGroupId );
135 return saveConfiguration( configuration );
138 public String addRepositoryToGroup()
140 Configuration config = archivaConfiguration.getConfiguration();
141 RepositoryGroupConfiguration group = config.findRepositoryGroupById( repoGroupId );
143 validateRepository();
150 if ( group.getRepositories().contains( repoId ) )
152 addActionError( "Repository with id [" + repoId + "] is already in the group" );
156 // remove the old repository group configuration
157 config.removeRepositoryGroup( group );
159 // save repository group configuration
160 group.addRepository( repoId );
161 config.addRepositoryGroup( group );
163 triggerAuditEvent( repoId, null, AuditEvent.ADD_REPO_TO_GROUP + " " + repoGroupId );
165 return saveConfiguration( config );
168 public String removeRepositoryFromGroup()
170 Configuration config = archivaConfiguration.getConfiguration();
171 RepositoryGroupConfiguration group = config.findRepositoryGroupById( repoGroupId );
173 validateRepository();
180 if ( !group.getRepositories().contains( repoId ) )
182 addActionError( "No repository with id[" + repoId + "] found in the group" );
186 // remove the old repository group configuration
187 config.removeRepositoryGroup( group );
189 // save repository group configuration
190 group.removeRepository( repoId );
191 config.addRepositoryGroup( group );
193 triggerAuditEvent( repoId, null, AuditEvent.DELETE_REPO_FROM_GROUP + " " + repoGroupId );
195 return saveConfiguration( config );
198 public void validateRepository()
200 Configuration config = archivaConfiguration.getConfiguration();
201 RepositoryGroupConfiguration group = config.findRepositoryGroupById( repoGroupId );
202 ManagedRepositoryConfiguration repo = config.findManagedRepositoryById( repoId );
206 addActionError( "A repository group with that id does not exist." );
211 addActionError( "A repository with that id does not exist." );
215 public RepositoryGroupConfiguration getRepositoryGroup()
217 return repositoryGroup;
220 public void setRepositoryGroup( RepositoryGroupConfiguration repositoryGroup )
222 this.repositoryGroup = repositoryGroup;
225 public Map<String, RepositoryGroupConfiguration> getRepositoryGroups()
227 return repositoryGroups;
230 public void setRepositoryGroups( Map<String, RepositoryGroupConfiguration> repositoryGroups )
232 this.repositoryGroups = repositoryGroups;
235 public Map<String, ManagedRepositoryConfiguration> getManagedRepositories()
237 return managedRepositories;
240 public Map<String, List<String>> getGroupToRepositoryMap()
242 return this.groupToRepositoryMap;
245 public String getRepoGroupId()
250 public void setRepoGroupId( String repoGroupId )
252 this.repoGroupId = repoGroupId;
255 public String getRepoId()
260 public void setRepoId( String repoId )
262 this.repoId = repoId;
265 public String getBaseUrl()