Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » RAP File downloading
RAP File downloading [message #1016177] Tue, 05 March 2013 06:05 Go to next message
Markus Duft is currently offline Markus Duft
Messages: 95
Registered: February 2013
Member
Hey. Me again Smile

In our application i want to send a download to the client. The file is generated on the fly and sent to the client. So far everything works like a charm with this code:

    public boolean sendDownload(byte[] data, String filename) {
        DownloadService service = new DownloadService(data, filename);
        service.register();

        UrlLauncher launcher = RWT.getClient().getService(UrlLauncher.class);
        launcher.openURL(service.getURL());
        return true;
    }

    private static final class DownloadService implements ServiceHandler {

        private final byte[] data;
        private final String filename;
        private String id;

        public DownloadService(byte[] data, String filename) {
            this.data = data;
            this.filename = filename;
            this.id = calculateId();
        }

        public String getURL() {
            return RWT.getServiceManager().getServiceHandlerUrl(getId());
        }

        private String getId() {
            return id;
        }

        private String calculateId() {
            return String.valueOf(System.currentTimeMillis()) + data.length;
        }

        public boolean register() {
            try {
                RWT.getServiceManager().registerServiceHandler(getId(), this);
                return true;
            } catch (Exception e) {
                log.error("failed to register download service handler", e);
                return false;
            }
        }

        private boolean unregister() {
            try {
                RWT.getServiceManager().unregisterServiceHandler(getId());
                return true;
            } catch (Exception e) {
                log.error("failed to unregister download service handler", e);
                return false;
            }
        }

        @Override
        public void service(HttpServletRequest request, HttpServletResponse response)
                throws IOException, ServletException {
            try {
                response.setContentType("application/octet-stream");
                response.setContentLength(data.length);
                response.setHeader("Content-Disposition", "attachment; filename=\"" + filename
                        + "\"");
                response.getOutputStream().write(data);
            } catch (Exception e) {
                log.error("failed to dispatch download");
            } finally {
                unregister();
            }
        }

    }


the problem i have with this solution is, that it will open a separate browser window, and leave it open (at least with chrome), after the download was sent. is there a way to

1) close the browser window again?
2) don't open a browser window at all, but send the download in the main window (without destroying the running application Smile)?

Thanks!
Re: RAP File downloading [message #1018844 is a reply to message #1016177] Thu, 14 March 2013 11:01 Go to previous message
Tim Buschtoens is currently offline Tim Buschtoens
Messages: 382
Registered: July 2009
Senior Member
Hi.

We are aware of the issue. It was discussed, but no full solution could
be implemented for 2.0. I suggest you open an enhancement request for a
RAP downloader service on Bugzilla. As a workaround you could use the
JavaScriptExecuter to render something like "window.location = myURL".

Greetings,
Tim

Am 05.03.2013 12:05, schrieb Markus Duft:
> Hey. Me again :)
>
> In our application i want to send a download to the client. The file is
> generated on the fly and sent to the client. So far everything works
> like a charm with this code:
>
>
> public boolean sendDownload(byte[] data, String filename) {
> DownloadService service = new DownloadService(data, filename);
> service.register();
>
> UrlLauncher launcher =
> RWT.getClient().getService(UrlLauncher.class);
> launcher.openURL(service.getURL());
> return true;
> }
>
> private static final class DownloadService implements ServiceHandler {
>
> private final byte[] data;
> private final String filename;
> private String id;
>
> public DownloadService(byte[] data, String filename) {
> this.data = data;
> this.filename = filename;
> this.id = calculateId();
> }
>
> public String getURL() {
> return RWT.getServiceManager().getServiceHandlerUrl(getId());
> }
>
> private String getId() {
> return id;
> }
>
> private String calculateId() {
> return String.valueOf(System.currentTimeMillis()) +
> data.length;
> }
>
> public boolean register() {
> try {
> RWT.getServiceManager().registerServiceHandler(getId(),
> this);
> return true;
> } catch (Exception e) {
> log.error("failed to register download service handler",
> e);
> return false;
> }
> }
>
> private boolean unregister() {
> try {
> RWT.getServiceManager().unregisterServiceHandler(getId());
> return true;
> } catch (Exception e) {
> log.error("failed to unregister download service
> handler", e);
> return false;
> }
> }
>
> @Override
> public void service(HttpServletRequest request,
> HttpServletResponse response)
> throws IOException, ServletException {
> try {
> response.setContentType("application/octet-stream");
> response.setContentLength(data.length);
> response.setHeader("Content-Disposition", "attachment;
> filename=\"" + filename
> + "\"");
> response.getOutputStream().write(data);
> } catch (Exception e) {
> log.error("failed to dispatch download");
> } finally {
> unregister();
> }
> }
>
> }
>
>
> the problem i have with this solution is, that it will open a separate
> browser window, and leave it open (at least with chrome), after the
> download was sent. is there a way to
>
> 1) close the browser window again?
> 2) don't open a browser window at all, but send the download in the main
> window (without destroying the running application :))?
>
> Thanks!

--
Tim Buschtöns

Twitter: @EclipseRAP
Blog: http://eclipsesource.com/blogs/

Professional services for RAP and RCP?
http://eclipsesource.com/services/rap/
Previous Topic:JMeter RAP/LoadTesting on RAP 1.5.0 Problem
Next Topic:Custom widgets example source code
Goto Forum:
  


Current Time: Sat Aug 30 20:39:01 EDT 2014

Powered by FUDForum. Page generated in 0.01834 seconds