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