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.io.PrintStream;
017import java.lang.annotation.Annotation;
018import java.lang.reflect.Type;
019
020import javax.ws.rs.Produces;
021import javax.ws.rs.WebApplicationException;
022import javax.ws.rs.core.MediaType;
023import javax.ws.rs.core.MultivaluedMap;
024import javax.ws.rs.ext.MessageBodyWriter;
025import javax.ws.rs.ext.Provider;
026
027import org.nuxeo.ecm.automation.OperationDocumentation;
028import org.nuxeo.ecm.automation.jaxrs.io.JsonWriter;
029
030/**
031 * Writer producing html for Json Operation export.
032 *
033 * @since 5.9.4
034 */
035@Provider
036@Produces("text/html")
037public class JsonHtmlOperationWriter implements MessageBodyWriter<OperationDocumentation> {
038
039    @Override
040    public long getSize(OperationDocumentation arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4) {
041        return -1;
042    }
043
044    @Override
045    public boolean isWriteable(Class<?> arg0, Type arg1, Annotation[] arg2, MediaType arg3) {
046        return OperationDocumentation.class.isAssignableFrom(arg0);
047    }
048
049    @Override
050    public void writeTo(OperationDocumentation op, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4,
051            MultivaluedMap<String, Object> arg5, OutputStream out) throws IOException, WebApplicationException {
052        PrintStream writer = new PrintStream(out);
053        writer.print("<!DOCTYPE html>\n");
054        writer.print("<html>\n<head><title>" + op.getId() + "</title></head>\n<body>\n<pre>");
055        JsonWriter.writeOperation(out, op, true);
056        writer.print("</pre>\n</html>");
057    }
058}