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 java.io.IOException;
024import java.io.Serializable;
025import java.time.Instant;
026import java.time.format.DateTimeParseException;
027import java.util.Date;
028import java.util.List;
029import java.util.Map;
030
031import org.apache.commons.lang3.SerializationUtils;
032import org.nuxeo.ecm.platform.audit.api.AuditLogger;
033import org.nuxeo.ecm.platform.audit.api.ExtendedInfo;
034import org.nuxeo.runtime.api.Framework;
035
036import com.fasterxml.jackson.core.JsonParser;
037import com.fasterxml.jackson.databind.DeserializationContext;
038import com.fasterxml.jackson.databind.JsonDeserializer;
039import com.fasterxml.jackson.databind.JsonNode;
040import com.fasterxml.jackson.databind.ObjectMapper;
041import com.sun.jersey.core.util.Base64;
042
043/**
044 * Deserializer class for extended info from a JSON object
045 *
046 * @since 9.3
047 */
048public class ExtendedInfoDeserializer extends JsonDeserializer<ExtendedInfo> {
049
050    @Override
051    public ExtendedInfo deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
052
053        ObjectMapper mapper = (ObjectMapper) jp.getCodec();
054        JsonNode node = mapper.readTree(jp);
055        Serializable value;
056        switch (node.getNodeType()) {
057        case STRING:
058            value = node.textValue();
059            try {
060                value = Date.from(Instant.parse((String) value));
061            } catch (DateTimeParseException e) {
062                // ignore
063            }
064            break;
065        case BOOLEAN:
066            value = node.booleanValue();
067            break;
068        case NUMBER:
069            value = node.numberValue();
070            if (value instanceof Integer) {
071                // convert it to long, it is the original type and json can't differentiate int and long
072                value = Long.valueOf((Integer) value);
073            }
074            break;
075        case BINARY:
076            value = SerializationUtils.deserialize(Base64.decode(node.binaryValue()));
077            break;
078        case ARRAY:
079            value = (Serializable) mapper.convertValue(node, List.class);
080            break;
081        case OBJECT:
082            value = (Serializable) mapper.convertValue(node, Map.class);
083            break;
084        default:
085            throw new UnsupportedOperationException("Error when deserializing type: " + node.getNodeType());
086        }
087        return Framework.getService(AuditLogger.class).newExtendedInfo(value);
088    }
089
090}