You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by dk...@apache.org on 2020/09/08 19:30:01 UTC

[sling-org-apache-sling-app-cms] 02/02: Adding missed files

This is an automated email from the ASF dual-hosted git repository.

dklco pushed a commit to branch SLING-8913-multiple-instance-types
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-app-cms.git

commit 72c319753fb24b3d7247a5a60ca9f890fe6f4cf3
Author: Dan Klco <dk...@apache.org>
AuthorDate: Tue Sep 8 15:29:45 2020 -0400

    Adding missed files
---
 .../IsPublishableResourceContainer.java            |  40 +++++++
 .../cms/publication/IsPublishableResourceType.java |  36 ++++++
 .../cms/core/publication/BulkPublicationJob.java   | 124 +++++++++++++++++++++
 .../libs/sling-cms/content/publication/bulk.json   |  48 ++++++++
 4 files changed, 248 insertions(+)

diff --git a/api/src/main/java/org/apache/sling/cms/publication/IsPublishableResourceContainer.java b/api/src/main/java/org/apache/sling/cms/publication/IsPublishableResourceContainer.java
new file mode 100644
index 0000000..a2a1436
--- /dev/null
+++ b/api/src/main/java/org/apache/sling/cms/publication/IsPublishableResourceContainer.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.cms.publication;
+
+import java.util.Optional;
+import java.util.function.Predicate;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.cms.CMSConstants;
+import org.apache.sling.jcr.resource.api.JcrResourceConstants;
+
+/**
+ * A predicate for evaluating if a resource can contain publishable resources or
+ * be a publishable resource itself
+ */
+public class IsPublishableResourceContainer implements Predicate<Resource> {
+
+    @Override
+    public boolean test(Resource r) {
+        return Optional.ofNullable(r).map(Resource::getResourceType)
+                .map(rt -> JcrResourceConstants.NT_SLING_FOLDER.equals(rt)
+                        || JcrResourceConstants.NT_SLING_ORDERED_FOLDER.equals(rt) || CMSConstants.NT_FILE.equals(rt)
+                        || CMSConstants.NT_SITE.equals(rt) || CMSConstants.NT_PAGE.equals(rt))
+                .orElse(false);
+    }
+}
\ No newline at end of file
diff --git a/api/src/main/java/org/apache/sling/cms/publication/IsPublishableResourceType.java b/api/src/main/java/org/apache/sling/cms/publication/IsPublishableResourceType.java
new file mode 100644
index 0000000..0cc1ecb
--- /dev/null
+++ b/api/src/main/java/org/apache/sling/cms/publication/IsPublishableResourceType.java
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.cms.publication;
+
+import java.util.Optional;
+import java.util.function.Predicate;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.cms.CMSConstants;
+import org.apache.sling.cms.ResourceTree;
+
+/**
+ * A predicate for evaluating if a resource is a publishable resource type.
+ */
+public class IsPublishableResourceType implements Predicate<ResourceTree> {
+
+    @Override
+    public boolean test(ResourceTree rt) {
+        return Optional.ofNullable(rt).map(ResourceTree::getResource).map(Resource::getResourceType)
+                .map(type -> CMSConstants.NT_FILE.equals(type) || CMSConstants.NT_PAGE.equals(type)).orElse(false);
+    }
+}
\ No newline at end of file
diff --git a/core/src/main/java/org/apache/sling/cms/core/publication/BulkPublicationJob.java b/core/src/main/java/org/apache/sling/cms/core/publication/BulkPublicationJob.java
new file mode 100644
index 0000000..6239692
--- /dev/null
+++ b/core/src/main/java/org/apache/sling/cms/core/publication/BulkPublicationJob.java
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.cms.core.publication;
+
+import java.util.Collections;
+import java.util.stream.Stream;
+
+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.cms.ConfigurableJobExecutor;
+import org.apache.sling.cms.PublishableResource;
+import org.apache.sling.cms.ResourceTree;
+import org.apache.sling.cms.publication.IsPublishableResourceContainer;
+import org.apache.sling.cms.publication.IsPublishableResourceType;
+import org.apache.sling.cms.publication.PublicationException;
+import org.apache.sling.cms.publication.PublicationManager;
+import org.apache.sling.cms.publication.PublicationManagerFactory;
+import org.apache.sling.cms.publication.PublicationType;
+import org.apache.sling.event.jobs.Job;
+import org.apache.sling.event.jobs.consumer.JobConsumer;
+import org.apache.sling.event.jobs.consumer.JobExecutionContext;
+import org.apache.sling.event.jobs.consumer.JobExecutionResult;
+import org.apache.sling.event.jobs.consumer.JobExecutor;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A Sling Post Operation to unpublish content
+ */
+@Component(service = { JobExecutor.class, ConfigurableJobExecutor.class }, property = {
+        JobConsumer.PROPERTY_TOPICS + "=" + BulkPublicationJob.TOPIC })
+public class BulkPublicationJob extends ConfigurableJobExecutor {
+
+    private static final Logger log = LoggerFactory.getLogger(BulkPublicationJob.class);
+
+    public static final String TOPIC = "cmsjob/org/apache/sling/cms/publication/Bulk";
+
+    @Reference
+    private PublicationManagerFactory publicationManagerFactory;
+
+    @Reference
+    private ResourceResolverFactory factory;
+
+    @Override
+    public JobExecutionResult doProcess(Job job, JobExecutionContext context, ResourceResolver resolver) {
+        String[] paths = job.getProperty("paths", String[].class);
+        PublicationType type = PublicationType.valueOf(job.getProperty("type", String.class));
+        boolean deep = job.getProperty("deep", false);
+
+        PublicationManager publicationManager = publicationManagerFactory.getPublicationManager();
+
+        context.initProgress(paths.length, paths.length * 5000L);
+
+        log.info("Starting bulk publication: paths={}, type={}, deep={}", paths, type, deep);
+
+        for (String path : paths) {
+            Stream<PublishableResource> toPublish = null;
+            Resource resource = resolver.getResource(path);
+            if (deep) {
+                toPublish = ResourceTree
+                        .stream(resource, new IsPublishableResourceContainer(), new IsPublishableResourceType())
+                        .map(rt -> rt.getResource().adaptTo(PublishableResource.class));
+            } else {
+                toPublish = Collections.singletonList(resource.adaptTo(PublishableResource.class)).stream();
+            }
+            toPublish.forEach(pr -> {
+                try {
+                    if (type == PublicationType.ADD) {
+                        publicationManager.publish(pr);
+                    } else {
+                        publicationManager.unpublish(pr);
+                    }
+                    context.log("{0} complete for {1}", type, pr.getPath());
+                } catch (PublicationException e) {
+                    context.log("{0} failed for {1}", type, pr.getPath());
+                    log.warn("Failed to publish {}", pr, e);
+                }
+            });
+
+            context.log("Publication complete for path: {0}", path);
+            context.incrementProgressCount(1);
+        }
+
+        context.log("Publication complete!");
+        return context.result().succeeded();
+    }
+
+    @Override
+    public String getConfigurationPath() {
+        return "/mnt/overlay/sling-cms/content/publication/bulk";
+    }
+
+    @Override
+    public ResourceResolverFactory getResolverFactory() {
+        return factory;
+    }
+
+    @Override
+    public String getTitleKey() {
+        return "slingcms.bulkpublication.title";
+    }
+
+    @Override
+    public String getTopic() {
+        return TOPIC;
+    }
+}
\ No newline at end of file
diff --git a/ui/src/main/resources/jcr_root/libs/sling-cms/content/publication/bulk.json b/ui/src/main/resources/jcr_root/libs/sling-cms/content/publication/bulk.json
new file mode 100644
index 0000000..c32debf
--- /dev/null
+++ b/ui/src/main/resources/jcr_root/libs/sling-cms/content/publication/bulk.json
@@ -0,0 +1,48 @@
+{
+    "jcr:primaryType": "nt:unstructured",
+    "sling:resourceType": "sling-cms/components/general/container",
+    "paths": {
+        "jcr:primaryType": "nt:unstructured",
+        "sling:resourceType": "sling-cms/components/editor/fields/repeating",
+        "label": "Paths",
+        "name": "paths"
+    },
+    "type": {
+        "jcr:primaryType": "nt:unstructured",
+        "sling:resourceType": "sling-cms/components/editor/fields/select",
+        "label": "Type",
+        "name": "type",
+        "options": {
+            "ADD": {
+                "label": "Publish",
+                "value": "ADD"
+            },
+            "regex": {
+                "label": "Unpublish",
+                "value": "DELETE"
+            }
+        }
+    },
+    "deep": {
+        "jcr:primaryType": "nt:unstructured",
+        "sling:resourceType": "sling-cms/components/editor/fields/select",
+        "label": "Is Deep",
+        "name": "deep",
+        "options": {
+            "ADD": {
+                "label": "Yes",
+                "value": "true"
+            },
+            "regex": {
+                "label": "No",
+                "value": "false"
+            }
+        }
+    },
+    "topic": {
+        "jcr:primaryType": "nt:unstructured",
+        "sling:resourceType": "sling-cms/components/editor/fields/hidden",
+        "name": "job.topics",
+        "value": "cmsjob/org/apache/sling/cms/publication/Bulk"
+    }
+}
\ No newline at end of file