can you check the below example
https://www.linkedin.com/pulse/akamai-cache-purge-aem-through-java-code-shubham-saxena/
=====================
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import javax.jcr.Session;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.jcr.resource.JcrResourceConstants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.day.cq.commons.Externalizer;
import com.day.cq.replication.ContentBuilder;
import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationContent;
import com.day.cq.replication.ReplicationContentFactory;
import com.day.cq.replication.ReplicationException;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.myproject.bundle.core.constants.FsbpConstants;
import com.myproject.bundle.core.search.services.MyProjectConfigurationService;
/**
* Akamai content builder to create replication content containing a JSON array
* of URLs for Akamai to purge through the Akamai Transport Handler. This class
* takes the internal resource path and converts it to external URLs as well as
* adding vanity URLs and pages that may Sling include the activated resource.
*/
@Component(service = ContentBuilder.class,
property = {"name=myProjectAkamai", "value=akamai"},
immediate = true)
public class AkamaiContentBuilder implements ContentBuilder {
@Reference
private ResourceResolverFactory resolverFactory;
/** The name of the replication agent */
public static final String NAME = "myProjectAkamai";
/**
* The serialization type as it will display in the replication
* agent edit dialog selection field.
*/
public static final String TITLE = "My Project Akamai Purge Content Builder";
@Reference
MyProjectConfigurationService myProjectConfigurationService;
private static final Logger LOG = LoggerFactory.getLogger(AkamaiContentBuilder.class);
/**
* {@inheritDoc}
*/
@Override
public ReplicationContent create(Session session, ReplicationAction action,
ReplicationContentFactory factory) throws ReplicationException {
return create(session, action, factory, null);
}
/**
* Create the replication content containing the public facing URLs for
* Akamai to purge.
*/
@Override
public ReplicationContent create(Session session, ReplicationAction action,
ReplicationContentFactory factory, Map<String, Object> parameters)
throws ReplicationException {
final String path = action.getPath();
ResourceResolver resolver = null;
JSONArray jsonArray = new JSONArray();
if (StringUtils.isNotBlank(path)) {
HashMap<String, Object> sessionMap = new HashMap<>();
sessionMap.put(JcrResourceConstants.AUTHENTICATION_INFO_SESSION, session);
try {
resolver = resolverFactory.getResourceResolver(sessionMap);
if (StringUtils.contains(path, myProjectConfigurationService.getContentpath())) { // my project's specific page root path - /content/myproject
jsonArray = createPageContent(resolver, jsonArray, path);
} else if (StringUtils.contains(path, myProjectConfigurationService.getAssetpath())) { // my project's specific dam root path - /content/dam/myproject
jsonArray = createAssetContent(resolver, jsonArray, path);
}
} catch (LoginException e) {
LOG.error("Could not retrieve Page Manager", e);
}
return createContent(factory, jsonArray);
}
return ReplicationContent.VOID;
}
private JSONArray createPageContent(ResourceResolver resolver, JSONArray jsonArray, String path) {
PageManager pageManager = resolver.adaptTo(PageManager.class);
if (null != pageManager) {
Page purgedPage = pageManager.getContainingPage(path);
Externalizer externalizer = resolver.adaptTo(Externalizer.class);
if (null != purgedPage && null != externalizer) {
final String link = externalizer.externalLink(resolver, "firestonebpco", path);
jsonArray.put(link);
LOG.info("Page link added: {}", link);
final String vanityUrl = purgedPage.getVanityUrl();
if (StringUtils.isNotBlank(vanityUrl)) {
jsonArray.put(vanityUrl);
LOG.info("Vanity URL added: {}", vanityUrl);
}
} else {
jsonArray.put(path);
LOG.info("Page Resource path added: {}", path);
}
}
return jsonArray;
}
private JSONArray createAssetContent(ResourceResolver resolver, JSONArray jsonArray, String path) {
Resource purgedAssetResource = resolver.getResource(path);
Externalizer externalizer = resolver.adaptTo(Externalizer.class);
if (null != purgedAssetResource && null != externalizer) {
final String assetLink = externalizer.externalLink(resolver, "firestonebpco", path);
jsonArray.put(assetLink);
LOG.info("Asset link added: {}", assetLink);
} else {
jsonArray.put(path);
LOG.info("Asset Resource path added: {}", path);
}
return jsonArray;
}
/**
* Create the replication content containing
*
* @param factory Factory to create replication content
* @param jsonArray JSON array of URLS to include in replication content
* @return replication content
*
* @throws ReplicationException if an error occurs
*/
private ReplicationContent createContent(final ReplicationContentFactory factory,
final JSONArray jsonArray) throws ReplicationException {
Path tempFile;
try {
tempFile = Files.createTempFile("akamai_purge_agent", ".tmp");
} catch (IOException e) {
throw new ReplicationException("Could not create temporary file", e);
}
try (BufferedWriter writer = Files.newBufferedWriter(tempFile, Charset.forName("UTF-8"))) {
writer.write(jsonArray.toString());
writer.flush();
return factory.create("text/plain", tempFile.toFile(), true);
} catch (IOException e) {
throw new ReplicationException("Could not write to temporary file", e);
}
}
/**
* {@inheritDoc}
*
* @return {@value #NAME}
*/
@Override
public String getName() {
return NAME;
}
/**
* {@inheritDoc}
*
* @return {@value #TITLE}
*/
@Override
public String getTitle() {
return TITLE;
}
}
=============================
https://github.com/AvionosLLC/purge-surgeon/tree/develop