Search in sources :

Example 6 with JcrPackage

use of org.apache.jackrabbit.vault.packaging.JcrPackage in project acs-aem-commons by Adobe-Consulting-Services.

the class JcrPackageReplicationStatusEventHandler method process.

@Override
public final JobResult process(final Job job) {
    final String[] paths = (String[]) job.getProperty(PROPERTY_PATHS);
    final String replicatedBy = StringUtils.defaultIfEmpty(this.replicatedByOverride, (String) job.getProperty(PROPERTY_REPLICATED_BY));
    log.debug("Processing Replication Status Update for JCR Package: {}", paths);
    ResourceResolver resourceResolver = null;
    try {
        resourceResolver = resourceResolverFactory.getServiceResourceResolver(AUTH_INFO);
        final List<JcrPackage> jcrPackages = this.getJcrPackages(resourceResolver, paths);
        if (CollectionUtils.isEmpty(jcrPackages)) {
            log.warn("JCR Package is unavailable for Replication Status Update at: {}", paths);
            return JobResult.OK;
        }
        for (final JcrPackage jcrPackage : jcrPackages) {
            setReplicationStatus(jcrPackage, replicatedBy, resourceResolver);
        }
    } catch (LoginException e) {
        log.error("Could not obtain a resource resolver for applying replication status updates", e);
        return JobResult.FAILED;
    } finally {
        if (resourceResolver != null) {
            resourceResolver.close();
        }
    }
    return JobResult.OK;
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) JcrPackage(org.apache.jackrabbit.vault.packaging.JcrPackage) LoginException(org.apache.sling.api.resource.LoginException)

Example 7 with JcrPackage

use of org.apache.jackrabbit.vault.packaging.JcrPackage in project acs-aem-commons by Adobe-Consulting-Services.

the class JcrPackageReplicationStatusEventHandler method handleEvent.

