1 package org.apache.archiva.admin.repository;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.admin.model.AuditInformation;
22 import org.apache.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.RepositoryCommonValidator;
24 import org.apache.archiva.admin.model.beans.AbstractRepository;
25 import org.apache.archiva.configuration.model.AbstractRepositoryConfiguration;
26 import org.apache.archiva.configuration.provider.ArchivaConfiguration;
27 import org.apache.archiva.configuration.model.Configuration;
28 import org.apache.archiva.configuration.provider.IndeterminateConfigurationException;
29 import org.apache.archiva.metadata.model.facets.AuditEvent;
30 import org.apache.archiva.components.registry.Registry;
31 import org.apache.archiva.redback.users.User;
32 import org.apache.archiva.repository.Repository;
33 import org.apache.archiva.metadata.audit.AuditListener;
34 import org.apache.archiva.repository.features.IndexCreationFeature;
35 import org.apache.commons.lang3.StringUtils;
36 import org.modelmapper.ModelMapper;
37 import org.modelmapper.convention.MatchingStrategies;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
42 import javax.inject.Inject;
43 import javax.inject.Named;
45 import java.nio.file.Paths;
46 import java.util.ArrayList;
47 import java.util.List;
50 * @author Olivier Lamy
53 public abstract class AbstractRepositoryAdmin
55 protected Logger log = LoggerFactory.getLogger( getClass() );
58 @Autowired(required = false)
59 private List<AuditListener> auditListeners = new ArrayList<>();
62 private RepositoryCommonValidator repositoryCommonValidator;
65 private ArchivaConfiguration archivaConfiguration;
68 @Named(value = "commons-configuration")
69 private Registry registry;
71 protected void triggerAuditEvent( String repositoryId, String resource, String action,
72 AuditInformation auditInformation )
74 User user = auditInformation == null ? null : auditInformation.getUser();
75 AuditEvent event = new AuditEvent( repositoryId, user == null ? "null" : user.getUsername(), resource, action );
76 event.setRemoteIP( auditInformation == null ? "null" : auditInformation.getRemoteAddr() );
78 for ( AuditListener listener : getAuditListeners() )
80 listener.auditEvent( event );
85 protected void saveConfiguration( Configuration config )
86 throws RepositoryAdminException
90 getArchivaConfiguration().save( config );
92 catch ( org.apache.archiva.components.registry.RegistryException e )
94 throw new RepositoryAdminException( "Error occurred in the registry: " + e.getLocalizedMessage(), e );
96 catch ( IndeterminateConfigurationException e )
98 throw new RepositoryAdminException(
99 "Error occurred while saving the configuration: " + e.getLocalizedMessage(), e );
103 protected String convertUriToString( URI uri ) {
108 if (uri.getScheme()==null) {
109 result = uri.getPath();
110 } else if ("file".equals(uri.getScheme())) {
111 result = Paths.get(uri).normalize().toString();
113 result = uri.toString();
115 log.debug("Converted uri {} -> {}", uri, result);
119 protected void setBaseRepoAttributes( AbstractRepository adminRepo, Repository repo){
120 adminRepo.setId(repo.getId());
121 adminRepo.setName( repo.getName() );
122 adminRepo.setLayout( repo.getLayout( ) );
123 adminRepo.setDescription( repo.getDescription() );
124 adminRepo.setType(repo.getType()==null?"MAVEN": repo.getType().name());
125 if (repo.supportsFeature( IndexCreationFeature.class )) {
126 IndexCreationFeature icf = repo.getFeature( IndexCreationFeature.class ).get();
127 adminRepo.setIndexDirectory( convertUriToString( icf.getIndexPath() ) );
128 adminRepo.setPackedIndexDirectory(convertUriToString(icf.getPackedIndexPath()));
132 protected void setBaseRepoAttributes( AbstractRepositoryConfiguration repoConfig, AbstractRepository repo) {
133 repoConfig.setId( repo.getId() );
134 repoConfig.setName( repo.getName() );
135 repoConfig.setLayout( repo.getLayout() );
136 repoConfig.setDescription( repo.getDescription() );
137 repoConfig.setIndexDir( repo.getIndexDirectory() );
138 repoConfig.setPackedIndexDir(repo.getPackedIndexDirectory());
139 repoConfig.setType( StringUtils.isEmpty( repo.getType() ) ? "MAVEN" : repo.getType() );
142 private static class ModelMapperHolder
144 private static ModelMapper MODEL_MAPPER = new ModelMapper();
148 MODEL_MAPPER.getConfiguration().setMatchingStrategy( MatchingStrategies.STRICT );
153 protected ModelMapper getModelMapper()
155 return ModelMapperHolder.MODEL_MAPPER;
158 public List<AuditListener> getAuditListeners()
160 return auditListeners;
163 public void setAuditListeners( List<AuditListener> auditListeners )
165 this.auditListeners = auditListeners;
168 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
170 this.archivaConfiguration = archivaConfiguration;
173 public ArchivaConfiguration getArchivaConfiguration()
175 return archivaConfiguration;
178 public RepositoryCommonValidator getRepositoryCommonValidator()
180 return repositoryCommonValidator;
183 public void setRepositoryCommonValidator( RepositoryCommonValidator repositoryCommonValidator )
185 this.repositoryCommonValidator = repositoryCommonValidator;
188 public Registry getRegistry()
193 public void setRegistry( org.apache.archiva.components.registry.Registry registry )
195 this.registry = registry;