View Javadoc
1   package org.exoplatform.ecm.webui.component.explorer.rightclick.manager;
2   
3   import org.exoplatform.ecm.webui.component.explorer.UIWorkingArea;
4   import org.exoplatform.ecm.webui.component.explorer.control.filter.IsNtFileFilter;
5   import org.exoplatform.ecm.webui.component.explorer.control.listener.UIWorkingAreaActionListener;
6   import org.exoplatform.services.cms.link.NodeFinder;
7   import org.exoplatform.services.jcr.ext.common.SessionProvider;
8   import org.exoplatform.services.log.ExoLogger;
9   import org.exoplatform.services.log.Log;
10  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
11  import org.exoplatform.wcm.webui.Utils;
12  import org.exoplatform.webui.config.annotation.ComponentConfig;
13  import org.exoplatform.webui.config.annotation.EventConfig;
14  import org.exoplatform.webui.event.Event;
15  import org.exoplatform.webui.ext.filter.UIExtensionFilter;
16  import org.exoplatform.webui.ext.filter.UIExtensionFilters;
17  import org.exoplatform.webui.ext.manager.UIAbstractManager;
18  import org.exoplatform.webui.ext.manager.UIAbstractManagerComponent;
19  
20  import javax.jcr.Node;
21  import javax.jcr.Session;
22  import java.util.Arrays;
23  import java.util.List;
24  import java.util.regex.Matcher;
25  
26  @ComponentConfig(
27          events = {
28                  @EventConfig(listeners = DownloadManageComponent.DownloadActionListener.class)
29          }
30  )
31  public class DownloadManageComponent extends UIAbstractManagerComponent {
32  
33    private static final Log LOG = ExoLogger.getLogger(DownloadManageComponent.class.getName());
34  
35    private static final List<UIExtensionFilter> FILTERS
36            = Arrays.asList(new UIExtensionFilter[]{new IsNtFileFilter(),
37    });
38  
39    @UIExtensionFilters
40    public List<UIExtensionFilter> getFilters() {
41      return FILTERS;
42    }
43  
44    @Override
45    public Class<? extends UIAbstractManager> getUIAbstractManagerClass() {
46      return null;
47    }
48  
49    /**
50     * Parse node path with syntax [workspace:node path] to workspace name and path separately
51     *
52     * @param nodePath node path with syntax [workspace:node path]
53     * @return array of String. element with index 0 is workspace name, remaining one is node path
54     */
55    private String[] parseWorkSpaceNameAndNodePath(String nodePath) {
56      Matcher matcher = UIWorkingArea.FILE_EXPLORER_URL_SYNTAX.matcher(nodePath);
57      if (!matcher.find())
58        return null;
59      String[] workSpaceNameAndNodePath = new String[2];
60      workSpaceNameAndNodePath[0] = matcher.group(1);
61      workSpaceNameAndNodePath[1] = matcher.group(2);
62      return workSpaceNameAndNodePath;
63    }
64  
65    /**
66     * Gets user session from a specific workspace.
67     *
68     * @param workspaceName
69     * @return session
70     * @throws Exception
71     */
72    private Session getSession(String workspaceName) throws Exception {
73      SessionProvider sessionProvider = WCMCoreUtils.getUserSessionProvider();
74      return sessionProvider.getSession(workspaceName, WCMCoreUtils.getRepository());
75    }
76  
77    public static class DownloadActionListener extends UIWorkingAreaActionListener<DownloadManageComponent> {
78      public void processEvent(Event<DownloadManageComponent> event) throws Exception {
79        DownloadManageComponent downloadManageComponent = event.getSource();
80        String nodePath = event.getRequestContext().getRequestParameter(OBJECTID);
81        String[] workspaceAndPath = downloadManageComponent.parseWorkSpaceNameAndNodePath(nodePath);
82        Node downloadedNode = (Node) WCMCoreUtils.getService(NodeFinder.class)
83                .getItem(downloadManageComponent.getSession(workspaceAndPath[0]), workspaceAndPath[1], true);
84  
85        String downloadLink = Utils.getDownloadLink(downloadedNode);
86        event.getRequestContext().getJavascriptManager().require("SHARED/jquery", "gj")
87                .addScripts("setTimeout(\"window.location.assign('" + downloadLink + "');\", 1000);");
88      }
89    }
90  }