Repository: adobe-consulting-services/acs-aem-commons
Issue: Versioned ClientLibs

Hi team,

We are using the versioned clientlibs of ACS commons.Everything was working as expected with the hash numbers for example:

"//localhost:4502/etc/ui/xyz/clientlibs/**one_two**.51fa0469aee8be9249c96195c63e7b73.js"

"//localhost:4502/etc/ui/xyz/clientlibs/**one_two**.51fa0469aee8be9249c96195c67e7b73.css"

However when we renamed to the following naming convention,the hashing is not done for CSS files.

"//localhost:4502/etc/ui/xyz/clientlibs/**one.two**.51fa0469aee8be9249c96195c63e7b73.js"

"//localhost:4502/etc/ui/xyz/clientlibs/**one.two**.css"

Can you please let us know if there is way to get this # generated even with the dot notation for css since it is working for JS.

Regards
Kartheek


Changed files:
bundle/src/test/java/com/adobe/acs/commons/rewriter/impl/VersionedClientlibsTransformerFactoryTest.java
bundle/src/main/java/com/adobe/acs/commons/rewriter/impl/VersionedClientlibsTransformerFactory.java

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 f33aaa6f4..4b5f5944d 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
@@ -56,7 +56,6 @@ import org.xml.sax.helpers.AttributesImpl;
 import com.adobe.acs.commons.rewriter.AbstractTransformer;
 import com.adobe.acs.commons.util.impl.AbstractGuavaCacheMBean;
 import com.adobe.acs.commons.util.impl.GenericCacheMBean;
-import com.day.cq.commons.PathInfo;
 import com.day.cq.widget.HtmlLibrary;
 import com.day.cq.widget.HtmlLibraryManager;
 import com.day.cq.widget.LibraryType;
@@ -94,6 +93,9 @@ public final class VersionedClientlibsTransformerFactory extends AbstractGuavaCa
     private static final String CSS_TYPE = "text/css";
     private static final String JS_TYPE = "text/javascript";
 
+    private static final String MIN_SELECTOR = "min";
+    private static final String MIN_SELECTOR_SEGMENT = "." + MIN_SELECTOR;
+
     private Cache<VersionedClientLibraryMd5CacheKey, String> md5Cache;
 
     @Reference
@@ -180,25 +182,29 @@ public final class VersionedClientlibsTransformerFactory extends AbstractGuavaCa
 
     private String getVersionedPath(final String originalPath, final LibraryType libraryType) {
         try {
-            final PathInfo pathInfo = new PathInfo(originalPath);
+            boolean appendMinSelector = false;
+            String libraryPath = StringUtils.substringBeforeLast(originalPath, ".");
+            if (libraryPath.endsWith(MIN_SELECTOR_SEGMENT)) {
+                appendMinSelector = true;
+                libraryPath = StringUtils.substringBeforeLast(libraryPath, ".");
+            }
 
-            final HtmlLibrary htmlLibrary = htmlLibraryManager.getLibrary(libraryType, pathInfo.getResourcePath());
+            final HtmlLibrary htmlLibrary = htmlLibraryManager.getLibrary(libraryType, libraryPath);
 
             if (htmlLibrary != null) {
                 StringBuilder builder = new StringBuilder();
                 builder.append(htmlLibrary.getLibraryPath());
                 builder.append(".");
 
-                String selector = pathInfo.getSelectorString();
-                if (selector != null) {
-                    builder.append(selector).append(".");
+                if (appendMinSelector) {
+                    builder.append(MIN_SELECTOR).append(".");
                 }
                 builder.append(getMd5(htmlLibrary));
                 builder.append(libraryType.extension);
 
                 return builder.toString();
             } else {
-                log.debug("Could not find HtmlLibrary at path: {}", pathInfo.getResourcePath());
+                log.debug("Could not find HtmlLibrary at path: {}", libraryPath);
                 return null;
             }
         } catch (Exception ex) {
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 81d5471d5..56466d1db 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
@@ -106,7 +106,6 @@ public class VersionedClientlibsTransformerFactoryTest {
         assertEquals(PATH + ".css", attributesCaptor.getValue().getValue(0));
     }
 
-
     @Test
     public void testCSSClientLibrary() throws Exception {
 
@@ -127,6 +126,28 @@ public class VersionedClientlibsTransformerFactoryTest {
         assertEquals(PATH + "."+ FAKE_STREAM_CHECKSUM +".css", attributesCaptor.getValue().getValue(0));
     }
 
+    @Test
+    public void testCSSClientLibraryWithDot() throws Exception {
+        final String path = PATH + ".foo";
+
+        when(htmlLibraryManager.getLibrary(eq(LibraryType.CSS), eq(path))).thenReturn(htmlLibrary);
+        when(htmlLibrary.getLibraryPath()).thenReturn(path);
+
+        final AttributesImpl in = new AttributesImpl();
+        in.addAttribute("", "href", "", "CDATA", path + ".css");
+        in.addAttribute("", "type", "", "CDATA", "text/css");
+        in.addAttribute("", "rel", "", "CDATA", "stylesheet");
+
+        transformer.startElement(null, "link", null, in);
+
+        ArgumentCaptor<Attributes> attributesCaptor = ArgumentCaptor.forClass(Attributes.class);
+
+        verify(handler, only()).startElement(isNull(String.class), eq("link"), isNull(String.class),
+                attributesCaptor.capture());
+
+        assertEquals(path + "."+ FAKE_STREAM_CHECKSUM +".css", attributesCaptor.getValue().getValue(0));
+    }
+
     @Test
     public void testMinifiedCSSClientLibrary() throws Exception {
 
@@ -166,6 +187,27 @@ public class VersionedClientlibsTransformerFactoryTest {
         assertEquals(PATH + "."+ FAKE_STREAM_CHECKSUM +".js", attributesCaptor.getValue().getValue(0));
     }
 
+    @Test
+    public void testJavaScriptClientLibraryWithDot() throws Exception {
+        final String path = PATH + ".foo";
+
+        when(htmlLibraryManager.getLibrary(eq(LibraryType.JS), eq(path))).thenReturn(htmlLibrary);
+        when(htmlLibrary.getLibraryPath()).thenReturn(path);
+
+        final AttributesImpl in = new AttributesImpl();
+        in.addAttribute("", "src", "", "CDATA", path + ".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(String.class), eq("script"), isNull(String.class),
+                attributesCaptor.capture());
+
+        assertEquals(path + "."+ FAKE_STREAM_CHECKSUM +".js", attributesCaptor.getValue().getValue(0));
+    }
+
     @Test
     public void testMinifiedJavaScriptClientLibrary() throws Exception {