001/* 002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors. 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 <nchapurlat@nuxeo.com> 016 */ 017 018package org.nuxeo.ecm.core.io.marshallers.json.enrichers; 019 020import java.io.IOException; 021import java.lang.reflect.Type; 022 023import javax.ws.rs.core.MediaType; 024 025import org.codehaus.jackson.JsonGenerator; 026import org.nuxeo.ecm.core.io.marshallers.json.AbstractJsonWriter; 027import org.nuxeo.ecm.core.io.marshallers.json.ExtensibleEntityJsonWriter; 028 029/** 030 * Base class to write {@link ExtensibleEntityJsonWriter}'s enricher. 031 * 032 * @param <EntityType> The Java type whose the generated JSON will be enriched. 033 * @since 7.2 034 */ 035public abstract class AbstractJsonEnricher<EntityType> extends AbstractJsonWriter<Enriched<EntityType>> { 036 037 public static final String ENTITY_ENRICHER_NAME = "_EntityEnricherName"; 038 039 private final String name; 040 041 public AbstractJsonEnricher(String name) { 042 this.name = name; 043 } 044 045 @Override 046 public final boolean accept(Class<?> clazz, Type genericType, MediaType mediatype) { 047 return name.equals(ctx.<String> getParameter(ENTITY_ENRICHER_NAME)); 048 } 049 050 @Override 051 public void write(Enriched<EntityType> enrichable, JsonGenerator jg) throws IOException { 052 write(jg, enrichable.getEntity()); 053 } 054 055 /** 056 * When implementing this method, the provided {@link JsonGenerator} expect you write a field name and a field value 057 * (or many). 058 * 059 * @param jg The {@link JsonGenerator} to use. 060 * @param enriched The enriched entity. 061 * @since 7.2 062 */ 063 public abstract void write(JsonGenerator jg, EntityType enriched) throws IOException; 064 065}