1 package org.apache.maven.archiva.web.action.admin.repositories;
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.xwork.Preparable;
23 import org.apache.commons.io.FileUtils;
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
26 import org.apache.maven.archiva.configuration.Configuration;
27 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
28 import org.apache.maven.archiva.configuration.InvalidConfigurationException;
29 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
30 import org.apache.maven.archiva.security.ArchivaRoleConstants;
31 import org.codehaus.plexus.redback.rbac.Resource;
32 import org.codehaus.plexus.redback.role.RoleManager;
33 import org.codehaus.plexus.redback.role.RoleManagerException;
34 import org.codehaus.plexus.redback.xwork.interceptor.SecureAction;
35 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
36 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionException;
37 import org.codehaus.plexus.registry.RegistryException;
38 import org.codehaus.plexus.scheduler.CronExpressionValidator;
39 import org.codehaus.plexus.xwork.action.PlexusActionSupport;
42 import java.io.IOException;
45 * Configures the application repositories.
47 * @plexus.component role="com.opensymphony.xwork.Action" role-hint="configureRepositoryAction"
49 public class ConfigureRepositoryAction
50 extends PlexusActionSupport
51 implements Preparable, SecureAction
54 * @plexus.requirement role-hint="default"
56 private RoleManager roleManager;
61 private ArchivaConfiguration archivaConfiguration;
63 private String repoid;
65 // TODO! consider removing? was just meant to be for delete...
69 * The model for this action.
71 private AdminRepositoryConfiguration repository;
77 this.repository.setReleases( true );
78 this.repository.setIndexed( true );
83 public String confirm()
88 public String delete()
90 String result = SUCCESS;
91 if ( StringUtils.equals( mode, "delete-entry" ) || StringUtils.equals( mode, "delete-contents" ) )
93 AdminRepositoryConfiguration existingRepository = repository;
94 if ( existingRepository == null )
96 addActionError( "A repository with that id does not exist" );
100 // TODO: remove from index too!
104 removeRepository( repoid, archivaConfiguration.getConfiguration() );
105 result = saveConfiguration( archivaConfiguration.getConfiguration() );
107 if ( result.equals( SUCCESS ) )
109 removeRepositoryRoles( existingRepository );
110 if ( StringUtils.equals( mode, "delete-contents" ) )
112 removeContents( existingRepository );
116 catch ( IOException e )
118 addActionError( "Unable to delete repository: " + e.getMessage() );
121 catch ( RoleManagerException e )
123 addActionError( "Unable to delete repository: " + e.getMessage() );
126 catch ( InvalidConfigurationException e )
128 addActionError( "Unable to delete repository: " + e.getMessage() );
131 catch ( RegistryException e )
133 addActionError( "Unable to delete repository: " + e.getMessage() );
148 public String getMode()
153 public String getRepoid()
158 public AdminRepositoryConfiguration getRepository()
163 public SecureActionBundle getSecureActionBundle()
164 throws SecureActionException
166 SecureActionBundle bundle = new SecureActionBundle();
168 bundle.setRequiresAuthentication( true );
169 bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION, Resource.GLOBAL );
174 public void prepare()
179 this.repository = new AdminRepositoryConfiguration();
180 this.repository.setReleases( false );
181 this.repository.setIndexed( false );
185 ManagedRepositoryConfiguration repoconfig =
186 archivaConfiguration.getConfiguration().findManagedRepositoryById( id );
187 if ( repoconfig != null )
189 this.repository = new AdminRepositoryConfiguration( repoconfig );
195 String repoId = repository.getId();
197 Configuration configuration = archivaConfiguration.getConfiguration();
198 boolean containsError = validateFields( configuration );
200 if ( containsError && StringUtils.equalsIgnoreCase( "add", mode ) )
204 else if ( containsError && StringUtils.equalsIgnoreCase( "edit", this.mode ) )
209 if ( StringUtils.equalsIgnoreCase( "edit", this.mode ) )
211 removeRepository( repoId, configuration );
217 addRepository( repository, configuration );
218 result = saveConfiguration( configuration );
220 catch ( IOException e )
222 addActionError( "I/O Exception: " + e.getMessage() );
225 catch ( RoleManagerException e )
227 addActionError( "Role Manager Exception: " + e.getMessage() );
230 catch ( InvalidConfigurationException e )
232 addActionError( "Invalid Configuration Exception: " + e.getMessage() );
235 catch ( RegistryException e )
237 addActionError( "Configuration Registry Exception: " + e.getMessage() );
244 private boolean validateFields( Configuration config )
246 boolean containsError = false;
247 CronExpressionValidator validator = new CronExpressionValidator();
248 String repoId = repository.getId();
250 if ( StringUtils.isBlank( repoId ) )
252 addFieldError( "repository.id", "You must enter a repository identifier." );
253 containsError = true;
255 //if edit mode, do not validate existence of repoId
256 else if ( ( config.getManagedRepositoriesAsMap().containsKey( repoId ) ||
257 config.getRemoteRepositoriesAsMap().containsKey( repoId ) ) &&
258 !StringUtils.equalsIgnoreCase( mode, "edit" ) )
260 addFieldError( "repository.id",
261 "Unable to add new repository with id [" + repoId + "], that id already exists." );
262 containsError = true;
266 if ( StringUtils.isBlank( repository.getLocation() ) )
268 addFieldError( "repository.url", "You must enter a directory." );
269 containsError = true;
271 if ( StringUtils.isBlank( repository.getName() ) )
273 addFieldError( "repository.name", "You must enter a repository name." );
274 containsError = true;
276 if ( !validator.validate( repository.getRefreshCronExpression() ) )
278 addFieldError( "repository.refreshCronExpression", "Invalid cron expression." );
279 containsError = true;
282 return containsError;
285 public void setMode( String mode )
290 public void setRepoid( String repoid )
292 this.repoid = repoid;
295 public void setRepository( AdminRepositoryConfiguration repository )
297 this.repository = repository;
300 private void addRepository( AdminRepositoryConfiguration repository, Configuration configuration )
301 throws IOException, RoleManagerException
303 // Normalize the path
304 File file = new File( repository.getLocation() );
305 repository.setLocation( file.getCanonicalPath() );
306 if ( !file.exists() )
309 // TODO: error handling when this fails, or is not a directory!
313 configuration.addManagedRepository( repository );
315 // TODO: double check these are configured on start up
316 // TODO: belongs in the business logic
317 roleManager.createTemplatedRole( "archiva-repository-manager", repository.getId() );
319 roleManager.createTemplatedRole( "archiva-repository-observer", repository.getId() );
322 private void removeContents( AdminRepositoryConfiguration existingRepository )
325 FileUtils.deleteDirectory( new File( existingRepository.getLocation() ) );
328 private void removeRepository( String repoId, Configuration configuration )
330 // TODO! what about others?
331 ManagedRepositoryConfiguration toremove = configuration.findManagedRepositoryById( repoId );
332 if ( toremove != null )
334 configuration.removeManagedRepository( toremove );
338 private void removeRepositoryRoles( ManagedRepositoryConfiguration existingRepository )
339 throws RoleManagerException
341 roleManager.removeTemplatedRole( "archiva-repository-manager", existingRepository.getId() );
342 roleManager.removeTemplatedRole( "archiva-repository-observer", existingRepository.getId() );
344 getLogger().debug( "removed user roles associated with repository " + existingRepository.getId() );
347 private String saveConfiguration( Configuration configuration )
348 throws IOException, InvalidConfigurationException, RegistryException
352 archivaConfiguration.save( configuration );
353 addActionMessage( "Successfully saved configuration" );
355 catch ( IndeterminateConfigurationException e )
357 addActionError( e.getMessage() );
364 public void setRoleManager( RoleManager roleManager )
366 this.roleManager = roleManager;
369 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
371 this.archivaConfiguration = archivaConfiguration;