LicenseServlet.java
14.3 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.servlet.GenericServlet
* javax.servlet.Servlet
* javax.servlet.ServletException
* javax.servlet.ServletOutputStream
* javax.servlet.ServletRequest
* javax.servlet.ServletResponse
* javax.servlet.http.HttpServletRequest
* javax.servlet.http.HttpServletResponse
* org.apache.commons.lang3.StringEscapeUtils
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.osgi.framework.Version
*/
package com.adobe.granite.license.impl;
import com.adobe.granite.license.License;
import com.adobe.granite.license.ProductInfo;
import com.adobe.granite.license.ProductInfoService;
import com.adobe.granite.license.impl.LicenseImpl;
import com.adobe.granite.license.impl.LicenseProvider;
import com.adobe.granite.license.impl.ResourceFilteringWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.GenericServlet;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.osgi.framework.Version;
@Component
@Service(value={Servlet.class})
@Property(name="alias", value={"/system/granite/license"})
public class LicenseServlet
extends GenericServlet {
private static final long serialVersionUID = 414064471981385189L;
public static final String PATH = "/system/granite/license";
private static final int ERROR_NONE = 0;
private static final int ERROR_VALID = -1;
private static final int ERROR_EULA = 1;
private static final int ERROR_LICENSE = 2;
private static final int ERROR_NAME = 4;
@Reference
private ProductInfoService productInfoService;
@Reference
private LicenseProvider licenseProvider;
private EULA[] eulas;
private String eulasText;
@Activate
protected void activate() {
this.readEulas();
StringBuilder builder = new StringBuilder();
for (EULA e : this.eulas) {
builder.append("<textarea id=\"eula-");
builder.append(e.label);
builder.append("\" class=\"eula-hidden\">");
builder.append(e.text);
builder.append("</textarea>");
}
this.eulasText = builder.toString();
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
* Loose catch block
* Enabled aggressive block sorting
* Enabled unnecessary exception pruning
* Enabled aggressive exception aggregation
* Lifted jumps to return sites
*/
private void readEulas() {
block12 : {
InputStream is = this.getResourceAsStream("templates", "eulas.txt");
if (is != null) {
String line;
ArrayList<EULA> eulas = new ArrayList<EULA>();
InputStreamReader reader = new InputStreamReader(is, "UTF-8");
LineNumberReader lnr = new LineNumberReader(reader);
while ((line = lnr.readLine()) != null) {
int pos = line.indexOf(58);
if (pos == -1) continue;
eulas.add(new EULA(line.substring(0, pos), line.substring(pos + 1)));
}
this.eulas = eulas.toArray(new EULA[eulas.size()]);
try {
is.close();
}
catch (IOException ignore) {}
break block12;
catch (IOException ioe) {
try {
is.close();
}
catch (IOException ignore) {}
catch (Throwable throwable) {
try {
is.close();
throw throwable;
}
catch (IOException ignore) {
// empty catch block
}
throw throwable;
}
}
}
}
if (this.eulas != null) return;
this.eulas = new EULA[]{new EULA("en_US", "English"), new EULA("fr_FR", "Français"), new EULA("de_DE", "Deutsch"), new EULA("jp_JP", "日本語")};
}
private InputStream getResourceAsStream(String category, String name, String prefix) {
StringBuilder sb = new StringBuilder();
sb.append('/');
sb.append(prefix);
if (!category.startsWith("/")) {
sb.append('/');
}
sb.append(category);
if (!name.startsWith("/")) {
sb.append('/');
}
sb.append(name);
String resourceName = sb.toString();
return this.getClass().getResourceAsStream(resourceName);
}
private InputStream getResourceAsStream(String category, String name) {
int lastSlash;
String pckName = this.getClass().getPackage().getName().replace('.', '/');
InputStream result = this.getResourceAsStream(category, name, pckName.substring(0, lastSlash = pckName.lastIndexOf(47)));
if (result == null) {
result = this.getResourceAsStream(category, name, pckName);
}
return result;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private String readTemplate(String name) throws IOException {
InputStream in = this.getResourceAsStream("templates", name);
if (in == null) {
return null;
}
try {
int len;
InputStreamReader reader = new InputStreamReader(in, "UTF-8");
StringBuilder sb = new StringBuilder();
char[] buffer = new char[2048];
while ((len = reader.read(buffer)) > 0) {
sb.append(buffer, 0, len);
}
String string = sb.toString();
return string;
}
finally {
try {
in.close();
}
catch (IOException ignore) {}
}
}
public void service(ServletRequest sreq, ServletResponse sres) throws ServletException, IOException {
HttpServletRequest req = (HttpServletRequest)sreq;
HttpServletResponse res = (HttpServletResponse)sres;
String pathInfo = req.getPathInfo();
if (pathInfo == null || pathInfo.length() == 0 || pathInfo.endsWith("/")) {
res.sendRedirect(req.getContextPath() + req.getServletPath() + req.getPathInfo() + "index.html");
return;
}
if (!pathInfo.endsWith(".html")) {
this.doResource(req, res, pathInfo);
} else if ("/index.html".equals(pathInfo)) {
this.doService(req, res);
} else {
res.sendError(404);
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void doResource(HttpServletRequest req, HttpServletResponse res, String pathInfo) throws ServletException, IOException {
InputStream is = this.getResourceAsStream("resources", pathInfo);
if (is == null) {
res.sendError(404);
} else {
String type = null;
if (pathInfo.endsWith(".gif")) {
type = "image/gif";
} else if (pathInfo.endsWith(".css")) {
type = "text/css";
} else if (pathInfo.endsWith(".png")) {
type = "image/png";
} else if (pathInfo.endsWith(".ico")) {
type = "image/vnd.microsoft.icon";
}
if (type != null) {
res.setContentType(type);
}
ServletOutputStream os = res.getOutputStream();
try {
int l;
byte[] buf = new byte[2048];
while ((l = is.read(buf)) > 0) {
os.write(buf, 0, l);
}
}
finally {
try {
is.close();
}
catch (IOException ignore) {}
}
}
}
private String getParameter(HttpServletRequest req, String name, String defaultValue) {
String value = req.getParameter(name);
if (value != null) {
return value;
}
return defaultValue;
}
public void doService(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
String templateName;
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
License lic = this.licenseProvider.getLicense();
ProductInfo info = this.productInfoService.getInfos()[0];
HashMap<String, String> props = new HashMap<String, String>();
props.put("redirect", req.getContextPath() + "/");
props.put("product.name", StringEscapeUtils.escapeHtml4((String)info.getName()));
props.put("product.version", StringEscapeUtils.escapeHtml4((String)info.getVersion().toString()));
props.put("error", "");
String language = this.getParameter(req, "language", this.eulas[0].label);
props.put("language", StringEscapeUtils.escapeHtml4((String)language));
props.put("options", this.buildOptionText(language));
props.put("eulas", this.eulasText);
int error = 0;
if ("POST".equals(req.getMethod())) {
if (lic != null) {
response.sendError(405);
return;
}
String eula = this.getParameter(req, "eula", "");
String customer = this.getParameter(req, "name", "");
String id = this.getParameter(req, "id", "");
props.put("customer", StringEscapeUtils.escapeHtml4((String)customer));
props.put("id", StringEscapeUtils.escapeHtml4((String)id));
if (id.length() == 0) {
error += 2;
}
if (customer.length() == 0) {
error += 4;
}
if (!"on".equals(eula)) {
++error;
}
if (error == 0) {
HashMap<String, String> licenseProps = new HashMap<String, String>();
licenseProps.put("license.customer.name", customer);
licenseProps.put("license.downloadID", id);
licenseProps.put("license.product.name", info.getName());
licenseProps.put("license.product.version", info.getVersion().toString());
lic = LicenseImpl.createLicense(licenseProps);
if (lic == null) {
error = 2;
} else {
try {
this.licenseProvider.saveLicense(lic);
}
catch (IOException e) {
lic = null;
}
error = lic == null ? 2 : -1;
}
}
} else {
if (lic != null) {
response.sendRedirect(req.getContextPath() + "/system/console/productinfo");
return;
}
props.put("customer", "");
props.put("id", "");
error = 0;
}
if (error == -1) {
templateName = "panel.txt";
} else {
templateName = "form.txt";
if (error != 0) {
StringBuilder sb = new StringBuilder();
if ((error & 1) == 1) {
sb.append("markField('eula');");
}
if ((error & 4) == 4) {
sb.append("markField('name');");
}
if ((error & 2) == 2) {
sb.append("markField('license');");
}
props.put("error", sb.toString());
}
}
String template = this.readTemplate(templateName);
if (template == null) {
response.sendError(500);
return;
}
ResourceFilteringWriter pw = new ResourceFilteringWriter(response.getWriter(), props);
pw.write(template);
}
private String buildOptionText(String language) {
StringBuilder optBuilder = new StringBuilder();
for (EULA e : this.eulas) {
optBuilder.append("<option");
if (language.equals(e.label)) {
optBuilder.append(" selected");
}
optBuilder.append(" value=\"");
optBuilder.append(e.label);
optBuilder.append("\">");
optBuilder.append(e.name);
optBuilder.append("</option>");
}
return optBuilder.toString();
}
protected void bindProductInfoService(ProductInfoService productInfoService) {
this.productInfoService = productInfoService;
}
protected void unbindProductInfoService(ProductInfoService productInfoService) {
if (this.productInfoService == productInfoService) {
this.productInfoService = null;
}
}
protected void bindLicenseProvider(LicenseProvider licenseProvider) {
this.licenseProvider = licenseProvider;
}
protected void unbindLicenseProvider(LicenseProvider licenseProvider) {
if (this.licenseProvider == licenseProvider) {
this.licenseProvider = null;
}
}
private final class EULA {
private final String label;
private final String name;
private String text;
private EULA(String label, String name) {
this.label = label;
this.name = name;
try {
this.text = LicenseServlet.this.readTemplate("eula-" + label + ".txt");
}
catch (IOException e) {
this.text = "(resource not loaded)";
}
}
}
}