001/*
002 * (C) Copyright 2015-2017 Nuxeo (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 *     Nicolas Chapurlat <nchapurlat@nuxeo.com>
018 */
019
020package org.nuxeo.ecm.platform.audit.io;
021
022import static org.nuxeo.common.utils.DateUtils.formatISODateTime;
023import static org.nuxeo.common.utils.DateUtils.nowIfNull;
024import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
025import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE;
026import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_CATEGORY;
027import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_COMMENT;
028import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_DOC_LIFE_CYCLE;
029import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_DOC_PATH;
030import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_DOC_TYPE;
031import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_DOC_UUID;
032import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_EVENT_DATE;
033import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_EVENT_ID;
034import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_EXTENDED;
035import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_ID;
036import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_LOG_DATE;
037import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_PRINCIPAL_NAME;
038import static org.nuxeo.ecm.platform.audit.api.BuiltinLogEntryData.LOG_REPOSITORY_ID;
039
040import java.io.IOException;
041import java.io.Serializable;
042import java.util.Date;
043import java.util.List;
044import java.util.Map;
045import java.util.Map.Entry;
046
047import org.nuxeo.ecm.core.api.Blob;
048import org.nuxeo.ecm.core.io.marshallers.json.ExtensibleEntityJsonWriter;
049import org.nuxeo.ecm.core.io.marshallers.json.enrichers.AbstractJsonEnricher;
050import org.nuxeo.ecm.core.io.registry.reflect.Setup;
051import org.nuxeo.ecm.platform.audit.api.ExtendedInfo;
052import org.nuxeo.ecm.platform.audit.api.LogEntry;
053
054import com.fasterxml.jackson.core.JsonGenerator;
055
056/**
057 * Convert {@link LogEntry} to Json.
058 * <p>
059 * This marshaller is enrichable: register class implementing {@link AbstractJsonEnricher} and managing {@link LogEntry}
060 * .
061 * <p>
062 * This marshaller is also extensible: extend it and simply override
063 * {@link ExtensibleEntityJsonWriter#extend(Object, JsonGenerator)}.
064 * <p>
065 * Format is:
066 *
067 * <pre>
068 * {@code
069 * {
070 *   "entity-type":"logEntry",
071 *   "category": "LOG_ENTRY_CATEGORY",
072 *   "principalName": "LOG_ENTRY_PRINCIPAL",
073 *   "comment": "LOG_ENTRY_COMMENT",
074 *   "docLifeCycle": "DOC_LIFECYCLE",
075 *   "docPath": "DOC_PATH",
076 *   "docType": "DOC_TYPE",
077 *   "docUUID": "DOC_UUID",
078 *   "eventId": "EVENT_ID",
079 *   "repositoryId": "REPO_ID",
080 *   "eventDate": "LOG_EVENT_DATE",
081 *   "logDate": "LOG_DATE"
082 *             <-- contextParameters if there are enrichers activated
083 *             <-- additional property provided by extend() method
084 * }
085 * }
086 * </pre>
087 *
088 * @since 7.2
089 */
090@Setup(mode = SINGLETON, priority = REFERENCE)
091public class LogEntryJsonWriter extends ExtensibleEntityJsonWriter<LogEntry> {
092
093    public static final String ENTITY_TYPE = "logEntry";
094
095    public LogEntryJsonWriter() {
096        super(ENTITY_TYPE, LogEntry.class);
097    }
098
099    @Override
100    protected void writeEntityBody(LogEntry logEntry, JsonGenerator jg) throws IOException {
101        jg.writeNumberField(LOG_ID, logEntry.getId());
102        jg.writeStringField(LOG_CATEGORY, logEntry.getCategory());
103        jg.writeStringField(LOG_PRINCIPAL_NAME, logEntry.getPrincipalName());
104        jg.writeStringField(LOG_COMMENT, logEntry.getComment());
105        jg.writeStringField(LOG_DOC_LIFE_CYCLE, logEntry.getDocLifeCycle());
106        jg.writeStringField(LOG_DOC_PATH, logEntry.getDocPath());
107        jg.writeStringField(LOG_DOC_TYPE, logEntry.getDocType());
108        jg.writeStringField(LOG_DOC_UUID, logEntry.getDocUUID());
109        jg.writeStringField(LOG_EVENT_ID, logEntry.getEventId());
110        jg.writeStringField(LOG_REPOSITORY_ID, logEntry.getRepositoryId());
111        jg.writeStringField(LOG_EVENT_DATE, formatISODateTime(nowIfNull(logEntry.getEventDate())));
112        jg.writeStringField(LOG_LOG_DATE, formatISODateTime(nowIfNull(logEntry.getLogDate())));
113        writeExtendedInfos(jg, logEntry);
114    }
115
116    protected void writeExtendedInfos(JsonGenerator jg, LogEntry logEntry) throws IOException {
117        Map<String, ExtendedInfo> extended = logEntry.getExtendedInfos();
118        jg.writeObjectFieldStart(LOG_EXTENDED);
119        for (String key : extended.keySet()) {
120            ExtendedInfo ei = extended.get(key);
121            if (ei != null && ei.getSerializableValue() != null) {
122                writeExtendedInfo(jg, key, ei.getSerializableValue());
123            } else {
124                jg.writeNullField(key);
125            }
126        }
127        jg.writeEndObject();
128    }
129
130    protected void writeExtendedInfo(JsonGenerator jg, String key, Serializable value) throws IOException {
131        Class<?> clazz = value.getClass();
132        if (Long.class.isAssignableFrom(clazz)) {
133            jg.writeNumberField(key, (Long) value);
134        } else if (Integer.class.isAssignableFrom(clazz)) {
135            jg.writeNumberField(key, (Integer) value);
136        } else if (Double.class.isAssignableFrom(clazz)) {
137            jg.writeNumberField(key, (Double) value);
138        } else if (Date.class.isAssignableFrom(clazz)) {
139            jg.writeStringField(key, formatISODateTime((Date) value));
140        } else if (String.class.isAssignableFrom(clazz)) {
141            jg.writeStringField(key, (String) value);
142        } else if (Boolean.class.isAssignableFrom(clazz)) {
143            jg.writeBooleanField(key, (Boolean) value);
144        } else if (clazz.isArray() || List.class.isAssignableFrom(clazz)) {
145            jg.writeObjectField(key, value);
146        } else if (Map.class.isAssignableFrom(clazz)) {
147            @SuppressWarnings("unchecked")
148            Map<String, Serializable> map = (Map<String, Serializable>) value;
149            jg.writeObjectFieldStart(key);
150            for (Entry<String, Serializable> entry : map.entrySet()) {
151                Serializable v = entry.getValue();
152                if (v != null && !(v instanceof Blob)) {
153                    writeExtendedInfo(jg, entry.getKey(), v);
154                }
155            }
156            jg.writeEndObject();
157        } else {
158            // mainly blobs
159            jg.writeStringField(key, value.toString());
160        }
161    }
162
163}