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.admin.repository.RepositoryAdminException;
24 import org.apache.archiva.admin.repository.managed.ManagedRepository;
25 import org.apache.archiva.audit.AuditEvent;
26 import org.apache.archiva.web.util.ContextUtils;
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.maven.archiva.configuration.Configuration;
29 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
30 import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
31 import org.apache.struts2.interceptor.ServletRequestAware;
32 import org.springframework.context.annotation.Scope;
33 import org.springframework.stereotype.Controller;
35 import javax.servlet.http.HttpServletRequest;
36 import java.util.List;
38 import java.util.regex.Matcher;
39 import java.util.regex.Pattern;
42 * RepositoryGroupsAction
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, ManagedRepository> 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" );
75 throws RepositoryAdminException
77 Configuration config = archivaConfiguration.getConfiguration();
79 repositoryGroup = new RepositoryGroupConfiguration();
80 repositoryGroups = config.getRepositoryGroupsAsMap();
81 managedRepositories = getManagedRepositoryAdmin().getManagedRepositoriesAsMap();
82 groupToRepositoryMap = config.getGroupToRepositoryMap();
85 public String addRepositoryGroup()
87 Configuration configuration = archivaConfiguration.getConfiguration();
89 String repoGroupId = repositoryGroup.getId();
91 if ( repoGroupId == null || "".equals( repoGroupId.trim() ) )
93 addActionError( "Identifier field is required." );
97 if ( repoGroupId.length() > 100 )
99 addActionError( "Identifier [" + repoGroupId + "] is over the maximum limit of 100 characters" );
103 Matcher matcher = REPO_GROUP_ID_PATTERN.matcher( repoGroupId );
104 if ( !matcher.matches() )
107 "Invalid character(s) found in identifier. Only the following characters are allowed: alphanumeric, '.', '-' and '_'" );
111 if ( StringUtils.isBlank( repoGroupId ) )
113 addActionError( "You must enter a repository group id." );
117 if ( configuration.getRepositoryGroupsAsMap().containsKey( repoGroupId ) )
119 addActionError( "Unable to add new repository group with id [" + repoGroupId
120 + "], that id already exists as a repository group." );
123 else if ( configuration.getManagedRepositoriesAsMap().containsKey( repoGroupId ) )
125 addActionError( "Unable to add new repository group with id [" + repoGroupId
126 + "], that id already exists as a managed repository." );
129 else if ( configuration.getRemoteRepositoriesAsMap().containsKey( repoGroupId ) )
131 addActionError( "Unable to add new repository group with id [" + repoGroupId
132 + "], that id already exists as a remote repository." );
136 configuration.addRepositoryGroup( repositoryGroup );
137 triggerAuditEvent( AuditEvent.ADD_REPO_GROUP + " " + repoGroupId );
138 return saveConfiguration( configuration );
141 public String addRepositoryToGroup()
143 Configuration config = archivaConfiguration.getConfiguration();
144 RepositoryGroupConfiguration group = config.findRepositoryGroupById( repoGroupId );
146 validateRepository();
153 if ( group.getRepositories().contains( repoId ) )
155 addActionError( "Repository with id [" + repoId + "] is already in the group" );
159 // remove the old repository group configuration
160 config.removeRepositoryGroup( group );
162 // save repository group configuration
163 group.addRepository( repoId );
164 config.addRepositoryGroup( group );
166 triggerAuditEvent( repoId, null, AuditEvent.ADD_REPO_TO_GROUP + " " + repoGroupId );
168 return saveConfiguration( config );
171 public String removeRepositoryFromGroup()
173 Configuration config = archivaConfiguration.getConfiguration();
174 RepositoryGroupConfiguration group = config.findRepositoryGroupById( repoGroupId );
176 validateRepository();
183 if ( !group.getRepositories().contains( repoId ) )
185 addActionError( "No repository with id[" + repoId + "] found in the group" );
189 // remove the old repository group configuration
190 config.removeRepositoryGroup( group );
192 // save repository group configuration
193 group.removeRepository( repoId );
194 config.addRepositoryGroup( group );
196 triggerAuditEvent( repoId, null, AuditEvent.DELETE_REPO_FROM_GROUP + " " + repoGroupId );
198 return saveConfiguration( config );
201 public void validateRepository()
203 Configuration config = archivaConfiguration.getConfiguration();
204 RepositoryGroupConfiguration group = config.findRepositoryGroupById( repoGroupId );
205 ManagedRepositoryConfiguration repo = config.findManagedRepositoryById( repoId );
209 addActionError( "A repository group with that id does not exist." );
214 addActionError( "A repository with that id does not exist." );
218 public RepositoryGroupConfiguration getRepositoryGroup()
220 return repositoryGroup;
223 public void setRepositoryGroup( RepositoryGroupConfiguration repositoryGroup )
225 this.repositoryGroup = repositoryGroup;
228 public Map<String, RepositoryGroupConfiguration> getRepositoryGroups()
230 return repositoryGroups;
233 public void setRepositoryGroups( Map<String, RepositoryGroupConfiguration> repositoryGroups )
235 this.repositoryGroups = repositoryGroups;
238 public Map<String, ManagedRepository> getManagedRepositories()
240 return managedRepositories;
243 public Map<String, List<String>> getGroupToRepositoryMap()
245 return this.groupToRepositoryMap;
248 public String getRepoGroupId()
253 public void setRepoGroupId( String repoGroupId )
255 this.repoGroupId = repoGroupId;
258 public String getRepoId()
263 public void setRepoId( String repoId )
265 this.repoId = repoId;
268 public String getBaseUrl()