001package org.apache.archiva.audit; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.archiva.metadata.model.facets.AuditEvent; 023import org.apache.archiva.metadata.repository.MetadataRepositoryException; 024import org.apache.archiva.metadata.repository.RepositorySession; 025import org.apache.archiva.metadata.repository.RepositorySessionFactory; 026import org.apache.archiva.repository.events.AuditListener; 027import org.slf4j.Logger; 028import org.slf4j.LoggerFactory; 029import org.springframework.stereotype.Service; 030 031import javax.inject.Inject; 032 033/** 034 * 035 */ 036@Service("auditListener#metadata") 037public class MetadataAuditListener 038 implements AuditListener 039{ 040 private static final Logger log = LoggerFactory.getLogger( MetadataAuditListener.class ); 041 042 /** 043 * 044 */ 045 @Inject 046 private AuditManager auditManager; 047 048 /** 049 * FIXME: this could be multiple implementations and needs to be configured. 050 * It also starts a separate session to the originator of the audit event that we may rather want to pass through. 051 */ 052 @Inject 053 private RepositorySessionFactory repositorySessionFactory; 054 055 @Override 056 public void auditEvent( AuditEvent event ) 057 { 058 // for now we only log upload events, some of the others are quite noisy 059 if ( event.getAction().equals( AuditEvent.CREATE_FILE ) || event.getAction().equals( AuditEvent.UPLOAD_FILE ) || 060 event.getAction().equals( AuditEvent.MERGING_REPOSITORIES ) ) 061 { 062 RepositorySession repositorySession = repositorySessionFactory.createSession(); 063 try 064 { 065 auditManager.addAuditEvent( repositorySession.getRepository(), event ); 066 repositorySession.save(); 067 } 068 catch ( MetadataRepositoryException e ) 069 { 070 log.warn( "Unable to write audit event to repository: {}", e.getMessage(), e ); 071 } 072 finally 073 { 074 repositorySession.close(); 075 } 076 } 077 } 078}