/** * Get download link of a node which stored binary data * * @param node Node * @return download link * @throws Exception */ public static String getDownloadLink(Node node) throws Exception { if (!Utils.getRealNode(node).getPrimaryNodeType().getName().equals(NT_FILE)) return null; // Get binary data from node DownloadService dservice = WCMCoreUtils.getService(DownloadService.class); Node jcrContentNode = node.getNode(JCR_CONTENT); InputStream input = jcrContentNode.getProperty(JCR_DATA).getStream(); // Get mimeType of binary data String mimeType = jcrContentNode.getProperty(JCR_MIMETYPE).getString(); // Make download stream InputStreamDownloadResource dresource = new InputStreamDownloadResource(input, mimeType); // Make extension part for file if it have not yet DMSMimeTypeResolver mimeTypeSolver = DMSMimeTypeResolver.getInstance(); String ext = "." + mimeTypeSolver.getExtension(mimeType); String fileName = Utils.getRealNode(node).getName(); if (fileName.lastIndexOf(ext) < 0 && !mimeTypeSolver.getMimeType(fileName).equals(mimeType)) { dresource.setDownloadName(fileName + ext); } else { dresource.setDownloadName(fileName); } return dservice.getDownloadLink(dservice.addDownloadResource(dresource)); }
/** * @param : node: nt:file node with have the data stream * @return : Link to download the jcr:data of the given node * @throws Exception */ public static String getDownloadRestServiceLink(Node node) throws Exception { ExoContainer container = ExoContainerContext.getCurrentContainer(); PortalContainerInfo containerInfo = (PortalContainerInfo) container.getComponentInstanceOfType(PortalContainerInfo.class); String portalName = containerInfo.getContainerName(); PortalContainerConfig portalContainerConfig = (PortalContainerConfig) container.getComponentInstance(PortalContainerConfig.class); String restContextName = portalContainerConfig.getRestContextName(portalName); StringBuilder sb = new StringBuilder(); Node currentNode = org.exoplatform.wcm.webui.Utils.getRealNode(node); String ndPath = currentNode.getPath(); if (ndPath.startsWith("/")) { ndPath = ndPath.substring(1); } String encodedPath = URLEncoder.encode(ndPath, "utf-8"); encodedPath = encodedPath.replaceAll("%2F", "/"); // we won't encode the slash characters in the path sb.append("/").append(restContextName).append("/contents/download/"); sb.append(currentNode.getSession().getWorkspace().getName()).append("/").append(encodedPath); if (node.isNodeType("nt:frozenNode")) { sb.append("?version=" + node.getParent().getName()); } return sb.toString(); }