Skip to content

Commit 00f7839

Browse files
committed
LPD-52709 Use hashed URLs for portlet's JS files
Note that we are not hashing the file names yet because that needs changes in the tooling, but at least we leverage the new configurable time based caching infrastructure.
1 parent c8e63bc commit 00f7839

File tree

1 file changed

+11
-143
lines changed

1 file changed

+11
-143
lines changed

portal-kernel/src/com/liferay/portal/kernel/portlet/render/PortletRenderUtil.java

Lines changed: 11 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,14 @@
66
package com.liferay.portal.kernel.portlet.render;
77

88
import com.liferay.petra.io.StreamUtil;
9-
import com.liferay.petra.string.StringBundler;
109
import com.liferay.petra.string.StringPool;
1110
import com.liferay.portal.kernel.content.security.policy.ContentSecurityPolicyNonceProviderUtil;
12-
import com.liferay.portal.kernel.frontend.esm.FrontendESMUtil;
1311
import com.liferay.portal.kernel.frontend.hashed.files.HashedFilesRegistryUtil;
1412
import com.liferay.portal.kernel.frontend.hashed.files.HashedFilesUtil;
1513
import com.liferay.portal.kernel.log.Log;
1614
import com.liferay.portal.kernel.log.LogFactoryUtil;
1715
import com.liferay.portal.kernel.model.LayoutTypePortlet;
1816
import com.liferay.portal.kernel.model.Portlet;
19-
import com.liferay.portal.kernel.model.Theme;
2017
import com.liferay.portal.kernel.theme.ThemeDisplay;
2118
import com.liferay.portal.kernel.util.HashMapBuilder;
2219
import com.liferay.portal.kernel.util.HtmlUtil;
@@ -47,7 +44,6 @@
4744
import java.util.Map;
4845
import java.util.Set;
4946
import java.util.concurrent.ConcurrentHashMap;
50-
import java.util.function.Predicate;
5147

