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 // TODO: rename to confirmDelete
84 public String confirm()
89 public String delete()
91 String result = SUCCESS;
92 if ( StringUtils.equals( mode, "delete-entry" ) || StringUtils.equals( mode, "delete-contents" ) )
94 AdminRepositoryConfiguration existingRepository = repository;
95 if ( existingRepository == null )
97 addActionError( "A repository with that id does not exist" );
101 // TODO: remove from index too!
105 Configuration configuration = archivaConfiguration.getConfiguration();
106 removeRepository( repoid, configuration );
107 result = saveConfiguration( configuration );
109 if ( result.equals( SUCCESS ) )
111 removeRepositoryRoles( existingRepository );
112 if ( StringUtils.equals( mode, "delete-contents" ) )
114 removeContents( existingRepository );
118 catch ( IOException e )
120 addActionError( "Unable to delete repository: " + e.getMessage() );
123 catch ( RoleManagerException e )
125 addActionError( "Unable to delete repository: " + e.getMessage() );
128 catch ( InvalidConfigurationException e )
130 addActionError( "Unable to delete repository: " + e.getMessage() );
133 catch ( RegistryException e )
135 addActionError( "Unable to delete repository: " + e.getMessage() );
150 public String getMode()
155 public String getRepoid()
160 public AdminRepositoryConfiguration getRepository()
165 public SecureActionBundle getSecureActionBundle()
166 throws SecureActionException
168 SecureActionBundle bundle = new SecureActionBundle();
170 bundle.setRequiresAuthentication( true );
171 bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION, Resource.GLOBAL );
176 public void prepare()
181 this.repository = new AdminRepositoryConfiguration();
182 this.repository.setReleases( false );
183 this.repository.setIndexed( false );
187 ManagedRepositoryConfiguration repoconfig =
188 archivaConfiguration.getConfiguration().findManagedRepositoryById( id );
189 if ( repoconfig != null )
191 this.repository = new AdminRepositoryConfiguration( repoconfig );
197 String repoId = repository.getId();
199 Configuration configuration = archivaConfiguration.getConfiguration();
200 boolean containsError = validateFields( configuration );
202 if ( containsError && StringUtils.equalsIgnoreCase( "add", mode ) )
206 else if ( containsError && StringUtils.equalsIgnoreCase( "edit", this.mode ) )
211 if ( StringUtils.equalsIgnoreCase( "edit", this.mode ) )
213 removeRepository( repoId, configuration );
219 addRepository( repository, configuration );
220 result = saveConfiguration( configuration );
222 catch ( IOException e )
224 addActionError( "I/O Exception: " + e.getMessage() );
227 catch ( RoleManagerException e )
229 addActionError( "Role Manager Exception: " + e.getMessage() );
232 catch ( InvalidConfigurationException e )
234 addActionError( "Invalid Configuration Exception: " + e.getMessage() );
237 catch ( RegistryException e )
239 addActionError( "Configuration Registry Exception: " + e.getMessage() );
246 private boolean validateFields( Configuration config )
248 boolean containsError = false;
249 CronExpressionValidator validator = new CronExpressionValidator();
250 String repoId = repository.getId();
252 if ( StringUtils.isBlank( repoId ) )
254 addFieldError( "repository.id", "You must enter a repository identifier." );
255 containsError = true;
257 //if edit mode, do not validate existence of repoId
258 else if ( ( config.getManagedRepositoriesAsMap().containsKey( repoId ) ||
259 config.getRemoteRepositoriesAsMap().containsKey( repoId ) ) &&
260 !StringUtils.equalsIgnoreCase( mode, "edit" ) )
262 addFieldError( "repository.id",
263 "Unable to add new repository with id [" + repoId + "], that id already exists." );
264 containsError = true;
268 if ( StringUtils.isBlank( repository.getLocation() ) )
270 addFieldError( "repository.location", "You must enter a directory." );
271 containsError = true;
273 if ( StringUtils.isBlank( repository.getName() ) )
275 addFieldError( "repository.name", "You must enter a repository name." );
276 containsError = true;
278 if ( !validator.validate( repository.getRefreshCronExpression() ) )
280 addFieldError( "repository.refreshCronExpression", "Invalid cron expression." );
281 containsError = true;
284 return containsError;
287 public void setMode( String mode )
292 public void setRepoid( String repoid )
294 this.repoid = repoid;
297 private void addRepository( AdminRepositoryConfiguration repository, Configuration configuration )
298 throws IOException, RoleManagerException
300 // Normalize the path
301 File file = new File( repository.getLocation() );
302 repository.setLocation( file.getCanonicalPath() );
303 if ( !file.exists() )
306 // TODO: error handling when this fails, or is not a directory!
310 configuration.addManagedRepository( repository );
312 // TODO: double check these are configured on start up
313 // TODO: belongs in the business logic
314 roleManager.createTemplatedRole( "archiva-repository-manager", repository.getId() );
316 roleManager.createTemplatedRole( "archiva-repository-observer", repository.getId() );
319 private void removeContents( AdminRepositoryConfiguration existingRepository )
322 FileUtils.deleteDirectory( new File( existingRepository.getLocation() ) );
325 private void removeRepository( String repoId, Configuration configuration )
327 // TODO! what about others?
328 ManagedRepositoryConfiguration toremove = configuration.findManagedRepositoryById( repoId );
329 if ( toremove != null )
331 configuration.removeManagedRepository( toremove );
335 private void removeRepositoryRoles( ManagedRepositoryConfiguration existingRepository )
336 throws RoleManagerException
338 roleManager.removeTemplatedRole( "archiva-repository-manager", existingRepository.getId() );
339 roleManager.removeTemplatedRole( "archiva-repository-observer", existingRepository.getId() );
341 getLogger().debug( "removed user roles associated with repository " + existingRepository.getId() );
344 private String saveConfiguration( Configuration configuration )
345 throws IOException, InvalidConfigurationException, RegistryException
349 archivaConfiguration.save( configuration );
350 addActionMessage( "Successfully saved configuration" );
352 catch ( IndeterminateConfigurationException e )
354 addActionError( e.getMessage() );
361 public void setRoleManager( RoleManager roleManager )
363 this.roleManager = roleManager;
366 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
368 this.archivaConfiguration = archivaConfiguration;