001/*
002 * (C) Copyright 2013 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 *     Vladimir Pasquier <vpasquier@nuxeo.com>
018 */
019package org.nuxeo.ecm.automation.jaxrs.io.audit;
020
021import java.io.IOException;
022import java.io.Serializable;
023import java.util.Date;
024import java.util.Map;
025
026import javax.ws.rs.Produces;
027import javax.ws.rs.ext.Provider;
028
029import org.joda.time.DateTime;
030import org.joda.time.format.ISODateTimeFormat;
031import org.nuxeo.ecm.automation.jaxrs.io.EntityWriter;
032import org.nuxeo.ecm.platform.audit.api.ExtendedInfo;
033import org.nuxeo.ecm.platform.audit.api.LogEntry;
034import org.nuxeo.ecm.webengine.jaxrs.coreiodelegate.JsonCoreIODelegate;
035
036import com.fasterxml.jackson.core.JsonGenerator;
037
038/**
039 * @since 5.7.3 - LogEntry Writer for Audit.
040 * @deprecated since 7.10 this marshaller was migrated to org.nuxeo.ecm.platform.audit.io.LogEntryJsonWriter. To use it
041 *             in JAX-RS, register the {@link JsonCoreIODelegate} to forward the JAX-RS marshalling to nuxeo-core-io.
042 */
043@Deprecated
044@Provider
045@Produces({ "application/json+nxentity", "application/json" })
046public class LogEntryWriter extends EntityWriter<LogEntry> {
047
048    public static final String ENTITY_TYPE = "logEntry";
049
050    @Override
051    protected String getEntityType() {
052        return ENTITY_TYPE;
053    }
054
055    @Override
056    protected void writeEntityBody(JsonGenerator jg, LogEntry logEntry) throws IOException {
057        jg.writeStringField("entity-type", "logEntry");
058        jg.writeStringField("category", logEntry.getCategory());
059        jg.writeStringField("principalName", logEntry.getPrincipalName());
060        jg.writeStringField("comment", logEntry.getComment());
061        jg.writeStringField("docLifeCycle", logEntry.getDocLifeCycle());
062        jg.writeStringField("docPath", logEntry.getDocPath());
063        jg.writeStringField("docType", logEntry.getDocType());
064        jg.writeStringField("docUUID", logEntry.getDocUUID());
065        jg.writeStringField("eventId", logEntry.getEventId());
066        jg.writeStringField("repositoryId", logEntry.getRepositoryId());
067        jg.writeStringField("eventDate", ISODateTimeFormat.dateTime().print(new DateTime(logEntry.getEventDate())));
068        jg.writeNumberField("id", logEntry.getId());
069        jg.writeStringField("logDate", ISODateTimeFormat.dateTime().print(new DateTime(logEntry.getLogDate())));
070        writeExtendedInfos(jg, logEntry);
071    }
072
073    protected void writeExtendedInfos(JsonGenerator jg, LogEntry logEntry) throws IOException {
074        Map<String, ExtendedInfo> extended = logEntry.getExtendedInfos();
075        jg.writeObjectFieldStart("extended");
076        for (String key : extended.keySet()) {
077            ExtendedInfo ei = extended.get(key);
078            if (ei != null && ei.getSerializableValue() != null) {
079                writeExtendedInfo(jg, key, ei.getSerializableValue());
080            } else {
081                jg.writeNullField(key);
082            }
083        }
084        jg.writeEndObject();
085    }
086
087    protected void writeExtendedInfo(JsonGenerator jg, String key, Serializable value) throws IOException {
088        Class<?> clazz = value.getClass();
089        if (Long.class.isAssignableFrom(clazz)) {
090            jg.writeNumberField(key, (Long) value);
091        } else if (Integer.class.isAssignableFrom(clazz)) {
092            jg.writeNumberField(key, (Integer) value);
093        } else if (Double.class.isAssignableFrom(clazz)) {
094            jg.writeNumberField(key, (Double) value);
095        } else if (Date.class.isAssignableFrom(clazz)) {
096            jg.writeStringField(key, ISODateTimeFormat.dateTime().print(new DateTime(value)));
097        } else if (String.class.isAssignableFrom(clazz)) {
098            jg.writeStringField(key, (String) value);
099        } else if (Boolean.class.isAssignableFrom(clazz)) {
100            jg.writeBooleanField(key, (Boolean) value);
101        } else {
102            jg.writeStringField(key, value.toString());
103        }
104    }
105}