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.ActionContext;
23 import com.opensymphony.xwork.Preparable;
24 import org.apache.commons.io.FileUtils;
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.maven.archiva.common.utils.PathUtil;
27 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
28 import org.apache.maven.archiva.configuration.Configuration;
29 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
30 import org.apache.maven.archiva.configuration.InvalidConfigurationException;
31 import org.apache.maven.archiva.configuration.RepositoryConfiguration;
32 import org.apache.maven.archiva.security.ArchivaRoleConstants;
33 import org.codehaus.plexus.redback.authorization.AuthorizationException;
34 import org.codehaus.plexus.redback.authorization.AuthorizationResult;
35 import org.codehaus.plexus.redback.rbac.Resource;
36 import org.codehaus.plexus.redback.role.RoleManager;
37 import org.codehaus.plexus.redback.role.RoleManagerException;
38 import org.codehaus.plexus.redback.system.SecuritySession;
39 import org.codehaus.plexus.redback.system.SecuritySystem;
40 import org.codehaus.plexus.redback.xwork.interceptor.SecureAction;
41 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
42 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionException;
43 import org.codehaus.plexus.registry.RegistryException;
44 import org.codehaus.plexus.scheduler.CronExpressionValidator;
45 import org.codehaus.plexus.xwork.action.PlexusActionSupport;
48 import java.io.IOException;
51 * Configures the application repositories.
53 * @plexus.component role="com.opensymphony.xwork.Action" role-hint="configureRepositoryAction"
55 public class ConfigureRepositoryAction
56 extends PlexusActionSupport
57 implements Preparable, SecureAction
60 * @plexus.requirement role-hint="default"
62 private RoleManager roleManager;
67 private SecuritySystem securitySystem;
72 private ArchivaConfiguration archivaConfiguration;
74 private String repoid;
79 * The model for this action.
81 private AdminRepositoryConfiguration repository;
85 getLogger().info( ".add()" );
91 public String confirm()
93 getLogger().info( ".confirm()" );
95 if ( operationAllowed( ArchivaRoleConstants.OPERATION_DELETE_REPOSITORY, getRepoid() ) )
98 "You do not have the appropriate permissions to delete the " + getRepoid() + " repository." );
105 public String delete()
107 getLogger().info( ".delete()" );
109 if ( operationAllowed( ArchivaRoleConstants.OPERATION_DELETE_REPOSITORY, getRepoid() ) )
112 "You do not have the appropriate permissions to delete the " + getRepoid() + " repository." );
116 String result = SUCCESS;
117 if ( StringUtils.equals( mode, "delete-entry" ) || StringUtils.equals( mode, "delete-contents" ) )
119 AdminRepositoryConfiguration existingRepository = getRepository();
120 if ( existingRepository == null )
122 addActionError( "A repository with that id does not exist" );
126 // TODO: remove from index too!
130 removeRepository( getRepoid() );
131 result = saveConfiguration();
133 if ( result.equals( SUCCESS ) )
135 removeRepositoryRoles( existingRepository );
136 if ( StringUtils.equals( mode, "delete-contents" ) )
138 removeContents( existingRepository );
142 catch ( IOException e )
144 addActionError( "Unable to delete repository: " + e.getMessage() );
147 catch ( RoleManagerException e )
149 addActionError( "Unable to delete repository: " + e.getMessage() );
152 catch ( InvalidConfigurationException e )
154 addActionError( "Unable to delete repository: " + e.getMessage() );
157 catch ( RegistryException e )
159 addActionError( "Unable to delete repository: " + e.getMessage() );
169 getLogger().info( ".edit()" );
172 if ( operationAllowed( ArchivaRoleConstants.OPERATION_EDIT_REPOSITORY, getRepoid() ) )
174 addActionError( "You do not have the appropriate permissions to edit the " + getRepoid() + " repository." );
181 public String getMode()
186 public String getRepoid()
191 public AdminRepositoryConfiguration getRepository()
196 public SecureActionBundle getSecureActionBundle()
197 throws SecureActionException
199 SecureActionBundle bundle = new SecureActionBundle();
201 bundle.setRequiresAuthentication( true );
202 bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION, Resource.GLOBAL );
207 public void prepare()
210 String id = getRepoid();
213 this.repository = new AdminRepositoryConfiguration();
216 RepositoryConfiguration repoconfig = archivaConfiguration.getConfiguration().findRepositoryById( id );
217 if ( repoconfig != null )
219 this.repository = new AdminRepositoryConfiguration( repoconfig );
225 String mode = getMode();
226 String repoId = getRepository().getId();
227 boolean containsError = false;
229 getLogger().info( ".save(" + mode + ":" + repoId + ")" );
231 containsError = validateFields( mode );
233 if ( containsError && StringUtils.equalsIgnoreCase( "add", mode ) )
237 else if ( containsError && StringUtils.equalsIgnoreCase( "edit", mode ) )
242 if ( StringUtils.equalsIgnoreCase( "edit", mode ) )
244 removeRepository( repoId );
250 addRepository( getRepository() );
251 result = saveConfiguration();
253 catch ( IOException e )
255 addActionError( "I/O Exception: " + e.getMessage() );
258 catch ( RoleManagerException e )
260 addActionError( "Role Manager Exception: " + e.getMessage() );
263 catch ( InvalidConfigurationException e )
265 addActionError( "Invalid Configuration Exception: " + e.getMessage() );
268 catch ( RegistryException e )
270 addActionError( "Configuration Registry Exception: " + e.getMessage() );
277 private boolean validateFields( String mode )
279 boolean containsError = false;
280 CronExpressionValidator validator = new CronExpressionValidator();
281 Configuration config = archivaConfiguration.getConfiguration();
282 String repoId = getRepository().getId();
284 if ( StringUtils.isBlank( repoId ) )
286 addFieldError( "repository.id", "You must enter a repository identifier." );
287 containsError = true;
289 //if edit mode, do not validate existence of repoId
290 else if ( config.findRepositoryById( repoId ) != null && !StringUtils.equalsIgnoreCase( mode, "edit" ) )
292 addFieldError( "repository.id",
293 "Unable to add new repository with id [" + repoId + "], that id already exists." );
294 containsError = true;
297 if ( StringUtils.isBlank( repository.getUrl() ) )
300 addFieldError( "repository.url", "You must enter a directory or url." );
301 containsError = true;
303 if ( StringUtils.isBlank( repository.getName() ) )
305 addFieldError( "repository.name", "You must enter a repository name." );
306 containsError = true;
308 if ( !validator.validate( repository.getRefreshCronExpression() ) )
310 addFieldError( "repository.refreshCronExpression", "Invalid cron expression." );
311 containsError = true;
314 return containsError;
317 public void setMode( String mode )
322 public void setRepoid( String repoid )
324 this.repoid = repoid;
327 public void setRepository( AdminRepositoryConfiguration repository )
329 this.repository = repository;
332 private void addRepository( AdminRepositoryConfiguration repository )
333 throws IOException, RoleManagerException
335 getLogger().info( ".addRepository(" + repository + ")" );
337 // Fix the URL entry (could possibly be a filesystem path)
338 String rawUrlEntry = repository.getUrl();
339 if ( !rawUrlEntry.startsWith( "http://" ) )
341 repository.setUrl( PathUtil.toUrl( rawUrlEntry ) );
344 if ( repository.isManaged() )
346 // Normalize the path
347 File file = new File( repository.getDirectory() );
348 repository.setDirectory( file.getCanonicalPath() );
349 if ( !file.exists() )
352 // TODO: error handling when this fails, or is not a directory!
356 archivaConfiguration.getConfiguration().addRepository( repository );
358 // TODO: double check these are configured on start up
359 roleManager.createTemplatedRole( "archiva-repository-manager", repository.getId() );
361 roleManager.createTemplatedRole( "archiva-repository-observer", repository.getId() );
365 private boolean operationAllowed( String permission, String repoid )
367 ActionContext context = ActionContext.getContext();
368 SecuritySession securitySession = (SecuritySession) context.get( SecuritySession.ROLE );
370 AuthorizationResult authzResult;
373 authzResult = securitySystem.authorize( securitySession, permission, repoid );
375 return authzResult.isAuthorized();
377 catch ( AuthorizationException e )
379 getLogger().info( "Unable to authorize permission: " + permission + " against repo: " + repoid +
380 " due to: " + e.getMessage() );
385 private void removeContents( AdminRepositoryConfiguration existingRepository )
388 if ( existingRepository.isManaged() )
390 getLogger().info( "Removing " + existingRepository.getDirectory() );
391 FileUtils.deleteDirectory( new File( existingRepository.getDirectory() ) );
395 private void removeRepository( String repoId )
397 getLogger().info( ".removeRepository()" );
399 RepositoryConfiguration toremove = archivaConfiguration.getConfiguration().findRepositoryById( repoId );
400 if ( toremove != null )
402 archivaConfiguration.getConfiguration().removeRepository( toremove );
406 private void removeRepositoryRoles( RepositoryConfiguration existingRepository )
407 throws RoleManagerException
409 roleManager.removeTemplatedRole( "archiva-repository-manager", existingRepository.getId() );
410 roleManager.removeTemplatedRole( "archiva-repository-observer", existingRepository.getId() );
412 getLogger().info( "removed user roles associated with repository " + existingRepository.getId() );
415 private String saveConfiguration()
416 throws IOException, InvalidConfigurationException, RegistryException
418 getLogger().info( ".saveConfiguration()" );
422 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
423 addActionMessage( "Successfully saved configuration" );
425 catch ( IndeterminateConfigurationException e )
427 addActionError( e.getMessage() );