Sunday, January 14, 2018

How to upload and install the packages through Java API - Adobe Experience Manager(AEM)

How to upload and install the packages through Java API - Adobe Experience Manager(AEM)

This post will explain the approach to upload and install the packages through JAVA.

Refer the post for details on creating the package through Java -

Sample Servlet to upload and install the package - Convert the servlet to scheduler and import/replicate the package based on the scheduled time

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.jcr.Session;
import javax.servlet.ServletException;

import org.apache.felix.scr.annotations.Reference;
import org.apache.jackrabbit.vault.packaging.JcrPackage;
import org.apache.jackrabbit.vault.packaging.JcrPackageManager;
import org.apache.jackrabbit.vault.packaging.Packaging;


 * Servlet that writes some sample content into the response. It is mounted for
 * all resources of a specific Sling resource type. The
 * {@link SlingSafeMethodsServlet} shall be used for HTTP methods that are
 * idempotent. For write operations use the {@link SlingAllMethodsServlet}.
@SlingServlet(paths= "/services/packageupload")
public class PackageUploadServlet extends SlingSafeMethodsServlet {

private Packaging packaging;

private SlingRepository repository;

private ResourceResolverFactory resolverFactory;

private SlingSettingsService settingsService;

Replicator replicator;

protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp)
throws ServletException, IOException {

Session session = null;
if (isRunMode("author") && isMasterRepository()) {//Only run in author - change the servlet to scheduler and import/replicate the package based on the scheduled time
try {

String packagePath = "C:\\Albin\\";
Map<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE, "packageService");

ResourceResolver resolver;

resolver = resolverFactory.getServiceResourceResolver(param);

session = resolver.adaptTo(Session.class);
JcrPackageManager jcrPackageManager = packaging.getPackageManager(session);

JcrPackage inputPackage=jcrPackageManager.upload(new File(packagePath), false, true, "test12018-01-14-15-00-00-023-1.0");
ImportOptions importOption=new ImportOptions();//Specify the import configurations


//Replicate the package to publishers
replicator.replicate(session, ReplicationActionType.ACTIVATE, inputPackage.getDefinition().getNode().getPath().split("/jcr:content")[0]);

resp.getOutputStream().println("This package successfully uploaded and installed to the server....");

} catch (Exception e) {
// TODO Auto-generated catch block
resp.getOutputStream().println("Excpetion while uploading/installing the package...."+e.getMessage());
}finally {

private Boolean isRunMode(String mode) {
Set<String> runModes = settingsService.getRunModes();
for (String runMode : runModes) {
if (runMode.equalsIgnoreCase(mode)) {
return true;
return false;

public boolean isMasterRepository() {
final String isMaster = repository.getDescriptor("crx.cluster.master");
return isMaster != null && !isMaster.equals("") && Boolean.parseBoolean(isMaster);

Details on package manager upload method

JcrPackage upload(@Nonnull
                           boolean isTmpFile,
                           boolean replace,
                           java.lang.String nameHint)
                    throws javax.jcr.RepositoryException,

Uploads a package. The location is chosen from the installation path of the package. if the package does not provide such a path, the nameHint is respected and the package is placed below the package root. if the package already exists at that path it is not installed and null is returned unless replace is true.

file - package file to upload
isTmpFile - indicates if the given file is a temp file and can be deleted when the package is closed
replace - if true existing packages are replaced.
nameHint - hint for the name if package does not provide one

the new jcr package

Make sure the service user(packageassempler) is having the access to the filter paths specified in the package and also the create/modify/delete/replicate permission to /etc/packages - Refer the post for the steps on configuring the service user.

The configured package is create with the filter - /etc/cloudservices/search-promote so provided the modify/create/delete access to the service user (the service user should be having the modify/create/delete access for the filter path defined in the package)



Verify the package status in CRXD package manager and CRXDE



The package also successfully replicated to the agent - I could not able to validate the replication through publisher(not having the local setup) but the replication requests are queued in author agent.


No comments:

Post a Comment