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.xwork2.Preparable;
23 import org.apache.archiva.admin.repository.RepositoryAdminException;
24 import org.apache.archiva.admin.repository.managed.ManagedRepository;
25 import org.apache.archiva.admin.repository.managed.ManagedRepositoryAdmin;
26 import org.apache.archiva.admin.repository.remote.RemoteRepository;
27 import org.apache.archiva.admin.repository.remote.RemoteRepositoryAdmin;
28 import org.apache.archiva.admin.repository.utils.RepositoryComparator;
29 import org.apache.archiva.metadata.repository.MetadataRepository;
30 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
31 import org.apache.archiva.metadata.repository.RepositorySession;
32 import org.apache.archiva.metadata.repository.stats.RepositoryStatistics;
33 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
34 import org.apache.archiva.security.common.ArchivaRoleConstants;
35 import org.apache.archiva.web.util.ContextUtils;
36 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
37 import org.apache.maven.archiva.configuration.Configuration;
38 import org.apache.maven.archiva.web.action.AbstractActionSupport;
39 import org.apache.struts2.interceptor.ServletRequestAware;
40 import org.codehaus.plexus.redback.rbac.Resource;
41 import org.codehaus.redback.integration.interceptor.SecureAction;
42 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
43 import org.codehaus.redback.integration.interceptor.SecureActionException;
44 import org.springframework.context.annotation.Scope;
45 import org.springframework.stereotype.Controller;
47 import javax.inject.Inject;
48 import javax.servlet.http.HttpServletRequest;
49 import java.util.ArrayList;
50 import java.util.Collections;
51 import java.util.HashMap;
52 import java.util.List;
56 * Shows the Repositories Tab for the administrator.
60 @Controller( "repositoriesAction" )
62 public class RepositoriesAction
63 extends AbstractActionSupport
64 implements SecureAction, ServletRequestAware, Preparable
68 private ArchivaConfiguration archivaConfiguration;
71 private ManagedRepositoryAdmin managedRepositoryAdmin;
74 private RemoteRepositoryAdmin remoteRepositoryAdmin;
76 private List<ManagedRepository> managedRepositories;
78 private List<RemoteRepository> remoteRepositories;
80 private Map<String, RepositoryStatistics> repositoryStatistics;
82 private Map<String, List<String>> repositoryToGroupMap;
85 * Used to construct the repository WebDAV URL in the repository action.
87 private String baseUrl;
91 private RepositoryStatisticsManager repositoryStatisticsManager;
93 public void setServletRequest( HttpServletRequest request )
95 // TODO: is there a better way to do this?
96 this.baseUrl = ContextUtils.getBaseURL( request, "repository" );
99 public SecureActionBundle getSecureActionBundle()
100 throws SecureActionException
102 SecureActionBundle bundle = new SecureActionBundle();
104 bundle.setRequiresAuthentication( true );
105 bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION, Resource.GLOBAL );
110 @SuppressWarnings( "unchecked" )
111 public void prepare()
112 throws RepositoryAdminException
114 Configuration config = archivaConfiguration.getConfiguration();
116 remoteRepositories = new ArrayList<RemoteRepository>( getRemoteRepositoryAdmin().getRemoteRepositories() );
117 managedRepositories = new ArrayList<ManagedRepository>( getManagedRepositoryAdmin().getManagedRepositories() );
118 repositoryToGroupMap = config.getRepositoryToGroupMap();
120 Collections.sort( managedRepositories, new RepositoryComparator() );
121 Collections.sort( remoteRepositories, new RepositoryComparator() );
123 repositoryStatistics = new HashMap<String, RepositoryStatistics>();
124 RepositorySession repositorySession = repositorySessionFactory.createSession();
127 MetadataRepository metadataRepository = repositorySession.getRepository();
128 for ( ManagedRepository repo : managedRepositories )
130 RepositoryStatistics stats = null;
133 stats = repositoryStatisticsManager.getLastStatistics( metadataRepository, repo.getId() );
135 catch ( MetadataRepositoryException e )
138 "Error retrieving statistics for repository " + repo.getId() + " - consult application logs" );
139 log.warn( "Error retrieving repository statistics: " + e.getMessage(), e );
143 repositoryStatistics.put( repo.getId(), stats );
149 repositorySession.close();
153 public List<ManagedRepository> getManagedRepositories()
155 List<ManagedRepository> managedRepositoriesList = new ArrayList<ManagedRepository>();
156 for ( ManagedRepository repoConfig : managedRepositories )
158 if ( !repoConfig.getId().endsWith( "-stage" ) )
160 managedRepositoriesList.add( repoConfig );
163 return managedRepositoriesList;
166 public List<RemoteRepository> getRemoteRepositories()
168 return remoteRepositories;
171 public Map<String, RepositoryStatistics> getRepositoryStatistics()
173 return repositoryStatistics;
176 public String getBaseUrl()
181 public Map<String, List<String>> getRepositoryToGroupMap()
183 return repositoryToGroupMap;
186 public ManagedRepositoryAdmin getManagedRepositoryAdmin()
188 return managedRepositoryAdmin;
191 public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin )
193 this.managedRepositoryAdmin = managedRepositoryAdmin;
196 public RemoteRepositoryAdmin getRemoteRepositoryAdmin()
198 return remoteRepositoryAdmin;
201 public void setRemoteRepositoryAdmin( RemoteRepositoryAdmin remoteRepositoryAdmin )
203 this.remoteRepositoryAdmin = remoteRepositoryAdmin;