I implemented a tree with an outputResource as "content" (see Downloading file from IceFaces tree).
However, when I try to download a file (I have only txt or csv files) I get the HTTP 500 Status error.
The tree structure is something like
Root
|-infoFiles
| |-A.csv
| |-B.csv
|-validateFiles
| |-C.txt
| |-D.txt
And the exception once I click on the resource is
java.io.FileNotFoundException: C:\SRC\dataFiles\998\validateFiles (Access denied)
java.io.FileInputStream.open(Native Method)
java.io.FileInputStream.<init>(FileInputStream.java:138)
java.io.FileInputStream.<init>(FileInputStream.java:97)
mx.gob.sagarpa.utilidades.FileResource.open(FileResource.java:39)
com.icesoft.faces.component.outputresource.RegisteredResource.open(OutputResource.java:474)
com.icesoft.faces.context.ResourceRegistryLocator$DynamicResourceDispatcherAdapter$DynamicResourceAdapter.open(ResourceRegistryLocator.java:117)
org.icefaces.impl.push.DynamicResourceDispatcher$ResourceServer.respond(DynamicResourceDispatcher.java:224)
org.icefaces.impl.push.DynamicResourceDispatcher$ResourceServer.handleResourceRequest(DynamicResourceDispatcher.java:201)
org.icefaces.impl.push.DynamicResourceDispatcher$Mapping.handleResourceRequest(DynamicResourceDispatcher.java:370)
org.icefaces.impl.push.DynamicResourceDispatcher.handleResourceRequest(DynamicResourceDispatcher.java:89)
org.icefaces.application.ResourceRegistry.handleResourceRequest(ResourceRegistry.java:75)
org.icefaces.impl.application.WindowScopeManager.handleResourceRequest(WindowScopeManager.java:165)
javax.faces.application.ResourceHandlerWrapper.handleResourceRequest(ResourceHandlerWrapper.java:125)
javax.faces.application.ResourceHandlerWrapper.handleResourceRequest(ResourceHandlerWrapper.java:125)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:591)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
Sometimes it happens just with csv files and sometimes even with txt
Tree.xhtml
<ice:tree id="tree"
value="#{treeBean.model}"
var="item"
hideRootNode="false"
hideNavigation="false"
>
<ice:treeNode>
<f:facet name="icon">
<ice:panelGroup style="display: inline">
<h:graphicImage value="#{item.userObject.icon}" />
</ice:panelGroup>
</f:facet>
<f:facet name="content">
<ice:panelGroup style="display: inline-block">
<ice:outputResource resource="#{item.userObject.resource}"
fileName="#{item.userObject.resource.filename}"
mimeType="#{item.userObject.resource.mimeType}"
/>
</ice:panelGroup>
</f:facet>
</ice:treeNode>
</ice:tree>
TreeBean.java
@ManagedBean
@ViewScoped
public class TreeBean implements Serializable {
private DefaultTreeModel model;
public final String openFolderImg = "./img/tree_folder_open.gif";
public final String closeFolderImg = "./img/tree_folder_close.gif";
public final String fileImg = "./img/tree_document.gif";
@ManagedProperty("#{userBean}")
private UserBean userBean;
@PostConstruct
public void init() {
// create root node with its children expanded
DefaultMutableTreeNode rootTreeNode = new DefaultMutableTreeNode();
FileResourceUserObject rootObject = new FileResourceUserObject(rootTreeNode);
rootObject.setText("Sistema de Rendición de Cuentas");
rootObject.setExpanded(true);
rootObject.setResource(new FileResource("Sistema de Rendición de Cuentas", null));
rootObject.setBranchContractedIcon(openFolderImg);
rootObject.setBranchExpandedIcon(closeFolderImg);
rootTreeNode.setUserObject(rootObject);
// model is accessed by the ice:tree component
model = new DefaultTreeModel(rootTreeNode);
File f = new File("./998/");
createTree(f, rootTreeNode);
}
public DefaultTreeModel getModel() {
return model;
}
public UserBean getUserBean() {
return userBean;
}
public void setUserBean(UserBean userBean) {
this.userBean = userBean;
}
private void createTree(File fileRoot, DefaultMutableTreeNode treeRoot) {
File[] files = fileRoot.listFiles();
DefaultMutableTreeNode branchNode;
for (File f : files) {
if (f.isDirectory()) {
branchNode = new DefaultMutableTreeNode();
FileResourceUserObject branchObject = new FileResourceUserObject(branchNode);
branchObject.setExpanded(false);
branchObject.setText(f.getName());
branchObject.setResource(new FileResource(f.getName(), f.getAbsolutePath()));
branchObject.setBranchContractedIcon(openFolderImg);
branchObject.setBranchExpandedIcon(closeFolderImg);
branchNode.setUserObject(branchObject);
treeRoot.add(branchNode);
createTree(f, branchNode);
}
if (f.isFile()) {
branchNode = new DefaultMutableTreeNode();
FileResourceUserObject branchObject = new FileResourceUserObject(branchNode);
branchObject.setText(f.getName());
branchObject.setResource(new FileResource(f.getName(), f.getAbsolutePath()));
branchObject.setLeaf(true);
branchObject.setLeafIcon(fileImg);
branchNode.setUserObject(branchObject);
treeRoot.add(branchNode);
}
}
return;
}
}
FileResourceUserObject.java
public class FileResourceUserObject extends IceUserObject{
private FileResource resource;
public FileResourceUserObject(DefaultMutableTreeNode wrapper) {
super(wrapper);
}
public FileResource getResource() {
return resource;
}
public void setResource(FileResource resource) {
this.resource = resource;
}
}
FileResource.java
public class FileResource implements Resource{
private String filename;
private String fileAbsolutePath;
private String mimeType;
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
public FileResource(String filename, String fileAbsolutePath) {
this.filename = filename;
this.fileAbsolutePath = fileAbsolutePath;
this.mimeType = ec.getMimeType(filename);
}
@Override
public String calculateDigest() {
return filename;
}
@Override
public InputStream open() throws IOException {
return new FileInputStream(fileAbsolutePath);
}
@Override
public Date lastModified() {
return new Date();
}
@Override
public void withOptions(Options optns) throws IOException {
}
public String getFileAbsolutePath() {
return fileAbsolutePath;
}
public void setFileAbsolutePath(String fileAbsolutePath) {
this.fileAbsolutePath = fileAbsolutePath;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public String getMimeType() {
return mimeType;
}
}
UPDATE
What I noticed was that when I get the 500 Status Error the FileNotFoundException is always pointing to the same path java.io.FileNotFoundException: C:\SRC\dataFiles\998\validateFiles (Access denied) I'm starting to think that all my FileResources are pointing to the same path... why is this happening?
There was a report of a bug on the IceFaces page where it says that using multiple outputResource tags had a strange behavior. See this link http://jira.icefaces.org/browse/ICE-3667 I think this was happening with I had the tree and many outputResource tags.
Moreover, I also read that using the outputResource tag it creates an Object at render time (or something like that I'm very new to JSF and all the stuff related) and it was memory-cpu consuming and it was better to use a servlet to perform the download. See http://www.dantoomeysoftware.com/pencils-down/2009/09/08/dont-use-icefaces-resource-for-download-use-a-download-servlet/
So it's better to implement a servlet, you can find useful info in http://balusc.blogspot.com/2007/07/fileservlet.html and (if your are new to all this stuff) Custom download servlet