Wednesday, July 30, 2014

How to Remove White Space From Generated HTML In CQ (Or In general)

Use Case: There are a lot of white spaces in generated Output of CQ increasing size of page and decreasing load time.

Solutions:

Option 1: You can use trimDirectiveWhitespaces directive in jsp response. something like

<%@page  trimDirectiveWhitespaces="true"%>

Problem: Using this directive can cause white space to be removed from taglibs for same property. To avoid this issue make sure that you manually add space there. For example if you have tag lib like <Something class="${test1} ${test2}" class2="test"> replace it with <Something class="${test1} ${space} ${test2}" class2="test"> where ${space} is actual space " "
This approach might not work with Slighly framework.

Option 2: Use %><% tags to start and end scriplets tag and in between html tag

Problem: Code very hard to read and not pretty.

Option 3: Create your own tag library and using html parser remove white spaces during run time. for that check http://www.cqblueprints.com/tipsandtricks/jsp-custom-tag-lib.html

And code to remove White space would be



Problem: Maintenance of your own tag library. Can Miss Some condition. Have to wrap up your code with tag lib.

Option 4 (Preferred): Use Google Page Speed Module at apache.

Link to Module: https://developers.google.com/speed/pagespeed/module
Link to All available Filters for Module: https://developers.google.com/speed/pagespeed/module/config_filters
Instruction of how to install and build: https://developers.google.com/speed/pagespeed/module/download

Steps to integrate it in Dispatcher:

1) Create Apache module using step above. This will give you mod_pagespeed.so and mod_pagespeed_ap24.so

2) Move this file to <Apache location>/modules

3) Change permission (to daemon:daemon (Or Your Apache User)) and permission level (766) using chown and chmod command

4) Open conf/httpd.conf and add following line (If some include is already there ignore that)
Include <Apache Location>/conf/pagespeed.conf

5) Create a folder called <Doc Root>/mod_pagespeed/

6) Add pagespeed.conf under <Apache Location>/conf
* Make sure that All paths mentioned in conf file exist.

7) Restart Apache


Problem:
1) Only Apache module is available. If you are using IIS or any other web server then there is no module yet.
2) You might have to do build distribution for your own OS if above module build does not work (One attached here is build for Red Hat Linux).
3) When you upgrade your Apache make sure to upgrade your module as well. If there is no distribution for newer version of apache, then also you are out of luck.


Note: Test above methods before using them in production. Feel free to ask any question you have.

Monday, July 28, 2014

How to include all CQ dependencies in CQ6

Use case: Prior to CQ6 you have to add dependencies for each class you are using in your pom.xml, Way to find dependencies was (Maven org http://mvnrepository.com or using adobe central http://repo.adobe.com/nexus/content/groups/public through dependency finder HOST:PORT/system/console/depfinder). With CQ6 now all dependencies are provided through one artifactID.

Prerequisite: Maven, CQ Project Set Up 

Solution: Include following line in your dependency management for your pom.xml (Depending upon project this could be at any location usually it is your Project parent pom)


<dependencyManagement>
<!-- All Your Third party dependency -->
<!-- AEM 6.0 -->

      <!-- All AEM Dependencies for JSP compilation -->

      <dependency>
        <groupId>com.adobe.aem</groupId>
        <artifactId>aem-api</artifactId>
        <version>6.0.0.1</version>
        <scope>provided</scope>
      </dependency>

      <!-- All AEM 6.1 Dependency added to end of file -->


      <dependency>
        <groupId>com.adobe.cq.social</groupId>
        <artifactId>cq-socialcommunities-api</artifactId>
        <version>1.7.197</version>
        <scope>provided</scope>
      </dependency>


      <dependency>
        <groupId>com.adobe.aem</groupId>
        <artifactId>uber-jar</artifactId>
        <version>6.1.0</version>
        <scope>provided</scope>
        <classifier>obfuscated-apis</classifier>
      </dependency>


</dependencyManagement>

Note that this version could change depending upon new releases of CQ, You can track them from http://repo.adobe.com/nexus/content/repositories/releases/com/adobe/aem/aem-api/

Some Trick: Note that above will include all AEM-API dependencies, other way to check what minimum dependency is needed is to create a multi module project using AEM plugin for eclipse more example http://docs.adobe.com/content/docs/en/dev-tools/aem-eclipse.html .

You can also upload uber-jar with apis classifiers instead of obfuscate-apis classifier to your Nexus or dependency management system. 

To upload this uber-jar to your nexus you can use something like,

mvn deploy:deploy-file -Durl=YOUR-REPO -Dfile=uber-jar-6.1.0-apis.jar -DartifactId=uber-jar -Dversion=6.1.0 -DgroupId=com.adobe.aem -Dclassifier=apis -Dpackaging=jar -DrepositoryId=YOUR-REPO-ID

Once you have uber-jar with apis classifier ready, You can use something like

      <dependency>
        <groupId>com.adobe.aem</groupId>
        <artifactId>uber-jar</artifactId>
        <version>6.1.0</version>
        <classifier>apis</classifier>
        <scope>provided</scope>
      </dependency>


I see that minimum these are needed when you use this,

As always let me know if you want me to add more details in this posting.