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