JSONCreatePageServlet.java
4.15 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.api.WCMException
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.servlet.ServletException
* org.apache.felix.scr.annotations.sling.SlingServlet
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.mcm.campaign.servlets;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.WCMException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SlingServlet(paths={"/libs/mcm/campaign/content/createPage"}, methods={"POST"})
public class JSONCreatePageServlet
extends SlingAllMethodsServlet {
private static final String METADATA_PARAM = "metadataURL";
private static final String SEEDDATA_PARAM = "dataURL";
private final Logger log;
public JSONCreatePageServlet() {
this.log = LoggerFactory.getLogger(this.getClass());
}
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
String parentPath = request.getParameter("parentPath");
String pageLabel = request.getParameter("label");
String template = request.getParameter("template");
String pageTitle = request.getParameter("title");
String metaDataURL = request.getParameter("metadataURL");
String dataURL = request.getParameter("dataURL");
try {
PageManager pageManager = (PageManager)request.getResourceResolver().adaptTo(PageManager.class);
Page page = pageManager.create(parentPath, pageLabel, template, pageTitle);
Node node = (Node)page.adaptTo(Node.class);
node = node.getNode("jcr:content");
Session session = null;
try {
session = node.getSession();
if (metaDataURL != null && metaDataURL.length() > 0) {
node.setProperty("acMetaDataURL", metaDataURL);
}
if (dataURL != null && dataURL.length() > 0) {
node.setProperty("acDataURL", dataURL);
}
session.save();
}
catch (RepositoryException re) {
node.remove();
throw re;
}
finally {
if (session != null) {
session.refresh(false);
}
}
response.setContentType("application/json");
JSONWriter writer = new JSONWriter((Writer)response.getWriter());
try {
writer.object();
writer.key("path").value((Object)page.getPath());
writer.endObject();
}
catch (JSONException je) {
throw new ServletException((Throwable)je);
}
}
catch (RepositoryException re) {
response.sendError(500);
this.log.error("Repository issue while creating a new page", (Throwable)re);
}
catch (WCMException wcme) {
response.sendError(500);
this.log.error("WCM issue while creating a new page", (Throwable)wcme);
}
}
}