Skip to content

Commit

Permalink
Fix issue with proxied clientlib (#2581)
Browse files Browse the repository at this point in the history
Co-authored-by: Pablo Castelo <[email protected]>
  • Loading branch information
pcastelog and Pablo Castelo authored May 6, 2021
1 parent ff750e9 commit 9f6c158
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -84,6 +85,9 @@ public class VersionedClientlibsTransformerFactoryTest {
@Mock
private HtmlLibrary proxiedHtmlLibrary;

@Mock
private HtmlLibrary resourceResolverHtmlLibrary;

@Mock
private ContentHandler handler;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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/" });
Expand All @@ -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;
}

Expand Down Expand Up @@ -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<Attributes> 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 {

Expand Down

0 comments on commit 9f6c158

Please sign in to comment.