SiteImporterServlet.java
8.85 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.RepositoryException
* javax.servlet.Servlet
* javax.servlet.ServletException
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.ReferenceCardinality
* org.apache.felix.scr.annotations.ReferencePolicy
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.osgi.framework.BundleContext
* org.osgi.service.component.ComponentContext
*/
package com.day.cq.wcm.siteimporter.internal.servlet;
import com.day.cq.wcm.siteimporter.ImporterContext;
import com.day.cq.wcm.siteimporter.ImporterStep;
import com.day.cq.wcm.siteimporter.ResourcesBoard;
import com.day.cq.wcm.siteimporter.internal.resource.ImporterResource;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.jcr.RepositoryException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
@Component(metatype=0)
@Service(value={Servlet.class})
@Properties(value={@Property(name="sling.servlet.paths", value={"/libs/wcm/bin/siteimporter"}, propertyPrivate=1), @Property(name="sling.servlet.methods", value={"POST"}), @Property(name="service.description", value={"Site Importer POST Servlet"})})
public class SiteImporterServlet
extends SlingAllMethodsServlet {
private static final long serialVersionUID = 9112167990766136591L;
private BundleContext bundleContext;
@Reference(name="Step", cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC, referenceInterface=ImporterStep.class)
private List<ImporterStep> steps = new ArrayList<ImporterStep>();
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
URL original;
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
try {
original = new URL(request.getParameter("original"));
}
catch (MalformedURLException e) {
throw new ServletException("Url was malformed, please correct.");
}
ArrayList<ImporterStep> tasks = new ArrayList<ImporterStep>();
List<ImporterStep> list = this.steps;
synchronized (list) {
tasks.addAll(this.steps);
}
Collections.sort(tasks, new Comparator<ImporterStep>(){
@Override
public int compare(ImporterStep step1, ImporterStep step2) {
return step1.getOrder().compareTo(step2.getOrder());
}
});
PrintWriter out = response.getWriter();
out.write("<html><head>");
out.write("<style type=\"text/css\">");
out.write("div {");
out.write(" font-family:arial,tahoma,helvetica,sans-serif;");
out.write(" font-size:11px;");
out.write(" white-space:nowrap;");
out.write("}");
out.write(".action {");
out.write(" display: inline;");
out.write(" width: 120px;");
out.write(" float: left;");
out.write("}");
out.write(".error {");
out.write(" color: red;");
out.write(" font-weight: bold;");
out.write("}");
out.write(".skip {");
out.write(" color: gray;");
out.write(" font-style: italic;");
out.write("}");
out.write(".heading {");
out.write(" font-weight: bold;");
out.write(" cursor: pointer;");
out.write("}");
out.write(".title {");
out.write(" display: inline;");
out.write(" width: 150px;");
out.write(" float: left;");
out.write(" margin: 0 8px 0 0;");
out.write(" overflow: hidden;");
out.write("}");
out.write(".activate {");
out.write("}");
out.write(".ignore {");
out.write(" color: #888888;");
out.write("}");
out.write(".cf {");
out.write(" color: #888888;");
out.write("}");
out.write(".path {");
out.write(" display: inline;");
out.write(" width: 100%;");
out.write("}");
out.write("ol {");
out.write(" list-style-type:none;");
out.write(" margin:0px 0px 0px 10px;");
out.write(" padding:0px;");
out.write("}");
out.write("</style>");
out.write("<script type=\"text/javascript\">\n");
out.write("var success = false;\n\n");
out.write("function finish(result) {\n");
out.write(" success = result;\n");
out.write(" parent.CQ.Ext.getCmp(\"cq-siteimporter-progress\").reset(true);\n");
out.write("}\n\n");
out.write("function toggleDetails(heading) {\n");
out.write(" var details = heading.nextSibling;\n");
out.write(" while (details != null) {\n");
out.write(" if (details.tagName && (details.tagName.toLowerCase() == \"ol\")) {\n");
out.write(" if (details.style.display == \"none\") {\n");
out.write(" details.style.display = \"block\";\n");
out.write(" } else {\n");
out.write(" details.style.display = \"none\";\n");
out.write(" }\n");
out.write(" break;\n");
out.write(" } else {\n");
out.write(" details = details.nextSibling;\n");
out.write(" }\n");
out.write(" }\n");
out.write("}\n\n");
out.write("parent.CQ.Ext.getCmp(\"cq-siteimporter-progress\").show();\n");
out.write("parent.CQ.Ext.getCmp(\"cq-siteimporter-progress\").wait({interval:200, increment:40});\n");
out.write("</script>\n");
out.write("</head><body><div><ol style=\"margin:0\">");
ImporterContext ctx = ImporterContext.createContext(request, response, this.bundleContext);
long time = new Date().getTime();
boolean success = true;
String errMsg = "";
Iterator it = tasks.iterator();
while (success && it.hasNext()) {
ImporterStep next = (ImporterStep)it.next();
try {
success = next.execute(ctx);
}
catch (Exception e) {
success = false;
errMsg = " " + e.getMessage();
this.log("Error while importing site", (Throwable)e);
}
}
time = (new Date().getTime() - time) / 1000;
if (success) {
ctx.output("Import successfully finished in " + time + " seconds", "heading");
} else {
ctx.repairIndent();
ctx.error("An error occured during the import.");
ctx.error(errMsg);
ctx.error("Please check the log for details.");
}
out.write("</ol></div>");
if (success) {
ImporterResource origRes = ctx.getBoard().getResource(original, ctx);
out.write("<script language=\"javascript\">finish(\"" + origRes.getDownloadLocation() + "\");</script>");
} else {
out.write("<script language=\"javascript\">finish(null);</script>");
}
out.write("</body></html>");
ctx.dispose();
}
protected void activate(ComponentContext context) throws RepositoryException {
this.bundleContext = context.getBundleContext();
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void bindStep(ImporterStep step) {
List<ImporterStep> list = this.steps;
synchronized (list) {
this.steps.add(step);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void unbindStep(ImporterStep step) {
List<ImporterStep> list = this.steps;
synchronized (list) {
this.steps.remove(step);
}
}
}