001/* 002 * (C) Copyright 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 * Funsho David 018 * 019 */ 020 021package org.nuxeo.ecm.platform.audit.io; 022 023import com.fasterxml.jackson.core.JsonGenerator; 024import com.fasterxml.jackson.databind.JsonSerializer; 025import com.fasterxml.jackson.databind.ObjectMapper; 026import com.fasterxml.jackson.databind.SerializerProvider; 027import org.apache.commons.codec.binary.Base64; 028import org.apache.commons.lang3.SerializationUtils; 029import org.nuxeo.ecm.platform.audit.api.ExtendedInfo; 030import org.nuxeo.ecm.platform.audit.impl.ExtendedInfoImpl; 031 032import java.io.IOException; 033import java.io.Serializable; 034import java.time.Instant; 035import java.time.format.DateTimeFormatter; 036import java.time.format.DateTimeFormatterBuilder; 037 038/** 039 * Serializer class for extended info to a JSON object 040 * 041 * @since 9.3 042 */ 043public class ExtendedInfoSerializer extends JsonSerializer<ExtendedInfo> { 044 045 @Override 046 public void serialize(ExtendedInfo info, JsonGenerator jg, 047 SerializerProvider provider) throws IOException { 048 049 ObjectMapper mapper = new ObjectMapper(); 050 jg.setCodec(mapper); 051 if (info instanceof ExtendedInfoImpl.DateInfo) { 052 ExtendedInfoImpl.DateInfo dateInfo = (ExtendedInfoImpl.DateInfo) info; 053 DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendInstant(3).toFormatter(); 054 Instant instant = dateInfo.getDateValue().toInstant(); 055 jg.writeObject(formatter.format(instant)); 056 } else if (info instanceof ExtendedInfoImpl.BlobInfo) { 057 Serializable value = ((ExtendedInfoImpl.BlobInfo) info).getBlobValue(); 058 jg.writeObject(Base64.encodeBase64(SerializationUtils.serialize(value))); 059 } else if (info instanceof ExtendedInfoImpl.StringInfo) { 060 String stringValue = ((ExtendedInfoImpl.StringInfo) info).getStringValue().trim(); 061 if ((stringValue.startsWith("{") && stringValue.endsWith("}")) 062 || (stringValue.startsWith("[") && stringValue.endsWith("]"))) { 063 try { 064 mapper.readTree(stringValue); 065 jg.writeRawValue(stringValue); 066 } catch (IOException e) { 067 // If the value represents an invalid JSON, send a null value to ES to prevent potential 068 // mapping exceptions 069 jg.writeObject(null); 070 } 071 } else { 072 jg.writeString(stringValue); 073 } 074 } else { 075 jg.writeObject(info.getSerializableValue()); 076 } 077 } 078 079}