001/* 002 * (C) Copyright 2015-2019 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 * Nicolas Chapurlat <nchapurlat@nuxeo.com> 018 */ 019 020package org.nuxeo.ecm.core.io.marshallers.json.types; 021 022import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE; 023import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON; 024import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE; 025 026import java.io.IOException; 027import java.io.OutputStream; 028 029import org.nuxeo.ecm.core.io.marshallers.json.ExtensibleEntityJsonWriter; 030import org.nuxeo.ecm.core.io.marshallers.json.OutputStreamWithJsonWriter; 031import org.nuxeo.ecm.core.io.marshallers.json.enrichers.AbstractJsonEnricher; 032import org.nuxeo.ecm.core.io.registry.Writer; 033import org.nuxeo.ecm.core.io.registry.reflect.Setup; 034import org.nuxeo.ecm.core.schema.types.CompositeType; 035import org.nuxeo.ecm.core.schema.types.Schema; 036 037import com.fasterxml.jackson.core.JsonGenerator; 038 039/** 040 * Convert {@link CompositeType} to Json. 041 * <p> 042 * This marshaller is enrichable: register class implementing {@link AbstractJsonEnricher} and managing 043 * {@link CompositeType}. 044 * <p> 045 * This marshaller is also extensible: extend it and simply override 046 * {@link ExtensibleEntityJsonWriter#extend(Object, JsonGenerator)}. 047 * <p> 048 * Format is: 049 * 050 * <pre> 051 * { 052 * "entity-type":"facet", 053 * "name": "FACET_NAME", 054 * "schemas": [ { see {@link SchemaJsonWriter} for format }, { ... }, ... ], 055 * <-- contextParameters if there are enrichers activated 056 * <-- additional property provided by extend() method 057 * } 058 * </pre> 059 * 060 * @since 7.2 061 */ 062@Setup(mode = SINGLETON, priority = REFERENCE) 063public class FacetJsonWriter extends ExtensibleEntityJsonWriter<CompositeType> { 064 065 public static final String ENTITY_TYPE = "facet"; 066 067 public FacetJsonWriter() { 068 super(ENTITY_TYPE, CompositeType.class); 069 } 070 071 @Override 072 protected void writeEntityBody(CompositeType facet, JsonGenerator jg) throws IOException { 073 jg.writeStringField("name", facet.getName()); 074 if (facet.getSchemaNames() != null && facet.getSchemaNames().length > 0) { 075 jg.writeArrayFieldStart("schemas"); 076 Writer<Schema> schemaWriter = registry.getWriter(ctx, Schema.class, APPLICATION_JSON_TYPE); 077 for (Schema schema : facet.getSchemas()) { 078 OutputStream out = new OutputStreamWithJsonWriter(jg); 079 schemaWriter.write(schema, Schema.class, Schema.class, APPLICATION_JSON_TYPE, out); 080 } 081 jg.writeEndArray(); 082 } 083 } 084 085}