Contents
Objective
After reading this Article, You should have an Understanding of –
- The step in the workflow process that transfers a package from one AEM instance to another without installing them into newer instance.
- cURL command execution from a Java program
Introduction
In the approval workflow, we mostly come across the situation where you have to approve the content, asset, or package and, once approved, migrate that content to the upper instance (i.e., Dev to Stage) or vice versa (i.e., Prod to Stage). This way, you can sync the content in different instances. Even if you don’t want to override or install the content, simply upload the package using the cURL command. If you have to install the package as well, then you can use the Replicator API available in AEM.
Hence, without any further delay, let’s get started:
Dependency to use cURL command in Java
Dependency in core pom.xml
org.toile-libre.libe
curl
LATEST
Workflow Process Step for Package Migration using cURL Command
The cURL command helps to download and upload the package from or to an AEM instance. The below workflow process step shows the use of the cURL command to download the package from the AEM author instance and upload the same to the AEM publish instance programatically. So let’s see the implementation code :
PackageMigrationStep.java
package com.adobe.learning.core.workflow;
import com.day.cq.workflow.WorkflowException;
import com.day.cq.workflow.WorkflowSession;
import com.day.cq.workflow.exec.WorkItem;
import com.day.cq.workflow.exec.WorkflowProcess;
import com.day.cq.workflow.metadata.MetaDataMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.settings.SlingSettingsService;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
/**
* @author Shiv
*/
@Component(service = WorkflowProcess.class, property = {Constants.SERVICE_DESCRIPTION + "= Using the cURL command, upload the package to the publish instance.",
Constants.SERVICE_VENDOR + "= workflow.com",
"process.label" + "= Package Migration Step"})
public class PackageMigrationStep implements WorkflowProcess {
private static final Logger logger = LoggerFactory.getLogger(PackageMigrationStep.class);
String packagePath = StringUtils.EMPTY;
@Reference
SlingSettingsService slingSettingsService;
@Override
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
//Hard Coded Package Path
packagePath = "/etc/packages/my_packages/sample.zip";
try {
// Getting Home Path using SlingSettingService
String homePath = slingSettingsService.getSlingHomePath();
//Path where we download the package
String packageQuickStartPath = homePath + packagePath.substring(packagePath.lastIndexOf("/"));
// Curl for downloading the package
ProcessBuilder downloadBuilder = new ProcessBuilder("curl", "-u", "admin:admin", "http://localhost:4502/etc/packages/my_packages/sample.zip", "-o", packageQuickStartPath);
Process downloadProcess = downloadBuilder.start();
downloadProcess.waitFor();
int downloadExitCode = downloadProcess.exitValue();
logger.info("Exit Code : {}", downloadExitCode);
downloadProcess.destroy();
//Curl Command for Uploading the Package
//Use double quote after @
String[] command = {"curl", "-u", "admin:admin", "-F", "force=true", "-F", "package=@\"" + packageQuickStartPath + "\"", "http://localhost:4503/crx/packmgr/service/.json/?cmd=upload"};
ProcessBuilder processBuilder = new ProcessBuilder(command);
Process process = processBuilder.start();
process.waitFor();
int exitCode = process.exitValue();
logger.info("Exit Code : {}", exitCode);
process.destroy();
//Removing file from QuickStart Folder
File packageFile = new File(packageQuickStartPath);
boolean packageStatus = packageFile.delete();
logger.info("Package Delete Status : {}", packageStatus);
} catch (InterruptedException | IOException e) {
logger.error("Exception Occurred in Curl Command Execution {}", e.getMessage());
}
}
}
Conclusion
- So in this post, we tried to upload the package to a Publish instance via a workflow process. If you want to upload and install as well, you can use the Replicator API that I will cover in the next article. I hope you enjoyed this post. If you find it useful, leave us a comment. I would love to hear your thoughts and suggestions to make it better. Also, you can connect with me on LinkedIn.
Good work shiv👍
Hey Shiv, did you require any access modification on unix servers to move the packages?