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.rest.services.utils.AddAdminRepoConsumerClosure;
32 import org.apache.archiva.rest.services.utils.AdminRepositoryConsumerComparator;
33 import org.apache.archiva.security.common.ArchivaRoleConstants;
34 import org.apache.commons.collections.CollectionUtils;
35 import org.apache.commons.lang.StringUtils;
36 import org.apache.archiva.web.action.AbstractActionSupport;
37 import org.apache.archiva.redback.rbac.Resource;
38 import org.apache.archiva.redback.integration.interceptor.SecureAction;
39 import org.apache.archiva.redback.integration.interceptor.SecureActionBundle;
40 import org.apache.archiva.redback.integration.interceptor.SecureActionException;
41 import org.springframework.context.annotation.Scope;
42 import org.springframework.stereotype.Controller;
44 import javax.inject.Inject;
45 import java.util.ArrayList;
46 import java.util.Collections;
47 import java.util.List;
51 * RepositoryScanningAction
55 @Controller( "repositoryScanningAction" )
57 public class RepositoryScanningAction
58 extends AbstractActionSupport
59 implements Preparable, Validateable, SecureAction, Auditable
63 private RepositoryContentConsumers repoconsumerUtil;
66 private ArchivaAdministration archivaAdministration;
68 private Map<String, FileType> fileTypeMap;
70 private List<String> fileTypeIds;
73 * List of {@link org.apache.archiva.rest.api.model.AdminRepositoryConsumer} objects for consumers of known content.
75 private List<AdminRepositoryConsumer> knownContentConsumers;
78 * List of enabled {@link AdminRepositoryConsumer} objects for consumers of known content.
80 private List<String> enabledKnownContentConsumers;
83 * List of {@link AdminRepositoryConsumer} objects for consumers of invalid/unknown content.
85 private List<AdminRepositoryConsumer> invalidContentConsumers;
88 * List of enabled {@link AdminRepositoryConsumer} objects for consumers of invalid/unknown content.
90 private List<String> enabledInvalidContentConsumers;
92 private String pattern;
94 private String fileTypeId;
96 public void addActionError( String anErrorMessage )
98 super.addActionError( anErrorMessage );
99 log.warn( "[ActionError] {}", anErrorMessage );
102 public void addActionMessage( String aMessage )
104 super.addActionMessage( aMessage );
105 log.info( "[ActionMessage] {}", aMessage );
108 public void prepare()
111 FiletypeToMapClosure filetypeToMapClosure = new FiletypeToMapClosure();
113 CollectionUtils.forAllDo( archivaAdministration.getFileTypes(), filetypeToMapClosure );
114 fileTypeMap = filetypeToMapClosure.getMap();
116 AddAdminRepoConsumerClosure addAdminRepoConsumer =
117 new AddAdminRepoConsumerClosure( archivaAdministration.getKnownContentConsumers() );
118 CollectionUtils.forAllDo( repoconsumerUtil.getAvailableKnownConsumers(), addAdminRepoConsumer );
119 this.knownContentConsumers = addAdminRepoConsumer.getList();
120 Collections.sort( knownContentConsumers, AdminRepositoryConsumerComparator.getInstance() );
122 addAdminRepoConsumer = new AddAdminRepoConsumerClosure( archivaAdministration.getInvalidContentConsumers() );
123 CollectionUtils.forAllDo( repoconsumerUtil.getAvailableInvalidConsumers(), addAdminRepoConsumer );
124 this.invalidContentConsumers = addAdminRepoConsumer.getList();
125 Collections.sort( invalidContentConsumers, AdminRepositoryConsumerComparator.getInstance() );
127 fileTypeIds = new ArrayList<String>();
128 fileTypeIds.addAll( fileTypeMap.keySet() );
129 Collections.sort( fileTypeIds );
132 public String addFiletypePattern()
134 log.info( "Add New File Type Pattern [{}:{}]", getFileTypeId(), getPattern() );
136 if ( !isValidFiletypeCommand() )
143 getArchivaAdministration().addFileTypePattern( getFileTypeId(), getPattern(), getAuditInformation() );
145 catch ( RepositoryAdminException e )
147 addActionError( e.getMessage() );
153 public String removeFiletypePattern()
154 throws RepositoryAdminException
156 log.info( "Remove File Type Pattern [{}:{}]", getFileTypeId(), getPattern() );
158 if ( !isValidFiletypeCommand() )
165 getArchivaAdministration().removeFileTypePattern( getFileTypeId(), getPattern(), getAuditInformation() );
167 catch ( RepositoryAdminException e )
169 addActionError( "error adding file type pattern " + e.getMessage() );
176 public String getFileTypeId()
181 public List<String> getFileTypeIds()
186 public Map<String, FileType> getFileTypeMap()
191 public List<AdminRepositoryConsumer> getInvalidContentConsumers()
193 return invalidContentConsumers;
196 public List<AdminRepositoryConsumer> getKnownContentConsumers()
198 return knownContentConsumers;
201 public String getPattern()
206 public SecureActionBundle getSecureActionBundle()
207 throws SecureActionException
209 SecureActionBundle bundle = new SecureActionBundle();
211 bundle.setRequiresAuthentication( true );
212 bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION, Resource.GLOBAL );
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().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().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 boolean isValidFiletypeCommand()
311 if ( StringUtils.isBlank( getFileTypeId() ) )
313 addActionError( "Unable to process blank filetype id." );
316 if ( StringUtils.isBlank( getPattern() ) )
318 addActionError( "Unable to process blank pattern." );
321 return !hasActionErrors();
325 public List<String> getEnabledInvalidContentConsumers()
327 return enabledInvalidContentConsumers;
330 public void setEnabledInvalidContentConsumers( List<String> enabledInvalidContentConsumers )
332 this.enabledInvalidContentConsumers = enabledInvalidContentConsumers;
335 public List<String> getEnabledKnownContentConsumers()
337 return enabledKnownContentConsumers;
340 public void setEnabledKnownContentConsumers( List<String> enabledKnownContentConsumers )
342 this.enabledKnownContentConsumers = enabledKnownContentConsumers;
345 public ArchivaAdministration getArchivaAdministration()
347 return archivaAdministration;
350 public void setArchivaAdministration( ArchivaAdministration archivaAdministration )
352 this.archivaAdministration = archivaAdministration;