-2

I'm trying to do the following: 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.

how to achieve this?

Suren Konathala
  • 3,497
  • 5
  • 43
  • 73

1 Answers1

0

With help from Scott: http://help-forums.adobe.com/content/adobeforums/en/experience-manager-forum/adobe-experience-manager.topic.html/forum__xhh5-objective_therespo.html

Here's my solution:

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

    }

Suren Konathala
  • 3,497
  • 5
  • 43
  • 73