Wednesday, February 15, 2017

Custom Transport Handler in CQ/AEM6

Custom Transport Handler in CQ5.6/AEM6


There are a lot of blogs written on this one. However, none of them explain about how it should be unit tested and what are the point a programmer should have in mind while using a custom transport handler. I am going to list down few of them below.

1. There must be a custom ContentBuilder defined. An example definition could be as follows.

@Component(metatype = false)
@Service(ContentBuilder.class)
@Property(name = "name", value = "Custom Content Builder")
public class CustomContentBuilder implements ContentBuilder {
public static final String NAME = "Custom Content Builder";
public static final String TITLE = "Custom Content Builder";
public ReplicationContent create(Session session, ReplicationAction action,
ReplicationContentFactory factory) throws ReplicationException {
return ReplicationContent.VOID;
}
public String getName() {
return TITLE;
}
public String getTitle() {
return TITLE;
}
public ReplicationContent create(Session session, ReplicationAction action,
ReplicationContentFactory factory, Map<String, Object> arg3){
// TODO Auto-generated method stub
return ReplicationContent.VOID;
}
}
2. While defining your agent in author agents that uses your custom transport handler, select your custom content builder's name i.e.  in above case it is "Custom Content Builder" as a value of Synchronization Type.


In above example instead of default it should be "Custom Content Builder"

Notes : The custom handler can be invoked on the author environment by invoking activate request on the page. This works fine even when the custom handler is configured on the publish side and the request is invoked from author side in case of AEM 6.0 but, this will not work fine in CQ5.6


Monday, January 30, 2017

Programmatic Cache Invalidation in AEM 6

Programmatic Cache Invalidation in AEM 6


There are three ways of doing it.

1. By sending post request to the dispatcher cache invalidation URL as follows.

                HttpClient client = new HttpClient();
HostConfiguration configuration = new HostConfiguration();
                // This uri is http://dispatcherhost:dispatcherport/dispatcher/invalidate.cache
                // Without this host configuration there will be and exception with the message "Host is null"
configuration.setHost(uri);
client.setHostConfiguration(configuration);
PostMethod post = new PostMethod(uri);
                // CQ-Action:Delete will delete the cache from dispatcher.
                // For cache invalidation Activate action should be used.
post.setRequestHeader("CQ-Action", "Activate");
                // pagePath specifies the page that needs to be invalidated
post.setRequestHeader("CQ-Handle", pagePath);
StringRequestEntity body = new StringRequestEntity(pagePath, null, null);
post.setRequestEntity(body);
post.setRequestHeader("Content-length",String.valueOf(body.getContentLength()));
client.executeMethod(post);
post.releaseConnection();

2. Using DispatcherFlusher provided by ACS commons.

First make reference to DispatcherFlusher

       @Reference
      private DispatcherFlusher dispatcherFlusher;

Next make use of it to flush the cache

             dispatcherFlusher.flush(getResourceResolver(repo.loginAdministrative(null)), pagePath);

3. FlushService (Available in AEM 6.2+ versions)
   
             FlushService flushService = sling.getService(Replicator.class);
             flushService.sendFlushUrl("admin",FlushService.FlushType.IMMEDIATE_FLUSH, pagePath,FlushService.RefetchType.IMMEDIATE_REFETCH,new String[] {pagePath});






Thursday, December 10, 2015

Internationalization of CQ/AEM Components

1. First of all, you need to configure languages in /etc/languages
2. Then, enable internationalization in your app by adding i18n folder and providing dictionaries.
3. Now, provide details and descriptions of your component in i18n keys.
4. Apply the same while creating your component as shown in image. You can do the same for all the titles/descriptions in Dialog, editConfig etc












5. Now, configure your user (admin/admin) to the language you want to see your component details in. For me it was en_US as follows.












And you're done

P.S dont leave space when you're putting keys in dialog/component/editconfig/actionconfig titles e.g.

If you'll have the value of text in cq:actions as follows, key will not be interpreted.

text: your_component_editconfig_title

Instead remove the space btwn text: and your_component_editconfig_title

like text:your_component_editconfig_title

Some more AEM 6 Interview Questions for Architects

 Some more AEM 6 Interview Questions for Architects 1. Consider you have a workflow with two steps. One step is to transfer the asset from s...