Expand my Community achievements bar.

SOLVED

How to display Excel, PDf files from Webservice on AEM pages

Avatar

Former Community Member

Objective: The response of a webservice is an excel (a separate call for pdf) file. I need to show this file as a link on the aem-page, and whne users click the link, the browser opens (or downloads) the file. 

Use case: On the customer page, there is a section with links to Order History (Excel file), Invoice(PDF file), Products catalog(Excel file). Clicking on each link, makes a call to webservice and fetches the respective file.

Would appreciate any thoughts on how to achieve this.

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

With Scott's inputs, my solution with code snippets.

1. From the UI, submit the action to Sling Servlet

 

<form name="importFileForm" method="get" action="/services/getData"> <input type="submit" title="Submit" value="Submit" name="bttnAction"> </form>

2. Your Servlet class        
 

public class TTIGetServlet extends SlingAllMethodsServlet { @Override protected void doGet(SlingHttpServletRequest request,SlingHttpServletResponse response) throws ServletException,IOException { ... ... String serviceurl = <<< your webservice url>>> HttpClient httpclient = HttpClients.custom().build(); generateFile(serviceurl, httpclient, request, response); RequestDispatcher dispatcher = request.getRequestDispatcher("/content/ttii/en/importfiletest.html"); dispatcher.forward(request, response); } }

3. Generate File method that pops up the file download on browser

public static void generateFile(String serviceurl, HttpClient httpclient, SlingHttpServletRequest httpRequest, SlingHttpServletResponse httpResponse) throws ClientProtocolException, IOException { HttpResponse response; HttpGet httpGet = new HttpGet(serviceURL); // Makes the call to WebService response = httpclient.execute(httpGet); // CORE LOGIC if (response!=null) { ContentType contentType = ContentType.getOrDefault(response.getEntity()); String mimeType = contentType.getMimeType(); if (mimeType.equals(MIMETYPE_JSON)) { // Out of context here... } else { // SHOW THE FILE ServletOutputStream sos = httpResponse.getOutputStream(); httpResponse.setContentType("application/vnd.ms-excel"); httpResponse.setHeader("Content-Disposition", "attachment;filename=test.xls"); BufferedHttpEntity buf = new BufferedHttpEntity(response.getEntity()); InputStream istream = buf.getContent(); sos.write(FileHelper.writeFiles(istream)); sos.flush(); } } }

View solution in original post

4 Replies

Avatar

Level 10

Write an AEM service to handle the return value (which you say is a respective file). The have the service be able to serve the file as a download. We have a community artilce that shows you to dynamically create a ZIP file of assets retrieved from the DAM. Then the file is able to be downloaded. See this article: 

https://helpx.adobe.com/experience-manager/using/downloading-dam-assets.html

So what is common with your use case is setting up the service to let a file be downloaded.  In this use case - its a ZIP of assets. In your use case - it will be files returned from the J2EE server. 

Avatar

Former Community Member

smacdonald2008 wrote...

Write an AEM service to handle the return value (which you say is a respective file). The have the service be able to serve the file as a download. We have a community artilce that shows you to dynamically create a ZIP file of assets retrieved from the DAM. Then the file is able to be downloaded. See this article: 

https://helpx.adobe.com/experience-manager/using/downloading-dam-assets.html

So what is common with your use case is setting up the service to let a file be downloaded.  In this use case - its a ZIP of assets. In your use case - it will be files returned from the J2EE server. 

 

Thanks.

So the flow would be as follows?

  • In SlingServlet, call your webservice, get the response 
  • Define a new helper-class, that takes in the response and write the file to AEM's content
  • then back in SlingServlet, get the data/file from AEM's repository and send it to UI

Avatar

Level 10

Yes - like in the article - notice how we create the ZIP on the fly and it can be downloaded - it would be quite similar in that respect. 

Avatar

Correct answer by
Former Community Member

With Scott's inputs, my solution with code snippets.

1. From the UI, submit the action to Sling Servlet

 

<form name="importFileForm" method="get" action="/services/getData"> <input type="submit" title="Submit" value="Submit" name="bttnAction"> </form>

2. Your Servlet class        
 

public class TTIGetServlet extends SlingAllMethodsServlet { @Override protected void doGet(SlingHttpServletRequest request,SlingHttpServletResponse response) throws ServletException,IOException { ... ... String serviceurl = <<< your webservice url>>> HttpClient httpclient = HttpClients.custom().build(); generateFile(serviceurl, httpclient, request, response); RequestDispatcher dispatcher = request.getRequestDispatcher("/content/ttii/en/importfiletest.html"); dispatcher.forward(request, response); } }

3. Generate File method that pops up the file download on browser

public static void generateFile(String serviceurl, HttpClient httpclient, SlingHttpServletRequest httpRequest, SlingHttpServletResponse httpResponse) throws ClientProtocolException, IOException { HttpResponse response; HttpGet httpGet = new HttpGet(serviceURL); // Makes the call to WebService response = httpclient.execute(httpGet); // CORE LOGIC if (response!=null) { ContentType contentType = ContentType.getOrDefault(response.getEntity()); String mimeType = contentType.getMimeType(); if (mimeType.equals(MIMETYPE_JSON)) { // Out of context here... } else { // SHOW THE FILE ServletOutputStream sos = httpResponse.getOutputStream(); httpResponse.setContentType("application/vnd.ms-excel"); httpResponse.setHeader("Content-Disposition", "attachment;filename=test.xls"); BufferedHttpEntity buf = new BufferedHttpEntity(response.getEntity()); InputStream istream = buf.getContent(); sos.write(FileHelper.writeFiles(istream)); sos.flush(); } } }
The following has evaluated to null or missing: ==> liqladmin("SELECT id, value FROM metrics WHERE id = 'net_accepted_solutions' and user.id = '${acceptedAnswer.author.id}'").data.items [in template "analytics-container" at line 83, column 41] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign answerAuthorNetSolutions = li... [in template "analytics-container" at line 83, column 5] ----