5248
/**
5349
* @author Iván Zaera Avellón
@@ -197,110 +193,6 @@ private static List<Portlet> _getAllPortlets(
197193
return allPortlets;
198194
}
199195

200-
private static List<String> _getComboServletURLs(
201-
Collection<PortletResourceAccessor> portletResourceAccessors,
202-
Collection<Portlet> portlets, Predicate<String> predicate,
203-
long timestamp, String urlPrefix, Set<String> visitedURLs) {
204-
205-
if (predicate == null) {
206-
predicate = s -> true;
207-
}
208-
209-
List<String> urls = new ArrayList<>();
210-
211-
StringBundler comboServletSB = new StringBundler();
212-
213-
for (Portlet portlet : portlets) {
214-
for (PortletResourceAccessor portletResourceAccessor :
215-
portletResourceAccessors) {
216-
217-
String contextPath = null;
218-
219-
if (portletResourceAccessor.isPortalResource()) {
220-
contextPath = PortalUtil.getPathContext();
221-
}
222-
else {
223-
contextPath =
224-
PortalUtil.getPathProxy() + portlet.getContextPath();
225-
}
226-
227-
Collection<String> portletResources =
228-
portletResourceAccessor.get(portlet);
229-
230-
for (String portletResource : portletResources) {
231-
if (!predicate.test(portletResource)) {
232-
continue;
233-
}
234-
235-
String prefix = null;
236-
237-
for (String specialPrefix : _specialPrefixes) {
238-
if (portletResource.startsWith(specialPrefix)) {
239-
portletResource = portletResource.substring(
240-
specialPrefix.length());
241-
242-
prefix = specialPrefix;
243-
244-
break;
245-
}
246-
}
247-
248-
boolean absolute = HttpComponentsUtil.hasProtocol(
249-
portletResource);
250-
251-
if (!absolute) {
252-
portletResource = contextPath + portletResource;
253-
}
254-
255-
if (Validator.isNotNull(prefix)) {
256-
portletResource = prefix + portletResource;
257-
}
258-
259-
if (visitedURLs.contains(portletResource)) {
260-
continue;
261-
}
262-
263-
visitedURLs.add(portletResource);
264-
265-
if (absolute || Validator.isNotNull(prefix)) {
266-
urls.add(portletResource);
267-
}
268-
else {
269-
comboServletSB.append(StringPool.AMPERSAND);
270-
271-
if (!portletResourceAccessor.isPortalResource()) {
272-
comboServletSB.append(portlet.getPortletId());
273-
comboServletSB.append(StringPool.COLON);
274-
}
275-
276-
comboServletSB.append(
277-
HtmlUtil.escapeURL(portletResource));
278-
279-
timestamp = Math.max(timestamp, portlet.getTimestamp());
280-
}
281-
}
282-
}
283-
}
284-
285-
if (comboServletSB.length() > 0) {
286-
String url = urlPrefix + comboServletSB;
287-
288-
url = HttpComponentsUtil.addParameter(url, "t", timestamp);
289-
290-
urls.add(url);
291-
}
292-
293-
return urls;
294-
}
295-
296-
private static String _getMinifierType(URLType urlType) {
297-
if (urlType == URLType.CSS) {
298-
return "css";
299-
}
300-
301-
return "js";
302-
}
303-
304196
private static PortletRenderParts _getPortletRenderParts(
305197
HttpServletRequest httpServletRequest, String portletHTML,
306198
Portlet portlet, boolean portletOnLayout) {
@@ -432,12 +324,15 @@ private static List<String> _getStaticURLs(
432324
contextPath + portletResource);
433325
}
434326
else if (urlType == URLType.JAVASCRIPT) {
435-
Portlet rootPortlet = portlet.getRootPortlet();
327+
portletResource = contextPath + portletResource;
328+
329+
String hashedFileURI =
330+
HashedFilesRegistryUtil.getHashedFileURI(
331+
portletResource);
436332

437-
portletResource = PortalUtil.getStaticResourceURL(
438-
httpServletRequest,
439-
contextPath + portletResource,
440-
rootPortlet.getTimestamp());
333+
if (hashedFileURI != null) {
334+
portletResource = hashedFileURI;
335+
}
441336
}
442337
else {
443338
throw new UnsupportedOperationException(
@@ -492,36 +387,9 @@ private static Collection<String> _getURLs(
492387
visitedURLs);
493388
}
494389
else if (urlType == URLType.JAVASCRIPT) {
495-
ThemeDisplay themeDisplay =
496-
(ThemeDisplay)httpServletRequest.getAttribute(
497-
WebKeys.THEME_DISPLAY);
498-
499-
boolean fastLoad = themeDisplay.isThemeJsFastLoad();
500-
501-
if (fastLoad) {
502-
Predicate<String> predicate =
503-
resource -> !themeDisplay.isIncludedJs(resource);
504-
505-
Theme theme = themeDisplay.getTheme();
506-
507-
urls = _getComboServletURLs(
508-
portletResourceAccessors, portlets, predicate,
509-
theme.getTimestamp(),
510-
PortalUtil.getStaticResourceURL(
511-
httpServletRequest,
512-
themeDisplay.getCDNDynamicResourcesHost() +
513-
themeDisplay.getPathContext() + "/combo",
514-
StringBundler.concat(
515-
"minifierType=", _getMinifierType(urlType),
516-
"&themeId=", themeDisplay.getThemeId()),
517-
-1),
518-
visitedURLs);
519-
}
520-
else {
521-
urls = _getStaticURLs(
522-
httpServletRequest, portletResourceAccessors, portlets,
523-
urlType, visitedURLs);
524-
}
390+
urls = _getStaticURLs(
391+
httpServletRequest, portletResourceAccessors, portlets, urlType,
392+
visitedURLs);
525393
}
526394
else {
527395
throw new UnsupportedOperationException(

0 commit comments

Comments
 (0)