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