OfflineServlet.java
10.4 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.servlets.HtmlStatusResponseHelper
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.api.Template
* com.day.cq.wcm.api.components.Component
* com.day.cq.wcm.api.components.ComponentManager
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.servlet.ServletException
* javax.servlet.ServletOutputStream
* javax.servlet.http.HttpServletResponse
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.request.RequestParameter
* org.apache.sling.api.request.RequestPathInfo
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.api.servlets.HtmlResponse
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.offline;
import com.day.cq.commons.servlets.HtmlStatusResponseHelper;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.Template;
import com.day.cq.wcm.api.components.ComponentManager;
import com.day.cq.wcm.offline.OfflineExporter;
import com.day.cq.wcm.offline.OfflineImporter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.request.RequestPathInfo;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.HtmlResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service
@Properties(value={@org.apache.felix.scr.annotations.Property(name="sling.servlet.resourceTypes", value={"sling/servlet/default"}), @org.apache.felix.scr.annotations.Property(name="sling.servlet.paths", value={"/libs/wcm/offline/import"}), @org.apache.felix.scr.annotations.Property(name="sling.servlet.methods", value={"POST", "GET"}), @org.apache.felix.scr.annotations.Property(name="sling.servlet.selectors", value={"offline", "offlinecomponents"}), @org.apache.felix.scr.annotations.Property(name="sling.servlet.extensions", value={"doc", "json"})})
public class OfflineServlet
extends SlingAllMethodsServlet {
private static final long serialVersionUID = -7939838253258809729L;
private static final Logger log = LoggerFactory.getLogger(OfflineServlet.class);
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
HtmlResponse htmlResponse;
block11 : {
htmlResponse = null;
if (request.getParameter("parent") != null) {
String parentPath = request.getParameter("parent");
Node node = (Node)request.getResourceResolver().getResource(parentPath).adaptTo(Node.class);
if (node == null) {
htmlResponse = HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)"Parent node not found");
} else if (request.getRequestParameter("document") != null && request.getRequestParameter("document").getInputStream() != null) {
try {
String templatePath;
ValueMap config = this.getConfig(request);
String string = templatePath = config != null ? (String)config.get((Object)"template") : null;
if (templatePath == null) {
log.error("Required template parameter not given");
htmlResponse = HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)"Required template parameter not given");
break block11;
}
PageManager pm = (PageManager)request.getResourceResolver().adaptTo(PageManager.class);
Template template = pm.getTemplate(templatePath);
Node tNode = (Node)template.adaptTo(Node.class);
String resourceType = tNode.getNode("jcr:content").getProperty("sling:resourceType").getString();
OfflineImporter importer = new OfflineImporter(node, request.getRequestParameter("document"));
importer.setFilename(this.formatFileName(request.getRequestParameter("document").getFileName()));
importer.setResourceType(resourceType);
importer.setTemplate(templatePath);
importer.setConfig(config);
try {
importer.importDocument();
node.getSession().save();
}
finally {
node.refresh(false);
}
htmlResponse = HtmlStatusResponseHelper.createStatusResponse((boolean)true, (String)"Document got imported");
}
catch (Exception e) {
log.error("Error while importing document", (Throwable)e);
htmlResponse = HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)("Error while importing document: " + e.getMessage()));
}
}
} else {
htmlResponse = HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)"Cannot import due of missing document");
}
}
htmlResponse.send((HttpServletResponse)response, true);
}
private String formatFileName(String fileName) {
if (fileName.contains("\\")) {
return fileName.substring(fileName.lastIndexOf("\\") + 1);
}
return fileName;
}
private ValueMap getConfig(SlingHttpServletRequest request) {
String configPath = request.getParameter("configPath");
if (configPath != null) {
Resource config = request.getResourceResolver().getResource(configPath);
return (ValueMap)config.adaptTo(ValueMap.class);
}
return null;
}
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
if (request.getRequestPathInfo().getSelectorString().indexOf("offlinecomponents") >= 0) {
try {
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
ResourceResolver resolver = request.getResourceResolver();
ComponentManager compMgr = (ComponentManager)resolver.adaptTo(ComponentManager.class);
ArrayList<JSONObject> components = new ArrayList<JSONObject>();
for (com.day.cq.wcm.api.components.Component cmp : compMgr.getComponents()) {
JSONObject o = new JSONObject();
String title = cmp.getTitle();
String text = (title == null || title.length() == 0 ? "" : new StringBuilder().append(title).append(" ").toString()) + "(" + cmp.getPath() + ")";
o.put("text", (Object)text);
o.put("value", (Object)cmp.getPath());
components.add(o);
}
Collections.sort(components, new Comparator<JSONObject>(){
@Override
public int compare(JSONObject one, JSONObject two) {
try {
return one.getString("text").compareToIgnoreCase(two.getString("text"));
}
catch (JSONException ex) {
return 0;
}
}
});
JSONObject result = new JSONObject();
result.put("results", components.size());
result.put("components", components);
result.write((Writer)response.getWriter());
}
catch (JSONException ex) {
log.error("Error while getting components", (Throwable)ex);
throw new ServletException((Throwable)ex);
}
}
String parentPath = request.getParameter("parent");
Node node = parentPath == null ? (Node)request.getResource().adaptTo(Node.class) : (Node)request.getResourceResolver().getResource(parentPath).adaptTo(Node.class);
if (node != null) {
try {
int level = 0;
String subpages = request.getParameter("subpages");
if (subpages != null && !"".equals(subpages)) {
try {
level = Integer.parseInt(subpages);
}
catch (NumberFormatException e) {
throw new ServletException((Throwable)e);
}
}
response.setContentType("application/msword");
OfflineExporter.export(node, level, (OutputStream)response.getOutputStream());
}
catch (RepositoryException e) {
throw new ServletException((Throwable)e);
}
}
response.sendError(404);
}
}