Skip to content

Commit

Permalink
Resolve ClientLib before trying to get it with the HtmlLibraryManager (
Browse files Browse the repository at this point in the history
…#2533)

* Resolve ClientLib before trying to get it with the HtmlLibraryManager
* Update VersionedClientlibsTransformerFactory.java

Co-authored-by: Pablo Castelo <[email protected]>
  • Loading branch information
pcastelog and Pablo Castelo authored Feb 22, 2021
1 parent 597440b commit bd1438a
Showing 1 changed file with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import org.apache.sling.api.SlingConstants;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.NonExistingResource;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.rewriter.ProcessingComponentConfiguration;
Expand Down Expand Up @@ -214,7 +216,7 @@ private Attributes rebuildAttributes(final AttributesImpl newAttributes, final i
libraryPath = path.substring(contextPath.length());
}

String versionedPath = this.getVersionedPath(libraryPath, libraryType, request.getResourceResolver());
String versionedPath = this.getVersionedPath(libraryPath, libraryType, request);

if (StringUtils.isNotBlank(versionedPath)) {
if(StringUtils.isNotBlank(contextPath)) {
Expand All @@ -229,7 +231,8 @@ private Attributes rebuildAttributes(final AttributesImpl newAttributes, final i
return newAttributes;
}

private String getVersionedPath(final String originalPath, final LibraryType libraryType, final ResourceResolver resourceResolver) {
private String getVersionedPath(final String originalPath, final LibraryType libraryType,
final SlingHttpServletRequest request) {
if (originalPath.startsWith(PROXY_PREFIX) && originalPath.contains(PROXIED_STATIC_RESOURCE_PATH)) {
log.debug("Static resource accessed via the clientlib proxy: '{}'", originalPath);
return null;
Expand All @@ -242,7 +245,7 @@ private String getVersionedPath(final String originalPath, final LibraryType lib
libraryPath = StringUtils.substringBeforeLast(libraryPath, ".");
}

final HtmlLibrary htmlLibrary = getLibrary(libraryType, libraryPath, resourceResolver);
final HtmlLibrary htmlLibrary = getLibrary(libraryType, libraryPath, request);

if (htmlLibrary != null) {
StringBuilder builder = new StringBuilder();
Expand Down Expand Up @@ -271,11 +274,19 @@ private String getVersionedPath(final String originalPath, final LibraryType lib
}
}

private HtmlLibrary getLibrary(LibraryType libraryType, String libraryPath, ResourceResolver resourceResolver) {
String resolvedLibraryPath = resolvePathIfProxied(libraryType, libraryPath, resourceResolver);
private HtmlLibrary getLibrary(LibraryType libraryType, String libraryPath, SlingHttpServletRequest request) {
String resolvedLibraryPath = resolvePath(libraryType, libraryPath, request);
return resolvedLibraryPath == null ? null : htmlLibraryManager.getLibrary(libraryType, resolvedLibraryPath);
}

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)) {
return libraryPath;
Expand Down Expand Up @@ -402,7 +413,7 @@ protected CompositeType getCacheEntryType() throws OpenDataException {
}

@Nonnull
UriInfo getUriInfo(@Nullable final String uri, @Nonnull ResourceResolver resourceResolver) {
UriInfo getUriInfo(@Nullable final String uri, @Nonnull SlingHttpServletRequest request) {
if (uri != null) {
Matcher matcher = FILTER_PATTERN.matcher(uri);
if (matcher.matches()) {
Expand All @@ -417,7 +428,7 @@ UriInfo getUriInfo(@Nullable final String uri, @Nonnull ResourceResolver resourc
libraryType = LibraryType.JS;
}

final HtmlLibrary htmlLibrary = getLibrary(libraryType, libraryPath, resourceResolver);
final HtmlLibrary htmlLibrary = getLibrary(libraryType, libraryPath, request);
return new UriInfo(libraryPath + "." + extension, md5, libraryType, htmlLibrary);
}
}
Expand All @@ -436,7 +447,7 @@ public void doFilter(final ServletRequest request,
final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
final SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response;
String uri = slingRequest.getRequestURI();
UriInfo uriInfo = getUriInfo(uri, slingRequest.getResourceResolver());
UriInfo uriInfo = getUriInfo(uri, slingRequest);
if (uriInfo.cacheKey != null) {
if ("".equals(uriInfo.md5)) {
log.debug("MD5 is blank for '{}' in Versioned ClientLibs cache, allowing {} to pass", uriInfo.cleanedUri, uri);
Expand Down

0 comments on commit bd1438a

Please sign in to comment.