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.wcm.connector.fckeditor;
18  
19  import java.text.DateFormat;
20  import java.text.SimpleDateFormat;
21  import java.util.Date;
22  
23  import javax.jcr.Node;
24  import javax.jcr.NodeIterator;
25  import javax.ws.rs.GET;
26  import javax.ws.rs.Path;
27  import javax.ws.rs.QueryParam;
28  import javax.ws.rs.core.Response;
29  
30  import org.exoplatform.ecm.connector.fckeditor.FCKUtils;
31  import org.exoplatform.services.jcr.ext.common.SessionProvider;
32  import org.exoplatform.services.log.ExoLogger;
33  import org.exoplatform.services.log.Log;
34  import org.exoplatform.services.rest.resource.ResourceContainer;
35  import org.exoplatform.services.wcm.portal.PortalFolderSchemaHandler;
36  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
37  import org.exoplatform.wcm.connector.BaseConnector;
38  import org.w3c.dom.Document;
39  import org.w3c.dom.Element;
40  
41  /**
42   *
43   * @LevelAPI Experimental
44   *
45   * @anchor LinkConnector
46   */
47  @Path("/wcmLink/")
48  public class LinkConnector extends BaseConnector implements ResourceContainer {
49  
50    /** The link file handler. */
51    private LinkFileHandler linkFileHandler;
52  
53    /** The log. */
54    private static final Log LOG = ExoLogger.getLogger(LinkConnector.class.getName());
55  
56    /**  Instantiates a new link connector. */
57    public LinkConnector() {
58      linkFileHandler = new LinkFileHandler();
59    }
60  
61    /**
62     * Gets the folders and files.
63     *
64     * @param repositoryName The repository name.
65     * @param workspaceName The workspace name.
66     * @param jcrPath The JCR path.
67     * @param currentFolder The current folder.
68     * @param command The command.
69     * @param type The type of folder/file.
70     * @param currentPortal The current portal.
71     * @return The folders and files.
72     * @throws Exception The exception
73     *
74     * @anchor LinkConnector.getFoldersAndFiles
75     */
76    @GET
77    @Path("/getFoldersAndFiles/")
78  //  @OutputTransformer(XMLOutputTransformer.class)
79    public Response getFoldersAndFiles(@QueryParam("repositoryName") String repositoryName,
80                                       @QueryParam("workspaceName") String workspaceName,
81                                       @QueryParam("jcrPath") String jcrPath,
82                                       @QueryParam("currentFolder") String currentFolder,
83                                       @QueryParam("currentPortal") String currentPortal,
84                                       @QueryParam("command") String command,
85                                       @QueryParam("type") String type) throws Exception {
86      try {
87        Response response = buildXMLResponseOnExpand(currentFolder, currentPortal, workspaceName,
88                                                     jcrPath, command);
89        if (response != null)
90          return response;
91      } catch (Exception e) {
92        if (LOG.isErrorEnabled()) {
93          LOG.error("Error when perform getFoldersAndFiles: ", e);
94        }
95      }
96      DateFormat dateFormat = new SimpleDateFormat(IF_MODIFIED_SINCE_DATE_FORMAT);
97      return Response.ok().header(LAST_MODIFIED_PROPERTY, dateFormat.format(new Date())).build();
98    }
99  
100   /*
101    * (non-Javadoc)
102    * @see
103    * org.exoplatform.wcm.connector.fckeditor.BaseConnector#buildXMLResponseOnExpand
104    * (java.lang.String, java.lang.String, java.lang.String, java.lang.String,
105    * java.lang.String)
106    */
107   protected Response buildXMLResponseOnExpand(String currentFolder,
108                                               String runningPortal,
109                                               String workspaceName,
110                                               String jcrPath,
111                                               String command) throws Exception {
112     SessionProvider sessionProvider = WCMCoreUtils.getSystemSessionProvider();
113     Node sharedPortal = livePortalManagerService.getLiveSharedPortal(sessionProvider);
114     Node currentPortalNode = getCurrentPortalNode(jcrPath,
115                                                   runningPortal,
116                                                   sharedPortal);
117     if (currentFolder.length() == 0 || "/".equals(currentFolder))
118       return buildXMLResponseForRoot(currentPortalNode, sharedPortal, command);
119     String currentPortalRelPath = "/" + currentPortalNode.getName() + "/";
120     String sharePortalRelPath = "/" + sharedPortal.getName() + "/";
121     if (!currentPortalNode.getPath().equals(sharedPortal.getPath())
122         && currentFolder.startsWith(sharePortalRelPath)) {
123       if (currentFolder.equals(sharePortalRelPath)) {
124         return buildXMLResponseForPortal(sharedPortal, null, command);
125       }
126       Node currentContentStorageNode = getCorrectContentStorage(sharedPortal, null, currentFolder);
127       return buildXMLResponseForContentStorage(currentContentStorageNode, command);
128     } else if (!currentPortalNode.getPath().equals(sharedPortal.getPath())
129         && currentFolder.startsWith(currentPortalRelPath)) {
130       return buildXMLResponseCommon(currentPortalNode, null, currentFolder, command);
131     } else {
132       return buildXMLResponseCommon(sharedPortal, null, currentFolder, command);
133     }
134   }
135 
136   /*
137    * (non-Javadoc)
138    * @seeorg.exoplatform.wcm.connector.fckeditor.BaseConnector#
139    * buildXMLResponseForContentStorage(javax.jcr.Node, java.lang.String)
140    */
141   protected Response buildXMLResponseForContentStorage(Node node, String command) throws Exception {
142     Element rootElement = FCKUtils.createRootElement(command,
143                                                      node,
144                                                      folderHandler.getFolderType(node));
145     Document document = rootElement.getOwnerDocument();
146     Element folders = document.createElement("Foders");
147     Element files = document.createElement("Files");
148     for (NodeIterator iterator = node.getNodes(); iterator.hasNext();) {
149       Node child = iterator.nextNode();
150       if (child.isNodeType(FCKUtils.EXO_HIDDENABLE))
151         continue;
152       String folderType = folderHandler.getFolderType(child);
153       if (folderType != null) {
154         Element folder = folderHandler.createFolderElement(document, child, folderType);
155         folders.appendChild(folder);
156       }
157       String sourceType = getContentStorageType();
158       String fileType = linkFileHandler.getFileType(child, sourceType);
159       if (fileType != null) {
160         Element file = linkFileHandler.createFileElement(document, child, fileType);
161         files.appendChild(file);
162       }
163     }
164     rootElement.appendChild(folders);
165     rootElement.appendChild(files);
166     return getResponse(document);
167   }
168 
169   /*
170    * (non-Javadoc)
171    * @see
172    * org.exoplatform.wcm.connector.fckeditor.BaseConnector#getRootContentStorage
173    * (javax.jcr.Node)
174    */
175   @Override
176   protected Node getRootContentStorage(Node parentNode) throws Exception {
177     try {
178       PortalFolderSchemaHandler folderSchemaHandler = webSchemaConfigService.
179           getWebSchemaHandlerByType(PortalFolderSchemaHandler.class);
180       return folderSchemaHandler.getLinkFolder(parentNode);
181     } catch (Exception e) {
182       if (LOG.isDebugEnabled()) {
183         LOG.debug(e);
184       }
185       return null;
186     }
187   }
188 
189   /*
190    * (non-Javadoc)
191    * @see
192    * org.exoplatform.wcm.connector.fckeditor.BaseConnector#getContentStorageType
193    * ()
194    */
195   @Override
196   protected String getContentStorageType() throws Exception {
197     return FCKUtils.LINK_TYPE;
198   }
199 }