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