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.
Solved! Go to Solution.
Views
Replies
Total Likes
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(); } } }
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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?
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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(); } } }
Views
Replies
Total Likes
Views
Likes
Replies
Views
Like
Replies