[AEM6.5] Servlet to Unpublish and Removing Bulk Assets [using path from CSV]
Hi All,
I have asked a question https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aem6-5-unpublish-and-removing-bulk-assets-using-path-from-csv/m-p/562317#M139982 [AEM6.5] Unpublish and Removing Bulk Assets [using path from CSV] from which I came across the best way to unpublish and then remove asset for one time will be the Groovy Script.
But there is change in requirement, is I have to write a servlet, which will read the assets path through CSV and then Unpublish and remove those assets from AEM instance.
I need help to correct the servlet or any other suggested psudo code:
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.servlets.annotations.SlingServletPaths;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
@Component(service = Servlet.class)
@SlingServletPaths(
value = {"/test/unpublish"}
)
public class UnpublishAssetServlet extends SlingAllMethodsServlet {
private static final long serialVersionUID = 1L;
@Reference
private PageManager pageManager;
final Logger log = LoggerFactory.getLogger(UnpublishAssetServlet.class);
@Override
protected void doPost(SlingHttpServletRequest request,
SlingHttpServletResponse response) throws ServletException, IOException {
String filePath = "/tmp/assetPath.csv";
List<String> assetPaths = readFromCsv(filePath);
for (String assetPath : assetPaths) {
unpublishAsset(assetPath);
deleteAsset(assetPath);
}
log.info("Successfully removed assets");
response.getWriter().write("Successfully removed assets");
}
private List<String> readFromCsv(String filePath) {
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
List<String> assetPaths = new ArrayList<String>();
try {
br = new BufferedReader(new FileReader(filePath));
while ((line = br.readLine()) != null) {
String[] values = line.split(cvsSplitBy);
String assetPath = values[0];
assetPaths.add(assetPath);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return assetPaths;
}
private void unpublishAsset(String assetPath) {
Page page = pageManager.getPage(assetPath);
if (page != null) {
try {
pageManager.unpublish(page);
log.info("Successfully unpublished asset at path " + assetPath);
} catch (WCMException e) {
log.error("Error unpublishing asset at path " + assetPath);
e.printStackTrace();
}
}
}
private void deleteAsset(String assetPath) {
try {
pageManager.delete(assetPath, false);
log.info("Successfully deleted asset at path " + assetPath);
} catch (WCMException e) {
log.error("Error deleting asset at path " + assetPath);
e.printStackTrace();
}
}
}
Thanks
@arunpatidar @kautuk_sahni @theo_pendle @kiran_vedantam