001/* 002 * (C) Copyright 2017 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 * Kevin Leturc 018 */ 019package org.nuxeo.mongodb.audit; 020 021import static org.nuxeo.mongodb.audit.LogEntryConstants.PROPERTY_CATEGORY; 022import static org.nuxeo.mongodb.audit.LogEntryConstants.PROPERTY_COMMENT; 023import static org.nuxeo.mongodb.audit.LogEntryConstants.PROPERTY_DOC_LIFE_CYCLE; 024import static org.nuxeo.mongodb.audit.LogEntryConstants.PROPERTY_DOC_PATH; 025import static org.nuxeo.mongodb.audit.LogEntryConstants.PROPERTY_DOC_TYPE; 026import static org.nuxeo.mongodb.audit.LogEntryConstants.PROPERTY_DOC_UUID; 027import static org.nuxeo.mongodb.audit.LogEntryConstants.PROPERTY_EVENT_DATE; 028import static org.nuxeo.mongodb.audit.LogEntryConstants.PROPERTY_EVENT_ID; 029import static org.nuxeo.mongodb.audit.LogEntryConstants.PROPERTY_EXTENDED; 030import static org.nuxeo.mongodb.audit.LogEntryConstants.PROPERTY_LOG_DATE; 031import static org.nuxeo.mongodb.audit.LogEntryConstants.PROPERTY_PRINCIPAL_NAME; 032import static org.nuxeo.mongodb.audit.LogEntryConstants.PROPERTY_REPOSITORY_ID; 033 034import java.io.Serializable; 035import java.util.HashMap; 036import java.util.List; 037import java.util.Map; 038import java.util.Map.Entry; 039 040import org.apache.commons.logging.Log; 041import org.apache.commons.logging.LogFactory; 042import org.bson.Document; 043import org.nuxeo.ecm.platform.audit.api.ExtendedInfo; 044import org.nuxeo.ecm.platform.audit.api.LogEntry; 045import org.nuxeo.ecm.platform.audit.impl.ExtendedInfoImpl; 046import org.nuxeo.ecm.platform.audit.impl.LogEntryImpl; 047import org.nuxeo.mongodb.core.MongoDBSerializationHelper; 048 049import com.mongodb.DBObject; 050 051/** 052 * Reader for MongoDB Audit. 053 * 054 * @since 9.1 055 */ 056public class MongoDBAuditEntryReader { 057 058 private static final Log log = LogFactory.getLog(MongoDBAuditEntryReader.class); 059 060 public static LogEntry read(Document doc) { 061 LogEntryImpl entry = new LogEntryImpl(); 062 for (String key : doc.keySet()) { 063 switch (key) { 064 case MongoDBSerializationHelper.MONGODB_ID: 065 entry.setId(doc.getLong(key).longValue()); 066 break; 067 case PROPERTY_CATEGORY: 068 entry.setCategory(doc.getString(key)); 069 break; 070 case PROPERTY_PRINCIPAL_NAME: 071 entry.setPrincipalName(doc.getString(key)); 072 break; 073 case PROPERTY_COMMENT: 074 entry.setComment(doc.getString(key)); 075 break; 076 case PROPERTY_DOC_LIFE_CYCLE: 077 entry.setDocLifeCycle(doc.getString(key)); 078 break; 079 case PROPERTY_DOC_PATH: 080 entry.setDocPath(doc.getString(key)); 081 break; 082 case PROPERTY_DOC_TYPE: 083 entry.setDocType(doc.getString(key)); 084 break; 085 case PROPERTY_DOC_UUID: 086 entry.setDocUUID(doc.getString(key)); 087 break; 088 case PROPERTY_EVENT_ID: 089 entry.setEventId(doc.getString(key)); 090 break; 091 case PROPERTY_REPOSITORY_ID: 092 entry.setRepositoryId(doc.getString(key)); 093 break; 094 case PROPERTY_EVENT_DATE: 095 entry.setEventDate(doc.getDate(key)); 096 break; 097 case PROPERTY_LOG_DATE: 098 entry.setLogDate(doc.getDate(key)); 099 break; 100 case PROPERTY_EXTENDED: 101 entry.setExtendedInfos(readExtendedInfo(doc.get(key, Document.class))); 102 break; 103 default: 104 log.warn("Property with key '" + key + "' is not a known LogEntry property, skip it."); 105 break; 106 } 107 } 108 return entry; 109 } 110 111 public static Map<String, ExtendedInfo> readExtendedInfo(Document extInfos) { 112 Map<String, ExtendedInfo> info = new HashMap<>(); 113 for (Entry<String, Object> entry : extInfos.entrySet()) { 114 String key = entry.getKey(); 115 Object value = entry.getValue(); 116 ExtendedInfoImpl ei; 117 if (value instanceof List || value instanceof DBObject) { 118 ei = ExtendedInfoImpl.createExtendedInfo(value.toString()); 119 } else { 120 ei = ExtendedInfoImpl.createExtendedInfo((Serializable) value); 121 } 122 info.put(key, ei); 123 } 124 return info; 125 } 126 127}