001/* 002 * (C) Copyright 2012-2013 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 */ 017package org.nuxeo.connect.tools.report.web; 018 019import java.io.IOException; 020import java.io.OutputStream; 021import java.util.ArrayList; 022import java.util.HashSet; 023import java.util.List; 024import javax.ws.rs.Consumes; 025import javax.ws.rs.FormParam; 026import javax.ws.rs.GET; 027import javax.ws.rs.POST; 028import javax.ws.rs.Path; 029import javax.ws.rs.Produces; 030import javax.ws.rs.WebApplicationException; 031import javax.ws.rs.core.Context; 032import javax.ws.rs.core.Response; 033import javax.ws.rs.core.StreamingOutput; 034import javax.ws.rs.core.UriInfo; 035 036import org.nuxeo.connect.tools.report.ReportRunner; 037import org.nuxeo.ecm.webengine.model.Template; 038import org.nuxeo.ecm.webengine.model.WebObject; 039import org.nuxeo.ecm.webengine.model.impl.DefaultObject; 040import org.nuxeo.runtime.api.Framework; 041 042/** 043 * The report runner exposed as a resource itself. 044 * 045 * @since 8.3 046 */ 047@WebObject(type = "report") 048public class ReportRunnerObject extends DefaultObject { 049 050 ReportRunner runner = Framework.getService(ReportRunner.class); 051 052 List<String> list = new ArrayList<>(runner.list()); 053 054 List<String> selection = new ArrayList<>(list); 055 056 @GET 057 @Produces("text/html") 058 public Template index(@Context UriInfo info) { 059 return getView("index"); 060 } 061 062 @POST 063 @Path("run") 064 @Consumes("application/x-www-form-urlencoded") 065 @Produces("text/json") 066 public Response run(@FormParam("report") List<String> reports) throws IOException { 067 selection = reports; 068 069 StreamingOutput stream = new StreamingOutput() { 070 @Override 071 public void write(OutputStream sink) throws IOException, WebApplicationException { 072 runner.run(sink, new HashSet<>(selection)); 073 } 074 }; 075 return Response.ok(stream) 076 .header("Content-Disposition", 077 "attachment; filename=nuxeo-connect-tools-report.json") 078 .build(); 079 } 080 081 public List<String> availables() { 082 return list; 083 } 084 085 public boolean isSelected(String report) { 086 return selection.contains(report); 087 } 088 089 @Override 090 public String toString() { 091 return "Report runner of " + selection; 092 } 093}