@Override
@SuppressWarnings("squid:S3776")
public final void handleEvent(final Event event) {
    if (this.isMaster) {
        // Only run on master
        final Map<String, Object> jobConfig = getInfoFromEvent(event);
        final String[] paths = (String[]) jobConfig.get(PROPERTY_PATHS);
        if (this.containsJcrPackagePath(paths)) {
            ResourceResolver resourceResolver = null;
            try {
                resourceResolver = resourceResolverFactory.getServiceResourceResolver(AUTH_INFO);
                final List<JcrPackage> jcrPackages = this.getJcrPackages(resourceResolver, paths);
                if (CollectionUtils.isNotEmpty(jcrPackages)) {
                    for (final JcrPackage jcrPackage : jcrPackages) {
                        // Close jcrPackages after they've been used to check if a Job should be invoked.
                        jcrPackage.close();
                    }
                    jobManager.addJob(JOB_TOPIC, jobConfig);
                }
            } catch (LoginException e) {
                log.error("Could not obtain a resource resolver.", e);
            } finally {
                if (resourceResolver != null) {
                    resourceResolver.close();
                }
            }
        }
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) JcrPackage(org.apache.jackrabbit.vault.packaging.JcrPackage) LoginException(org.apache.sling.api.resource.LoginException)

Example 8 with JcrPackage

use of org.apache.jackrabbit.vault.packaging.JcrPackage in project acs-aem-commons by Adobe-Consulting-Services.

the class JcrPackageReplicationStatusEventHandler method getJcrPackages.

/**
 * Resolves paths to Jcr Packages. If any path does not resolve to a valid Jcr Package, it is discarded.
 *
 * @param paths the list of paths to resolve to Jcr Packages
 * @return a list of Jcr Packages that correspond to the provided paths
 */
private List<JcrPackage> getJcrPackages(final ResourceResolver resourceResolver, final String[] paths) {
    final List<JcrPackage> packages = new ArrayList<JcrPackage>();
    for (final String path : paths) {
        final Resource eventResource = resourceResolver.getResource(path);
        JcrPackage jcrPackage = null;
        try {
            jcrPackage = packaging.open(eventResource.adaptTo(Node.class), false);
            if (jcrPackage != null) {
                packages.add(jcrPackage);
            }
        } catch (RepositoryException e) {
            log.warn("Error checking if the path [ {} ] is a JCR Package.", path);
        }
    }
    return packages;
}
Also used : JcrPackage(org.apache.jackrabbit.vault.packaging.JcrPackage) ArrayList(java.util.ArrayList) Resource(org.apache.sling.api.resource.Resource) RepositoryException(javax.jcr.RepositoryException)

Example 9 with JcrPackage

use of org.apache.jackrabbit.vault.packaging.JcrPackage in project acs-aem-commons by Adobe-Consulting-Services.

the class AbstractPackagerServlet method doPackaging.

protected void doPackaging(SlingHttpServletRequest request, SlingHttpServletResponse response, boolean preview, ValueMap properties, List<PathFilterSet> packageResources) throws IOException, JSONException, RepositoryException {
    // Add the ACL Packager Configuration page
    if (properties.get(INCLUDE_CONFIGURATION, DEFAULT_INCLUDE_CONFIGURATION)) {
        final PathFilterSet tmp = this.getPackagerPageResource(request);
        if (tmp != null) {
            packageResources.add(tmp);
        }
    }
    final Map<String, String> packageDefinitionProperties = new HashMap<String, String>();
    // ACL Handling
    packageDefinitionProperties.put(JcrPackageDefinition.PN_AC_HANDLING, properties.get(PACKAGE_ACL_HANDLING, AccessControlHandling.OVERWRITE.toString()));
    // Package Description
    packageDefinitionProperties.put(JcrPackageDefinition.PN_DESCRIPTION, properties.get(PACKAGE_DESCRIPTION, getDefaultPackageDescription()));
    if (preview) {
        // Handle preview mode
        response.getWriter().print(getPackageHelper().getPathFilterSetPreviewJSON(packageResources));
    } else if (packageResources == null || packageResources.isEmpty()) {
        // Do not create empty packages; This will only clutter up CRX Package Manager
        response.getWriter().print(getPackageHelper().getErrorJSON("Refusing to create a package with no filter " + "set rules."));
    } else {
        // Create JCR Package; Defaults should always be passed in via Request Parameters, but just in case
        final JcrPackage jcrPackage = getPackageHelper().createPackageFromPathFilterSets(packageResources, request.getResourceResolver().adaptTo(Session.class), properties.get(PACKAGE_GROUP_NAME, getDefaultPackageGroupName()), properties.get(PACKAGE_NAME, getDefaultPackageName()), properties.get(PACKAGE_VERSION, DEFAULT_PACKAGE_VERSION), PackageHelper.ConflictResolution.valueOf(properties.get(CONFLICT_RESOLUTION, PackageHelper.ConflictResolution.IncrementVersion.toString())), packageDefinitionProperties);
        String thumbnailPath = getPackageThumbnailPath();
        if (thumbnailPath != null) {
            // Add thumbnail to the package definition
            getPackageHelper().addThumbnail(jcrPackage, request.getResourceResolver().getResource(thumbnailPath));
        }
        log.debug("Successfully created JCR package");
        response.getWriter().print(getPackageHelper().getSuccessJSON(jcrPackage));
    }
}
Also used : PathFilterSet(org.apache.jackrabbit.vault.fs.api.PathFilterSet) HashMap(java.util.HashMap) JcrPackage(org.apache.jackrabbit.vault.packaging.JcrPackage)

Example 10 with JcrPackage

use of org.apache.jackrabbit.vault.packaging.JcrPackage in project acs-aem-commons by Adobe-Consulting-Services.

the class QueryPackagerServletImpl method doPost.

@Override
public final void doPost(final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws IOException {
    final ResourceResolver resourceResolver = request.getResourceResolver();
    final boolean preview = Boolean.parseBoolean(request.getParameter("preview"));
    log.debug("Preview mode: {}", preview);
    final ValueMap properties = this.getProperties(request);
    try {
        final List<Resource> packageResources = queryHelper.findResources(resourceResolver, properties.get("queryLanguage", Query.JCR_SQL2), properties.get("query", String.class), properties.get("relPath", String.class));
        final Map<String, String> packageDefinitionProperties = new HashMap<String, String>();
        // ACL Handling
        packageDefinitionProperties.put(JcrPackageDefinition.PN_AC_HANDLING, properties.get(PACKAGE_ACL_HANDLING, AccessControlHandling.OVERWRITE.toString()));
        // Package Description
        packageDefinitionProperties.put(JcrPackageDefinition.PN_DESCRIPTION, properties.get(PACKAGE_DESCRIPTION, DEFAULT_PACKAGE_DESCRIPTION));
        if (preview) {
            // Handle preview mode
            response.getWriter().print(packageHelper.getPreviewJSON(packageResources));
        } else if (packageResources == null || packageResources.isEmpty()) {
            // Do not create empty packages; This will only clutter up CRX Package Manager
            response.getWriter().print(packageHelper.getErrorJSON("Refusing to create a package with no filter " + "set rules."));
        } else {
            // Create JCR Package; Defaults should always be passed in via Request Parameters, but just in case
            final JcrPackage jcrPackage = packageHelper.createPackage(packageResources, request.getResourceResolver().adaptTo(Session.class), properties.get(PACKAGE_GROUP_NAME, DEFAULT_PACKAGE_GROUP_NAME), properties.get(PACKAGE_NAME, DEFAULT_PACKAGE_NAME), properties.get(PACKAGE_VERSION, DEFAULT_PACKAGE_VERSION), PackageHelper.ConflictResolution.valueOf(properties.get(CONFLICT_RESOLUTION, PackageHelper.ConflictResolution.IncrementVersion.toString())), packageDefinitionProperties);
            // Add thumbnail to the package definition
            packageHelper.addThumbnail(jcrPackage, request.getResourceResolver().getResource(QUERY_PACKAGE_THUMBNAIL_RESOURCE_PATH));
            log.debug("Successfully created JCR package");
            response.getWriter().print(packageHelper.getSuccessJSON(jcrPackage));
        }
    } catch (RepositoryException ex) {
        log.error("Repository error while creating Query Package", ex);
        response.getWriter().print(packageHelper.getErrorJSON(ex.getMessage()));
    } catch (IOException ex) {
        log.error("IO error while creating Query Package", ex);
        response.getWriter().print(packageHelper.getErrorJSON(ex.getMessage()));
    } catch (JSONException ex) {
        log.error("JSON error while creating Query Package response", ex);
        response.getWriter().print(packageHelper.getErrorJSON(ex.getMessage()));
    }
}
Also used : HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) JcrPackage(org.apache.jackrabbit.vault.packaging.JcrPackage) JSONException(org.apache.sling.commons.json.JSONException) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException)

Aggregations

JcrPackage (org.apache.jackrabbit.vault.packaging.JcrPackage)11 HashMap (java.util.HashMap)4 PackageId (org.apache.jackrabbit.vault.packaging.PackageId)4 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)4 JcrPackageManager (org.apache.jackrabbit.vault.packaging.JcrPackageManager)3 Resource (org.apache.sling.api.resource.Resource)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Node (javax.jcr.Node)2 RepositoryException (javax.jcr.RepositoryException)2 Session (javax.jcr.Session)2 PathFilterSet (org.apache.jackrabbit.vault.fs.api.PathFilterSet)2 JcrPackageDefinition (org.apache.jackrabbit.vault.packaging.JcrPackageDefinition)2 LoginException (org.apache.sling.api.resource.LoginException)2 BufferedInputStream (java.io.BufferedInputStream)1 Map (java.util.Map)1 ZipInputStream (java.util.zip.ZipInputStream)1 NodeIterator (javax.jcr.NodeIterator)1 DefaultWorkspaceFilter (org.apache.jackrabbit.vault.fs.config.DefaultWorkspaceFilter)1 VaultPackage (org.apache.jackrabbit.vault.packaging.VaultPackage)1