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.configuration.ArchivaConfiguration; 025import org.apache.archiva.configuration.Configuration; 026import org.apache.archiva.configuration.IndeterminateConfigurationException; 027import org.apache.archiva.metadata.model.facets.AuditEvent; 028import org.apache.archiva.redback.users.User; 029import org.apache.archiva.redback.components.registry.Registry; 030import org.apache.archiva.repository.events.AuditListener; 031import org.modelmapper.ModelMapper; 032import org.modelmapper.convention.MatchingStrategies; 033import org.slf4j.Logger; 034import org.slf4j.LoggerFactory; 035import org.springframework.beans.factory.annotation.Autowired; 036 037import javax.inject.Inject; 038import javax.inject.Named; 039import java.util.ArrayList; 040import java.util.List; 041 042/** 043 * @author Olivier Lamy 044 * @since 1.4-M1 045 */ 046public abstract class AbstractRepositoryAdmin 047{ 048 protected Logger log = LoggerFactory.getLogger( getClass() ); 049 050 @Inject 051 @Autowired(required = false) 052 private List<AuditListener> auditListeners = new ArrayList<>(); 053 054 @Inject 055 private RepositoryCommonValidator repositoryCommonValidator; 056 057 @Inject 058 private ArchivaConfiguration archivaConfiguration; 059 060 @Inject 061 @Named(value = "commons-configuration") 062 private Registry registry; 063 064 protected void triggerAuditEvent( String repositoryId, String resource, String action, 065 AuditInformation auditInformation ) 066 { 067 User user = auditInformation == null ? null : auditInformation.getUser(); 068 AuditEvent event = new AuditEvent( repositoryId, user == null ? "null" : user.getUsername(), resource, action ); 069 event.setRemoteIP( auditInformation == null ? "null" : auditInformation.getRemoteAddr() ); 070 071 for ( AuditListener listener : getAuditListeners() ) 072 { 073 listener.auditEvent( event ); 074 } 075 076 } 077 078 protected void saveConfiguration( Configuration config ) 079 throws RepositoryAdminException 080 { 081 try 082 { 083 getArchivaConfiguration().save( config ); 084 } 085 catch ( org.apache.archiva.redback.components.registry.RegistryException e ) 086 { 087 throw new RepositoryAdminException( "Error occurred in the registry: " + e.getLocalizedMessage(), e ); 088 } 089 catch ( IndeterminateConfigurationException e ) 090 { 091 throw new RepositoryAdminException( 092 "Error occurred while saving the configuration: " + e.getLocalizedMessage(), e ); 093 } 094 } 095 096 private static class ModelMapperHolder 097 { 098 private static ModelMapper MODEL_MAPPER = new ModelMapper(); 099 100 static 101 { 102 MODEL_MAPPER.getConfiguration().setMatchingStrategy( MatchingStrategies.STRICT ); 103 } 104 105 } 106 107 protected ModelMapper getModelMapper() 108 { 109 return ModelMapperHolder.MODEL_MAPPER; 110 } 111 112 public List<AuditListener> getAuditListeners() 113 { 114 return auditListeners; 115 } 116 117 public void setAuditListeners( List<AuditListener> auditListeners ) 118 { 119 this.auditListeners = auditListeners; 120 } 121 122 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration ) 123 { 124 this.archivaConfiguration = archivaConfiguration; 125 } 126 127 public ArchivaConfiguration getArchivaConfiguration() 128 { 129 return archivaConfiguration; 130 } 131 132 public RepositoryCommonValidator getRepositoryCommonValidator() 133 { 134 return repositoryCommonValidator; 135 } 136 137 public void setRepositoryCommonValidator( RepositoryCommonValidator repositoryCommonValidator ) 138 { 139 this.repositoryCommonValidator = repositoryCommonValidator; 140 } 141 142 public Registry getRegistry() 143 { 144 return registry; 145 } 146 147 public void setRegistry( org.apache.archiva.redback.components.registry.Registry registry ) 148 { 149 this.registry = registry; 150 } 151}