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 com.opensymphony.webwork.interceptor.ServletRequestAware;
30 import com.opensymphony.xwork.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
43 * @plexus.component role="com.opensymphony.xwork.Action" role-hint="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() )
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 return saveConfiguration( configuration );
137 public String addRepositoryToGroup()
139 Configuration config = archivaConfiguration.getConfiguration();
140 RepositoryGroupConfiguration group = config.findRepositoryGroupById( repoGroupId );
142 validateRepository();
149 if ( group.getRepositories().contains( repoId ) )
151 addActionError( "Repository with id [" + repoId + "] is already in the group" );
155 // remove the old repository group configuration
156 config.removeRepositoryGroup( group );
158 // save repository group configuration
159 group.addRepository( repoId );
160 config.addRepositoryGroup( group );
162 return saveConfiguration( config );
165 public String removeRepositoryFromGroup()
167 Configuration config = archivaConfiguration.getConfiguration();
168 RepositoryGroupConfiguration group = config.findRepositoryGroupById( repoGroupId );
170 validateRepository();
177 if ( !group.getRepositories().contains( repoId ) )
179 addActionError( "No repository with id[" + repoId + "] found in the group" );
183 // remove the old repository group configuration
184 config.removeRepositoryGroup( group );
186 // save repository group configuration
187 group.removeRepository( repoId );
188 config.addRepositoryGroup( group );
190 return saveConfiguration( config );
193 public void validateRepository()
195 Configuration config = archivaConfiguration.getConfiguration();
196 RepositoryGroupConfiguration group = config.findRepositoryGroupById( repoGroupId );
197 ManagedRepositoryConfiguration repo = config.findManagedRepositoryById( repoId );
201 addActionError( "A repository group with that id does not exist." );
206 addActionError( "A repository with that id does not exist." );
210 public RepositoryGroupConfiguration getRepositoryGroup()
212 return repositoryGroup;
215 public void setRepositoryGroup( RepositoryGroupConfiguration repositoryGroup )
217 this.repositoryGroup = repositoryGroup;
220 public Map<String, RepositoryGroupConfiguration> getRepositoryGroups()
222 return repositoryGroups;
225 public void setRepositoryGroups( Map<String, RepositoryGroupConfiguration> repositoryGroups )
227 this.repositoryGroups = repositoryGroups;
230 public Map<String, ManagedRepositoryConfiguration> getManagedRepositories()
232 return managedRepositories;
235 public Map<String, List<String>> getGroupToRepositoryMap()
237 return this.groupToRepositoryMap;
240 public String getRepoGroupId()
245 public void setRepoGroupId( String repoGroupId )
247 this.repoGroupId = repoGroupId;
250 public String getRepoId()
255 public void setRepoId( String repoId )
257 this.repoId = repoId;
260 public String getBaseUrl()