You can create your own API Endpoints using BellaDati Extensions feature. This tutorial describes how to create an endpoint which exports public Report to XLS.
You can download complete Extension with an endpoint here and import it to BellaDati Extensions. Refer to Importing Extensions section in Extensions documentation.
After creating the Extension you should be redirected to Extension detail.
After creating the Endpoint you should be redirected to Endpoint detail with code editor where you can write/paste your code.
In the Endpoint class are 2 methods, doGet() and doPost(). Each will be executed based on the endpoint call method (GET/POST).
We will call this endpoint as GET so we will write the code in the doGet() method. Refer to the code below:
package com.example; import com.belladati.extension.BaseExtensionEndpoint; import org.apache.tapestry5.StreamResponse; public class MyEndpoint extends BaseExtensionEndpoint { @Override public StreamResponse doGet() { try { // get parameter value from URL def reportIdParameter = getParameter("reportId"); int reportId = reportIdParameter.toInteger(); // export Report to xls File xlsFile = exportToXLS(reportId); InputStream is = new FileInputStream(xlsFile); /** * @param is data which are sent to client * @param mimeType MIME type of data * @param responseStatusCode response status */ // createStreamResponse( InputStream is, String mimeType, int responseStatusCode) // send Report exported as xls to client return createStreamResponse(is, "application/vnd.ms-excel", 200); } catch (Exception e) { return createStreamResponse(e); } } @Override public StreamResponse doPost() { // TODO Auto-generated method stub. Please insert here your code for POST operation. return null; } } |
Extension has to be activated before the endpoint can be used. We also have to activate Public access so public users will be able to use the endpoint.
Endpoint is now ready to use so we can try it.
Clicking on Link above the code editor will open URL with our endpoint. We just have to append parameter "reportId" with ID of Report which should be exported (refer to implementation above).
Your URL should look like this:
https://service.belladati.com/extension/detail:endpoint/104/exportReportToXLS?reportId=12345 |
Example usage in HTML:
<!--Using download attribute--> <!--Clicking on the button will open "Save as" prompt--> <a href="/extension/detail:endpoint/104/exportReportToXLS?reportId=12345" download="Revenue_March2017.xls" > <div class="my-button"> Export to XLS </div> </a> |