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