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.OperationDocumentation;
029import org.nuxeo.ecm.automation.jaxrs.io.JsonWriter;
030
031/**
032 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
033 */
034@Provider
035@Produces("application/json")
036public class JsonOperationWriter implements MessageBodyWriter<OperationDocumentation> {
037
038    @Context
039    protected HttpServletRequest request;
040
041    @Override
042    public long getSize(OperationDocumentation arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4) {
043        return -1;
044    }
045
046    @Override
047    public boolean isWriteable(Class<?> arg0, Type arg1, Annotation[] arg2, MediaType arg3) {
048        return OperationDocumentation.class.isAssignableFrom(arg0);
049    }
050
051    @Override
052    public void writeTo(OperationDocumentation op, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4,
053            MultivaluedMap<String, Object> arg5, OutputStream out) throws IOException, WebApplicationException {
054        boolean pretty = Boolean.parseBoolean(request.getParameter("pretty"));
055        JsonWriter.writeOperation(out, op, pretty);
056    }
057
058}