001/* 002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * bstefanescu 011 */ 012package org.nuxeo.ecm.automation.jaxrs.io.operations; 013 014import java.io.IOException; 015import java.io.OutputStream; 016import java.lang.annotation.Annotation; 017import java.lang.reflect.Type; 018 019import javax.servlet.http.HttpServletRequest; 020import javax.ws.rs.Produces; 021import javax.ws.rs.WebApplicationException; 022import javax.ws.rs.core.Context; 023import javax.ws.rs.core.MediaType; 024import javax.ws.rs.core.MultivaluedMap; 025import javax.ws.rs.ext.MessageBodyWriter; 026import javax.ws.rs.ext.Provider; 027 028import org.nuxeo.ecm.automation.jaxrs.io.JsonWriter; 029 030/** 031 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 032 */ 033@Provider 034@Produces({ "application/json", "application/json+nxautomation" }) 035public class JsonAutomationInfoWriter implements MessageBodyWriter<AutomationInfo> { 036 037 @Context 038 protected HttpServletRequest request; 039 040 @Override 041 public long getSize(AutomationInfo arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4) { 042 return -1; 043 } 044 045 @Override 046 public boolean isWriteable(Class<?> arg0, Type arg1, Annotation[] arg2, MediaType arg3) { 047 return AutomationInfo.class.isAssignableFrom(arg0); 048 } 049 050 @Override 051 public void writeTo(AutomationInfo info, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4, 052 MultivaluedMap<String, Object> arg5, OutputStream out) throws IOException, WebApplicationException { 053 boolean pretty = Boolean.parseBoolean(request.getParameter("pretty")); 054 JsonWriter.writeAutomationInfo(out, info, pretty); 055 } 056 057}