EGL Development User Group

EGL Development User Group

EGL Development User Group

The EGL Development User Group is dedicated to sharing news, knowledge, and insights regarding the EGL language and Business Developer product. Consisting of IBMers, HCL, and users, this community collaborates to advance the EGL ecosystem.

 View Only
Expand all | Collapse all

RUI sample providing download function

  • 1.  RUI sample providing download function

    Posted Fri April 15, 2016 05:43 AM

     

    Hello,

     

    My EGL class student is asking a RUI sample from which user can download files( setup instruction, readme ).  

    Very appreciate for any sample code or coding guidance.    

     

     

    L.H.Chao


  • 2.  Re: RUI sample providing download function

    Posted Fri April 15, 2016 06:50 AM

    I have solved this by creating a servlet with context parameters.

    I have an RUI external type function to open a link in a remote browser window. I can call this externaltype function from RUI with a link I create.

    This link goes to the servlet contains a parameter with the id of the file.

    The servlet consumes a web service to retrieve the file details from the database with the id of the file, and returns the file.

     

    The externaltype javascript code:

    egl.defineClass(        'customJavaScript.widgets', 'BrowserFuncts',                                    {                     "eze$$initializeDOMElement": function(){                                        },                "constructor": function(){                        //theWidget=this;                },                "openWindow" : function (windowURL, options) {                        m = window.open(windowURL,'_blank',options);                         return true;                }});                

    The egl externaltype code:

    package externalTypes;delegate EndPageEvent()endexternalType BrowserFunctions extends Widget type JavaScriptObject {                relativePath = "customJavaScript/widgets",                javaScriptName = "BrowserFuncts"         }                function openWindow(windowURL string in, options string in);end

     

    The code to use the openWindow function (from within the handler or the widget)

     

                    link string = "http://as400server:10021/PdfServices/getFile?fileID=" + id;                Browserfuncties BrowserFunctions = new Browserfunctions();                    browserFuncties.openWindow(link, "");

     

    And the servlet:

     

    package com.molcy.java.servlets.file;import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.math.BigDecimal;import java.rmi.RemoteException;import java.util.Random;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.xml.rpc.ServiceException;import com.molcy.java.util.PathUtils;import com.molcy.webservices.Services.BestandService;import com.molcy.webservices.Services.BestandServiceServiceLocator;import com.molcy.webservices.UIRecords.UIBestandRec;/* * Dit zorgt ervoor dat het mime type goed staat.  * Indien "Content-Disposition" als attachment staat, kan je in firefox niet automatisch openen.  * getfile zal dus altijd met dialoogvenster zijn en zal de bestandsnaam correct zijn  * getfilemime kan de browser kijken hoe hij het behandelt en zal de bestandsnaam getfile zijn *  */public class getFile extends HttpServlet {        private static final long serialVersionUID = 1L;           public getFile() {        super();        // TODO Auto-generated constructor stub    }        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        int fileID = Integer.parseInt(request.getParameter("fileID"));                BestandServiceServiceLocator blocator = new BestandServiceServiceLocator();                try {                        BestandService bserv = blocator.getBestandService();                        UIBestandRec bestRec = bserv.getBestand(new BigDecimal(fileID));                        String extension = PathUtils.getExtension(bestRec.getPad());                        Random generator = new Random();                        int i = generator.nextInt();                         if (i < 0) i = i * -1;                        //String ct = PathUtils.getMimeType(bestRec.getOrigBestandNaam()+ "." + extension);             response.addHeader("Content-Disposition", "attachment; filename=\"" + bestRec.getOmschr().trim() + "-" + i + "." + extension + "\"");            response.setContentType(PathUtils.getMimeType(bestRec.getOrigBestandNaam()));            File f = new File(PathUtils.movePathToQNC(bestRec.getPad()));            BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));            int len;            byte[] buf = new byte[1024];            while ((len = in.read(buf)) > 0){                response.getOutputStream().write(buf, 0, len);            }            in.close();            response.getOutputStream().close();                } catch (RemoteException e){                        System.out.println("Servlet getFile: Fout bij ophalen bestandsdetails voor bestand met id " + fileID + " " + e.getMessage());                       } catch (ServiceException servEx){                        System.out.println("Servlet getFile: Fout bij instellen service " + servEx.getMessage());                     }                    }        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {                // TODO Auto-generated method stub        }}

     

     

    I have just copied the code as it works for me.

     

    It might be too complex for your needs.

     

    Kind regards,

     

    Bram

    Bram_Callewaert


  • 3.  Re: RUI sample providing download function

    Posted Fri April 15, 2016 09:28 AM

    Hi,

    we have a similar servlet.

     

    package servlet;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.OutputStream;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class DownloadFileServlet extends HttpServlet {        protected void doGet(HttpServletRequest request,                        HttpServletResponse response) throws ServletException, IOException {                request.setCharacterEncoding("UTF-8");                final String fileName = request.getParameter("file");                // reads input file from an absolute path                String filePath = fileName;                File downloadFile = new File(filePath);                FileInputStream inStream = new FileInputStream(downloadFile);                // if you want to use a relative path to context root:                String relativePath = getServletContext().getRealPath("");                System.out.println("relativePath = " + relativePath);                // obtains ServletContext                ServletContext context = getServletContext();                // gets MIME type of the file                String mimeType = context.getMimeType(filePath);                if (mimeType == null) {                        // set to binary type if MIME mapping not found                        mimeType = "application/octet-stream";                }                System.out.println("MIME type: " + mimeType);                // modifies response                response.setContentType(mimeType);                response.setContentLength((int) downloadFile.length());                // forces download                String headerKey = "Content-Disposition";                String headerValue = String.format("attachment; filename=\"%s\"",                                downloadFile.getName());                response.setHeader(headerKey, headerValue);                // obtains response's output stream                OutputStream outStream = response.getOutputStream();                byte[] buffer = new byte[4096];                int bytesRead = -1;                while ((bytesRead = inStream.read(buffer)) != -1) {                        outStream.write(buffer, 0, bytesRead);                }                inStream.close();                outStream.close();        }}

     

    Marcel-D