001package org.apache.archiva.admin.repository; 002/* 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 */ 020 021import org.apache.archiva.admin.model.AuditInformation; 022import org.apache.archiva.admin.model.RepositoryAdminException; 023import org.apache.archiva.admin.model.RepositoryCommonValidator; 024import org.apache.archiva.admin.model.beans.AbstractRepository; 025import org.apache.archiva.configuration.AbstractRepositoryConfiguration; 026import org.apache.archiva.configuration.ArchivaConfiguration; 027import org.apache.archiva.configuration.Configuration; 028import org.apache.archiva.configuration.IndeterminateConfigurationException; 029import org.apache.archiva.metadata.model.facets.AuditEvent; 030import org.apache.archiva.components.registry.Registry; 031import org.apache.archiva.redback.users.User; 032import org.apache.archiva.repository.Repository; 033import org.apache.archiva.metadata.audit.AuditListener; 034import org.apache.archiva.repository.features.IndexCreationFeature; 035import org.apache.commons.lang3.StringUtils; 036import org.modelmapper.ModelMapper; 037import org.modelmapper.convention.MatchingStrategies; 038import org.slf4j.Logger; 039import org.slf4j.LoggerFactory; 040import org.springframework.beans.factory.annotation.Autowired; 041 042import javax.inject.Inject; 043import javax.inject.Named; 044import java.net.URI; 045import java.nio.file.Paths; 046import java.util.ArrayList; 047import java.util.List; 048 049/** 050 * @author Olivier Lamy 051 * @since 1.4-M1 052 */ 053public abstract class AbstractRepositoryAdmin 054{ 055 protected Logger log = LoggerFactory.getLogger( getClass() ); 056 057 @Inject 058 @Autowired(required = false) 059 private List<AuditListener> auditListeners = new ArrayList<>(); 060 061 @Inject 062 private RepositoryCommonValidator repositoryCommonValidator; 063 064 @Inject 065 private ArchivaConfiguration archivaConfiguration; 066 067 @Inject 068 @Named(value = "commons-configuration") 069 private Registry registry; 070 071 protected void triggerAuditEvent( String repositoryId, String resource, String action, 072 AuditInformation auditInformation ) 073 { 074 User user = auditInformation == null ? null : auditInformation.getUser(); 075 AuditEvent event = new AuditEvent( repositoryId, user == null ? "null" : user.getUsername(), resource, action ); 076 event.setRemoteIP( auditInformation == null ? "null" : auditInformation.getRemoteAddr() ); 077 078 for ( AuditListener listener : getAuditListeners() ) 079 { 080 listener.auditEvent( event ); 081 } 082 083 } 084 085 protected void saveConfiguration( Configuration config ) 086 throws RepositoryAdminException 087 { 088 try 089 { 090 getArchivaConfiguration().save( config ); 091 } 092 catch ( org.apache.archiva.components.registry.RegistryException e ) 093 { 094 throw new RepositoryAdminException( "Error occurred in the registry: " + e.getLocalizedMessage(), e ); 095 } 096 catch ( IndeterminateConfigurationException e ) 097 { 098 throw new RepositoryAdminException( 099 "Error occurred while saving the configuration: " + e.getLocalizedMessage(), e ); 100 } 101 } 102 103 protected String convertUriToString( URI uri ) { 104 if (uri==null) { 105 return ""; 106 } 107 String result; 108 if (uri.getScheme()==null) { 109 result = uri.getPath(); 110 } else if ("file".equals(uri.getScheme())) { 111 result = Paths.get(uri).normalize().toString(); 112 } else { 113 result = uri.toString(); 114 } 115 log.debug("Converted uri {} -> {}", uri, result); 116 return result; 117 } 118 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())); 129 } 130 } 131 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() ); 140 } 141 142 private static class ModelMapperHolder 143 { 144 private static ModelMapper MODEL_MAPPER = new ModelMapper(); 145 146 static 147 { 148 MODEL_MAPPER.getConfiguration().setMatchingStrategy( MatchingStrategies.STRICT ); 149 } 150 151 } 152 153 protected ModelMapper getModelMapper() 154 { 155 return ModelMapperHolder.MODEL_MAPPER; 156 } 157 158 public List<AuditListener> getAuditListeners() 159 { 160 return auditListeners; 161 } 162 163 public void setAuditListeners( List<AuditListener> auditListeners ) 164 { 165 this.auditListeners = auditListeners; 166 } 167 168 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration ) 169 { 170 this.archivaConfiguration = archivaConfiguration; 171 } 172 173 public ArchivaConfiguration getArchivaConfiguration() 174 { 175 return archivaConfiguration; 176 } 177 178 public RepositoryCommonValidator getRepositoryCommonValidator() 179 { 180 return repositoryCommonValidator; 181 } 182 183 public void setRepositoryCommonValidator( RepositoryCommonValidator repositoryCommonValidator ) 184 { 185 this.repositoryCommonValidator = repositoryCommonValidator; 186 } 187 188 public Registry getRegistry() 189 { 190 return registry; 191 } 192 193 public void setRegistry( org.apache.archiva.components.registry.Registry registry ) 194 { 195 this.registry = registry; 196 } 197}