HtmlLibraryServlet.java
6.82 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.servlet.Servlet
* javax.servlet.ServletOutputStream
* org.apache.commons.io.IOUtils
* 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.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.request.RequestPathInfo
* org.apache.sling.api.servlets.SlingSafeMethodsServlet
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.ui.clientlibs.impl;
import com.adobe.granite.ui.clientlibs.HtmlLibrary;
import com.adobe.granite.ui.clientlibs.HtmlLibraryManager;
import com.adobe.granite.ui.clientlibs.LibraryType;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.Servlet;
import javax.servlet.ServletOutputStream;
import org.apache.commons.io.IOUtils;
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.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.request.RequestPathInfo;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service(value={Servlet.class})
@Properties(value={@Property(name="sling.servlet.resourceTypes", value={"widgets/clientlib", "cq/ClientLibraryFolder"}), @Property(name="service.description", value={"service.description"})})
public class HtmlLibraryServlet
extends SlingSafeMethodsServlet {
private static final Logger log = LoggerFactory.getLogger(HtmlLibraryServlet.class);
private static final String DEBUG = "debug";
private static final int MAX_CSS_FILES_TO_INCLUDE = 31;
@Reference
protected HtmlLibraryManager libMgr;
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
try {
HtmlLibrary library = this.libMgr.getLibrary(request);
if (library == null) {
response.sendError(404);
return;
}
if (HtmlLibraryServlet.isDebug(request)) {
log.debug("Creating {} debug library for {}", (Object)library.getType(), (Object)library.getLibraryPath());
if (library.getType() == LibraryType.JS) {
HtmlLibraryServlet.createJsDebugScript(library, request, response, false);
} else {
HtmlLibraryServlet.createCssDebugScript(library, request, response, false);
}
log.debug("Created {} debug library for {}", (Object)library.getType(), (Object)library.getLibraryPath());
} else {
library.send(request, response);
}
}
catch (Exception e) {
log.error("Cannot create library for {}", (Object)request.getRequestURI(), (Object)e);
response.sendError(404);
}
}
protected static boolean isDebug(SlingHttpServletRequest request) {
return "true".equals(request.getParameter("debug"));
}
protected static String getLoaderScript() throws IOException {
InputStream is = HtmlLibraryServlet.class.getResourceAsStream("/com/adobe/granite/ui/clientlibs/impl/Loader.js");
return IOUtils.toString((InputStream)is);
}
protected static void createJsDebugScript(HtmlLibrary library, SlingHttpServletRequest request, SlingHttpServletResponse response, boolean proxy) throws Exception {
response.setContentType("application/x-javascript");
StringBuilder scriptList = new StringBuilder();
String delim = "";
for (String script : library.getScripts()) {
scriptList.append(delim).append('\"');
if (proxy && (script.startsWith("/libs/") || script.startsWith("/apps/"))) {
scriptList.append("/etc.clientlibs").append(script.substring(5));
} else {
scriptList.append(script);
}
scriptList.append('\"');
delim = "\n,";
}
String loaderScript = HtmlLibraryServlet.getLoaderScript();
loaderScript = loaderScript.replaceAll("__JS_FILES__", scriptList.toString());
loaderScript = loaderScript.replaceAll("__RESOURCE_PATH__", request.getContextPath());
response.getWriter().write(loaderScript);
}
protected static void createCssDebugScript(HtmlLibrary library, SlingHttpServletRequest request, SlingHttpServletResponse response, boolean proxy) throws Exception {
response.setContentType(library.getType().contentType);
List<String> cssFiles = library.getScripts();
Integer offset = null;
String[] selectors = request.getRequestPathInfo().getSelectors();
if (selectors.length > 0) {
try {
offset = Integer.valueOf(selectors[selectors.length - 1]);
}
catch (NumberFormatException e) {
// empty catch block
}
}
if (cssFiles.size() > 31 && offset == null) {
int slices = cssFiles.size() / 31;
ServletOutputStream out = response.getOutputStream();
String libPath = library.getLibraryPath();
if (proxy && (libPath.startsWith("/libs/") || libPath.startsWith("/apps/"))) {
libPath = "/etc.clientlibs" + libPath.substring(5);
}
for (int i = 0; i <= slices; ++i) {
int slice = i * 31;
out.println("@import url(\"" + request.getContextPath() + libPath + "." + slice + ".css?debug=true\");");
}
} else {
int iOffset = offset == null ? 0 : offset;
int len = Math.min(cssFiles.size(), iOffset + 31);
for (int i = iOffset; i < len; ++i) {
String cssFile = cssFiles.get(i);
if (proxy && (cssFile.startsWith("/libs/") || cssFile.startsWith("/apps/"))) {
cssFile = "/etc.clientlibs" + cssFile.substring(5);
}
response.getOutputStream().println("@import url(\"" + request.getContextPath() + cssFile + "\");");
}
log.debug("Included from " + offset + " to " + len);
}
}
protected void bindLibMgr(HtmlLibraryManager htmlLibraryManager) {
this.libMgr = htmlLibraryManager;
}
protected void unbindLibMgr(HtmlLibraryManager htmlLibraryManager) {
if (this.libMgr == htmlLibraryManager) {
this.libMgr = null;
}
}
}