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.JsonParser;
024import com.fasterxml.jackson.databind.DeserializationContext;
025import com.fasterxml.jackson.databind.JsonDeserializer;
026import com.fasterxml.jackson.databind.JsonNode;
027import com.fasterxml.jackson.databind.ObjectMapper;
028import com.sun.jersey.core.util.Base64;
029import org.apache.commons.lang3.SerializationUtils;
030import org.nuxeo.ecm.platform.audit.api.ExtendedInfo;
031import org.nuxeo.ecm.platform.audit.impl.ExtendedInfoImpl;
032
033import java.io.IOException;
034import java.time.Instant;
035import java.time.format.DateTimeParseException;
036import java.util.Date;
037
038/**
039 * Deserializer class for extended info from a JSON object
040 * 
041 * @since 9.3
042 */
043public class ExtendedInfoDeserializer extends JsonDeserializer<ExtendedInfo> {
044
045    @Override
046    public ExtendedInfo deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
047
048        ExtendedInfo info;
049        ObjectMapper mapper = (ObjectMapper) jp.getCodec();
050        JsonNode node = mapper.readTree(jp);
051        switch (node.getNodeType()) {
052        case STRING:
053            String value = node.textValue();
054            try {
055                Date date = Date.from(Instant.parse(value));
056                info = new ExtendedInfoImpl.DateInfo(date);
057            } catch (DateTimeParseException e) {
058                info = new ExtendedInfoImpl.StringInfo(value);
059            }
060            break;
061        case BOOLEAN:
062            info = new ExtendedInfoImpl.BooleanInfo(node.booleanValue());
063            break;
064        case NUMBER:
065            Number number = node.numberValue();
066            if (number instanceof Double) {
067                info = new ExtendedInfoImpl.DoubleInfo(node.doubleValue());
068            } else {
069                info = new ExtendedInfoImpl.LongInfo(node.longValue());
070            }
071            break;
072        case BINARY:
073            info = new ExtendedInfoImpl.BlobInfo(SerializationUtils.deserialize(Base64.decode(node.binaryValue())));
074            break;
075        case ARRAY:
076        case OBJECT:
077            info = new ExtendedInfoImpl.StringInfo(mapper.writeValueAsString(node));
078            break;
079        default:
080            throw new UnsupportedOperationException("Error when deserializing type: " + node.getNodeType());
081        }
082        return info;
083    }
084
085}