001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nicolas Chapurlat <nc@nuxeo.com>
016 *     Vladimir Pasquier <vpasquier@nuxeo.com>
017 */
018package org.nuxeo.ecm.automation.io.services.codec;
019
020import org.codehaus.jackson.JsonGenerator;
021import org.codehaus.jackson.JsonParser;
022import org.nuxeo.ecm.core.api.CoreSession;
023import org.nuxeo.ecm.core.io.marshallers.json.AbstractJsonReader;
024import org.nuxeo.ecm.core.io.marshallers.json.AbstractJsonWriter;
025import org.nuxeo.ecm.core.io.registry.MarshallerRegistry;
026import org.nuxeo.ecm.core.io.registry.Reader;
027import org.nuxeo.ecm.core.io.registry.Writer;
028import org.nuxeo.ecm.core.io.registry.context.RenderingContext;
029import org.nuxeo.runtime.api.Framework;
030
031import java.io.IOException;
032
033/**
034 * @since 7.3
035 */
036public abstract class AbstractMarshallingRegistryCodec<EntityType> extends ObjectCodec<EntityType> {
037
038    String entityType;
039    Class<? extends AbstractJsonReader<EntityType>> reader;
040    Class<? extends AbstractJsonWriter<EntityType>> writer;
041
042    public AbstractMarshallingRegistryCodec(Class<EntityType> clazz, String entityType,
043            Class<? extends AbstractJsonReader<EntityType>> reader, Class<? extends AbstractJsonWriter<EntityType>> writer) {
044        super(clazz);
045        this.entityType = entityType;
046        this.reader = reader;
047        this.writer = writer;
048    }
049
050    @Override
051    public String getType() {
052        return entityType;
053    }
054
055    @Override
056    public boolean isBuiltin() {
057        return true;
058    }
059
060    @Override
061    public void write(JsonGenerator jg, EntityType value) throws IOException {
062        MarshallerRegistry registry = Framework.getService(MarshallerRegistry.class);
063        AbstractJsonWriter<EntityType> writer = registry.getInstance(null, this.writer);
064        writer.write(value,jg);
065    }
066
067    @Override
068    public EntityType read(JsonParser jp, CoreSession session) throws
069            IOException {
070        MarshallerRegistry registry = Framework.getService(MarshallerRegistry.class);
071        RenderingContext ctx = RenderingContext.CtxBuilder.session(session).get();
072        AbstractJsonReader<EntityType> reader= registry.getInstance(ctx, this.reader);
073        return reader.read(jp.readValueAsTree());
074
075    }
076}