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