View Javadoc
1   /*
2    * Copyright (C) 2003-2008 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.ecm.connector.fckeditor;
18  
19  import java.security.AccessControlException;
20  
21  import javax.jcr.Node;
22  import javax.xml.parsers.DocumentBuilder;
23  import javax.xml.parsers.DocumentBuilderFactory;
24  
25  import org.exoplatform.container.PortalContainer;
26  import org.exoplatform.container.xml.PortalContainerInfo;
27  import org.exoplatform.services.jcr.access.PermissionType;
28  import org.exoplatform.services.jcr.core.ExtendedNode;
29  import org.exoplatform.services.jcr.core.ManageableRepository;
30  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
31  import org.w3c.dom.Document;
32  import org.w3c.dom.Element;
33  
34  /**
35   * Created by The eXo Platform SAS
36   * @author : Hoa.Pham
37   *          hoa.pham@exoplatform.com
38   * Jun 23, 2008
39   */
40  public class FCKUtils {
41  
42    /** The Constant LAST_MODIFIED_PROPERTY. */
43    public static final String LAST_MODIFIED_PROPERTY = "Last-Modified";
44  
45    /** The Constant IF_MODIFIED_SINCE_DATE_FORMAT. */
46    public static final String IF_MODIFIED_SINCE_DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss z";
47  
48    public static String GET_FOLDERS_AND_FILES="getFoldersAndFiles";
49    public static String CREATE_FOLDER = "createFolder";
50    public static String UPLOAD_FILE = "upload";
51  
52    public static final String EXO_HIDDENABLE = "exo:hiddenable";
53    public static final String NT_FILE = "nt:file";
54    public static final String NT_FOLDER = "nt:folder";
55    public static final String NT_UNSTRUCTURED = "nt:unstructured";
56  
57    public final static String DOCUMENT_TYPE = "file";
58  
59    /** The Constant IMAGE_TYPE. */
60    public final static String IMAGE_TYPE = "image";
61    public final static String FLASH_TYPE = "flash";
62    public final static String LINK_TYPE = "link";
63  
64  
65    /**
66     * Creates the root element for connector response. The full connector response looks like:
67     * {@code
68     * <Connector command="GetFolders" resourceType="">
69     *  <CurrentFolder folderType="" name="" path="" url=""/>
70     *  </CurrentFolder>
71     * </Connector>
72     * }
73     *
74     * @param command the command
75     * @param node the node
76     * @param folderType the folder type
77     * @return the org.w3c.dom.Element element
78     * @throws Exception the exception
79     */
80    public static Element createRootElement(String command, Node node, String folderType) throws Exception {
81      Document doc = null;
82      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
83      DocumentBuilder builder = factory.newDocumentBuilder();
84      doc = builder.newDocument();
85      StringBuffer currentPath = new StringBuffer(node.getPath());
86      if (!currentPath.toString().endsWith("/")) {
87        currentPath.append("/");
88      }
89      Element rootElement = doc.createElement("Connector");
90      doc.appendChild(rootElement);
91      rootElement.setAttribute("command", command);
92      rootElement.setAttribute("resourceType", "Node");
93      Element currentFolderElement = doc.createElement("CurrentFolder");
94      currentFolderElement.setAttribute("name", node.getName());
95      currentFolderElement.setAttribute("folderType", folderType);
96      currentFolderElement.setAttribute("path", currentPath.toString());
97      currentFolderElement.setAttribute("url", createWebdavURL(node));
98      rootElement.appendChild(currentFolderElement);
99      return rootElement;
100   }
101 
102   public static boolean hasAddNodePermission(Node node) throws Exception {
103     try {
104       ((ExtendedNode)node).checkPermission(PermissionType.ADD_NODE) ;
105       return true ;
106     } catch (AccessControlException e) {
107       return false ;
108     }
109   }
110 
111   public static String getPortalName() {
112     PortalContainerInfo containerInfo = WCMCoreUtils.getService(PortalContainerInfo.class) ;
113     return containerInfo.getContainerName() ;
114   }
115 
116   public static String createWebdavURL(final Node node) throws Exception {
117     String repository = ((ManageableRepository) node.getSession().getRepository()).getConfiguration().getName();
118     String workspace = node.getSession().getWorkspace().getName();
119     String currentPath = node.getPath();
120     String url = "/" + getPortalName() + "/" + PortalContainer.getCurrentRestContextName()
121         + "/jcr/" + repository + "/" + workspace + currentPath;
122     return url;
123   }
124 }