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.web.util.ContextUtils;
39 * RepositoryGroupsAction
42 * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="repositoryGroupsAction" instantiation-strategy="per-lookup"
44 public class RepositoryGroupsAction
45 extends AbstractRepositoriesAdminAction
46 implements ServletRequestAware, Preparable
48 private RepositoryGroupConfiguration repositoryGroup;
50 private Map<String, RepositoryGroupConfiguration> repositoryGroups;
52 private Map<String, ManagedRepositoryConfiguration> managedRepositories;
54 private Map<String, List<String>> groupToRepositoryMap;
56 private String repoGroupId;
58 private String repoId;
61 * Used to construct the repository WebDAV URL in the repository action.
63 private String baseUrl;
65 private static final Pattern REPO_GROUP_ID_PATTERN = Pattern.compile( "[A-Za-z0-9\\._\\-]+" );
67 public void setServletRequest( HttpServletRequest request )
69 this.baseUrl = ContextUtils.getBaseURL( request, "repository" );
74 Configuration config = archivaConfiguration.getConfiguration();
76 repositoryGroup = new RepositoryGroupConfiguration();
77 repositoryGroups = config.getRepositoryGroupsAsMap();
78 managedRepositories = config.getManagedRepositoriesAsMap();
79 groupToRepositoryMap = config.getGroupToRepositoryMap();
82 public String addRepositoryGroup()
84 Configuration configuration = archivaConfiguration.getConfiguration();
86 String repoGroupId = repositoryGroup.getId();
88 if( repoGroupId == null || "".equals( repoGroupId.trim() ) )
90 addActionError( "Identifier field is required." );
94 if( repoGroupId.length() > 100 )
96 addActionError( "Identifier [" + repoGroupId + "] is over the maximum limit of 100 characters" );
100 Matcher matcher = REPO_GROUP_ID_PATTERN.matcher( repoGroupId );
101 if( !matcher.matches() )
103 addActionError( "Invalid character(s) found in identifier. Only the following characters are allowed: alphanumeric, '.', '-' and '_'" );
107 if ( StringUtils.isBlank( repoGroupId ) )
109 addActionError( "You must enter a repository group id." );
113 if ( configuration.getRepositoryGroupsAsMap().containsKey( repoGroupId ) )
115 addActionError( "Unable to add new repository group with id [" + repoGroupId
116 + "], that id already exists as a repository group." );
119 else if ( configuration.getManagedRepositoriesAsMap().containsKey( repoGroupId ) )
121 addActionError( "Unable to add new repository group with id [" + repoGroupId
122 + "], that id already exists as a managed repository." );
125 else if ( configuration.getRemoteRepositoriesAsMap().containsKey( repoGroupId ) )
127 addActionError( "Unable to add new repository group with id [" + repoGroupId
128 + "], that id already exists as a remote repository." );
132 configuration.addRepositoryGroup( repositoryGroup );
133 return saveConfiguration( configuration );
136 public String addRepositoryToGroup()
138 Configuration config = archivaConfiguration.getConfiguration();
139 RepositoryGroupConfiguration group = config.findRepositoryGroupById( repoGroupId );
141 validateRepository();
148 if ( group.getRepositories().contains( repoId ) )
150 addActionError( "Repository with id [" + repoId + "] is already in the group" );
154 // remove the old repository group configuration
155 config.removeRepositoryGroup( group );
157 // save repository group configuration
158 group.addRepository( repoId );
159 config.addRepositoryGroup( group );
161 return saveConfiguration( config );
164 public String removeRepositoryFromGroup()
166 Configuration config = archivaConfiguration.getConfiguration();
167 RepositoryGroupConfiguration group = config.findRepositoryGroupById( repoGroupId );
169 validateRepository();
176 if ( !group.getRepositories().contains( repoId ) )
178 addActionError( "No repository with id[" + repoId + "] found in the group" );
182 // remove the old repository group configuration
183 config.removeRepositoryGroup( group );
185 // save repository group configuration
186 group.removeRepository( repoId );
187 config.addRepositoryGroup( group );
189 return saveConfiguration( config );
192 public void validateRepository()
194 Configuration config = archivaConfiguration.getConfiguration();
195 RepositoryGroupConfiguration group = config.findRepositoryGroupById( repoGroupId );
196 ManagedRepositoryConfiguration repo = config.findManagedRepositoryById( repoId );
200 addActionError( "A repository group with that id does not exist." );
205 addActionError( "A repository with that id does not exist." );
209 public RepositoryGroupConfiguration getRepositoryGroup()
211 return repositoryGroup;
214 public void setRepositoryGroup( RepositoryGroupConfiguration repositoryGroup )
216 this.repositoryGroup = repositoryGroup;
219 public Map<String, RepositoryGroupConfiguration> getRepositoryGroups()
221 return repositoryGroups;
224 public void setRepositoryGroups( Map<String, RepositoryGroupConfiguration> repositoryGroups )
226 this.repositoryGroups = repositoryGroups;
229 public Map<String, ManagedRepositoryConfiguration> getManagedRepositories()
231 return managedRepositories;
234 public Map<String, List<String>> getGroupToRepositoryMap()
236 return this.groupToRepositoryMap;
239 public String getRepoGroupId()
244 public void setRepoGroupId( String repoGroupId )
246 this.repoGroupId = repoGroupId;
249 public String getRepoId()
254 public void setRepoId( String repoId )
256 this.repoId = repoId;
259 public String getBaseUrl()