You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RepositoryScanningAction.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. package org.apache.maven.archiva.web.action.admin.scanning;
  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 com.opensymphony.xwork2.Preparable;
  21. import com.opensymphony.xwork2.Validateable;
  22. import org.apache.commons.collections.CollectionUtils;
  23. import org.apache.commons.lang.StringUtils;
  24. import org.apache.maven.archiva.configuration.ArchivaConfiguration;
  25. import org.apache.maven.archiva.configuration.Configuration;
  26. import org.apache.maven.archiva.configuration.FileType;
  27. import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
  28. import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
  29. import org.apache.maven.archiva.configuration.functors.FiletypeSelectionPredicate;
  30. import org.apache.maven.archiva.configuration.functors.FiletypeToMapClosure;
  31. import org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers;
  32. import org.apache.maven.archiva.security.ArchivaRoleConstants;
  33. import org.apache.maven.archiva.web.action.PlexusActionSupport;
  34. import org.codehaus.plexus.redback.rbac.Resource;
  35. import org.codehaus.plexus.redback.struts2.interceptor.SecureAction;
  36. import org.codehaus.plexus.redback.struts2.interceptor.SecureActionBundle;
  37. import org.codehaus.plexus.redback.struts2.interceptor.SecureActionException;
  38. import org.codehaus.plexus.registry.RegistryException;
  39. import java.util.ArrayList;
  40. import java.util.Collections;
  41. import java.util.List;
  42. import java.util.Map;
  43. /**
  44. * RepositoryScanningAction
  45. *
  46. * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
  47. * @version $Id$
  48. * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="repositoryScanningAction"
  49. */
  50. public class RepositoryScanningAction
  51. extends PlexusActionSupport
  52. implements Preparable, Validateable, SecureAction
  53. {
  54. /**
  55. * @plexus.requirement
  56. */
  57. private ArchivaConfiguration archivaConfiguration;
  58. /**
  59. * @plexus.requirement
  60. */
  61. private RepositoryContentConsumers repoconsumerUtil;
  62. private Map fileTypeMap;
  63. private List fileTypeIds;
  64. /**
  65. * List of {@link AdminRepositoryConsumer} objects for consumers of known content.
  66. */
  67. private List knownContentConsumers;
  68. /**
  69. * List of enabled {@link AdminRepositoryConsumer} objects for consumers of known content.
  70. */
  71. private List enabledKnownContentConsumers;
  72. /**
  73. * List of {@link AdminRepositoryConsumer} objects for consumers of invalid/unknown content.
  74. */
  75. private List invalidContentConsumers;
  76. /**
  77. * List of enabled {@link AdminRepositoryConsumer} objects for consumers of invalid/unknown content.
  78. */
  79. private List enabledInvalidContentConsumers;
  80. private String pattern;
  81. private String fileTypeId;
  82. public void addActionError( String anErrorMessage )
  83. {
  84. super.addActionError( anErrorMessage );
  85. getLogger().warn( "[ActionError] " + anErrorMessage );
  86. }
  87. public void addActionMessage( String aMessage )
  88. {
  89. super.addActionMessage( aMessage );
  90. getLogger().info( "[ActionMessage] " + aMessage );
  91. }
  92. public String addFiletypePattern()
  93. {
  94. getLogger().info( "Add New File Type Pattern [" + getFileTypeId() + ":" + getPattern() + "]" );
  95. if ( !isValidFiletypeCommand() )
  96. {
  97. return INPUT;
  98. }
  99. String id = getFileTypeId();
  100. String pattern = getPattern();
  101. FileType filetype = findFileType( id );
  102. if ( filetype == null )
  103. {
  104. addActionError( "Pattern not added, unable to find filetype " + id );
  105. return INPUT;
  106. }
  107. if ( filetype.getPatterns().contains( pattern ) )
  108. {
  109. addActionError( "Not adding pattern \"" + pattern + "\" to filetype " + id + " as it already exists." );
  110. return INPUT;
  111. }
  112. filetype.addPattern( pattern );
  113. addActionMessage( "Added pattern \"" + pattern + "\" to filetype " + id );
  114. return saveConfiguration();
  115. }
  116. public String getFileTypeId()
  117. {
  118. return fileTypeId;
  119. }
  120. public List getFileTypeIds()
  121. {
  122. return fileTypeIds;
  123. }
  124. public Map getFileTypeMap()
  125. {
  126. return fileTypeMap;
  127. }
  128. public List getInvalidContentConsumers()
  129. {
  130. return invalidContentConsumers;
  131. }
  132. public List getKnownContentConsumers()
  133. {
  134. return knownContentConsumers;
  135. }
  136. public String getPattern()
  137. {
  138. return pattern;
  139. }
  140. public SecureActionBundle getSecureActionBundle()
  141. throws SecureActionException
  142. {
  143. SecureActionBundle bundle = new SecureActionBundle();
  144. bundle.setRequiresAuthentication( true );
  145. bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION, Resource.GLOBAL );
  146. return bundle;
  147. }
  148. public void prepare()
  149. throws Exception
  150. {
  151. Configuration config = archivaConfiguration.getConfiguration();
  152. RepositoryScanningConfiguration reposcanning = config.getRepositoryScanning();
  153. FiletypeToMapClosure filetypeToMapClosure = new FiletypeToMapClosure();
  154. CollectionUtils.forAllDo( reposcanning.getFileTypes(), filetypeToMapClosure );
  155. fileTypeMap = filetypeToMapClosure.getMap();
  156. AddAdminRepoConsumerClosure addAdminRepoConsumer;
  157. addAdminRepoConsumer = new AddAdminRepoConsumerClosure( reposcanning.getKnownContentConsumers() );
  158. CollectionUtils.forAllDo( repoconsumerUtil.getAvailableKnownConsumers(), addAdminRepoConsumer );
  159. this.knownContentConsumers = addAdminRepoConsumer.getList();
  160. Collections.sort( knownContentConsumers, AdminRepositoryConsumerComparator.getInstance() );
  161. addAdminRepoConsumer = new AddAdminRepoConsumerClosure( reposcanning.getInvalidContentConsumers() );
  162. CollectionUtils.forAllDo( repoconsumerUtil.getAvailableInvalidConsumers(), addAdminRepoConsumer );
  163. this.invalidContentConsumers = addAdminRepoConsumer.getList();
  164. Collections.sort( invalidContentConsumers, AdminRepositoryConsumerComparator.getInstance() );
  165. fileTypeIds = new ArrayList();
  166. fileTypeIds.addAll( fileTypeMap.keySet() );
  167. Collections.sort( fileTypeIds );
  168. }
  169. public String removeFiletypePattern()
  170. {
  171. getLogger().info( "Remove File Type Pattern [" + getFileTypeId() + ":" + getPattern() + "]" );
  172. if ( !isValidFiletypeCommand() )
  173. {
  174. return INPUT;
  175. }
  176. FileType filetype = findFileType( getFileTypeId() );
  177. if ( filetype == null )
  178. {
  179. addActionError( "Pattern not removed, unable to find filetype " + getFileTypeId() );
  180. return INPUT;
  181. }
  182. filetype.removePattern( getPattern() );
  183. return saveConfiguration();
  184. }
  185. public void setFileTypeId( String fileTypeId )
  186. {
  187. this.fileTypeId = fileTypeId;
  188. }
  189. public void setPattern( String pattern )
  190. {
  191. this.pattern = pattern;
  192. }
  193. public String updateInvalidConsumers()
  194. {
  195. addActionMessage( "Update Invalid Consumers" );
  196. archivaConfiguration.getConfiguration().getRepositoryScanning().setInvalidContentConsumers(
  197. enabledInvalidContentConsumers );
  198. return saveConfiguration();
  199. }
  200. public String updateKnownConsumers()
  201. {
  202. addActionMessage( "Update Known Consumers" );
  203. archivaConfiguration.getConfiguration().getRepositoryScanning().setKnownContentConsumers(
  204. enabledKnownContentConsumers );
  205. return saveConfiguration();
  206. }
  207. private FileType findFileType( String id )
  208. {
  209. RepositoryScanningConfiguration scanning = archivaConfiguration.getConfiguration().getRepositoryScanning();
  210. return (FileType) CollectionUtils.find( scanning.getFileTypes(), new FiletypeSelectionPredicate( id ) );
  211. }
  212. private boolean isValidFiletypeCommand()
  213. {
  214. if ( StringUtils.isBlank( getFileTypeId() ) )
  215. {
  216. addActionError( "Unable to process blank filetype id." );
  217. }
  218. if ( StringUtils.isBlank( getPattern() ) )
  219. {
  220. addActionError( "Unable to process blank pattern." );
  221. }
  222. return !hasActionErrors();
  223. }
  224. private String saveConfiguration()
  225. {
  226. try
  227. {
  228. archivaConfiguration.save( archivaConfiguration.getConfiguration() );
  229. addActionMessage( "Successfully saved configuration" );
  230. }
  231. catch ( RegistryException e )
  232. {
  233. addActionError( "Unable to save configuration: " + e.getMessage() );
  234. return INPUT;
  235. }
  236. catch ( IndeterminateConfigurationException e )
  237. {
  238. addActionError( e.getMessage() );
  239. return INPUT;
  240. }
  241. return SUCCESS;
  242. }
  243. public List getEnabledInvalidContentConsumers()
  244. {
  245. return enabledInvalidContentConsumers;
  246. }
  247. public void setEnabledInvalidContentConsumers( List enabledInvalidContentConsumers )
  248. {
  249. this.enabledInvalidContentConsumers = enabledInvalidContentConsumers;
  250. }
  251. public List getEnabledKnownContentConsumers()
  252. {
  253. return enabledKnownContentConsumers;
  254. }
  255. public void setEnabledKnownContentConsumers( List enabledKnownContentConsumers )
  256. {
  257. this.enabledKnownContentConsumers = enabledKnownContentConsumers;
  258. }
  259. }