SLING-7877 Provide Clam support for Sling
authorOliver Lietz <olli@apache.org>
Sat, 1 Sep 2018 10:14:15 +0000 (12:14 +0200)
committerOliver Lietz <olli@apache.org>
Sat, 1 Sep 2018 10:14:15 +0000 (12:14 +0200)
18 files changed:
.gitignore [new file with mode: 0644]
LICENSE [new file with mode: 0644]
README.md [new file with mode: 0644]
bnd.bnd [new file with mode: 0644]
pom.xml [new file with mode: 0644]
src/main/java/org/apache/sling/clam/internal/ClamUtil.java [new file with mode: 0644]
src/main/java/org/apache/sling/clam/job/internal/JcrPropertyScanJobConsumer.java [new file with mode: 0644]
src/main/java/org/apache/sling/clam/oak/internal/JcrPropertyDigger.java [new file with mode: 0644]
src/main/java/org/apache/sling/clam/oak/internal/JcrPropertyDiggerConfiguration.java [new file with mode: 0644]
src/main/java/org/apache/sling/clam/result/JcrPropertyScanResultHandler.java [new file with mode: 0644]
src/main/java/org/apache/sling/clam/result/internal/EventPublishingScanResultHandler.java [new file with mode: 0644]
src/main/java/org/apache/sling/clam/result/internal/EventPublishingScanResultHandlerConfiguration.java [new file with mode: 0644]
src/main/java/org/apache/sling/clam/result/internal/ResourcePersistingScanResultHandler.java [new file with mode: 0644]
src/main/java/org/apache/sling/clam/result/internal/ResourcePersistingScanResultHandlerConfiguration.java [new file with mode: 0644]
src/main/java/org/apache/sling/clam/result/package-info.java [new file with mode: 0644]
src/test/java/org/apache/sling/clam/it/tests/ClamTestSupport.java [new file with mode: 0644]
src/test/java/org/apache/sling/clam/it/tests/JcrPropertyScanJobConsumerIT.java [new file with mode: 0644]
src/test/java/org/apache/sling/clam/it/tests/ResourcePersistingScanResultHandlerIT.java [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..5b783ed
--- /dev/null
@@ -0,0 +1,17 @@
+/target
+.idea
+.classpath
+.metadata
+.project
+.settings
+.externalToolBuilders
+maven-eclipse.xml
+*.swp
+*.iml
+*.ipr
+*.iws
+*.bak
+.vlt
+.DS_Store
+jcr.log
+atlassian-ide-plugin.xml
diff --git a/LICENSE b/LICENSE
new file mode 100644 (file)
index 0000000..d645695
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,202 @@
+
+                                 Apache License
+                           Version 2.0, January 2004
+                        http://www.apache.org/licenses/
+
+   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+   1. Definitions.
+
+      "License" shall mean the terms and conditions for use, reproduction,
+      and distribution as defined by Sections 1 through 9 of this document.
+
+      "Licensor" shall mean the copyright owner or entity authorized by
+      the copyright owner that is granting the License.
+
+      "Legal Entity" shall mean the union of the acting entity and all
+      other entities that control, are controlled by, or are under common
+      control with that entity. For the purposes of this definition,
+      "control" means (i) the power, direct or indirect, to cause the
+      direction or management of such entity, whether by contract or
+      otherwise, or (ii) ownership of fifty percent (50%) or more of the
+      outstanding shares, or (iii) beneficial ownership of such entity.
+
+      "You" (or "Your") shall mean an individual or Legal Entity
+      exercising permissions granted by this License.
+
+      "Source" form shall mean the preferred form for making modifications,
+      including but not limited to software source code, documentation
+      source, and configuration files.
+
+      "Object" form shall mean any form resulting from mechanical
+      transformation or translation of a Source form, including but
+      not limited to compiled object code, generated documentation,
+      and conversions to other media types.
+
+      "Work" shall mean the work of authorship, whether in Source or
+      Object form, made available under the License, as indicated by a
+      copyright notice that is included in or attached to the work
+      (an example is provided in the Appendix below).
+
+      "Derivative Works" shall mean any work, whether in Source or Object
+      form, that is based on (or derived from) the Work and for which the
+      editorial revisions, annotations, elaborations, or other modifications
+      represent, as a whole, an original work of authorship. For the purposes
+      of this License, Derivative Works shall not include works that remain
+      separable from, or merely link (or bind by name) to the interfaces of,
+      the Work and Derivative Works thereof.
+
+      "Contribution" shall mean any work of authorship, including
+      the original version of the Work and any modifications or additions
+      to that Work or Derivative Works thereof, that is intentionally
+      submitted to Licensor for inclusion in the Work by the copyright owner
+      or by an individual or Legal Entity authorized to submit on behalf of
+      the copyright owner. For the purposes of this definition, "submitted"
+      means any form of electronic, verbal, or written communication sent
+      to the Licensor or its representatives, including but not limited to
+      communication on electronic mailing lists, source code control systems,
+      and issue tracking systems that are managed by, or on behalf of, the
+      Licensor for the purpose of discussing and improving the Work, but
+      excluding communication that is conspicuously marked or otherwise
+      designated in writing by the copyright owner as "Not a Contribution."
+
+      "Contributor" shall mean Licensor and any individual or Legal Entity
+      on behalf of whom a Contribution has been received by Licensor and
+      subsequently incorporated within the Work.
+
+   2. Grant of Copyright License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      copyright license to reproduce, prepare Derivative Works of,
+      publicly display, publicly perform, sublicense, and distribute the
+      Work and such Derivative Works in Source or Object form.
+
+   3. Grant of Patent License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      (except as stated in this section) patent license to make, have made,
+      use, offer to sell, sell, import, and otherwise transfer the Work,
+      where such license applies only to those patent claims licensable
+      by such Contributor that are necessarily infringed by their
+      Contribution(s) alone or by combination of their Contribution(s)
+      with the Work to which such Contribution(s) was submitted. If You
+      institute patent litigation against any entity (including a
+      cross-claim or counterclaim in a lawsuit) alleging that the Work
+      or a Contribution incorporated within the Work constitutes direct
+      or contributory patent infringement, then any patent licenses
+      granted to You under this License for that Work shall terminate
+      as of the date such litigation is filed.
+
+   4. Redistribution. You may reproduce and distribute copies of the
+      Work or Derivative Works thereof in any medium, with or without
+      modifications, and in Source or Object form, provided that You
+      meet the following conditions:
+
+      (a) You must give any other recipients of the Work or
+          Derivative Works a copy of this License; and
+
+      (b) You must cause any modified files to carry prominent notices
+          stating that You changed the files; and
+
+      (c) You must retain, in the Source form of any Derivative Works
+          that You distribute, all copyright, patent, trademark, and
+          attribution notices from the Source form of the Work,
+          excluding those notices that do not pertain to any part of
+          the Derivative Works; and
+
+      (d) If the Work includes a "NOTICE" text file as part of its
+          distribution, then any Derivative Works that You distribute must
+          include a readable copy of the attribution notices contained
+          within such NOTICE file, excluding those notices that do not
+          pertain to any part of the Derivative Works, in at least one
+          of the following places: within a NOTICE text file distributed
+          as part of the Derivative Works; within the Source form or
+          documentation, if provided along with the Derivative Works; or,
+          within a display generated by the Derivative Works, if and
+          wherever such third-party notices normally appear. The contents
+          of the NOTICE file are for informational purposes only and
+          do not modify the License. You may add Your own attribution
+          notices within Derivative Works that You distribute, alongside
+          or as an addendum to the NOTICE text from the Work, provided
+          that such additional attribution notices cannot be construed
+          as modifying the License.
+
+      You may add Your own copyright statement to Your modifications and
+      may provide additional or different license terms and conditions
+      for use, reproduction, or distribution of Your modifications, or
+      for any such Derivative Works as a whole, provided Your use,
+      reproduction, and distribution of the Work otherwise complies with
+      the conditions stated in this License.
+
+   5. Submission of Contributions. Unless You explicitly state otherwise,
+      any Contribution intentionally submitted for inclusion in the Work
+      by You to the Licensor shall be under the terms and conditions of
+      this License, without any additional terms or conditions.
+      Notwithstanding the above, nothing herein shall supersede or modify
+      the terms of any separate license agreement you may have executed
+      with Licensor regarding such Contributions.
+
+   6. Trademarks. This License does not grant permission to use the trade
+      names, trademarks, service marks, or product names of the Licensor,
+      except as required for reasonable and customary use in describing the
+      origin of the Work and reproducing the content of the NOTICE file.
+
+   7. Disclaimer of Warranty. Unless required by applicable law or
+      agreed to in writing, Licensor provides the Work (and each
+      Contributor provides its Contributions) on an "AS IS" BASIS,
+      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+      implied, including, without limitation, any warranties or conditions
+      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+      PARTICULAR PURPOSE. You are solely responsible for determining the
+      appropriateness of using or redistributing the Work and assume any
+      risks associated with Your exercise of permissions under this License.
+
+   8. Limitation of Liability. In no event and under no legal theory,
+      whether in tort (including negligence), contract, or otherwise,
+      unless required by applicable law (such as deliberate and grossly
+      negligent acts) or agreed to in writing, shall any Contributor be
+      liable to You for damages, including any direct, indirect, special,
+      incidental, or consequential damages of any character arising as a
+      result of this License or out of the use or inability to use the
+      Work (including but not limited to damages for loss of goodwill,
+      work stoppage, computer failure or malfunction, or any and all
+      other commercial damages or losses), even if such Contributor
+      has been advised of the possibility of such damages.
+
+   9. Accepting Warranty or Additional Liability. While redistributing
+      the Work or Derivative Works thereof, You may choose to offer,
+      and charge a fee for, acceptance of support, warranty, indemnity,
+      or other liability obligations and/or rights consistent with this
+      License. However, in accepting such obligations, You may act only
+      on Your own behalf and on Your sole responsibility, not on behalf
+      of any other Contributor, and only if You agree to indemnify,
+      defend, and hold each Contributor harmless for any liability
+      incurred by, or claims asserted against, such Contributor by reason
+      of your accepting any such warranty or additional liability.
+
+   END OF TERMS AND CONDITIONS
+
+   APPENDIX: How to apply the Apache License to your work.
+
+      To apply the Apache License to your work, attach the following
+      boilerplate notice, with the fields enclosed by brackets "[]"
+      replaced with your own identifying information. (Don't include
+      the brackets!)  The text should be enclosed in the appropriate
+      comment syntax for the file format. We also recommend that a
+      file or class name and description of purpose be included on the
+      same "printed page" as the copyright notice for easier
+      identification within third-party archives.
+
+   Copyright [yyyy] [name of copyright owner]
+
+   Licensed 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.
diff --git a/README.md b/README.md
new file mode 100644 (file)
index 0000000..1080707
--- /dev/null
+++ b/README.md
@@ -0,0 +1,27 @@
+# Apache Sling Clam
+
+This module is part of the [Apache Sling](https://sling.apache.org) project.
+
+This module provides support for Clam in Sling.
+
+
+## Finding data to scan for malware
+
+`JcrPropertyDigger` observes Oak's NodeStore, digs properties based on type, path and length and creates scan jobs.
+
+**NOTE**: Ensure to exclude scan jobs in `/var/eventing` and scan results in `/var/clam/results` from scanning.
+
+
+## Scanning data
+
+`JcrPropertyScanJobConsumer` processes scan jobs by reading property values from JCR, sends data to Clam service for scanning and invokes optional scan result handlers.
+
+The service requires read-only access to all paths to be scanned which can be allowed by adding the service user mapping `org.apache.sling.clam=sling-readall`.
+
+
+## Handling of scan results
+
+`EventPublishingScanResultHandler` publishes scan results via OSGi Event Admin Service.
+
+`ResourcePersistingScanResultHandler` persists scan results via ResourceResolver in JCR. The result handler requires write access to a configurable root path for subservice `result-writer`.
+
diff --git a/bnd.bnd b/bnd.bnd
new file mode 100644 (file)
index 0000000..e36a0d6
--- /dev/null
+++ b/bnd.bnd
@@ -0,0 +1,3 @@
+-removeheaders:\
+  Include-Resource,\
+  Private-Package
diff --git a/pom.xml b/pom.xml
new file mode 100644 (file)
index 0000000..356e06c
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,254 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.apache.sling</groupId>
+    <artifactId>sling</artifactId>
+    <version>34</version>
+    <relativePath />
+  </parent>
+
+  <artifactId>org.apache.sling.clam</artifactId>
+  <version>1.0.0-SNAPSHOT</version>
+
+  <name>Apache Sling Clam</name>
+  <description>Support for scanning Sling's repository data with Clam service</description>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+    <sling.java.version>8</sling.java.version>
+    <org.ops4j.pax.exam.version>4.12.0</org.ops4j.pax.exam.version>
+  </properties>
+
+  <scm>
+    <connection>scm:git:https://gitbox.apache.org/repos/asf/sling-org-apache-sling-clam.git</connection>
+    <developerConnection>scm:git:https://gitbox.apache.org/repos/asf/sling-org-apache-sling-clam.git</developerConnection>
+    <url>https://gitbox.apache.org/repos/asf?p=sling-org-apache-sling-clam.git</url>
+  </scm>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>biz.aQute.bnd</groupId>
+        <artifactId>bnd-maven-plugin</artifactId>
+      </plugin>
+      <!--
+      <plugin>
+        <groupId>biz.aQute.bnd</groupId>
+        <artifactId>bnd-baseline-maven-plugin</artifactId>
+      </plugin>
+      -->
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-failsafe-plugin</artifactId>
+        <executions>
+          <execution>
+            <goals>
+              <goal>integration-test</goal>
+              <goal>verify</goal>
+            </goals>
+          </execution>
+        </executions>
+        <configuration>
+          <redirectTestOutputToFile>true</redirectTestOutputToFile>
+          <systemProperties>
+            <property>
+              <name>bundle.filename</name>
+              <value>${basedir}/target/${project.build.finalName}.jar</value>
+            </property>
+          </systemProperties>
+        </configuration>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.servicemix.tooling</groupId>
+        <artifactId>depends-maven-plugin</artifactId>
+      </plugin>
+    </plugins>
+  </build>
+
+  <dependencies>
+    <!-- javax -->
+    <dependency>
+      <groupId>javax.inject</groupId>
+      <artifactId>javax.inject</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>javax.jcr</groupId>
+      <artifactId>jcr</artifactId>
+      <scope>provided</scope>
+    </dependency>
+    <!-- OSGi -->
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.annotation.versioning</artifactId>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>osgi.cmpn</artifactId>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>osgi.core</artifactId>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.service.component.annotations</artifactId>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.service.metatype.annotations</artifactId>
+      <scope>provided</scope>
+    </dependency>
+    <!-- Apache Commons -->
+    <dependency>
+      <groupId>commons-io</groupId>
+      <artifactId>commons-io</artifactId>
+      <version>2.5</version>
+      <scope>provided</scope>
+    </dependency>
+    <!-- Apache Felix -->
+    <dependency>
+      <groupId>org.apache.felix</groupId>
+      <artifactId>org.apache.felix.framework</artifactId>
+      <version>5.6.10</version>
+      <scope>test</scope>
+    </dependency>
+    <!-- Apache Jackrabbit -->
+    <dependency>
+      <groupId>org.apache.jackrabbit</groupId>
+      <artifactId>jackrabbit-api</artifactId>
+      <version>2.16.3</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.jackrabbit</groupId>
+      <artifactId>jackrabbit-jcr-commons</artifactId>
+      <version>2.16.3</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.jackrabbit</groupId>
+      <artifactId>oak-core</artifactId>
+      <version>1.8.6</version>
+      <scope>provided</scope>
+    </dependency>
+    <!-- Apache Sling -->
+    <dependency>
+      <groupId>org.apache.sling</groupId>
+      <artifactId>org.apache.sling.api</artifactId>
+      <version>2.11.0</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.sling</groupId>
+      <artifactId>org.apache.sling.event</artifactId>
+      <version>4.2.10</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.sling</groupId>
+      <artifactId>org.apache.sling.commons.clam</artifactId>
+      <version>1.0.0-SNAPSHOT</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.sling</groupId>
+      <artifactId>org.apache.sling.commons.threads</artifactId>
+      <version>3.2.18</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.sling</groupId>
+      <artifactId>org.apache.sling.serviceusermapper</artifactId>
+      <version>1.4.0</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.sling</groupId>
+      <artifactId>org.apache.sling.testing.paxexam</artifactId>
+      <version>2.0.0</version>
+      <scope>test</scope>
+    </dependency>
+    <!-- nullability -->
+    <dependency>
+      <groupId>org.jetbrains</groupId>
+      <artifactId>annotations</artifactId>
+      <scope>provided</scope>
+    </dependency>
+    <!-- logging -->
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-api</artifactId>
+      <scope>provided</scope>
+    </dependency>
+    <!-- testing -->
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.servicemix.bundles</groupId>
+      <artifactId>org.apache.servicemix.bundles.hamcrest</artifactId>
+      <version>1.3_1</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.ops4j.pax.exam</groupId>
+      <artifactId>pax-exam</artifactId>
+      <version>${org.ops4j.pax.exam.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.ops4j.pax.exam</groupId>
+      <artifactId>pax-exam-cm</artifactId>
+      <version>${org.ops4j.pax.exam.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.ops4j.pax.exam</groupId>
+      <artifactId>pax-exam-container-forked</artifactId>
+      <version>${org.ops4j.pax.exam.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.ops4j.pax.exam</groupId>
+      <artifactId>pax-exam-junit4</artifactId>
+      <version>${org.ops4j.pax.exam.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.ops4j.pax.exam</groupId>
+      <artifactId>pax-exam-link-mvn</artifactId>
+      <version>${org.ops4j.pax.exam.version}</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+
+</project>
diff --git a/src/main/java/org/apache/sling/clam/internal/ClamUtil.java b/src/main/java/org/apache/sling/clam/internal/ClamUtil.java
new file mode 100644 (file)
index 0000000..bd0ce54
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * 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.clam.internal;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.jcr.PropertyType;
+
+import org.apache.sling.commons.clam.ScanResult;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+public final class ClamUtil {
+
+    public static final String PROPERTY_PATH = "path";
+
+    public static final String VALUE_INDEX = "index";
+
+    public static final String USER_ID = "userId";
+
+    private static final String SCAN_JOB_TOPIC_ROOT = "org/apache/sling/clam/scan/jcr/property";
+
+    private static final String RESULT_EVENT_TOPIC_ROOT = "org/apache/sling/clam/result/jcr/property";
+
+    public static String scanJobTopic(final int propertyType) {
+        return String.format("%s/%s", SCAN_JOB_TOPIC_ROOT, PropertyType.nameFromValue(propertyType));
+    }
+
+    public static String resultEventTopic(final int propertyType) {
+        return String.format("%s/%s", RESULT_EVENT_TOPIC_ROOT, PropertyType.nameFromValue(propertyType));
+    }
+
+    public static Map<String, Object> properties(@NotNull final String path, @Nullable final String userId) {
+        return properties(path, null, userId);
+    }
+
+    public static Map<String, Object> properties(@NotNull final String path, @Nullable final Integer index, @Nullable final String userId) {
+        final Map<String, Object> properties = new HashMap<>();
+        properties.put(PROPERTY_PATH, path);
+        if (index != null) {
+            properties.put(VALUE_INDEX, index);
+        }
+        if (userId != null) {
+            properties.put(USER_ID, userId);
+        }
+        return properties;
+    }
+
+    public static Map<String, Object> properties(@NotNull final String path, @Nullable final String userId, @NotNull final ScanResult scanResult) {
+        return properties(path, null, userId, scanResult);
+    }
+
+    public static Map<String, Object> properties(@NotNull final String path, @Nullable final Integer index, @Nullable final String userId, @NotNull final ScanResult scanResult) {
+        final Map<String, Object> properties = properties(path, index, userId);
+        properties.put("timestamp", scanResult.getTimestamp());
+        properties.put("message", scanResult.getMessage());
+        properties.put("status", scanResult.getStatus().name());
+        return properties;
+    }
+
+}
diff --git a/src/main/java/org/apache/sling/clam/job/internal/JcrPropertyScanJobConsumer.java b/src/main/java/org/apache/sling/clam/job/internal/JcrPropertyScanJobConsumer.java
new file mode 100644 (file)
index 0000000..3ca666a
--- /dev/null
@@ -0,0 +1,147 @@
+/*
+ * 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.clam.job.internal;
+
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+
+import javax.jcr.Property;
+import javax.jcr.PropertyType;
+import javax.jcr.Session;
+import javax.jcr.Value;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.clam.result.JcrPropertyScanResultHandler;
+import org.apache.sling.clam.internal.ClamUtil;
+import org.apache.sling.commons.clam.ClamService;
+import org.apache.sling.commons.clam.ScanResult;
+import org.apache.sling.event.jobs.Job;
+import org.apache.sling.event.jobs.consumer.JobConsumer;
+import org.apache.sling.event.jobs.consumer.JobExecutor;
+import org.apache.sling.serviceusermapping.ServiceUserMapped;
+import org.osgi.framework.Constants;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.osgi.service.component.annotations.ReferencePolicy;
+import org.osgi.service.component.annotations.ReferencePolicyOption;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Component(
+    property = {
+        JobExecutor.PROPERTY_TOPICS + "=org/apache/sling/clam/scan/jcr/property/*",
+        Constants.SERVICE_DESCRIPTION + "=Apache Sling Clam JCR Property Scan Job Consumer",
+        Constants.SERVICE_VENDOR + "=The Apache Software Foundation"
+    }
+)
+public class JcrPropertyScanJobConsumer implements JobConsumer {
+
+    @Reference(
+        policy = ReferencePolicy.DYNAMIC,
+        policyOption = ReferencePolicyOption.GREEDY
+    )
+    private volatile ResourceResolverFactory resourceResolverFactory;
+
+    @Reference(
+        policy = ReferencePolicy.DYNAMIC,
+        policyOption = ReferencePolicyOption.GREEDY
+    )
+    private volatile ClamService clamService;
+
+    @Reference(
+        cardinality = ReferenceCardinality.MULTIPLE,
+        policy = ReferencePolicy.DYNAMIC,
+        policyOption = ReferencePolicyOption.GREEDY
+    )
+    private volatile List<JcrPropertyScanResultHandler> scanResultHandlers;
+
+    @Reference(
+        policy = ReferencePolicy.DYNAMIC,
+        policyOption = ReferencePolicyOption.GREEDY
+    )
+    private volatile ServiceUserMapped serviceUserMapped;
+
+    private final Logger logger = LoggerFactory.getLogger(JcrPropertyScanJobConsumer.class);
+
+    public JcrPropertyScanJobConsumer() {
+    }
+
+    @Override
+    public JobResult process(final Job job) {
+        try (final ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver(null)) {
+            final String path = job.getProperty(ClamUtil.PROPERTY_PATH, String.class);
+            final String userId = job.getProperty(ClamUtil.USER_ID, String.class);
+            final Session session = resourceResolver.adaptTo(Session.class);
+            assert session != null;
+            final Property property = session.getProperty(path);
+            final int propertyType = property.getType();
+            if (property.isMultiple()) { // multiple property values
+                final int index = job.getProperty(ClamUtil.VALUE_INDEX, Integer.class);
+                final Value[] values = property.getValues();
+                final Value value = values[index];
+                try (final InputStream inputStream = getInputStream(value)) {
+                    final ScanResult scanResult = clamService.scan(inputStream);
+                    invokeScanResultHandlers(scanResult, path, index, propertyType, userId);
+                }
+            } else { // single property value
+                try (final InputStream inputStream = getInputStream(property.getValue())) {
+                    final ScanResult scanResult = clamService.scan(inputStream);
+                    invokeScanResultHandlers(scanResult, path, null, propertyType, userId);
+                }
+            }
+            return JobResult.OK;
+        } catch (Exception e) {
+            logger.error(e.getMessage(), e);
+            return JobResult.FAILED;
+        }
+    }
+
+    private InputStream getInputStream(final Value value) throws Exception {
+        final int propertyType = value.getType();
+        switch (propertyType) {
+            case PropertyType.BINARY:
+                return value.getBinary().getStream();
+            case PropertyType.STRING:
+                return IOUtils.toInputStream(value.getString(), StandardCharsets.UTF_8);
+            default:
+                throw new Exception("Unsupported property type: " + PropertyType.nameFromValue(propertyType));
+        }
+    }
+
+    private void invokeScanResultHandlers(final ScanResult scanResult, final String path, final Integer index, final int propertyType, final String userId) {
+        if (scanResultHandlers != null) {
+            for (final JcrPropertyScanResultHandler scanResultHandler : scanResultHandlers) {
+                try {
+                    if (index == null) { // single-value property
+                        scanResultHandler.handleJcrPropertyScanResult(scanResult, path, propertyType, userId);
+                    } else { // multi-value property
+                        scanResultHandler.handleJcrPropertyScanResult(scanResult, path, index, propertyType, userId);
+                    }
+                } catch (Exception e) {
+                    logger.error(e.getMessage(), e);
+                }
+            }
+        }
+    }
+
+}
diff --git a/src/main/java/org/apache/sling/clam/oak/internal/JcrPropertyDigger.java b/src/main/java/org/apache/sling/clam/oak/internal/JcrPropertyDigger.java
new file mode 100644 (file)
index 0000000..1dced59
--- /dev/null
@@ -0,0 +1,251 @@
+/*
+ * 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.clam.oak.internal;
+
+import java.io.Closeable;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Pattern;
+
+import javax.jcr.Property;
+import javax.jcr.PropertyType;
+import javax.jcr.Session;
+
+import org.apache.jackrabbit.oak.plugins.observation.NodeObserver;
+import org.apache.jackrabbit.oak.spi.commit.CommitInfo;
+import org.apache.jackrabbit.oak.spi.commit.Observable;
+import org.apache.jackrabbit.oak.spi.state.NodeStore;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.clam.internal.ClamUtil;
+import org.apache.sling.commons.threads.ThreadPool;
+import org.apache.sling.commons.threads.ThreadPoolManager;
+import org.apache.sling.event.jobs.JobManager;
+import org.apache.sling.serviceusermapping.ServiceUserMapped;
+import org.jetbrains.annotations.NotNull;
+import org.osgi.framework.Constants;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Modified;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferencePolicy;
+import org.osgi.service.component.annotations.ReferencePolicyOption;
+import org.osgi.service.metatype.annotations.Designate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.apache.sling.clam.internal.ClamUtil.properties;
+import static org.apache.sling.clam.internal.ClamUtil.scanJobTopic;
+
+@Component(
+    immediate = true,
+    property = {
+        Constants.SERVICE_DESCRIPTION + "=Apache Sling Clam JCR Property Digger",
+        Constants.SERVICE_VENDOR + "=The Apache Software Foundation"
+    }
+)
+@Designate(
+    ocd = JcrPropertyDiggerConfiguration.class,
+    factory = true
+)
+public class JcrPropertyDigger extends NodeObserver {
+
+    @Reference
+    private volatile NodeStore nodeStore;
+
+    @Reference(
+        policy = ReferencePolicy.DYNAMIC,
+        policyOption = ReferencePolicyOption.GREEDY
+    )
+    private volatile ResourceResolverFactory resourceResolverFactory;
+
+    @Reference
+    private volatile ThreadPoolManager threadPoolManager;
+
+    @Reference(
+        policy = ReferencePolicy.DYNAMIC,
+        policyOption = ReferencePolicyOption.GREEDY
+    )
+    private volatile JobManager jobManager;
+
+    @Reference(
+        policy = ReferencePolicy.DYNAMIC,
+        policyOption = ReferencePolicyOption.GREEDY
+    )
+    private volatile ServiceUserMapped serviceUserMapped;
+
+    private Pattern pattern;
+
+    private int propertyType;
+
+    private ThreadPool threadPool;
+
+    private Closeable closeable;
+
+    private JcrPropertyDiggerConfiguration configuration;
+
+    private final Logger logger = LoggerFactory.getLogger(JcrPropertyDigger.class);
+
+    public JcrPropertyDigger() {
+        super("/");
+    }
+
+    @Activate
+    private void activate(final JcrPropertyDiggerConfiguration configuration) {
+        logger.debug("activating");
+        this.configuration = configuration;
+        configure(configuration);
+        threadPool = threadPoolManager.get(configuration.threadpool_name());
+        final Observable observable = (Observable) nodeStore;
+        closeable = observable.addObserver(this);
+    }
+
+    @Modified
+    private void modified(final JcrPropertyDiggerConfiguration configuration) {
+        logger.debug("modifying");
+        this.configuration = configuration;
+        configure(configuration);
+    }
+
+    @Deactivate
+    private void deactivate() {
+        logger.debug("deactivating");
+        try {
+            closeable.close();
+        } catch (Exception e) {
+            logger.error(e.getMessage());
+        }
+        threadPoolManager.release(threadPool);
+        configuration = null;
+    }
+
+    private void configure(final JcrPropertyDiggerConfiguration configuration) {
+        pattern = Pattern.compile(configuration.property_path_pattern());
+        propertyType = PropertyType.valueFromName(configuration.property_type());
+    }
+
+    @Override
+    protected void added(@NotNull final String path, @NotNull final Set<String> added, @NotNull final Set<String> deleted, @NotNull final Set<String> changed, @NotNull final Map<String, String> properties, @NotNull final CommitInfo commitInfo) {
+        final Set<String> names = concat(added, changed);
+        dig(path, names, pattern, propertyType, configuration.property_length_max(), commitInfo.getUserId());
+    }
+
+    @Override
+    protected void deleted(@NotNull final String path, @NotNull final Set<String> added, @NotNull final Set<String> deleted, @NotNull final Set<String> changed, @NotNull final Map<String, String> properties, @NotNull final CommitInfo commitInfo) {
+    }
+
+    @Override
+    protected void changed(@NotNull final String path, @NotNull final Set<String> added, @NotNull final Set<String> deleted, @NotNull final Set<String> changed, @NotNull final Map<String, String> properties, @NotNull final CommitInfo commitInfo) {
+        final Set<String> names = concat(added, changed);
+        dig(path, names, pattern, propertyType, configuration.property_length_max(), commitInfo.getUserId());
+    }
+
+    private void dig(final String path, final Set<String> names, final Pattern pattern, final int propertyType, final long maxLength, final String userId) {
+        final DigTask digTask = new DigTask(path, names, pattern, propertyType, maxLength, userId);
+        threadPool.submit(digTask);
+    }
+
+    private Set<String> filter(final String path, final Set<String> names, final Pattern pattern) {
+        final Set<String> paths = new HashSet<>();
+        for (final String name : names) {
+            final String p = String.format("%s/%s", path, name);
+            if (pattern.matcher(p).matches()) {
+                logger.debug("path '{}' matches pattern '{}'", p, pattern.pattern());
+                paths.add(p);
+            } else {
+                logger.debug("path '{}' doesn't match pattern '{}'", p, pattern.pattern());
+            }
+        }
+        return paths;
+    }
+
+    private boolean checkLength(final long length, final long maxLength) {
+        if (maxLength == -1) {
+            return true;
+        }
+        return length <= maxLength;
+    }
+
+    private Set<String> concat(final Set<String> a, Set<String> b) {
+        final Set<String> set = new HashSet<>(a);
+        set.addAll(b);
+        return set;
+    }
+
+    private class DigTask implements Runnable {
+
+        private final String path;
+
+        private final Set<String> names;
+
+        private final Pattern pattern;
+
+        private final int propertyType;
+
+        private final long maxLength;
+
+        private final String userId;
+
+        DigTask(final String path, final Set<String> names, final Pattern pattern, final int propertyType, final long maxLength, final String userId) {
+            this.path = path;
+            this.names = names;
+            this.pattern = pattern;
+            this.propertyType = propertyType;
+            this.maxLength = maxLength;
+            this.userId = userId;
+        }
+
+        @Override
+        public void run() {
+            final Set<String> paths = filter(path, names, pattern);
+            try (final ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver(null)) {
+                final Session session = resourceResolver.adaptTo(Session.class);
+                assert session != null;
+                for (final String path : paths) {
+                    final Property property = session.getProperty(path);
+                    final int propertyType = property.getType();
+                    if (propertyType == this.propertyType) {
+                        if (property.isMultiple()) { // multiple property values
+                            final long[] lengths = property.getLengths();
+                            for (int index = 0; index < lengths.length; index++) {
+                                final long length = lengths[index];
+                                if (checkLength(length, maxLength)) {
+                                    jobManager.addJob(scanJobTopic(propertyType), properties(path, index, userId));
+                                } else {
+                                    logger.warn("Length of property '{}' [{}] greater than configured max length ({}).", path, index, maxLength);
+                                }
+                            }
+                        } else { // single property value
+                            if (checkLength(property.getLength(), maxLength)) {
+                                jobManager.addJob(scanJobTopic(propertyType), ClamUtil.properties(path, userId));
+                            } else {
+                                logger.warn("Length of property '{}' greater than configured max length ({}).", path, maxLength);
+                            }
+                        }
+                    }
+                }
+            } catch (Exception e) {
+                logger.error(e.getMessage());
+            }
+        }
+    }
+
+}
diff --git a/src/main/java/org/apache/sling/clam/oak/internal/JcrPropertyDiggerConfiguration.java b/src/main/java/org/apache/sling/clam/oak/internal/JcrPropertyDiggerConfiguration.java
new file mode 100644 (file)
index 0000000..0363964
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * 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.clam.oak.internal;
+
+import org.apache.sling.commons.threads.ThreadPoolManager;
+import org.osgi.service.metatype.annotations.AttributeDefinition;
+import org.osgi.service.metatype.annotations.ObjectClassDefinition;
+import org.osgi.service.metatype.annotations.Option;
+
+@ObjectClassDefinition(
+    name = "Apache Sling Clam JCR Property Digger",
+    description = "Observes the node store and adds scan jobs for matching JCR properties"
+)
+@interface JcrPropertyDiggerConfiguration {
+
+    @AttributeDefinition(
+        name = "property type",
+        description = "Type of properties",
+        options = {
+            @Option(label = "Binary", value = "Binary"),
+            @Option(label = "String", value = "String")
+        }
+    )
+    String property_type() default "Binary";
+
+    @AttributeDefinition(
+        name = "property path pattern",
+        description = "Pattern a property path has to match, e.g. '^/content/.*/jcr:content/jcr:data$'"
+    )
+    String property_path_pattern() default "^/.*$";
+
+    @AttributeDefinition(
+        name = "property length max",
+        description = "Max length of property value, -1 for unlimited length. Scanning data greater 4GB may result in errors due to limitations in Clam."
+    )
+    long property_length_max() default -1L;
+
+    @AttributeDefinition(
+        name = "threadpool name",
+        description = "Name of the ThreadPool to use for digging"
+    )
+    String threadpool_name() default ThreadPoolManager.DEFAULT_THREADPOOL_NAME;
+
+    String webconsole_configurationFactory_nameHint() default "{property.type}:{property.path.pattern}";
+
+}
diff --git a/src/main/java/org/apache/sling/clam/result/JcrPropertyScanResultHandler.java b/src/main/java/org/apache/sling/clam/result/JcrPropertyScanResultHandler.java
new file mode 100644 (file)
index 0000000..291d4c2
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * 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.clam.result;
+
+import org.apache.sling.commons.clam.ScanResult;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.osgi.annotation.versioning.ConsumerType;
+
+@ConsumerType
+public interface JcrPropertyScanResultHandler {
+
+    /**
+     * @param scanResult   The scan result from Clam service
+     * @param path         The path of the scanned single-value property
+     * @param propertyType The type of the scanned property
+     * @param userId       The id of the user who added or changed the property
+     */
+    void handleJcrPropertyScanResult(@NotNull final ScanResult scanResult, @NotNull final String path, final int propertyType, @Nullable final String userId);
+
+    /**
+     * @param scanResult   The scan result from Clam service
+     * @param path         The path of the scanned multi-value property
+     * @param index        The index of the scanned property value
+     * @param propertyType The type of the scanned property
+     * @param userId       The id of the user who added or changed the property
+     */
+    void handleJcrPropertyScanResult(@NotNull final ScanResult scanResult, @NotNull final String path, final int index, final int propertyType, @Nullable final String userId);
+
+}
diff --git a/src/main/java/org/apache/sling/clam/result/internal/EventPublishingScanResultHandler.java b/src/main/java/org/apache/sling/clam/result/internal/EventPublishingScanResultHandler.java
new file mode 100644 (file)
index 0000000..cdcf3e7
--- /dev/null
@@ -0,0 +1,107 @@
+/*
+ * 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.clam.result.internal;
+
+import org.apache.sling.clam.internal.ClamUtil;
+import org.apache.sling.clam.result.JcrPropertyScanResultHandler;
+import org.apache.sling.commons.clam.ScanResult;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.osgi.framework.Constants;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.ConfigurationPolicy;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Modified;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferencePolicy;
+import org.osgi.service.component.annotations.ReferencePolicyOption;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventAdmin;
+import org.osgi.service.metatype.annotations.Designate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.apache.sling.clam.internal.ClamUtil.properties;
+import static org.apache.sling.clam.internal.ClamUtil.resultEventTopic;
+
+@Component(
+    configurationPolicy = ConfigurationPolicy.REQUIRE,
+    property = {
+        Constants.SERVICE_DESCRIPTION + "=Apache Sling Clam Event Publishing Scan Result Handler",
+        Constants.SERVICE_VENDOR + "=The Apache Software Foundation"
+    }
+)
+@Designate(
+    ocd = EventPublishingScanResultHandlerConfiguration.class
+)
+public class EventPublishingScanResultHandler implements JcrPropertyScanResultHandler {
+
+    @Reference(
+        policy = ReferencePolicy.DYNAMIC,
+        policyOption = ReferencePolicyOption.GREEDY
+    )
+    private volatile EventAdmin eventAdmin;
+
+    private EventPublishingScanResultHandlerConfiguration configuration;
+
+    private final Logger logger = LoggerFactory.getLogger(EventPublishingScanResultHandler.class);
+
+    public EventPublishingScanResultHandler() {
+    }
+
+    @Activate
+    private void activate(final EventPublishingScanResultHandlerConfiguration configuration) {
+        logger.debug("activating");
+        this.configuration = configuration;
+    }
+
+    @Modified
+    private void modified(final EventPublishingScanResultHandlerConfiguration configuration) {
+        logger.debug("modifying");
+        this.configuration = configuration;
+    }
+
+    @Deactivate
+    private void deactivate() {
+        logger.debug("deactivating");
+        this.configuration = null;
+    }
+
+    @Override
+    public void handleJcrPropertyScanResult(@NotNull ScanResult scanResult, @NotNull String path, int propertyType, @Nullable String userId) {
+        if (checkPublish(scanResult)) {
+            final Event event = new Event(resultEventTopic(propertyType), ClamUtil.properties(path, userId, scanResult));
+            eventAdmin.postEvent(event);
+        }
+    }
+
+    @Override
+    public void handleJcrPropertyScanResult(@NotNull ScanResult scanResult, @NotNull String path, int index, int propertyType, @Nullable String userId) {
+        if (checkPublish(scanResult)) {
+            final Event event = new Event(resultEventTopic(propertyType), properties(path, index, userId, scanResult));
+            eventAdmin.postEvent(event);
+        }
+    }
+
+    private boolean checkPublish(final ScanResult scanResult) {
+        return !scanResult.isOk() || scanResult.isOk() && configuration.result_status_ok_publish();
+    }
+
+}
diff --git a/src/main/java/org/apache/sling/clam/result/internal/EventPublishingScanResultHandlerConfiguration.java b/src/main/java/org/apache/sling/clam/result/internal/EventPublishingScanResultHandlerConfiguration.java
new file mode 100644 (file)
index 0000000..e7d9acc
--- /dev/null
@@ -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.clam.result.internal;
+
+import org.osgi.service.metatype.annotations.AttributeDefinition;
+import org.osgi.service.metatype.annotations.ObjectClassDefinition;
+
+@ObjectClassDefinition(
+    name = "Apache Sling Clam Event Publishing Scan Result Handler",
+    description = "Publishes JCR property scan results via Event Admin Service"
+)
+@interface EventPublishingScanResultHandlerConfiguration {
+
+    @AttributeDefinition(
+        name = "publish status ok",
+        description = "Publish scan results with status OK also"
+    )
+    boolean result_status_ok_publish() default false;
+
+}
diff --git a/src/main/java/org/apache/sling/clam/result/internal/ResourcePersistingScanResultHandler.java b/src/main/java/org/apache/sling/clam/result/internal/ResourcePersistingScanResultHandler.java
new file mode 100644 (file)
index 0000000..edc2823
--- /dev/null
@@ -0,0 +1,166 @@
+/*
+ * 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.clam.result.internal;
+
+import java.text.SimpleDateFormat;
+import java.util.Collections;
+import java.util.Date;
+import java.util.Map;
+
+import javax.jcr.PropertyType;
+
+import org.apache.sling.api.resource.LoginException;
+import org.apache.sling.api.resource.PersistenceException;
+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.api.resource.ResourceUtil;
+import org.apache.sling.clam.result.JcrPropertyScanResultHandler;
+import org.apache.sling.commons.clam.ScanResult;
+import org.apache.sling.serviceusermapping.ServiceUserMapped;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.osgi.framework.Constants;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.ConfigurationPolicy;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Modified;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferencePolicy;
+import org.osgi.service.component.annotations.ReferencePolicyOption;
+import org.osgi.service.metatype.annotations.Designate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static javax.jcr.nodetype.NodeType.MIX_CREATED;
+import static javax.jcr.nodetype.NodeType.NT_UNSTRUCTURED;
+import static org.apache.jackrabbit.JcrConstants.JCR_MIXINTYPES;
+import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE;
+import static org.apache.sling.clam.internal.ClamUtil.properties;
+
+@Component(
+    configurationPolicy = ConfigurationPolicy.REQUIRE,
+    property = {
+        Constants.SERVICE_DESCRIPTION + "=Apache Sling Clam Resource Persisting Scan Result Handler",
+        Constants.SERVICE_VENDOR + "=The Apache Software Foundation"
+    }
+)
+@Designate(
+    ocd = ResourcePersistingScanResultHandlerConfiguration.class
+)
+public class ResourcePersistingScanResultHandler implements JcrPropertyScanResultHandler {
+
+    @Reference(
+        policy = ReferencePolicy.DYNAMIC,
+        policyOption = ReferencePolicyOption.GREEDY
+    )
+    private volatile ResourceResolverFactory resourceResolverFactory;
+
+    @Reference(
+        policy = ReferencePolicy.DYNAMIC,
+        policyOption = ReferencePolicyOption.GREEDY,
+        target = ("(" + ServiceUserMapped.SUBSERVICENAME + "=" + SUBSERVICE + ")")
+    )
+    private volatile ServiceUserMapped serviceUserMapped;
+
+    private ResourcePersistingScanResultHandlerConfiguration configuration;
+
+    private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy/MM/dd/HH/mm/ss/SSS");
+
+    private static final String NT_SLING_ORDERED_FOLDER = "sling:OrderedFolder";
+
+    private static final String SLING_RESOURCE_TYPE_PROPERTY = "sling:resourceType";
+
+    private static final String RESULT_RESOURCE_TYPE = "sling/clam/result";
+
+    private static final String SUBSERVICE = "result-writer";
+
+    private final Logger logger = LoggerFactory.getLogger(ResourcePersistingScanResultHandler.class);
+
+    public ResourcePersistingScanResultHandler() {
+    }
+
+    @Activate
+    private void activate(final ResourcePersistingScanResultHandlerConfiguration configuration) {
+        logger.debug("activating");
+        this.configuration = configuration;
+    }
+
+    @Modified
+    private void modified(final ResourcePersistingScanResultHandlerConfiguration configuration) {
+        logger.debug("modifying");
+        this.configuration = configuration;
+    }
+
+    @Deactivate
+    private void deactivate() {
+        logger.debug("deactivating");
+        this.configuration = null;
+    }
+
+    @Override
+    public void handleJcrPropertyScanResult(@NotNull ScanResult scanResult, @NotNull String path, int propertyType, @NotNull String userId) {
+        if (checkPersist(scanResult)) {
+            persistResult(scanResult, path, null, propertyType, userId);
+        }
+    }
+
+    @Override
+    public void handleJcrPropertyScanResult(@NotNull ScanResult scanResult, @NotNull String path, int index, int propertyType, @Nullable String userId) {
+        if (checkPersist(scanResult)) {
+            persistResult(scanResult, path, index, propertyType, userId);
+        }
+    }
+
+    private boolean checkPersist(final ScanResult scanResult) {
+        return !scanResult.isOk() || scanResult.isOk() && configuration.result_status_ok_persist();
+    }
+
+    private void persistResult(@NotNull ScanResult scanResult, @NotNull String path, Integer index, int propertyType, @Nullable String userId) {
+        try (final ResourceResolver resourceResolver = serviceResourceResolver()) {
+            final Map<String, Object> properties = properties(path, index, userId, scanResult);
+            properties.put(JCR_PRIMARYTYPE, NT_UNSTRUCTURED);
+            properties.put(JCR_MIXINTYPES, MIX_CREATED);
+            properties.put(SLING_RESOURCE_TYPE_PROPERTY, RESULT_RESOURCE_TYPE);
+            final Resource parent = getOrCreateParent(resourceResolver, scanResult.getTimestamp());
+            final String name = ResourceUtil.createUniqueChildName(parent, PropertyType.nameFromValue(propertyType));
+            final Resource result = resourceResolver.create(parent, name, properties);
+            resourceResolver.commit();
+            if (index == null) {
+                logger.debug("Scan result for {} persisted at {}.", path, result.getPath());
+            } else {
+                logger.debug("Scan result for {} [{}] persisted at {}.", path, index, result.getPath());
+            }
+        } catch (LoginException | PersistenceException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    private Resource getOrCreateParent(final ResourceResolver resourceResolver, final long timestamp) throws PersistenceException {
+        final Date date = new Date(timestamp);
+        final String path = String.format("%s/%s", configuration.result_root_path(), FORMAT.format(date));
+        return ResourceUtil.getOrCreateResource(resourceResolver, path, NT_SLING_ORDERED_FOLDER, NT_SLING_ORDERED_FOLDER, true);
+    }
+
+    private ResourceResolver serviceResourceResolver() throws LoginException {
+        return resourceResolverFactory.getServiceResourceResolver(Collections.singletonMap(ResourceResolverFactory.SUBSERVICE, SUBSERVICE));
+    }
+
+}
diff --git a/src/main/java/org/apache/sling/clam/result/internal/ResourcePersistingScanResultHandlerConfiguration.java b/src/main/java/org/apache/sling/clam/result/internal/ResourcePersistingScanResultHandlerConfiguration.java
new file mode 100644 (file)
index 0000000..9fa0129
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * 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.clam.result.internal;
+
+import org.osgi.service.metatype.annotations.AttributeDefinition;
+import org.osgi.service.metatype.annotations.ObjectClassDefinition;
+
+@ObjectClassDefinition(
+    name = "Apache Sling Clam Resource Persisting Scan Result Handler",
+    description = "Persists JCR property scan results as resource"
+)
+@interface ResourcePersistingScanResultHandlerConfiguration {
+
+    @AttributeDefinition(
+        name = "persist status ok",
+        description = "Persist scan results with status OK also"
+    )
+    boolean result_status_ok_persist() default false;
+
+    @AttributeDefinition(
+        name = "path",
+        description = "Root path where to persist scan results in repository"
+    )
+    String result_root_path() default "/var/clam/results";
+
+}
diff --git a/src/main/java/org/apache/sling/clam/result/package-info.java b/src/main/java/org/apache/sling/clam/result/package-info.java
new file mode 100644 (file)
index 0000000..412d2db
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * 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.
+ */
+@Version("0.0.1")
+package org.apache.sling.clam.result;
+
+import org.osgi.annotation.versioning.Version;
diff --git a/src/test/java/org/apache/sling/clam/it/tests/ClamTestSupport.java b/src/test/java/org/apache/sling/clam/it/tests/ClamTestSupport.java
new file mode 100644 (file)
index 0000000..8eed774
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * 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.clam.it.tests;
+
+import org.apache.sling.testing.paxexam.TestSupport;
+import org.ops4j.pax.exam.Configuration;
+import org.ops4j.pax.exam.Option;
+
+import static org.apache.sling.testing.paxexam.SlingOptions.slingEvent;
+import static org.apache.sling.testing.paxexam.SlingOptions.slingQuickstartOakTar;
+import static org.apache.sling.testing.paxexam.SlingVersionResolver.SLING_GROUP_ID;
+import static org.ops4j.pax.exam.CoreOptions.composite;
+import static org.ops4j.pax.exam.CoreOptions.junitBundles;
+import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
+import static org.ops4j.pax.exam.cm.ConfigurationAdminOptions.factoryConfiguration;
+
+public abstract class ClamTestSupport extends TestSupport {
+
+    public Option[] configuration() {
+        return new Option[]{
+            baseConfiguration(),
+            quickstart(),
+            // Sling Clam
+            testBundle("bundle.filename"),
+            factoryConfiguration("org.apache.sling.jcr.repoinit.RepositoryInitializer")
+                .put("scripts", new String[]{"create service user sling-clam\ncreate path (sling:Folder) /var/clam/results\nset ACL for sling-clam\nallow jcr:all on /var/clam\nend"})
+                .asOption(),
+            factoryConfiguration("org.apache.sling.serviceusermapping.impl.ServiceUserMapperImpl.amended")
+                .put("user.mapping", new String[]{"org.apache.sling.clam=sling-readall", "org.apache.sling.clam:result-writer=sling-clam"})
+                .asOption(),
+            // Sling Commons Clam
+            mavenBundle().groupId(SLING_GROUP_ID).artifactId("org.apache.sling.commons.clam").versionAsInProject(),
+            // testing
+            mavenBundle().groupId("org.apache.servicemix.bundles").artifactId("org.apache.servicemix.bundles.hamcrest").versionAsInProject(),
+            junitBundles()
+        };
+    }
+
+    protected Option quickstart() {
+        final int httpPort = findFreePort();
+        final String workingDirectory = workingDirectory();
+        return composite(
+            slingQuickstartOakTar(workingDirectory, httpPort),
+            slingEvent()
+        );
+    }
+
+}
diff --git a/src/test/java/org/apache/sling/clam/it/tests/JcrPropertyScanJobConsumerIT.java b/src/test/java/org/apache/sling/clam/it/tests/JcrPropertyScanJobConsumerIT.java
new file mode 100644 (file)
index 0000000..c538eda
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * 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.clam.it.tests;
+
+import javax.inject.Inject;
+
+import org.apache.sling.event.jobs.consumer.JobConsumer;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Configuration;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.PaxExam;
+import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
+import org.ops4j.pax.exam.spi.reactors.PerClass;
+import org.ops4j.pax.exam.util.Filter;
+
+import static org.junit.Assert.assertNotNull;
+
+@RunWith(PaxExam.class)
+@ExamReactorStrategy(PerClass.class)
+public class JcrPropertyScanJobConsumerIT extends ClamTestSupport {
+
+    @Inject
+    @Filter("(job.topics=org/apache/sling/clam/scan/jcr/property/*)")
+    private JobConsumer jobConsumer;
+
+    @Configuration
+    public Option[] configuration() {
+        return super.configuration();
+    }
+
+    @Test
+    public void testJobConsumer() {
+        assertNotNull(jobConsumer);
+    }
+
+}
diff --git a/src/test/java/org/apache/sling/clam/it/tests/ResourcePersistingScanResultHandlerIT.java b/src/test/java/org/apache/sling/clam/it/tests/ResourcePersistingScanResultHandlerIT.java
new file mode 100644 (file)
index 0000000..ae87309
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * 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.clam.it.tests;
+
+import javax.inject.Inject;
+
+import org.apache.sling.clam.result.JcrPropertyScanResultHandler;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Configuration;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.PaxExam;
+import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
+import org.ops4j.pax.exam.spi.reactors.PerClass;
+import org.ops4j.pax.exam.util.Filter;
+
+import static org.junit.Assert.assertNotNull;
+import static org.ops4j.pax.exam.OptionUtils.combine;
+import static org.ops4j.pax.exam.cm.ConfigurationAdminOptions.newConfiguration;
+
+@RunWith(PaxExam.class)
+@ExamReactorStrategy(PerClass.class)
+public class ResourcePersistingScanResultHandlerIT extends ClamTestSupport {
+
+    @Inject
+    @Filter("(service.pid=org.apache.sling.clam.result.internal.ResourcePersistingScanResultHandler)")
+    private JcrPropertyScanResultHandler jcrPropertyScanResultHandler;
+
+    @Configuration
+    public Option[] configuration() {
+        return combine(
+            super.configuration(),
+            newConfiguration("org.apache.sling.clam.result.internal.ResourcePersistingScanResultHandler")
+                .put("result.status.ok.persist", true)
+                .put("result.root.path", "/var/clam/results")
+                .asOption()
+        );
+    }
+
+    @Test
+    public void testJcrPropertyScanResultHandler() {
+        assertNotNull(jcrPropertyScanResultHandler);
+    }
+
+}