Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with proxied clientlib #2581

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com)
## Unreleased ([details][unreleased changes details])
<!-- Keep this up to date! After a release, change the tag name to the latest release -->
[unreleased changes details]: https://github.com/Adobe-Consulting-Services/acs-aem-commons/compare/acs-aem-commons-5.0.4...HEAD
### Fixed
kwin marked this conversation as resolved.
Show resolved Hide resolved
- #2581 - Versioned ClientLibs no longer works with proxied clientlibs

### Fixed
- #2562 - Fixed cache refresh on versioned clientlibs request when enforceMd5 is false (default).
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