Wednesday, March 25, 2020

CDN | Clearing Cloudflare cache

In order to clear Cloudflare cache automatically via code, follow below steps:

1. Develop Custom TransportHandler

Develop a custom Transport handler to send the test and purge requests to Cloudflare API server. The handler sets up basic authentication with the user/pass from the replication agent's transport config and sends a GET request as a test and POST as purge request. A valid test response is 200 while a valid purge response is 201.

Sample code is here:

2. Create custom dispatcher flush agent

The transport handler builds the POST request body in accordance with Cloudflare's REST APIs using the replication agent properties.

A. Create New Replication Agent at-

      http://localhost:4503/miscadmin#/etc/replication/agents.publish

B. Agent Configuration:

     The Flush agent must be configured with following 3 properties:
     1. The transport handler is triggered by setting agent's transport URL's protocol to "cloudflare://".
        E.g. - cloudflare://api.cloudflare.com/client/v4/zones/{zone-id}/purge_cache
     2. User: The X-Auth-Email value of cloudflare account
     3. Password: X-Auth-Key value of cloudflare account

C. Agent User Id in AEM:


Create a custom service user to control permission what should be allowed to flush CDN cache: cloudflare-flush

Reference: https://api.cloudflare.com/#zone-purge-files-by-url/

AEM6 | Enable component programmatically

Problem:

An OSGi component is disabled by default, need to enable it programmatically in AEM6.x.

Solution:

Use SCR API to enable it. Sample code here:
for (Bundle bundle : bundleContext.getBundles()) {
    ComponentDescriptionDTO dto = scr.getComponentDescriptionDTO(bundle, componentName);
    if (dto != null && !scr.isComponentEnabled(dto)) {
        LOGGER.info("Component {} disabled by configuration.", dto.implementationClass);
        scr.enableComponent(dto);
    }
}
Complete code can be found here:

Use Case:

Sample use case is to enable "com.day.cq.dam.core.impl.MissingMetadataNotificationJob" component in https://helpx.adobe.com/experience-manager/6-3/assets/using/metadata-schemas.html#Definemandatorymetadata

CDN | Clearing Cloudflare cache

In order to clear Cloudflare cache automatically via code, follow below steps: 1. Develop Custom TransportHandler Develop a custom Trans...