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            writeString(jg, mapper, ((ExtendedInfoImpl.StringInfo) info).getStringValue().trim());
061        } else { // ESExtendedInfo or MongoDBExtendedInfo
062            Serializable value = info.getSerializableValue();
063            if (value instanceof String) {
064                writeString(jg, mapper, (String) value);
065            } else {
066                jg.writeObject(value);
067            }
068        }
069    }
070
071    private void writeString(JsonGenerator jg, ObjectMapper mapper, String stringValue) throws IOException {
072        if ((stringValue.startsWith("{") && stringValue.endsWith("}"))
073                || (stringValue.startsWith("[") && stringValue.endsWith("]"))) {
074            try {
075                mapper.readTree(stringValue);
076                jg.writeRawValue(stringValue);
077            } catch (IOException e) {
078                // If the value represents an invalid JSON, send a null value to ES to prevent potential
079                // mapping exceptions
080                jg.writeObject(null);
081            }
082        } else {
083            jg.writeString(stringValue);
084        }
085    }
086
087}