001/* 002 * (C) Copyright 2014 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 * tdelprat 016 */ 017 018package org.nuxeo.ecm.restapi.jaxrs.io.types; 019 020import java.io.IOException; 021import java.io.OutputStream; 022import java.lang.annotation.Annotation; 023import java.lang.reflect.Type; 024 025import javax.ws.rs.Produces; 026import javax.ws.rs.core.MediaType; 027import javax.ws.rs.core.MultivaluedMap; 028import javax.ws.rs.ext.MessageBodyWriter; 029import javax.ws.rs.ext.Provider; 030 031import org.codehaus.jackson.JsonGenerator; 032import org.nuxeo.ecm.core.schema.DocumentType; 033import org.nuxeo.ecm.core.schema.types.Schema; 034 035@Provider 036@Produces(MediaType.APPLICATION_JSON) 037public class DocumentTypesWriter extends AbstractTypeDefWriter implements MessageBodyWriter<DocumentTypes> { 038 039 @Override 040 public void writeTo(DocumentTypes typesDef, Class<?> type, Type genericType, Annotation[] annotations, 041 MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) 042 throws IOException { 043 JsonGenerator jg = getGenerator(entityStream); 044 045 // start root 046 jg.writeStartObject(); 047 048 // write types 049 jg.writeObjectFieldStart("doctypes"); 050 for (DocumentType doctype : typesDef.getDocTypes()) { 051 jg.writeObjectFieldStart(doctype.getName()); 052 writeDocType(jg, doctype, false); 053 jg.writeEndObject(); 054 } 055 jg.writeEndObject(); 056 057 // write schemas 058 jg.writeObjectFieldStart("schemas"); 059 for (Schema schema : typesDef.getSchemas()) { 060 writeSchema(jg, schema); 061 } 062 jg.writeEndObject(); 063 064 // end root 065 jg.writeEndObject(); 066 067 // flush 068 jg.flush(); 069 jg.close(); 070 entityStream.flush(); 071 } 072 073 @Override 074 public long getSize(DocumentTypes arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4) { 075 return -1; 076 } 077 078 @Override 079 public boolean isWriteable(Class<?> arg0, Type type, Annotation[] arg2, MediaType arg3) { 080 return DocumentTypes.class == arg0; 081 } 082 083}