Download monitoring won't close

UI Components for JSF
euan
Posts: 14
Joined: 10 Jul 2018, 15:56

17 Jul 2018, 16:58

I am following up with a new roadblock met during migration from 3.4 to 6.2.

This is about the "PrimeFaces.monitorDownload".

The file has been successfully downloaded on local computer. It can be opened.

Then the progress bar displayed during the downloading won't hide once download is complete.

Thus blocking the entire page.

Code: Select all

							<div id="pageRightSection" class="rightSectionTop">
								<p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" />
								<p:outputPanel id="mypanel">
									<h:panelGroup rendered="#{myModelBean.telechargeable}">
										<p:commandButton id="telechargerFichier" value="#{msg.telecharger}" ajax="false" onclick="PrimeFaces.monitorDownload(start, stop)"   
										        icon="ui-icon-arrowthichk-s">  
										    <p:fileDownload value="#{myController.actionTelecharger()}" />  
										</p:commandButton> #{msg.le_resultat}
										
										<p:dialog appendTo="@(body)" modal="true" widgetVar="statusDialog" header="#{msg.telechargement}" draggable="false" closable="false" resizable="false">
										    <p:graphicImage value="/images/ajaxloadingbar.gif" />  
										</p:dialog>
									</h:panelGroup>
								</p:outputPanel>
							</div>
						</div>
					</div>
				</div>
			</div>
		</h:form>
		
		<script type="text/javascript">  
			function start() {  
			    PF('statusDialog').show();
			}  
			  
			function stop() {  
			    PF('statusDialog').hide();
			}  
        </script>  
Looks like the "stop" function is not triggered.

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

17 Jul 2018, 17:53

PrimeFaces downloadMonitor uses a cookie that is set in the download response to check for completeness.

Code: Select all

primefaces.download=true
Can you debug network traffic to see if it is actually set?

euan
Posts: 14
Joined: 10 Jul 2018, 15:56

18 Jul 2018, 09:53

Yes the firefox debugger shows a cookies with "primefaces.download= true", linked with the session id.

The start js function is called when the download starts.
If I replace showing the loading progress bar with an alert, the alert is well displayed.

Code: Select all

			
function start() {  
	if(PrimeFaces.cookiesEnabled()) {
		alert('on show');
	}
}  
But looks like the stop function with the hide() part is now invoked.

Should I see something like "primefaces.download=false" on the network monitoring on complete ?

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

18 Jul 2018, 13:47

euan wrote:
18 Jul 2018, 09:53
Should I see something like "primefaces.download=false" on the network monitoring on complete ?
That is not what I see in the showcase and looking at the source, the 'true' for the cookie means it is completed!

https://github.com/primefaces/primeface ... re.js#L442

Are you running in a portal? Do you have more fileDownloads in parallel? Can the cookie be actually set from the response?

euan
Posts: 14
Joined: 10 Jul 2018, 15:56

18 Jul 2018, 15:00

It seems like "primefaces.download" value is always null on the primefaces side.

If I replace the start function with:

Code: Select all

function start() {  
	if(PrimeFaces.cookiesEnabled()) {
		var downloadComplete = PrimeFaces.getCookie('primefaces.download');
		alert('init primefaces.download: ' + downloadComplete);

		setInterval(function() {
			var downloadComplete = PrimeFaces.getCookie('primefaces.download');
			alert('poll primefaces.download: ' + downloadComplete);
		}, 2000);
		//PF('statusDialog').show();
	}
}  
I get always null displayed: "poll primefaces.download: null".
Last edited by euan on 18 Jul 2018, 18:28, edited 1 time in total.

euan
Posts: 14
Joined: 10 Jul 2018, 15:56

18 Jul 2018, 18:15

A bit tricky, the cookies list shown by firefox contains two "primefaces.download" cookies that exists at the same time.

One with 'true' value associated with the path '/myapp/pages/mypage'.
One with 'null' value associated with the path '/myapp/pages/mypage/'.

Explanation:

Looks like it works well the first time I am downloading on this page.

But once a download has been completed from the page, the second cookies with a null value is created and then the stop method is never called if another download is requested.

I guess it has something to do with "b.setCookie(e, null)" in the monitorDownload function:

Code: Select all

monitorDownload: function (f, c, d) {
	if (this.cookiesEnabled()) {
		if (f) {
			f()
		}
		var e = d ? "primefaces.download_" + d : "primefaces.download";
		a.downloadMonitor = setInterval(function () {
			var g = b.getCookie(e);
			if (g === "true") {
				if (c) {
					c()
				}
				clearInterval(a.downloadMonitor);
				b.setCookie(e, null)
			}
		}, 1000)
	}
}

mrluglio
Posts: 3
Joined: 18 Jul 2018, 17:49

18 Jul 2018, 18:27

Try this:

Code: Select all

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

import org.primefaces.util.Constants;


@javax.servlet.annotation.WebFilter("*")
public class WebFilter implements Filter {


	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	}

	@Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        filterChain.doFilter(request, new RespWrapper((HttpServletResponse) response));
    } 

	@Override
	public void destroy() {

	}
	
	class RespWrapper extends HttpServletResponseWrapper {
		
		public RespWrapper(HttpServletResponse response) {
			super(response);
		}
		
		@Override
		public void addCookie(Cookie cookie) {
			if(Constants.DOWNLOAD_COOKIE.equals(cookie.getName())) cookie.setPath("/"); // funziona se poi js chiama PrimeFaces.deleteCookie('primefaces.download',{path:'/'});
			super.addCookie(cookie);
		}
	}
}
and in a js:

Code: Select all

PrimeFaces.monitorDownload = function(start, complete, monitorKey) {
			// Primefaces 5.3 / 6.0 / 6.1 (monitorKey) / 6.2
            if(this.cookiesEnabled()) {
                if(start) {
                    start();
                }

                var cookieName = monitorKey ? 'primefaces.download_' + monitorKey : 'primefaces.download';
                window.downloadMonitor = setInterval(function() {
                    var downloadComplete = PrimeFaces.getCookie(cookieName);

                    if(downloadComplete === 'true') {
                        if(complete) {
                            complete();
                        }
                        clearInterval(window.downloadMonitor);
                        //PrimeFaces.setCookie(cookieName, null); <<< wrong!
                        PrimeFaces.deleteCookie(cookieName,{path:'/'}); 
                    }
                }, 1000);
            }
        }

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

19 Jul 2018, 09:18

What are the changes and why? Maybe file an issue in github if this is a generic 'fix'

euan
Posts: 14
Joined: 10 Jul 2018, 15:56

23 Jul 2018, 10:07

The main change in the "monitorDownload" function is :

Code: Select all

//PrimeFaces.setCookie(cookieName, null); <<< wrong!
PrimeFaces.deleteCookie(cookieName,{path:'/'}); 
Looks like the "setCookie" duplicate the cookie, adding a "/" at the end of the "path".
When calling the "PrimeFaces.monitorDownload" a second time:
- The first cookie is set to true, at the end of the download.
- The cookie with 'null' value is looked after, but the value is never set to true = the download progress bar is never closed.

So instead of setting value to null, once download is finished, it is cleaner to delete the cookie.

Works fine but better if done directly on the source code ;)

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

23 Jul 2018, 20:51

Please file an issue in github with this change.

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 36 guests