Hi all,
this is probably simple to explain but I can't work it out. I'm trying to navigate through directories but I just can't keep the current directory.
In my jsp I show a datatable of File objects, basically just showing the names of the files as a commandlink:
<h:form>
<h:outputText id="subdir" value="#{publicDownload.currentSubdir}"/>
<t:dataTable value="#{publicDownload.files}" var="file">
<t:column>
<h:commandLink action="#{publicDownload.download}">
<h:outputText value="#{file.name}"/>
</h:commandLink>
</t:column>
</t:dataTable>
</h:form>
The action is as follows:
public String download() throws IOException
{
File download =
(File)FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("file");
if (download != null)
{
if (download.isFile()) {
// A file was clicked to be downloaded
// Keep track of the downloads
getLog().debug("Logging download of file " + download.getPath());
File log = new File(download.getPath() + ".dltrack");
if (!log.exists())
{
log.createNewFile();
}
String username =
((WebSession)getBean("webSession")).getUser().getName();
HttpServletRequest request = (HttpServletRequest)
FacesContext.getCurrentInstance().getExternalContext().getRequest();
String date = new SimpleDateFormat(DATE_FORMAT).format(Calendar.getInstance().getTime());
FileWriter fw = new FileWriter(log);
PrintWriter out = new PrintWriter(fw);
out.println(download.getPath() + download.getName() + ";" + username.trim() +
";" + request.getRemoteAddr() + ";" + date);
out.close();
fw.close();
String downloadpath = download.getPath();
downloadpath = downloadpath.replace(SiteConfig.getCurrentInstance().get("file.dir.base").replace("\\\\","\\"), "");
getLog().debug("Downloading of http://" + downloadpath);
//FacesContext.getCurrentInstance().getExternalContext().redirect("http://" + downloadpath);
}
else
{
// A directory was clicked, so move to the underlying folder
currentSubdir = download.getPath();
currentSubdir = currentSubdir.replace(downloadPath.replace("\\\\","\\"), "");
getLog().debug("Moving to directory");
getLog().debug("currentSubdir: " + currentSubdir);
getLog().debug("downloadPath: " + downloadPath);
}
}
return "";
}
Unfortunately, currentSubdir gets lost. It works for going 1 directory deep, but further down or downloading a file from any subdir fails. Instead it presents me a file from the main directory. For example, when clicking a file from subdir, the logs show:
12:15:22,500 - [com.sun.faces.lifecycle.LifecycleImpl] : DEBUG : execute(com.sun.faces.context.FacesContextImpl@64881d)
1,com.sun.faces.context.FacesContextImpl@64881d)
12:15:22,500 - [com.sun.faces.lifecycle.LifecycleImpl] : DEBUG : phase(RESTORE_VIEW 1,com.sun.faces.context.FacesContextImpl@64881d)
12:15:22,500 - [com.sun.faces.lifecycle.RestoreViewPhase] : DEBUG : Entering RestoreViewPhase
12:15:22,500 - [com.sun.faces.lifecycle.RestoreViewPhase] : DEBUG : Postback: Restored view for /dynamic/files/public.jsf
12:15:22,500 - [com.sun.faces.lifecycle.RestoreViewPhase] : DEBUG : Exiting RestoreViewPhase
2,com.sun.faces.context.FacesContextImpl@64881d)
12:15:22,500 - [com.sun.faces.lifecycle.LifecycleImpl] : DEBUG : phase(APPLY_REQUEST_VALUES 2,com.sun.faces.context.FacesContextImpl@64881d)
12:15:22,500 - [com.sun.faces.lifecycle.ApplyRequestValuesPhase] : DEBUG : Entering ApplyRequestValuesPhase
12:15:22,500 - [be.sofico.extranet.dynamic.files.PublicDownload] : DEBUG : Instantiating PublicDownload class
12:15:22,500 - [be.sofico.extranet.dynamic.files.PublicDownload] : DEBUG : Instantiating PublicDownload class
12:15:22,500 - [be.sofico.framework.SiteConfig] : DEBUG : getConfig(file.dir.base) = C:\test\
12:15:22,500 - [be.sofico.extranet.dynamic.files.PublicDownload] : DEBUG : List files from directory C:\test\download
Would anyone know why the currentSubdir variable gets lost?
I get no errors at all so I suspect it's me somewhat misunderstanding the JSF lifecycle :(