PageExporterImpl.java
11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.jcr.JcrUtil
* com.day.cq.contentsync.ContentSyncManager
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.WCMException
* com.day.cq.wcm.api.designer.Designer
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.servlet.http.HttpServletResponse
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.jackrabbit.api.JackrabbitSession
* org.apache.jackrabbit.api.security.principal.PrincipalManager
* org.apache.jackrabbit.api.security.user.Authorizable
* org.apache.jackrabbit.api.security.user.UserManager
* org.apache.jackrabbit.commons.jackrabbit.authorization.AccessControlUtils
* org.apache.jackrabbit.util.Text
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.jcr.resource.JcrResourceUtil
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.contentsync.impl;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.contentsync.ContentSyncManager;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.api.designer.Designer;
import com.day.cq.wcm.contentsync.PageExporter;
import java.io.IOException;
import java.security.Principal;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.http.HttpServletResponse;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.api.JackrabbitSession;
import org.apache.jackrabbit.api.security.principal.PrincipalManager;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.jackrabbit.commons.jackrabbit.authorization.AccessControlUtils;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.jcr.resource.JcrResourceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(label="Day CQ Page Exporter")
@Service
public class PageExporterImpl
implements PageExporter {
private static final Logger log = LoggerFactory.getLogger(PageExporterImpl.class);
private static final String TEMPLATE_PROPERTY = "cq:exportTemplate";
private static final String DEFAULT_TEMPLATE_PATH = "/etc/contentsync/templates/default";
private static final String ZIPS_SUFFIX = "-zips";
@Reference
private ContentSyncManager contentSyncManager;
@Reference
private ResourceResolverFactory resolverFactory;
@Override
public void export(Page page, ResourceResolver resolver, HttpServletResponse response) throws WCMException {
try {
ResourceResolver serviceResolver = this.resolverFactory.getServiceResourceResolver(null);
String configPath = "/var/contentsync/tmp" + page.getPath() + "_" + System.currentTimeMillis();
this.createConfig(page, configPath, resolver, serviceResolver);
Resource config = serviceResolver.getResource(configPath);
Session userSession = (Session)resolver.adaptTo(Session.class);
String zipPath = this.contentSyncManager.getZip(config, null, userSession);
String zipFileRequestURI = this.getZipFileRequestURI(configPath, zipPath);
this.contentSyncManager.sendZip(userSession, response, zipFileRequestURI);
this.cleanup(configPath, zipPath, serviceResolver);
}
catch (Exception e) {
throw new WCMException((Throwable)e);
}
}
@Override
public void export(Page page, SlingHttpServletRequest request, HttpServletResponse response) throws WCMException {
try {
ResourceResolver serviceResolver = this.resolverFactory.getServiceResourceResolver(null);
String configPath = "/var/contentsync/tmp" + page.getPath() + "_" + System.currentTimeMillis();
this.createConfig(page, configPath, request.getResourceResolver(), serviceResolver);
Session serviceSession = (Session)serviceResolver.adaptTo(Session.class);
if (serviceSession.nodeExists(configPath + "/page")) {
Node paramNode = JcrResourceUtil.createPath((String)(configPath + "/page/parameters"), (String)"nt:unstructured", (String)"nt:unstructured", (Session)serviceSession, (boolean)false);
for (Object param : request.getParameterMap().keySet()) {
String[] values = request.getParameterValues((String)param);
if (values.length == 1) {
paramNode.setProperty((String)param, values);
continue;
}
if (values.length <= 1) continue;
paramNode.setProperty((String)param, values);
}
serviceSession.save();
}
Resource config = serviceResolver.getResource(configPath);
Session userSession = (Session)request.getResourceResolver().adaptTo(Session.class);
String zipPath = this.contentSyncManager.getZip(config, null, userSession);
String zipFileRequestURI = this.getZipFileRequestURI(configPath, zipPath);
this.contentSyncManager.sendZip(userSession, response, zipFileRequestURI);
this.cleanup(configPath, zipPath, serviceResolver);
}
catch (Exception e) {
throw new WCMException((Throwable)e);
}
}
@Override
public void export(Page page, ResourceResolver resolver, String targetPath) throws WCMException {
try {
ResourceResolver serviceResolver = this.resolverFactory.getServiceResourceResolver(null);
String configPath = "/var/contentsync/tmp" + page.getPath() + System.currentTimeMillis();
this.createConfig(page, configPath, resolver, serviceResolver);
Session session = (Session)resolver.adaptTo(Session.class);
Resource config = serviceResolver.getResource(configPath);
String zipPath = this.contentSyncManager.getZip(config, null, session);
session.move(zipPath, targetPath);
session.save();
this.cleanup(configPath, zipPath, serviceResolver);
}
catch (Exception e) {
throw new WCMException((Throwable)e);
}
}
protected void createConfig(Page page, String configPath, ResourceResolver resolver, ResourceResolver serviceResolver) throws RepositoryException, IOException {
PrincipalManager pm;
Principal principal;
Session serviceSession = (Session)serviceResolver.adaptTo(Session.class);
Designer designer = (Designer)resolver.adaptTo(Designer.class);
String designPath = designer.getDesignPath(page);
Node configNode = JcrResourceUtil.createPath((String)configPath, (String)"sling:Folder", (String)"cq:ContentSyncConfig", (Session)serviceSession, (boolean)false);
configNode.setProperty("personalized", true);
configNode.setProperty("authorizable", resolver.getUserID());
Session userSession = (Session)resolver.adaptTo(Session.class);
JackrabbitSession jrSession = (JackrabbitSession)userSession;
Authorizable authorizable = jrSession.getUserManager().getAuthorizable(userSession.getUserID());
if (null != authorizable && (principal = (pm = jrSession.getPrincipalManager()).getPrincipal(authorizable.getPrincipal().getName())) != null) {
AccessControlUtils.addAccessControlEntry((Session)serviceSession, (String)configPath, (Principal)principal, (String[])new String[]{"{http://www.jcp.org/jcr/1.0}read", "{http://www.jcp.org/jcr/1.0}addChildNodes", "{http://www.jcp.org/jcr/1.0}modifyProperties"}, (boolean)true);
}
String templatePath = this.findConfigTemplate(page, (Session)resolver.adaptTo(Session.class));
log.debug("Content sync configuration template selected: {}", (Object)templatePath);
Node templateNode = userSession.getNode(templatePath);
NodeIterator nodes = templateNode.getNodes();
while (nodes.hasNext()) {
Node n = nodes.nextNode();
JcrUtil.copy((Node)n, (Node)configNode, (String)n.getName(), (boolean)true);
}
if (configNode.hasNode("design") && designPath != null && designPath.length() > 0) {
Node designNode = configNode.getNode("design");
designNode.setProperty("path", designPath);
}
if (configNode.hasNode("page")) {
Node pageNode = configNode.getNode("page");
pageNode.setProperty("path", page.getPath());
}
JackrabbitSession jrServiceSession = (JackrabbitSession)serviceSession;
jrServiceSession.save();
}
protected void cleanup(String configPath, String zipPath, ResourceResolver serviceResolver) throws RepositoryException {
Session serviceSession = (Session)serviceResolver.adaptTo(Session.class);
Node configNode = serviceSession.getNode(configPath);
configNode.remove();
String zipsCachePath = Text.getRelativeParent((String)zipPath, (int)1);
String cachePath = zipsCachePath.substring(0, zipsCachePath.lastIndexOf("-zips"));
serviceSession.getNode(zipsCachePath).remove();
serviceSession.getNode(cachePath).remove();
serviceSession.save();
}
protected String findConfigTemplate(Page page, Session session) throws RepositoryException {
String templatePath = null;
do {
String path;
ValueMap props;
if ((path = (String)(props = ResourceUtil.getValueMap((Resource)page.getContentResource())).get("cq:exportTemplate", String.class)) == null || !session.nodeExists(path)) continue;
templatePath = path;
break;
} while ((page = page.getParent()) != null);
return templatePath != null ? templatePath : "/etc/contentsync/templates/default";
}
protected String getZipFileRequestURI(String configPath, String zipPath) throws RepositoryException {
String zipFileRequestURI = Text.getRelativeParent((String)configPath, (int)1) + "/" + Text.getName((String)zipPath);
return zipFileRequestURI;
}
protected void bindContentSyncManager(ContentSyncManager contentSyncManager) {
this.contentSyncManager = contentSyncManager;
}
protected void unbindContentSyncManager(ContentSyncManager contentSyncManager) {
if (this.contentSyncManager == contentSyncManager) {
this.contentSyncManager = null;
}
}
protected void bindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
this.resolverFactory = resourceResolverFactory;
}
protected void unbindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
if (this.resolverFactory == resourceResolverFactory) {
this.resolverFactory = null;
}
}
}