Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

RepositoryGroupsAction.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package org.apache.maven.archiva.web.action.admin.repositories;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.regex.Matcher;
  23. import java.util.regex.Pattern;
  24. import javax.servlet.http.HttpServletRequest;
  25. import org.apache.struts2.interceptor.ServletRequestAware;
  26. import com.opensymphony.xwork2.Preparable;
  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.maven.archiva.web.util.ContextUtils;
  32. /**
  33. * RepositoryGroupsAction
  34. *
  35. * @author
  36. * @version
  37. * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="repositoryGroupsAction"
  38. */
  39. public class RepositoryGroupsAction
  40. extends AbstractRepositoriesAdminAction
  41. implements ServletRequestAware, Preparable
  42. {
  43. private RepositoryGroupConfiguration repositoryGroup;
  44. private Map<String, RepositoryGroupConfiguration> repositoryGroups;
  45. private Map<String, ManagedRepositoryConfiguration> managedRepositories;
  46. private Map<String, List<String>> groupToRepositoryMap;
  47. private String repoGroupId;
  48. private String repoId;
  49. /**
  50. * Used to construct the repository WebDAV URL in the repository action.
  51. */
  52. private String baseUrl;
  53. private static final Pattern REPO_GROUP_ID_PATTERN = Pattern.compile( "[A-Za-z0-9\\._\\-]+" );
  54. public void setServletRequest( HttpServletRequest request )
  55. {
  56. this.baseUrl = ContextUtils.getBaseURL( request, "repository" );
  57. }
  58. public void prepare()
  59. {
  60. Configuration config = archivaConfiguration.getConfiguration();
  61. repositoryGroup = new RepositoryGroupConfiguration();
  62. repositoryGroups = config.getRepositoryGroupsAsMap();
  63. managedRepositories = config.getManagedRepositoriesAsMap();
  64. groupToRepositoryMap = config.getGroupToRepositoryMap();
  65. }
  66. public String addRepositoryGroup()
  67. {
  68. Configuration configuration = archivaConfiguration.getConfiguration();
  69. String repoGroupId = repositoryGroup.getId();
  70. if( repoGroupId == null || "".equals( repoGroupId.trim() ) )
  71. {
  72. addActionError( "Identifier field is required." );
  73. return ERROR;
  74. }
  75. if( repoGroupId.length() > 100 )
  76. {
  77. addActionError( "Identifier [" + repoGroupId + "] is over the maximum limit of 100 characters" );
  78. return ERROR;
  79. }
  80. Matcher matcher = REPO_GROUP_ID_PATTERN.matcher( repoGroupId );
  81. if( !matcher.matches() )
  82. {
  83. addActionError( "Invalid character(s) found in identifier. Only the following characters are allowed: alphanumeric, '.', '-' and '_'" );
  84. return ERROR;
  85. }
  86. if ( StringUtils.isBlank( repoGroupId ) )
  87. {
  88. addActionError( "You must enter a repository group id." );
  89. return ERROR;
  90. }
  91. if ( configuration.getRepositoryGroupsAsMap().containsKey( repoGroupId ) )
  92. {
  93. addActionError( "Unable to add new repository group with id [" + repoGroupId
  94. + "], that id already exists as a repository group." );
  95. return ERROR;
  96. }
  97. else if ( configuration.getManagedRepositoriesAsMap().containsKey( repoGroupId ) )
  98. {
  99. addActionError( "Unable to add new repository group with id [" + repoGroupId
  100. + "], that id already exists as a managed repository." );
  101. return ERROR;
  102. }
  103. else if ( configuration.getRemoteRepositoriesAsMap().containsKey( repoGroupId ) )
  104. {
  105. addActionError( "Unable to add new repository group with id [" + repoGroupId
  106. + "], that id already exists as a remote repository." );
  107. return ERROR;
  108. }
  109. configuration.addRepositoryGroup( repositoryGroup );
  110. return saveConfiguration( configuration );
  111. }
  112. public String addRepositoryToGroup()
  113. {
  114. Configuration config = archivaConfiguration.getConfiguration();
  115. RepositoryGroupConfiguration group = config.findRepositoryGroupById( repoGroupId );
  116. validateRepository();
  117. if ( hasErrors() )
  118. {
  119. return ERROR;
  120. }
  121. if ( group.getRepositories().contains( repoId ) )
  122. {
  123. addActionError( "Repository with id [" + repoId + "] is already in the group" );
  124. return ERROR;
  125. }
  126. // remove the old repository group configuration
  127. config.removeRepositoryGroup( group );
  128. // save repository group configuration
  129. group.addRepository( repoId );
  130. config.addRepositoryGroup( group );
  131. return saveConfiguration( config );
  132. }
  133. public String removeRepositoryFromGroup()
  134. {
  135. Configuration config = archivaConfiguration.getConfiguration();
  136. RepositoryGroupConfiguration group = config.findRepositoryGroupById( repoGroupId );
  137. validateRepository();
  138. if( hasErrors() )
  139. {
  140. return ERROR;
  141. }
  142. if ( !group.getRepositories().contains( repoId ) )
  143. {
  144. addActionError( "No repository with id[" + repoId + "] found in the group" );
  145. return ERROR;
  146. }
  147. // remove the old repository group configuration
  148. config.removeRepositoryGroup( group );
  149. // save repository group configuration
  150. group.removeRepository( repoId );
  151. config.addRepositoryGroup( group );
  152. return saveConfiguration( config );
  153. }
  154. public void validateRepository()
  155. {
  156. Configuration config = archivaConfiguration.getConfiguration();
  157. RepositoryGroupConfiguration group = config.findRepositoryGroupById( repoGroupId );
  158. ManagedRepositoryConfiguration repo = config.findManagedRepositoryById( repoId );
  159. if ( group == null )
  160. {
  161. addActionError( "A repository group with that id does not exist." );
  162. }
  163. if ( repo == null )
  164. {
  165. addActionError( "A repository with that id does not exist." );
  166. }
  167. }
  168. public RepositoryGroupConfiguration getRepositoryGroup()
  169. {
  170. return repositoryGroup;
  171. }
  172. public void setRepositoryGroup( RepositoryGroupConfiguration repositoryGroup )
  173. {
  174. this.repositoryGroup = repositoryGroup;
  175. }
  176. public Map<String, RepositoryGroupConfiguration> getRepositoryGroups()
  177. {
  178. return repositoryGroups;
  179. }
  180. public void setRepositoryGroups( Map<String, RepositoryGroupConfiguration> repositoryGroups )
  181. {
  182. this.repositoryGroups = repositoryGroups;
  183. }
  184. public Map<String, ManagedRepositoryConfiguration> getManagedRepositories()
  185. {
  186. return managedRepositories;
  187. }
  188. public Map<String, List<String>> getGroupToRepositoryMap()
  189. {
  190. return this.groupToRepositoryMap;
  191. }
  192. public String getRepoGroupId()
  193. {
  194. return repoGroupId;
  195. }
  196. public void setRepoGroupId( String repoGroupId )
  197. {
  198. this.repoGroupId = repoGroupId;
  199. }
  200. public String getRepoId()
  201. {
  202. return repoId;
  203. }
  204. public void setRepoId( String repoId )
  205. {
  206. this.repoId = repoId;
  207. }
  208. public String getBaseUrl()
  209. {
  210. return baseUrl;
  211. }
  212. }