AEM integration with Akamai Fast Purge API for Single/multiple urls

In AEM we can clear dispatcher cache on page activation by adding dispatcher flush agent but if we have Akamai on top of dispatcher as CDN then we need to manually clear Akamai Cache by going to akamai console.

Akamai provides fast purge API and using that we can clear page cache using single URL, CPCode, multiple urls etc

We can integrate Akamai Fast Purge API with replication agent so that for every page activation akamai cache gets cleared.

To integrate with Akamai Fast Purge we need below details (Contact Akamai for details related to your account)


1) Access Token
2) Client Secret
3) Client Token
4) Akamai Host



Once above details are gathered you can refer below code for multiple urls cache clear

Create a map with array list of urls 

Map<String, List<String>> akamaiRequestMap = Maps.newHashMap();

ArrayList<String> arrayList = new ArrayList<>();

arrayList.add("Add URLS Here"); // Add full public facing urls

akamaiRequestMap.put("objects", arrayList);

new Gson().toJson(akamaiRequestMap)



Using below code call Akamai Fast Purge API. Below code can be used for single url also

// Need to send a HTTP Request to the OPEN API Interface,

// therefore we are using the com.google.api.client.http Helper Classes

HttpTransport httpTransport = new ApacheHttpTransport();

HttpRequestFactory requestFactory = httpTransport.createRequestFactory();

URI uri = null;

try {

    // End Point Ex: /ccu/v3/invalidate/url/production

    uri = new URI("https", "AKAMAI HOST READ IT FROM CONFIGURATION", "REFER END POINT", null, null);

} catch (URISyntaxException e1) {

log.error("URL gone wrong! {}", e1.getMessage());

}

// It is important to send the Request with the Content-Type application/json

if (uri != null && purgeObject != null) {

     HttpRequest akamaiRequest = requestFactory.buildPostRequest(new GenericUrl(uri),

    ByteArrayContent.fromString("application/json", purgeObject));

    HttpHeaders headers = akamaiRequest.getHeaders();

    headers.set("Host", "SET AKAMAI HOST FROM CONFIGURATION");


if (StringUtils.isNotBlank("CHECK ACCESS TOKEN IS BLANK OR NOT")

&& StringUtils.isNotBlank("CHECK CLIENT SECRET IS BLANK OR NOT")

&& StringUtils.isNotBlank("CHECK CLIENT TOKEN IS BLANK OR NOT"

&& StringUtils.isNotBlank("CHECK AKAMAI HOST IS BLANK OR NOT") {


GoogleHttpClientEdgeGridRequestSigner requestSigner = new GoogleHttpClientEdgeGridRequestSigner(akamaiClientCredentials(akamaiPurgeService));

try {

    requestSigner.sign(akamaiRequest);

    // Adding delay of minimum 5secs to allow dispatcher cache to be cleared before Akamai CC

    Thread.sleep(akamaiPurgeService.getDelayTime());

} catch (RequestSigningException | InterruptedException e) {

    log.error("Error in signing request to Akamai: {}", e.getMessage());

}

HttpResponse akamaiResponse = null;

try {

    akamaiResponse = akamaiRequest.execute();

} catch (Exception e) {

    log.error("Exception while executing akamaiRequest {}", e.getMessage());

}

    purgeResponse = akamaiResponseCheck(akamaiResponse);

} else {

    purgeResponse = "Akamai Fast Purge Not Configured";

    log.warn("Akamai Fast Purge Not Configured");

}


We can create a servlet and call the above method with request parameters or integrate the servlet with replication agent and get the page path using request.getHeader("Path"). Add the servlet path in Transport tab URI field

Comments

Popular posts from this blog

AEM Results count using query builder

AEM - Rollout a page programmatically and update references