1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
44
45
46
47 @Path("/wcmLink/")
48 public class LinkConnector extends BaseConnector implements ResourceContainer {
49
50
51 private LinkFileHandler linkFileHandler;
52
53
54 private static final Log LOG = ExoLogger.getLogger(LinkConnector.class.getName());
55
56
57 public LinkConnector() {
58 linkFileHandler = new LinkFileHandler();
59 }
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 @GET
77 @Path("/getFoldersAndFiles/")
78
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
102
103
104
105
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
138
139
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
171
172
173
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
191
192
193
194
195 @Override
196 protected String getContentStorageType() throws Exception {
197 return FCKUtils.LINK_TYPE;
198 }
199 }