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