1 package org.apache.archiva.web.action.admin.scanning;
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 com.opensymphony.xwork2.Validateable;
24 import org.apache.archiva.admin.model.RepositoryAdminException;
25 import org.apache.archiva.admin.model.admin.ArchivaAdministration;
26 import org.apache.archiva.admin.model.beans.FileType;
27 import org.apache.archiva.admin.repository.admin.FiletypeToMapClosure;
28 import org.apache.archiva.audit.Auditable;
29 import org.apache.archiva.repository.scanner.RepositoryContentConsumers;
30 import org.apache.archiva.rest.api.model.AdminRepositoryConsumer;
31 import org.apache.archiva.security.common.ArchivaRoleConstants;
32 import org.apache.commons.collections.CollectionUtils;
33 import org.apache.commons.lang.StringUtils;
34 import org.apache.archiva.web.action.AbstractActionSupport;
35 import org.codehaus.plexus.redback.rbac.Resource;
36 import org.codehaus.redback.integration.interceptor.SecureAction;
37 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
38 import org.codehaus.redback.integration.interceptor.SecureActionException;
39 import org.springframework.context.annotation.Scope;
40 import org.springframework.stereotype.Controller;
42 import javax.inject.Inject;
43 import java.util.ArrayList;
44 import java.util.Collections;
45 import java.util.List;
49 * RepositoryScanningAction
53 @Controller( "repositoryScanningAction" )
55 public class RepositoryScanningAction
56 extends AbstractActionSupport
57 implements Preparable, Validateable, SecureAction, Auditable
61 private RepositoryContentConsumers repoconsumerUtil;
64 private ArchivaAdministration archivaAdministration;
66 private Map<String, FileType> fileTypeMap;
68 private List<String> fileTypeIds;
71 * List of {@link org.apache.archiva.rest.api.model.AdminRepositoryConsumer} objects for consumers of known content.
73 private List<AdminRepositoryConsumer> knownContentConsumers;
76 * List of enabled {@link AdminRepositoryConsumer} objects for consumers of known content.
78 private List<String> enabledKnownContentConsumers;
81 * List of {@link AdminRepositoryConsumer} objects for consumers of invalid/unknown content.
83 private List<AdminRepositoryConsumer> invalidContentConsumers;
86 * List of enabled {@link AdminRepositoryConsumer} objects for consumers of invalid/unknown content.
88 private List<String> enabledInvalidContentConsumers;
90 private String pattern;
92 private String fileTypeId;
94 public void addActionError( String anErrorMessage )
96 super.addActionError( anErrorMessage );
97 log.warn( "[ActionError] {}", anErrorMessage );
100 public void addActionMessage( String aMessage )
102 super.addActionMessage( aMessage );
103 log.info( "[ActionMessage] {}", aMessage );
106 public void prepare()
109 FiletypeToMapClosure filetypeToMapClosure = new FiletypeToMapClosure();
111 CollectionUtils.forAllDo( archivaAdministration.getFileTypes(), filetypeToMapClosure );
112 fileTypeMap = filetypeToMapClosure.getMap();
114 AddAdminRepoConsumerClosure addAdminRepoConsumer =
115 new AddAdminRepoConsumerClosure( archivaAdministration.getKnownContentConsumers() );
116 CollectionUtils.forAllDo( repoconsumerUtil.getAvailableKnownConsumers(), addAdminRepoConsumer );
117 this.knownContentConsumers = addAdminRepoConsumer.getList();
118 Collections.sort( knownContentConsumers, AdminRepositoryConsumerComparator.getInstance() );
120 addAdminRepoConsumer = new AddAdminRepoConsumerClosure( archivaAdministration.getInvalidContentConsumers() );
121 CollectionUtils.forAllDo( repoconsumerUtil.getAvailableInvalidConsumers(), addAdminRepoConsumer );
122 this.invalidContentConsumers = addAdminRepoConsumer.getList();
123 Collections.sort( invalidContentConsumers, AdminRepositoryConsumerComparator.getInstance() );
125 fileTypeIds = new ArrayList<String>();
126 fileTypeIds.addAll( fileTypeMap.keySet() );
127 Collections.sort( fileTypeIds );
130 public String addFiletypePattern()
132 log.info( "Add New File Type Pattern [{}:{}]", getFileTypeId(), getPattern() );
134 if ( !isValidFiletypeCommand() )
141 getArchivaAdministration().addFileTypePattern( getFileTypeId(), getPattern(), getAuditInformation() );
143 catch ( RepositoryAdminException e )
145 addActionError( e.getMessage() );
151 public String removeFiletypePattern()
152 throws RepositoryAdminException
154 log.info( "Remove File Type Pattern [{}:{}]", getFileTypeId(), getPattern() );
156 if ( !isValidFiletypeCommand() )
163 getArchivaAdministration().removeFileTypePattern( getFileTypeId(), getPattern(), getAuditInformation() );
165 catch ( RepositoryAdminException e )
167 addActionError( "error adding file type pattern " + e.getMessage() );
174 public String getFileTypeId()
179 public List<String> getFileTypeIds()
184 public Map<String, FileType> getFileTypeMap()
189 public List<AdminRepositoryConsumer> getInvalidContentConsumers()
191 return invalidContentConsumers;
194 public List<AdminRepositoryConsumer> getKnownContentConsumers()
196 return knownContentConsumers;
199 public String getPattern()
204 public SecureActionBundle getSecureActionBundle()
205 throws SecureActionException
207 SecureActionBundle bundle = new SecureActionBundle();
209 bundle.setRequiresAuthentication( true );
210 bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION, Resource.GLOBAL );
215 public void setFileTypeId( String fileTypeId )
217 this.fileTypeId = fileTypeId;
220 public void setPattern( String pattern )
222 this.pattern = pattern;
225 public String updateInvalidConsumers()
230 List<String> oldConsumers = getArchivaAdministration().getInvalidContentConsumers();
232 if ( enabledInvalidContentConsumers != null )
234 for ( String oldConsumer : oldConsumers )
236 if ( !enabledInvalidContentConsumers.contains( oldConsumer ) )
238 getArchivaAdministration().removeInvalidContentConsumer( oldConsumer, getAuditInformation() );
241 for ( String enabledKnowContentConsumer : enabledInvalidContentConsumers )
243 getArchivaAdministration().addInvalidContentConsumer( enabledKnowContentConsumer,
244 getAuditInformation() );
249 for ( String oldConsumer : oldConsumers )
251 getArchivaAdministration().removeInvalidContentConsumer( oldConsumer, getAuditInformation() );
255 catch ( RepositoryAdminException e )
257 log.error( e.getMessage(), e );
258 addActionError( "Error update invalidContentConsumers " + e.getMessage() );
261 addActionMessage( "Update Invalid Consumers" );
266 public String updateKnownConsumers()
271 List<String> oldConsumers = getArchivaAdministration().getKnownContentConsumers();
273 if ( enabledKnownContentConsumers != null )
275 for ( String oldConsumer : oldConsumers )
277 if ( !enabledKnownContentConsumers.contains( oldConsumer ) )
279 getArchivaAdministration().removeKnownContentConsumer( oldConsumer, getAuditInformation() );
282 for ( String enabledKnowContentConsumer : enabledKnownContentConsumers )
284 getArchivaAdministration().addKnownContentConsumer( enabledKnowContentConsumer,
285 getAuditInformation() );
290 for ( String oldConsumer : oldConsumers )
292 getArchivaAdministration().removeKnownContentConsumer( oldConsumer, getAuditInformation() );
296 catch ( RepositoryAdminException e )
298 log.error( e.getMessage(), e );
299 addActionError( "Error update knowContentConsumers " + e.getMessage() );
302 addActionMessage( "Update Known Consumers" );
307 private boolean isValidFiletypeCommand()
309 if ( StringUtils.isBlank( getFileTypeId() ) )
311 addActionError( "Unable to process blank filetype id." );
314 if ( StringUtils.isBlank( getPattern() ) )
316 addActionError( "Unable to process blank pattern." );
319 return !hasActionErrors();
323 public List<String> getEnabledInvalidContentConsumers()
325 return enabledInvalidContentConsumers;
328 public void setEnabledInvalidContentConsumers( List<String> enabledInvalidContentConsumers )
330 this.enabledInvalidContentConsumers = enabledInvalidContentConsumers;
333 public List<String> getEnabledKnownContentConsumers()
335 return enabledKnownContentConsumers;
338 public void setEnabledKnownContentConsumers( List<String> enabledKnownContentConsumers )
340 this.enabledKnownContentConsumers = enabledKnownContentConsumers;
343 public ArchivaAdministration getArchivaAdministration()
345 return archivaAdministration;
348 public void setArchivaAdministration( ArchivaAdministration archivaAdministration )
350 this.archivaAdministration = archivaAdministration;