1 package org.apache.maven.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.repository.RepositoryAdminException;
25 import org.apache.archiva.admin.repository.admin.ArchivaAdministration;
26 import org.apache.archiva.admin.repository.admin.FileType;
27 import org.apache.archiva.admin.repository.admin.FiletypeToMapClosure;
28 import org.apache.archiva.admin.repository.admin.RepositoryScanning;
29 import org.apache.archiva.audit.Auditable;
30 import org.apache.archiva.repository.scanner.RepositoryContentConsumers;
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.maven.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 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 String addFiletypePattern()
108 log.info( "Add New File Type Pattern [{}:{}]", getFileTypeId(), getPattern() );
110 if ( !isValidFiletypeCommand() )
117 getArchivaAdministration().addFileTypePattern( getFileTypeId(), getPattern(), getAuditInformation() );
119 catch ( RepositoryAdminException e )
121 addActionError( "error adding file type pattern " + e.getMessage() );
127 public String removeFiletypePattern()
128 throws RepositoryAdminException
130 log.info( "Remove File Type Pattern [{}:{}]", getFileTypeId(), getPattern() );
132 if ( !isValidFiletypeCommand() )
139 getArchivaAdministration().removeFileTypePattern( getFileTypeId(), getPattern(), getAuditInformation() );
141 catch ( RepositoryAdminException e )
143 addActionError( "error adding file type pattern " + e.getMessage() );
150 public String getFileTypeId()
155 public List<String> getFileTypeIds()
160 public Map<String, FileType> getFileTypeMap()
165 public List<AdminRepositoryConsumer> getInvalidContentConsumers()
167 return invalidContentConsumers;
170 public List<AdminRepositoryConsumer> getKnownContentConsumers()
172 return knownContentConsumers;
175 public String getPattern()
180 public SecureActionBundle getSecureActionBundle()
181 throws SecureActionException
183 SecureActionBundle bundle = new SecureActionBundle();
185 bundle.setRequiresAuthentication( true );
186 bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION, Resource.GLOBAL );
191 public void prepare()
194 RepositoryScanning reposcanning = archivaAdministration.getRepositoryScanning();
196 FiletypeToMapClosure filetypeToMapClosure = new FiletypeToMapClosure();
198 CollectionUtils.forAllDo( reposcanning.getFileTypes(), filetypeToMapClosure );
199 fileTypeMap = filetypeToMapClosure.getMap();
201 AddAdminRepoConsumerClosure addAdminRepoConsumer =
202 new AddAdminRepoConsumerClosure( reposcanning.getKnownContentConsumers() );
203 CollectionUtils.forAllDo( repoconsumerUtil.getAvailableKnownConsumers(), addAdminRepoConsumer );
204 this.knownContentConsumers = addAdminRepoConsumer.getList();
205 Collections.sort( knownContentConsumers, AdminRepositoryConsumerComparator.getInstance() );
207 addAdminRepoConsumer = new AddAdminRepoConsumerClosure( reposcanning.getInvalidContentConsumers() );
208 CollectionUtils.forAllDo( repoconsumerUtil.getAvailableInvalidConsumers(), addAdminRepoConsumer );
209 this.invalidContentConsumers = addAdminRepoConsumer.getList();
210 Collections.sort( invalidContentConsumers, AdminRepositoryConsumerComparator.getInstance() );
212 fileTypeIds = new ArrayList<String>();
213 fileTypeIds.addAll( fileTypeMap.keySet() );
214 Collections.sort( fileTypeIds );
217 public void setFileTypeId( String fileTypeId )
219 this.fileTypeId = fileTypeId;
222 public void setPattern( String pattern )
224 this.pattern = pattern;
227 public String updateInvalidConsumers()
232 List<String> oldConsumers = getArchivaAdministration().getRepositoryScanning().getInvalidContentConsumers();
234 if ( enabledInvalidContentConsumers != null )
236 for ( String oldConsumer : oldConsumers )
238 if ( !enabledInvalidContentConsumers.contains( oldConsumer ) )
240 getArchivaAdministration().removeInvalidContentConsumer( oldConsumer, getAuditInformation() );
243 for ( String enabledKnowContentConsumer : enabledInvalidContentConsumers )
245 getArchivaAdministration().addInvalidContentConsumer( enabledKnowContentConsumer,
246 getAuditInformation() );
251 for ( String oldConsumer : oldConsumers )
253 getArchivaAdministration().removeInvalidContentConsumer( oldConsumer, getAuditInformation() );
257 catch ( RepositoryAdminException e )
259 log.error( e.getMessage(), e );
260 addActionError( "Error update invalidContentConsumers " + e.getMessage() );
263 addActionMessage( "Update Invalid Consumers" );
268 public String updateKnownConsumers()
273 List<String> oldConsumers = getArchivaAdministration().getRepositoryScanning().getKnownContentConsumers();
275 if ( enabledKnownContentConsumers != null )
277 for ( String oldConsumer : oldConsumers )
279 if ( !enabledKnownContentConsumers.contains( oldConsumer ) )
281 getArchivaAdministration().removeKnownContentConsumer( oldConsumer, getAuditInformation() );
284 for ( String enabledKnowContentConsumer : enabledKnownContentConsumers )
286 getArchivaAdministration().addKnownContentConsumer( enabledKnowContentConsumer,
287 getAuditInformation() );
292 for ( String oldConsumer : oldConsumers )
294 getArchivaAdministration().removeKnownContentConsumer( oldConsumer, getAuditInformation() );
298 catch ( RepositoryAdminException e )
300 log.error( e.getMessage(), e );
301 addActionError( "Error update knowContentConsumers " + e.getMessage() );
304 addActionMessage( "Update Known Consumers" );
309 private FileType findFileType( String id )
310 throws RepositoryAdminException
312 return getArchivaAdministration().getFileType( id );
315 private boolean isValidFiletypeCommand()
317 if ( StringUtils.isBlank( getFileTypeId() ) )
319 addActionError( "Unable to process blank filetype id." );
322 if ( StringUtils.isBlank( getPattern() ) )
324 addActionError( "Unable to process blank pattern." );
327 return !hasActionErrors();
331 public List<String> getEnabledInvalidContentConsumers()
333 return enabledInvalidContentConsumers;
336 public void setEnabledInvalidContentConsumers( List<String> enabledInvalidContentConsumers )
338 this.enabledInvalidContentConsumers = enabledInvalidContentConsumers;
341 public List<String> getEnabledKnownContentConsumers()
343 return enabledKnownContentConsumers;
346 public void setEnabledKnownContentConsumers( List<String> enabledKnownContentConsumers )
348 this.enabledKnownContentConsumers = enabledKnownContentConsumers;
351 public ArchivaAdministration getArchivaAdministration()
353 return archivaAdministration;
356 public void setArchivaAdministration( ArchivaAdministration archivaAdministration )
358 this.archivaAdministration = archivaAdministration;