001/*
002 * (C) Copyright 2014 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 *     tdelprat
018 */
019package org.nuxeo.ecm.restapi.jaxrs.io.types;
020
021import java.io.IOException;
022import java.io.OutputStream;
023import java.lang.annotation.Annotation;
024import java.lang.reflect.Type;
025
026import javax.ws.rs.Produces;
027import javax.ws.rs.core.MediaType;
028import javax.ws.rs.core.MultivaluedMap;
029import javax.ws.rs.ext.MessageBodyWriter;
030import javax.ws.rs.ext.Provider;
031
032import org.codehaus.jackson.JsonGenerator;
033import org.nuxeo.ecm.core.schema.DocumentType;
034
035@Provider
036@Produces(MediaType.APPLICATION_JSON)
037public class DocumentTypeWriter extends AbstractTypeDefWriter implements MessageBodyWriter<DocumentType> {
038
039    @Override
040    public void writeTo(DocumentType docType, Class<?> type, Type genericType, Annotation[] annotations,
041            MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
042            throws IOException {
043        JsonGenerator jg = getGenerator(entityStream);
044        jg.writeStartObject();
045        writeDocType(jg, docType, true);
046        jg.writeEndObject();
047        // flush
048        jg.flush();
049        jg.close();
050        entityStream.flush();
051    }
052
053    @Override
054    public long getSize(DocumentType arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4) {
055        return -1;
056    }
057
058    @Override
059    public boolean isWriteable(Class<?> arg0, Type type, Annotation[] arg2, MediaType arg3) {
060        return DocumentType.class.isAssignableFrom(arg0);
061    }
062
063}