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
43 @Controller( "repositoryGroupsAction" )
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() )
105 "Invalid character(s) found in identifier. Only the following characters are allowed: alphanumeric, '.', '-' and '_'" );
109 if ( StringUtils.isBlank( repoGroupId ) )
111 addActionError( "You must enter a repository group id." );
115 if ( configuration.getRepositoryGroupsAsMap().containsKey( repoGroupId ) )
117 addActionError( "Unable to add new repository group with id [" + repoGroupId
118 + "], that id already exists as a repository group." );
121 else if ( configuration.getManagedRepositoriesAsMap().containsKey( repoGroupId ) )
123 addActionError( "Unable to add new repository group with id [" + repoGroupId
124 + "], that id already exists as a managed repository." );
127 else if ( configuration.getRemoteRepositoriesAsMap().containsKey( repoGroupId ) )
129 addActionError( "Unable to add new repository group with id [" + repoGroupId
130 + "], that id already exists as a remote repository." );
134 configuration.addRepositoryGroup( repositoryGroup );
135 triggerAuditEvent( AuditEvent.ADD_REPO_GROUP + " " + repoGroupId );
136 return saveConfiguration( configuration );
139 public String addRepositoryToGroup()
141 Configuration config = archivaConfiguration.getConfiguration();
142 RepositoryGroupConfiguration group = config.findRepositoryGroupById( repoGroupId );
144 validateRepository();
151 if ( group.getRepositories().contains( repoId ) )
153 addActionError( "Repository with id [" + repoId + "] is already in the group" );
157 // remove the old repository group configuration
158 config.removeRepositoryGroup( group );
160 // save repository group configuration
161 group.addRepository( repoId );
162 config.addRepositoryGroup( group );
164 triggerAuditEvent( repoId, null, AuditEvent.ADD_REPO_TO_GROUP + " " + repoGroupId );
166 return saveConfiguration( config );
169 public String removeRepositoryFromGroup()
171 Configuration config = archivaConfiguration.getConfiguration();
172 RepositoryGroupConfiguration group = config.findRepositoryGroupById( repoGroupId );
174 validateRepository();
181 if ( !group.getRepositories().contains( repoId ) )
183 addActionError( "No repository with id[" + repoId + "] found in the group" );
187 // remove the old repository group configuration
188 config.removeRepositoryGroup( group );
190 // save repository group configuration
191 group.removeRepository( repoId );
192 config.addRepositoryGroup( group );
194 triggerAuditEvent( repoId, null, AuditEvent.DELETE_REPO_FROM_GROUP + " " + repoGroupId );
196 return saveConfiguration( config );
199 public void validateRepository()
201 Configuration config = archivaConfiguration.getConfiguration();
202 RepositoryGroupConfiguration group = config.findRepositoryGroupById( repoGroupId );
203 ManagedRepositoryConfiguration repo = config.findManagedRepositoryById( repoId );
207 addActionError( "A repository group with that id does not exist." );
212 addActionError( "A repository with that id does not exist." );
216 public RepositoryGroupConfiguration getRepositoryGroup()
218 return repositoryGroup;
221 public void setRepositoryGroup( RepositoryGroupConfiguration repositoryGroup )
223 this.repositoryGroup = repositoryGroup;
226 public Map<String, RepositoryGroupConfiguration> getRepositoryGroups()
228 return repositoryGroups;
231 public void setRepositoryGroups( Map<String, RepositoryGroupConfiguration> repositoryGroups )
233 this.repositoryGroups = repositoryGroups;
236 public Map<String, ManagedRepositoryConfiguration> getManagedRepositories()
238 return managedRepositories;
241 public Map<String, List<String>> getGroupToRepositoryMap()
243 return this.groupToRepositoryMap;
246 public String getRepoGroupId()
251 public void setRepoGroupId( String repoGroupId )
253 this.repoGroupId = repoGroupId;
256 public String getRepoId()
261 public void setRepoId( String repoId )
263 this.repoId = repoId;
266 public String getBaseUrl()