Thursday, February 9, 2012

How to use static agent in CQ / WEM

Use case:
1) You want to create file system view of repository on each activation.
2) You want to create backup of your site in file system (After that you can zip it and store it where ever you want)

Solution:

1) Login in to your system and then go to http://host:port/libs/wcm/core/content/misc.html#/etc/replication/agents.author
2) Create a new replication agent of type "Static Delivery Agent"



3) Double click on newly created agent and click on edit. Enter all description and click on "Rules" tab.
4) You can enter target repository (By default it is /tmp in unix system). Make sure that you have sufficient rights to that folder.
5) In definition section, specify for a specific path what rule you want to apply. For example if you have rule like
/content/geo* ${path}.html?wcmmode=preview this mean for any {path} activated that start with /content/geo* static agent will request http://host:port/{path}.html?wcmmode=preview and then store that content under {target path} you set above




6) Now we have to override default static setting from felix console. For that please go to http://host:port/system/console/configMgr and click on "Static Content Builder". Default port there is 4502, You can change that according to your need.



7) Now after each activation static representation of content will get stored in {target path}

8) Note that static agent get trigger on replicate action. So if you configure static agent on publish and activate page on author, It will not get triggered. In order to trigger this on publish, Please create static agent under agent.publish (http://HOST:PORT/miscadmin#/etc/replication/agents.publish) and then add "triggerModified" property to true using CRXDE light. You can also remove ?wcmmode=preview from publish, From set of rules (Do not forget to change felix configuration to point to publish instance)




Note that modification event only works for cq:page. For all other modification to get trigger on publish, Please refer http://dev.day.com/content/kb/home/cq5/CQ5SystemAdministration/HowToFlushAssetsPublish.html

You might have to entry of agent:static in this case.

Note that if you are making above changes in autor, You have to activate static agent on publish after making change.


You can write a code to iterate through all pages in repository and then use replication API to activate those pages. This way you can create file system snapshot of whole repository. You can use following code to call replication through replication,

com.day.cq.replication.Replicator replicator = sling.getService(Replicator.class) // in a JSP

OR

@Reference
com.day.cq.replication.Replicator replicator = null; // in an OSGi component

THEN

com.day.cq.replication.ReplicationOptions opts = new ReplicationOptions();
opts.setFilter(new com.day.cq.replication.AgentFilter() {
public boolean isIncluded(com.day.cq.replication.Agent agent) {
return agent.getId("static"); // the ID of the agent is the node name, e.g. "static" for /etc/replication/agents/static
}
}
); // filter by replication agent

THEN

replicator.replicate(javax.jcr.Session, ReplicationActionType.ACTIVATE, path, opts); // activate

Or

use following code to get all static replication agent

AgentManager agentMgr = sling.getService(AgentManager.class);
ArrayList allStaticAgent = new ArrayList();
for (Agent agent: agentMgr.getAgents().values()) {
if (agent.isEnabled()) {
if(agent.getId().equalsIgnoreCase("static"){
allStaticAgent.add(agent);
}
}

}
}

THEN

ReplicationOptions replicationOptions = new ReplicationOptions();
for(Agent agent:allStaticAgent){
replicationOptions.setFilter(new AgentIdFilter(agent.getId()));
replicator.replicate(javax.jcr.Session, ReplicationActionType.ACTIVATE, path, opts); // activate

}

Note For creating file system view of DAM renditions or DAM Asset you might have to create some custom plugin.

18 comments:

  1. Thanks, is it possible to replicate content to the cloud, rather than to a local file system?

    ReplyDelete
  2. Do you mean to store content in cloud statically ? Or to publish instance in cloud ? For case 1, You can create a mount point on local for cloud directly and then give path for mount point in options (Not tested but in theory should work). For case 2 you can use normal replication agent.

    Yogesh

    ReplyDelete
  3. I am getting the html files and dam files in target path but etc/design is not getting .

    ReplyDelete
    Replies
    1. I am surprised that it is even working for DAM files (I guess not all Asset works). This is still work in progress. You might have to create DayCare ticket to enhance this feature.

      Delete
  4. Great post! Thank you! Instead of writing a custom script to activate all content, wouldn't the built-in "activate tree" work as well?

    ReplyDelete
    Replies
    1. Thanks for your feedback. Yes if you want to get all data in file system then you can use tree activation. Custom code is for specific use case. For example in case you want to have static replication logic run as a part of workflow or as a part of custom event.

      Yogesh

      Delete
    2. Thanks Yogesh!

      If we create a static agent on publish with triggerModified set, will "activate tree" on the author instance cause the publish instance to create the static files?

      Delete
    3. If you have your static agent set then yes. But also make sure that Publish is listening to on modification event. Because As far as I remember there was a issue where replication on modification fails on publish.

      http://helpx.adobe.com/cq/kb/HowToFlushAssetsPublish.html

      Yogesh

      Delete
  5. Thanks Yogesh. Good post.

    ReplyDelete
  6. Hi Yogesh,

    Getting below error when we are running workflow to place the file in SFTP using static agent.Any idea?

    Unable to retrieve content: Unable to fetch content from
    http://localhost:4502/content/testproj/test.html?wcmmode=disabled : 401

    ReplyDelete
  7. Hi Yogesh,

    Do you know if there is a way to delete the files created by the static agent when a page gets deactivated?

    ReplyDelete
  8. Hi Yogesh
    i have a case for having to replicate xml representations of nodes (cq pages and dam-assets) to a filesystem. additionally to create an index of said filesystem.
    i have created a content-builder/transport-handler pair with which i can set up several agent instances (as required) handlling different subpaths of the repository. all works fine.
    thing is i do not know how to hook in best the generation of the index... currently i have it at the end of the transport-handler, which is ok for the activation case (since activation transaction only transport one asset at a time and the jcr node already has the final status...) but not for deactivation deletion (since the transaction handles multiple paths and only the "parent" asset has the final status (during transport) while the "children" don't).

    is there a way to provide ReplicationOptions (making the thing synchronous and providing a ReplicationListener) within the normal framework? means not by programmatically calling agent.replicate() ?

    or is there any other hook/event at the end of a given replication (or possibly replication-transaction or possibly general osgi event)?

    many thanks for any help

    ReplyDelete
  9. Getting below error while replicating static agent

    Unable to retrieve content: java.net.ConnectException: Connection refused: connect

    Pls suggest on this

    ReplyDelete
    Replies
    1. one more error is coming
      Error during replication com.day.cq.replication.ReplicationException: Test replication not supported by this transport handler.

      Delete
  10. This comment has been removed by the author.

    ReplyDelete
  11. Hi,

    I have a requirement to store all the html pages in a local disk. currently i am using a static agent on publisher to do that but i have a enhancement on the the above task to remove all the /content/myproject/english from the paths on the page and replace it with mycompany,com. i have to do it in the html file before i store it on to the disk. Any help is appreciated.

    ReplyDelete