JSF file download NullPointerException with View Parameter
Hope I get it straight:
I am trying to create a JSF 2.0 page optimized for mobile use on Tomcat 7. So I am using Mojarra 2.1.6 and PrimeFaces (Core 3.1.1 and Mobile 0.9.1).
My script:
- The user visits a simplified url eg. www.someurl.eu/view.xhtml/12345678
- ServletFilter rewrites url on www.someurl.eu/view.xhtml?id=12345678
- URL parameter is provided for ManagedBean
- The required data is evaluated and published to my view.xhtml
- On my view.xhtml a button is provided to download
My problem:
By clicking the button, the server shows me the NPE when the ManagedBean is set to @RequestScoped. My guess is that a button click starts a new request, my URL parameter is lost and the download file is no longer available. I don't know if I'm right, but if so, I don't know how to solve this graceful one.
My ManagedBean:
@ManagedBean
@RequestScoped
public class ViewBean {
// Elements
...
private StreamedContent file;
...
// Getter and Setter
...
public StreamedContent getFile() {
invalidateSession();
return file;
}
public void setFile(StreamedContent file) {
this.file = file;
}
...
// Init
@PostConstruct
public void init() {
//read URL-Parameter
urlSdbac = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id");
// validate
if(urlSdbac == null || urlSdbac.equals("") || urlSdbac.length() != 8) {
urlSdbac = "invalid";
}
// get Data
else {
SdbCustomerData cd = getFileMetadata(urlSdbac);
if(cd == null) {
this.name = "invalid code";
this.language = "no language options found";
}
else {
...
// prepare File for Download
this.file = new Helper().getWebDavFile(cd.getCid(), cd.getName());
}
}
}
My view.xhtml:
<h:form>
<pm:page title="Download">
<pm:view>
<pm:content>
...
<p:commandButton value="Download" ajax="false">
<p:fileDownload value="#{viewBean.file}" />
</p:commandButton>
</pm:content>
</pm:view>
</pm:page>
</h:form>
Last but not least, my filter:
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
String url = request.getRequestURI();
Pattern sdbac = Pattern.compile("[A-Za-z0-9]{8}");
Matcher fit = null;
// check Page
if(url.contains("view.xhtml/") && (!url.contains("?id=") && !url.endsWith("view.xhtml")) ) {
String code = url.substring(url.indexOf("view.xhtml/")+11, url.length());
// validate Param?
fit = sdbac.matcher(code);
if(fit.matches()) {
// redirect
response.sendRedirect("/getSDS/view.xhtml?id=" + code);
}
else {
// redirect
response.sendRedirect("/getSDS/view.xhtml?id=invalid");
}
}
else {
chain.doFilter(req,res);
}
}
My filter matches any URL <url-pattern>/*</url-pattern>
I've also tried setting ManagedBean to @SessionScoped. If I do this, the download is fine, but the user is unable to change the URL due to persistent data in the session managed by the ManagedBean session.
I may be too blind to see the obvious solution.
Any help would be appreciated! Thanks in advance!
PS Exception
java.lang.NullPointerException
org.primefaces.component.filedownload.FileDownloadActionListener.processAction(FileDownloadActionListener.java:58)
javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769)
javax.faces.component.UICommand.broadcast(UICommand.java:300)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
eu.qualisys.getsds.filter.SDBACParamFilter.doFilter(SDBACParamFilter.java:53)
source to share