How to display Excel, PDf files from Webservice on AEM pages | Community
Skip to main content
October 16, 2015
Solved

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

  • October 16, 2015
  • 4 replies
  • 2030 views

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by

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(); } } }

4 replies

smacdonald2008
New Participant
October 16, 2015

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. 

October 16, 2015

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
smacdonald2008
New Participant
October 16, 2015

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. 

Accepted solution
October 16, 2015

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(); } } }