123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- package org.apache.maven.archiva.web.action.admin.scanning;
-
- /*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
- import com.opensymphony.xwork2.Preparable;
- import com.opensymphony.xwork2.Validateable;
- import org.apache.commons.collections.CollectionUtils;
- import org.apache.commons.lang.StringUtils;
- import org.apache.maven.archiva.configuration.ArchivaConfiguration;
- import org.apache.maven.archiva.configuration.Configuration;
- import org.apache.maven.archiva.configuration.FileType;
- import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
- import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
- import org.apache.maven.archiva.configuration.functors.FiletypeSelectionPredicate;
- import org.apache.maven.archiva.configuration.functors.FiletypeToMapClosure;
- import org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers;
- import org.apache.maven.archiva.security.ArchivaRoleConstants;
- import org.apache.maven.archiva.web.action.PlexusActionSupport;
-
- import org.codehaus.plexus.redback.rbac.Resource;
- import org.codehaus.plexus.redback.struts2.interceptor.SecureAction;
- import org.codehaus.plexus.redback.struts2.interceptor.SecureActionBundle;
- import org.codehaus.plexus.redback.struts2.interceptor.SecureActionException;
- import org.codehaus.plexus.registry.RegistryException;
-
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- import java.util.Map;
-
- /**
- * RepositoryScanningAction
- *
- * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
- * @version $Id$
- * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="repositoryScanningAction"
- */
- public class RepositoryScanningAction
- extends PlexusActionSupport
- implements Preparable, Validateable, SecureAction
- {
- /**
- * @plexus.requirement
- */
- private ArchivaConfiguration archivaConfiguration;
-
- /**
- * @plexus.requirement
- */
- private RepositoryContentConsumers repoconsumerUtil;
-
- private Map fileTypeMap;
-
- private List fileTypeIds;
-
- /**
- * List of {@link AdminRepositoryConsumer} objects for consumers of known content.
- */
- private List knownContentConsumers;
-
- /**
- * List of enabled {@link AdminRepositoryConsumer} objects for consumers of known content.
- */
- private List enabledKnownContentConsumers;
-
- /**
- * List of {@link AdminRepositoryConsumer} objects for consumers of invalid/unknown content.
- */
- private List invalidContentConsumers;
-
- /**
- * List of enabled {@link AdminRepositoryConsumer} objects for consumers of invalid/unknown content.
- */
- private List enabledInvalidContentConsumers;
-
- private String pattern;
-
- private String fileTypeId;
-
- public void addActionError( String anErrorMessage )
- {
- super.addActionError( anErrorMessage );
- getLogger().warn( "[ActionError] " + anErrorMessage );
- }
-
- public void addActionMessage( String aMessage )
- {
- super.addActionMessage( aMessage );
- getLogger().info( "[ActionMessage] " + aMessage );
- }
-
- public String addFiletypePattern()
- {
- getLogger().info( "Add New File Type Pattern [" + getFileTypeId() + ":" + getPattern() + "]" );
-
- if ( !isValidFiletypeCommand() )
- {
- return INPUT;
- }
-
- String id = getFileTypeId();
- String pattern = getPattern();
-
- FileType filetype = findFileType( id );
- if ( filetype == null )
- {
- addActionError( "Pattern not added, unable to find filetype " + id );
- return INPUT;
- }
-
- if ( filetype.getPatterns().contains( pattern ) )
- {
- addActionError( "Not adding pattern \"" + pattern + "\" to filetype " + id + " as it already exists." );
- return INPUT;
- }
-
- filetype.addPattern( pattern );
- addActionMessage( "Added pattern \"" + pattern + "\" to filetype " + id );
-
- return saveConfiguration();
- }
-
- public String getFileTypeId()
- {
- return fileTypeId;
- }
-
- public List getFileTypeIds()
- {
- return fileTypeIds;
- }
-
- public Map getFileTypeMap()
- {
- return fileTypeMap;
- }
-
- public List getInvalidContentConsumers()
- {
- return invalidContentConsumers;
- }
-
- public List getKnownContentConsumers()
- {
- return knownContentConsumers;
- }
-
- public String getPattern()
- {
- return pattern;
- }
-
- public SecureActionBundle getSecureActionBundle()
- throws SecureActionException
- {
- SecureActionBundle bundle = new SecureActionBundle();
-
- bundle.setRequiresAuthentication( true );
- bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION, Resource.GLOBAL );
-
- return bundle;
- }
-
- public void prepare()
- throws Exception
- {
- Configuration config = archivaConfiguration.getConfiguration();
- RepositoryScanningConfiguration reposcanning = config.getRepositoryScanning();
-
- FiletypeToMapClosure filetypeToMapClosure = new FiletypeToMapClosure();
-
- CollectionUtils.forAllDo( reposcanning.getFileTypes(), filetypeToMapClosure );
- fileTypeMap = filetypeToMapClosure.getMap();
-
- AddAdminRepoConsumerClosure addAdminRepoConsumer;
-
- addAdminRepoConsumer = new AddAdminRepoConsumerClosure( reposcanning.getKnownContentConsumers() );
- CollectionUtils.forAllDo( repoconsumerUtil.getAvailableKnownConsumers(), addAdminRepoConsumer );
- this.knownContentConsumers = addAdminRepoConsumer.getList();
- Collections.sort( knownContentConsumers, AdminRepositoryConsumerComparator.getInstance() );
-
- addAdminRepoConsumer = new AddAdminRepoConsumerClosure( reposcanning.getInvalidContentConsumers() );
- CollectionUtils.forAllDo( repoconsumerUtil.getAvailableInvalidConsumers(), addAdminRepoConsumer );
- this.invalidContentConsumers = addAdminRepoConsumer.getList();
- Collections.sort( invalidContentConsumers, AdminRepositoryConsumerComparator.getInstance() );
-
- fileTypeIds = new ArrayList();
- fileTypeIds.addAll( fileTypeMap.keySet() );
- Collections.sort( fileTypeIds );
- }
-
- public String removeFiletypePattern()
- {
- getLogger().info( "Remove File Type Pattern [" + getFileTypeId() + ":" + getPattern() + "]" );
-
- if ( !isValidFiletypeCommand() )
- {
- return INPUT;
- }
-
- FileType filetype = findFileType( getFileTypeId() );
- if ( filetype == null )
- {
- addActionError( "Pattern not removed, unable to find filetype " + getFileTypeId() );
- return INPUT;
- }
-
- filetype.removePattern( getPattern() );
-
- return saveConfiguration();
- }
-
- public void setFileTypeId( String fileTypeId )
- {
- this.fileTypeId = fileTypeId;
- }
-
- public void setPattern( String pattern )
- {
- this.pattern = pattern;
- }
-
- public String updateInvalidConsumers()
- {
- addActionMessage( "Update Invalid Consumers" );
-
- archivaConfiguration.getConfiguration().getRepositoryScanning().setInvalidContentConsumers(
- enabledInvalidContentConsumers );
-
- return saveConfiguration();
- }
-
- public String updateKnownConsumers()
- {
- addActionMessage( "Update Known Consumers" );
-
- archivaConfiguration.getConfiguration().getRepositoryScanning().setKnownContentConsumers(
- enabledKnownContentConsumers );
-
- return saveConfiguration();
- }
-
- private FileType findFileType( String id )
- {
- RepositoryScanningConfiguration scanning = archivaConfiguration.getConfiguration().getRepositoryScanning();
- return (FileType) CollectionUtils.find( scanning.getFileTypes(), new FiletypeSelectionPredicate( id ) );
- }
-
- private boolean isValidFiletypeCommand()
- {
- if ( StringUtils.isBlank( getFileTypeId() ) )
- {
- addActionError( "Unable to process blank filetype id." );
- }
-
- if ( StringUtils.isBlank( getPattern() ) )
- {
- addActionError( "Unable to process blank pattern." );
- }
-
- return !hasActionErrors();
- }
-
- private String saveConfiguration()
- {
- try
- {
- archivaConfiguration.save( archivaConfiguration.getConfiguration() );
- addActionMessage( "Successfully saved configuration" );
- }
- catch ( RegistryException e )
- {
- addActionError( "Unable to save configuration: " + e.getMessage() );
- return INPUT;
- }
- catch ( IndeterminateConfigurationException e )
- {
- addActionError( e.getMessage() );
- return INPUT;
- }
-
- return SUCCESS;
- }
-
- public List getEnabledInvalidContentConsumers()
- {
- return enabledInvalidContentConsumers;
- }
-
- public void setEnabledInvalidContentConsumers( List enabledInvalidContentConsumers )
- {
- this.enabledInvalidContentConsumers = enabledInvalidContentConsumers;
- }
-
- public List getEnabledKnownContentConsumers()
- {
- return enabledKnownContentConsumers;
- }
-
- public void setEnabledKnownContentConsumers( List enabledKnownContentConsumers )
- {
- this.enabledKnownContentConsumers = enabledKnownContentConsumers;
- }
- }
|