FileDownload is Null ... AbortProcessingException [RESOLVED]

UI Components for JSF
Post Reply
dmunzopa
Posts: 11
Joined: 04 Mar 2011, 12:54
Location: Sevilla - Spain

04 Mar 2011, 13:47

Hi everyone:
I have this example with the p:fileDownload component at my app:
JAVA:

Code: Select all

public MyBean ...{
private StreamedContent file;

public void processFile(){
     ...
    this.file= .. a processed File;
   ....
}
}
// getters and setters
XHTML

Code: Select all

<h:commandButton actionListener="#{myBean.processFile}" >
<p:fileDownload value="#{myBean.file}" />
</h:commandButton>
The method processFile check, and find a file.. but when this method ends if the file is null I get this exception...

Code: Select all

java.lang.NullPointerException
	at org.primefaces.component.filedownload.FileDownloadActionListener.processAction(FileDownloadActionListener.java:53)
	at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
	at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:772)
	at javax.faces.component.UICommand.broadcast(UICommand.java:300)
	at javax.faces.component.UIData.broadcast(UIData.java:912)
	at org.primefaces.component.datatable.DataTable.broadcast(DataTable.java:574)
	at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
FileDownloadActionListener throws an AbortProcessingException...so
If I just wanna display a message to the user...

Where should I catch it?

This is my first post, I hope you have enough information.Thanks.
Last edited by dmunzopa on 23 Mar 2011, 14:31, edited 1 time in total.
Conf: Primefaces 2.2.RC - JSF-2 - Spring 3.0.3 - Hibernate 3.4.0GA.
Server - Eclipse Helios - Apache-tomcat-6.0.29.
Pc: Windows XP/Professinoal.

dmunzopa
Posts: 11
Joined: 04 Mar 2011, 12:54
Location: Sevilla - Spain

07 Mar 2011, 14:53

Ok.... Here is the solution if someone needs:
  • 1.Bean must be an ViewScoped.
    2.Throw the exception through the actionListener method.
JAVA:

Code: Select all

@ViewScoped
public MyBean...{
...
public void processFile() throws AbortProcessingException  {
     ...
    this.file= .. a processed File;
    if(this.file == null){
          //show user menssage
          throw new AbortProcessingException()
    }
   ....
}
Conf: Primefaces 2.2.RC - JSF-2 - Spring 3.0.3 - Hibernate 3.4.0GA.
Server - Eclipse Helios - Apache-tomcat-6.0.29.
Pc: Windows XP/Professinoal.

faprieto
Posts: 1
Joined: 21 Mar 2011, 21:46

01 Apr 2011, 15:28

Hi,
It works with ViewScoped but not for ApplicationScoped. Any idea?
Thanks

Matrium
Posts: 112
Joined: 16 May 2011, 08:27

09 Sep 2011, 15:08

sorry for bumping this old post up, but i have the same problem. i have an application bean serving the filedownloads and i just didn't find a way to handle exceptions so far.
PrimeFaces (Elite) 4.0.13, Majorra 2.1.28, Tomcat 7.0.53
Testing with Firefox, Chrome and IE9+IE10
<3 Primefaces!!!

Matrium
Posts: 112
Joined: 16 May 2011, 08:27

12 Sep 2011, 08:43

i tried with different scopes now, without sucess.

Code: Select all

javax.servlet.ServletException
	javax.faces.webapp.FacesServlet.service(FacesServlet.java:606)
root cause

Code: Select all

javax.faces.event.AbortProcessingException
	at.mypackage.FiletransferHelper.createReport(FiletransferHelper.java:101)
so far the only way i found to solve this is making download a 2-step-process for the user:
a) user clicks on "generate report"- button. if a problem occurred a message is displayed, else the download link is rendered
b) user clicks the download link. if he got so far i am sure the file is generated correctly, so there really shouldn't be a problem

is this the recommended way of using filedownload?
i would really love to do that with just one step, but i just can't find a way of aborting the download and display a message if anything goes wrong
PrimeFaces (Elite) 4.0.13, Majorra 2.1.28, Tomcat 7.0.53
Testing with Firefox, Chrome and IE9+IE10
<3 Primefaces!!!

MacNord
Posts: 4
Joined: 21 Oct 2021, 10:02

21 Oct 2021, 11:34

This solution worked for me and is NOT a two step solution (display download link only after checking) but combines exception handling and file download in one step nicely and even with AJAX.
This code is adapted and should illustrate the idea only. To make it run you must probably tweak it a bit.
1. Use custom ajax for e.g. commandLink that calls checkDownload in your bean, if the file is not available an error message is added to the context. Eventually the ajax calls the outputScript which also checks for 'alert-danger' and eventually starts the download:

Code: Select all

					
<h:outputScript>						
	function start_download() {location.href = "/your/link/executeDownload";}  // this link will call the executeDownload method that delivers the Stream
	function cond_start_download( data ) {if ( data.responseText.indexOf( 'alert-danger' ) >= 0 ) { return; } window.setTimeout( start_download, 500 ); }
</h:outputScript>
<p:growl id="fileNotFound" showIcon="false" for="ff:do_submit" />					 
<h:form id="ff" class="form-horizontal" role="form">
	<h:commandLink id='do_submit' action="#{yourDomainsController.checkDownload()}">
		<h:graphicImage name="images/csv.png" width="60"/>
		<f:ajax execute="@form" render=":fileNotFound" onevent="function(data) { if ( data.status === 'success' ) { cond_start_download( data ) } }" />
	</h:commandLink>
<h:form>

Code: Select all

public String checkDownload() {
	if (!Files.exists(this.downloadPath)) {
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Executed", "Media not found"));
	}
	return null;
}
The download link in the java script calls this function to download the file

Code: Select all

public StreamedContent executeDownload(FacesContext context) {
	InputStream in Files.newInputStream(this.downloadPath);
	StreamedContent content = DefaultStreamedContent.builder().contentEncoding("UTF-8").contentType("text/csv").name("domains.csv").stream(() -> in).build();  //since 8.0 or use your own
}
In my case it is even more complicated because I have different file types including an userid in the filename. Ask for more details if interested.

Cheers!

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 14 guests