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