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