001/* 002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * 018 * $Id$ 019 */ 020 021package org.nuxeo.ecm.platform.audit.io; 022 023import java.io.IOException; 024import java.io.InputStream; 025import java.io.OutputStream; 026import java.io.Serializable; 027import java.util.ArrayList; 028import java.util.Collection; 029import java.util.Collections; 030import java.util.HashMap; 031import java.util.List; 032import java.util.Map; 033 034import org.apache.commons.logging.Log; 035import org.apache.commons.logging.LogFactory; 036import org.nuxeo.ecm.core.api.CoreInstance; 037import org.nuxeo.ecm.core.api.CoreSession; 038import org.nuxeo.ecm.core.api.DocumentModel; 039import org.nuxeo.ecm.core.api.DocumentNotFoundException; 040import org.nuxeo.ecm.core.api.DocumentRef; 041import org.nuxeo.ecm.core.api.IdRef; 042import org.nuxeo.ecm.core.api.NuxeoException; 043import org.nuxeo.ecm.core.io.DocumentTranslationMap; 044import org.nuxeo.ecm.platform.audit.api.LogEntry; 045import org.nuxeo.ecm.platform.audit.api.Logs; 046import org.nuxeo.ecm.platform.io.api.AbstractIOResourceAdapter; 047import org.nuxeo.ecm.platform.io.api.IOResources; 048import org.nuxeo.runtime.api.Framework; 049 050/** 051 * Adapter for import/export of audit logs. 052 * 053 * @author <a href="mailto:dm@nuxeo.com">Dragos Mihalache</a> 054 */ 055public class IOAuditAdapter extends AbstractIOResourceAdapter { 056 057 private static final Log log = LogFactory.getLog(IOAuditAdapter.class); 058 059 private static final long serialVersionUID = -3661302796286246086L; 060 061 /** 062 * Should be overridden if IOLogEntryBase is subclassed. 063 * 064 * @return IOLogEntryBase instance that will know how to write and read log entries 065 */ 066 protected IOLogEntryBase getLogEntryHelper() { 067 return new IOLogEntryBase(); 068 } 069 070 @Override 071 public void setProperties(Map<String, Serializable> properties) { 072 } 073 074 /** 075 * Extract logs involving given documents. 076 * <p> 077 * The adapter properties will filter which logs must be taken into account. 078 */ 079 @Override 080 public IOResources extractResources(String repo, Collection<DocumentRef> sources) { 081 if (sources == null || sources.isEmpty()) { 082 return null; 083 } 084 CoreSession session = CoreInstance.getCoreSessionSystem(repo); 085 Map<DocumentRef, List<LogEntry>> docLogs = new HashMap<>(); 086 087 Logs logService = Framework.getService(Logs.class); 088 089 for (DocumentRef docRef : sources) { 090 try { 091 final String uuid; 092 if (docRef.type() == DocumentRef.ID) { 093 uuid = docRef.toString(); 094 } else { 095 DocumentModel doc = session.getDocument(docRef); 096 uuid = doc.getId(); 097 } 098 099 List<LogEntry> logEntries = logService.getLogEntriesFor(uuid, repo); 100 101 docLogs.put(docRef, logEntries); 102 } catch (DocumentNotFoundException e) { 103 List<LogEntry> emptyList = Collections.emptyList(); 104 docLogs.put(docRef, emptyList); 105 continue; 106 } 107 } 108 return new IOAuditResources(docLogs); 109 } 110 111 @Override 112 public void getResourcesAsXML(OutputStream out, IOResources resources) { 113 if (!(resources instanceof IOAuditResources)) { 114 return; 115 } 116 IOAuditResources auditResources = (IOAuditResources) resources; 117 118 List<LogEntry> logEntries = new ArrayList<>(); 119 120 Map<DocumentRef, List<LogEntry>> docLogs = auditResources.getLogsMap(); 121 122 Collection<List<LogEntry>> all = docLogs.values(); 123 for (List<LogEntry> list : all) { 124 logEntries.addAll(list); 125 } 126 127 try { 128 IOLogEntryBase.write(logEntries, out); 129 } catch (IOException e) { 130 throw new NuxeoException("Cannot write logs", e); 131 } 132 } 133 134 @Override 135 public IOResources loadResourcesFromXML(InputStream stream) { 136 List<LogEntry> allEntries; 137 try { 138 allEntries = IOLogEntryBase.read(stream); 139 } catch (IOException e) { 140 throw new NuxeoException("Cannot read entries from " + stream); 141 } 142 143 // will put each log entry to its correspondent document ref 144 Map<DocumentRef, List<LogEntry>> docLogs = new HashMap<>(); 145 for (LogEntry logEntry : allEntries) { 146 DocumentRef docRef = new IdRef(logEntry.getDocUUID()); 147 148 List<LogEntry> logEntries = docLogs.get(docRef); 149 if (logEntries == null) { 150 logEntries = new ArrayList<>(); 151 docLogs.put(docRef, logEntries); 152 } 153 logEntries.add(logEntry); 154 } 155 156 return new IOAuditResources(docLogs); 157 } 158 159 @Override 160 public void storeResources(IOResources newResources) { 161 if (!(newResources instanceof IOAuditResources)) { 162 return; 163 } 164 Logs logService = Framework.getService(Logs.class); 165 IOAuditResources auditResources = (IOAuditResources) newResources; 166 Map<DocumentRef, List<LogEntry>> docLogs = auditResources.getLogsMap(); 167 for (Map.Entry<DocumentRef, List<LogEntry>> mapEntry : docLogs.entrySet()) { 168 DocumentRef docRef = mapEntry.getKey(); 169 List<LogEntry> logs = mapEntry.getValue(); 170 // need to set the given docRef - so transfer with the help of 171 // IOLogEntryBase (subclass eventually) 172 List<LogEntry> newLogs = IOLogEntryBase.translate(logs, docRef); 173 logService.addLogEntries(newLogs); 174 } 175 } 176 177 @Override 178 public IOResources translateResources(String repo, IOResources resources, DocumentTranslationMap map) { 179 if (map == null) { 180 return null; 181 } 182 if (!(resources instanceof IOAuditResources)) { 183 return resources; 184 } 185 186 IOAuditResources auditResources = (IOAuditResources) resources; 187 Map<DocumentRef, List<LogEntry>> newResourcesMap = new HashMap<>(); 188 189 for (Map.Entry<DocumentRef, List<LogEntry>> entry : auditResources.getLogsMap().entrySet()) { 190 DocumentRef oldRef = entry.getKey(); 191 DocumentRef newRef = map.getDocRefMap().get(oldRef); 192 if (newRef == null) { 193 if (log.isErrorEnabled()) { 194 log.error("newRef does not exist in translation map for " + oldRef); 195 } 196 continue; 197 } 198 List<LogEntry> docLogs = auditResources.getDocumentLogs(oldRef); 199 200 // need to set the given docRef - so transfer with the help of 201 // IOLogEntryBase (subclass eventually) 202 List<LogEntry> newLogs = IOLogEntryBase.translate(docLogs, newRef); 203 newResourcesMap.put(newRef, newLogs); 204 } 205 206 return new IOAuditResources(newResourcesMap); 207 } 208 209}