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.AbstractRepositoryConfiguration;
26 import org.apache.archiva.configuration.ArchivaConfiguration;
27 import org.apache.archiva.configuration.Configuration;
28 import org.apache.archiva.configuration.IndeterminateConfigurationException;
29 import org.apache.archiva.metadata.model.facets.AuditEvent;
30 import org.apache.archiva.redback.users.User;
31 import org.apache.archiva.redback.components.registry.Registry;
32 import org.apache.archiva.repository.Repository;
33 import org.apache.archiva.repository.events.AuditListener;
34 import org.apache.archiva.repository.features.IndexCreationFeature;
35 import org.apache.commons.lang.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.redback.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() ) );
131 protected void setBaseRepoAttributes( AbstractRepositoryConfiguration repoConfig, AbstractRepository repo) {
132 repoConfig.setId( repo.getId() );
133 repoConfig.setName( repo.getName() );
134 repoConfig.setLayout( repo.getLayout() );
135 repoConfig.setDescription( repo.getDescription() );
136 repoConfig.setIndexDir( repo.getIndexDirectory() );
137 repoConfig.setType( StringUtils.isEmpty( repo.getType() ) ? "MAVEN" : repo.getType() );
140 private static class ModelMapperHolder
142 private static ModelMapper MODEL_MAPPER = new ModelMapper();
146 MODEL_MAPPER.getConfiguration().setMatchingStrategy( MatchingStrategies.STRICT );
151 protected ModelMapper getModelMapper()
153 return ModelMapperHolder.MODEL_MAPPER;
156 public List<AuditListener> getAuditListeners()
158 return auditListeners;
161 public void setAuditListeners( List<AuditListener> auditListeners )
163 this.auditListeners = auditListeners;
166 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
168 this.archivaConfiguration = archivaConfiguration;
171 public ArchivaConfiguration getArchivaConfiguration()
173 return archivaConfiguration;
176 public RepositoryCommonValidator getRepositoryCommonValidator()
178 return repositoryCommonValidator;
181 public void setRepositoryCommonValidator( RepositoryCommonValidator repositoryCommonValidator )
183 this.repositoryCommonValidator = repositoryCommonValidator;
186 public Registry getRegistry()
191 public void setRegistry( org.apache.archiva.redback.components.registry.Registry registry )
193 this.registry = registry;