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 *     <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a>
016 *
017 */
018
019package org.nuxeo.ecm.restapi.server.jaxrs.routing.io;
020
021import java.io.IOException;
022import java.io.OutputStream;
023import java.lang.annotation.Annotation;
024import java.lang.reflect.Type;
025import java.util.List;
026
027import javax.servlet.http.HttpServletRequest;
028import javax.ws.rs.Produces;
029import javax.ws.rs.WebApplicationException;
030import javax.ws.rs.core.Context;
031import javax.ws.rs.core.HttpHeaders;
032import javax.ws.rs.core.MediaType;
033import javax.ws.rs.core.MultivaluedMap;
034import javax.ws.rs.core.UriInfo;
035import javax.ws.rs.ext.Provider;
036
037import org.apache.commons.logging.Log;
038import org.apache.commons.logging.LogFactory;
039import org.codehaus.jackson.JsonEncoding;
040import org.codehaus.jackson.JsonFactory;
041import org.codehaus.jackson.JsonGenerator;
042import org.nuxeo.ecm.automation.jaxrs.io.EntityListWriter;
043import org.nuxeo.ecm.platform.routing.api.DocumentRoute;
044
045/**
046 * @since 7.2
047 */
048@Provider
049@Produces({ "application/json+nxentity", "application/json" })
050public class DocumentRouteListWriter extends EntityListWriter<DocumentRoute> {
051
052    private static final Log log = LogFactory.getLog(DocumentRouteListWriter.class);
053
054    @Context
055    JsonFactory factory;
056
057    @Context
058    protected HttpHeaders headers;
059
060    @Context
061    protected HttpServletRequest request;
062
063    @Context
064    UriInfo uriInfo;
065
066    @Override
067    protected String getEntityType() {
068        return "workflows";
069    }
070
071    @Override
072    protected void writeItem(JsonGenerator jg, DocumentRoute item) throws IOException {
073        // do nothing, everything is done in #writeTo
074    }
075
076    @Override
077    public void writeTo(List<DocumentRoute> docRoutes, Class<?> type, Type genericType, Annotation[] annotations,
078            MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
079            throws IOException, WebApplicationException {
080        try {
081            JsonGenerator jg = factory.createJsonGenerator(entityStream, JsonEncoding.UTF8);
082            jg.writeStartObject();
083            jg.writeStringField("entity-type", "worflows");
084            jg.writeArrayFieldStart("entries");
085            for (DocumentRoute docRoute : docRoutes) {
086                jg.writeStartObject();
087                jg.writeStringField("entity-type", "workflow");
088                DocumentRouteWriter.writeDocumentRoute(jg, docRoute, request, uriInfo);
089                jg.writeEndObject();
090            }
091            jg.writeEndArray();
092            jg.writeEndObject();
093            jg.flush();
094        } catch (IOException e) {
095            log.error("Failed to serialize document route list", e);
096            throw new WebApplicationException(500);
097        }
098    }
099}