From 9f6c158208068bc5a909162d043767be16a32320 Mon Sep 17 00:00:00 2001 From: Pablo Castelo <5844420+pcastelog@users.noreply.github.com> Date: Thu, 6 May 2021 10:31:31 +0200 Subject: [PATCH] Fix issue with proxied clientlib (#2581) Co-authored-by: Pablo Castelo --- CHANGELOG.md | 1 + ...VersionedClientlibsTransformerFactory.java | 16 +++++----- ...ionedClientlibsTransformerFactoryTest.java | 31 ++++++++++++++++++- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65b3214b90..b3498f585d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com) [unreleased changes details]: https://github.com/Adobe-Consulting-Services/acs-aem-commons/compare/acs-aem-commons-5.0.4...HEAD ### Fixed +- #2581 - Versioned ClientLibs no longer works with proxied clientlibs - #2562 - Fixed cache refresh on versioned clientlibs request when enforceMd5 is false (default). ### Added diff --git a/bundle/src/main/java/com/adobe/acs/commons/rewriter/impl/VersionedClientlibsTransformerFactory.java b/bundle/src/main/java/com/adobe/acs/commons/rewriter/impl/VersionedClientlibsTransformerFactory.java index 265d70cfa2..53053f2245 100644 --- a/bundle/src/main/java/com/adobe/acs/commons/rewriter/impl/VersionedClientlibsTransformerFactory.java +++ b/bundle/src/main/java/com/adobe/acs/commons/rewriter/impl/VersionedClientlibsTransformerFactory.java @@ -281,18 +281,16 @@ private HtmlLibrary getLibrary(LibraryType libraryType, String libraryPath, Slin } private String resolvePath(LibraryType libraryType, String libraryPath, SlingHttpServletRequest request) { - Resource libraryResource = request.getResourceResolver().resolve(request, libraryPath); - if (libraryResource != null && !(libraryResource instanceof NonExistingResource)) { - return libraryResource.getPath(); - } - return resolvePathIfProxied(libraryType, libraryPath, request.getResourceResolver()); - } - - private String resolvePathIfProxied(LibraryType libraryType, String libraryPath, ResourceResolver resourceResolver) { if (!libraryPath.startsWith(PROXY_PREFIX)) { + Resource libraryResource = request.getResourceResolver().resolve(request, libraryPath); + if (libraryResource != null && !(libraryResource instanceof NonExistingResource)) { + return libraryResource.getPath(); + } + // Default behavior, to keep consistency with previous implementation and to not return a null path in case + // the resolver can't find the clientlib return libraryPath; } - return resolveProxiedClientLibrary(libraryType, libraryPath, resourceResolver, true); + return resolveProxiedClientLibrary(libraryType, libraryPath, request.getResourceResolver(), true); } private String resolveProxiedClientLibrary(LibraryType libraryType, String proxiedPath, ResourceResolver resourceResolver, boolean refreshCacheIfNotFound) { diff --git a/bundle/src/test/java/com/adobe/acs/commons/rewriter/impl/VersionedClientlibsTransformerFactoryTest.java b/bundle/src/test/java/com/adobe/acs/commons/rewriter/impl/VersionedClientlibsTransformerFactoryTest.java index 3390a69cf3..32fdf9e686 100644 --- a/bundle/src/test/java/com/adobe/acs/commons/rewriter/impl/VersionedClientlibsTransformerFactoryTest.java +++ b/bundle/src/test/java/com/adobe/acs/commons/rewriter/impl/VersionedClientlibsTransformerFactoryTest.java @@ -31,6 +31,7 @@ import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.api.resource.ResourceResolver; +import org.apache.sling.commons.testing.sling.MockResource; import org.apache.sling.rewriter.ProcessingContext; import org.apache.sling.rewriter.Transformer; import org.junit.After; @@ -84,6 +85,9 @@ public class VersionedClientlibsTransformerFactoryTest { @Mock private HtmlLibrary proxiedHtmlLibrary; + @Mock + private HtmlLibrary resourceResolverHtmlLibrary; + @Mock private ContentHandler handler; @@ -115,6 +119,7 @@ public class VersionedClientlibsTransformerFactoryTest { private ResourceResolver resourceResolver; private static final String PATH = "/etc/clientlibs/test"; + private static final String MAP_PATH = "/mylib/test"; private static final String FAKE_STREAM_CHECKSUM="fcadcfb01c1367e9e5b7f2e6d455ba8f"; // md5 of "I love strings" private static final String PROXIED_FAKE_STREAM_CHECKSUM="669a712c318596cd7e7520e3e2000cfb"; // md5 of "I love strings when they are proxied" private static final byte[] BYTES; @@ -148,6 +153,9 @@ public void setUp() throws Exception { when(proxiedHtmlLibrary.getLibraryPath()).thenReturn(PROXIED_PATH); when(proxiedHtmlLibrary.getInputStream(false)).thenReturn(new java.io.ByteArrayInputStream("I love strings when they are proxied".getBytes())); + when(resourceResolverHtmlLibrary.getLibraryPath()).thenReturn(MAP_PATH); + when(resourceResolverHtmlLibrary.getInputStream(false)).thenReturn(new java.io.ByteArrayInputStream("I love strings when they are resolved".getBytes())); + when(processingContext.getRequest()).thenReturn(slingRequest); when(slingRequest.getResourceResolver()).thenReturn(resourceResolver); when(resourceResolver.getSearchPath()).thenReturn(new String[] { "/libs/", "/apps/" }); @@ -162,7 +170,7 @@ public void setUp() throws Exception { @After public void tearDown() throws Exception { - reset(htmlLibraryManager, htmlLibrary, handler); + reset(htmlLibraryManager, htmlLibrary, resourceResolverHtmlLibrary, handler); transformer = null; } @@ -471,6 +479,27 @@ public void testMinifiedJavaScriptClientLibrary() throws Exception { assertEquals(PATH + ".min."+ FAKE_STREAM_CHECKSUM +".js", attributesCaptor.getValue().getValue(0)); } + @Test + public void testClientLibFoundWithResourceResolverMapping() throws Exception { + + MockResource clientlib = new MockResource(resourceResolver, PATH,""); + when(resourceResolver.resolve(slingRequest, MAP_PATH)).thenReturn(clientlib); + when(htmlLibraryManager.getLibrary(eq(LibraryType.JS), eq(PATH))).thenReturn(htmlLibrary); + + final AttributesImpl in = new AttributesImpl(); + in.addAttribute("", "src", "", "CDATA", MAP_PATH + ".min.js"); + in.addAttribute("", "type", "", "CDATA", "text/javascript"); + + transformer.startElement(null, "script", null, in); + + ArgumentCaptor attributesCaptor = ArgumentCaptor.forClass(Attributes.class); + + verify(handler, only()).startElement(isNull(), eq("script"), isNull(), + attributesCaptor.capture()); + + assertEquals(MAP_PATH + ".min."+ FAKE_STREAM_CHECKSUM +".js", attributesCaptor.getValue().getValue(0)); + } + @Test public void testCssClientLibraryWithInvalidExtension() throws Exception {