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