Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ContentSync Audit #3426

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com)
<!-- Keep this up to date! After a release, change the tag name to the latest release -->-

## Unreleased ([details][unreleased changes details])
- #3426 - Content Sync: view history of completed jobs

### Changed

Expand Down
2 changes: 1 addition & 1 deletion all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<parent>
<groupId>com.adobe.acs</groupId>
<artifactId>acs-aem-commons</artifactId>
<version>6.6.5-SNAPSHOT</version>
<version>6.7.0-SNAPSHOT</version>
</parent>

<!-- ====================================================================== -->
Expand Down
2 changes: 1 addition & 1 deletion bundle-cloud/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<parent>
<groupId>com.adobe.acs</groupId>
<artifactId>acs-aem-commons</artifactId>
<version>6.6.5-SNAPSHOT</version>
<version>6.7.0-SNAPSHOT</version>
</parent>

<!-- ====================================================================== -->
Expand Down
2 changes: 1 addition & 1 deletion bundle-onprem/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<parent>
<groupId>com.adobe.acs</groupId>
<artifactId>acs-aem-commons</artifactId>
<version>6.6.5-SNAPSHOT</version>
<version>6.7.0-SNAPSHOT</version>
</parent>

<!-- ====================================================================== -->
Expand Down
2 changes: 1 addition & 1 deletion bundle/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<parent>
<groupId>com.adobe.acs</groupId>
<artifactId>acs-aem-commons</artifactId>
<version>6.6.5-SNAPSHOT</version>
<version>6.7.0-SNAPSHOT</version>
</parent>

<!-- ====================================================================== -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@
package com.adobe.acs.commons.contentsync;

import com.adobe.acs.commons.contentsync.impl.LastModifiedStrategy;
import org.apache.jackrabbit.JcrConstants;
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.ResourceUtil;

import java.io.ByteArrayInputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import static com.adobe.acs.commons.contentsync.RemoteInstance.CONNECT_TIMEOUT;
import static com.adobe.acs.commons.contentsync.RemoteInstance.SOCKET_TIMEOUT;
Expand Down Expand Up @@ -64,4 +68,34 @@ public static Resource getHostsResource(ResourceResolver resourceResolver) throw
resourceProperties.put(JCR_PRIMARYTYPE, NT_UNSTRUCTURED);
return ResourceUtil.getOrCreateResource(resourceResolver, HOSTS_PATH, resourceProperties, NT_SLING_FOLDER, true);
}

public static void persistAuditLog(ResourceResolver resourceResolver, String path, long count, String data) throws PersistenceException {

String auditHome = CONFIG_PATH + "/audit";
ResourceUtil.getOrCreateResource(resourceResolver, auditHome,
Collections.singletonMap(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED), JcrConstants.NT_FOLDER, false);

Map<String, Object> auditProps = new HashMap<>();
auditProps.put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_UNSTRUCTURED);
auditProps.put("syncPath", path);
auditProps.put(JcrConstants.JCR_MIXINTYPES, "mix:created");
auditProps.put("count", count);
String auditResourcePath = auditHome + "/" + UUID.randomUUID();
ResourceUtil.getOrCreateResource(resourceResolver, auditResourcePath, auditProps, null, false);

String auditLogPath = auditResourcePath + "/log";
ResourceUtil.getOrCreateResource(resourceResolver, auditLogPath,
Collections.singletonMap(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_FILE), null, false);

Map<String, Object> props = new HashMap<>();
props.put(JcrConstants.JCR_PRIMARYTYPE, JcrConstants.NT_RESOURCE);
props.put(JcrConstants.JCR_MIMETYPE, "text/plain");

props.put(JcrConstants.JCR_DATA, new ByteArrayInputStream(data.getBytes()));

ResourceUtil.getOrCreateResource(resourceResolver, auditLogPath + "/" + JcrConstants.JCR_CONTENT,
props, null, false);

resourceResolver.commit();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
* limitations under the License.
* #L%
*/
@org.osgi.annotation.versioning.Version("1.1.0")
@org.osgi.annotation.versioning.Version("1.2.0")
package com.adobe.acs.commons.contentsync;
2 changes: 1 addition & 1 deletion oakpal-checks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<parent>
<groupId>com.adobe.acs</groupId>
<artifactId>acs-aem-commons</artifactId>
<version>6.6.5-SNAPSHOT</version>
<version>6.7.0-SNAPSHOT</version>
</parent>

<!-- ====================================================================== -->
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

<groupId>com.adobe.acs</groupId>
<artifactId>acs-aem-commons</artifactId>
<version>6.6.5-SNAPSHOT</version>
<version>6.7.0-SNAPSHOT</version>
<packaging>pom</packaging>

<name>ACS AEM Commons - Reactor Project</name>
Expand Down
2 changes: 1 addition & 1 deletion ui.apps/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<parent>
<groupId>com.adobe.acs</groupId>
<artifactId>acs-aem-commons</artifactId>
<version>6.6.5-SNAPSHOT</version>
<version>6.7.0-SNAPSHOT</version>
</parent>

<!-- ====================================================================== -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
pageEncoding="UTF-8"
import="
java.util.List,
java.util.Arrays,
java.util.ArrayList,
java.util.Collection,
java.util.Set,
java.util.LinkedHashSet,
java.util.stream.Collectors,
java.io.InputStream,
java.io.IOException,
java.io.Writer,
java.io.StringWriter,
java.io.PrintWriter,
java.io.ByteArrayInputStream,
javax.jcr.Session,
Expand All @@ -36,6 +39,7 @@
org.apache.sling.jcr.contentloader.ContentImporter,
org.apache.sling.api.resource.ResourceUtil,
org.apache.commons.lang3.time.DurationFormatUtils,
org.apache.commons.io.output.TeeWriter,
com.adobe.acs.commons.contentsync.*

"%><%
Expand Down Expand Up @@ -83,21 +87,24 @@
Session session = resourceResolver.adaptTo(Session.class);
ContentReader contentReader = new ContentReader(session);

StringWriter tempWriter = new StringWriter();
TeeWriter printWriter = new TeeWriter(Arrays.asList(new PrintWriter(out), new PrintWriter(tempWriter)));

UpdateStrategy updateStrategy = sling.getServices(UpdateStrategy.class, "(component.name=" + strategyPid + ")")[0];
try(RemoteInstance remoteInstance = new RemoteInstance(hostConfig, generalSettings)){
ContentImporter importer = sling.getService(ContentImporter.class);
ContentSync contentSync = new ContentSync(remoteInstance, resourceResolver, importer);
ContentCatalog contentCatalog = new ContentCatalog(remoteInstance, catalogServlet);

out.println("building catalog from " + contentCatalog.getFetchURI(root, strategyPid) );
println(printWriter, "building catalog from " + contentCatalog.getFetchURI(root, strategyPid) );
out.flush();
List<CatalogItem> catalog;
List<CatalogItem> remoteItems = contentCatalog.fetch(root, strategyPid);
long t0 = System.currentTimeMillis();
out.println(remoteItems.size() + " resource"+(remoteItems.size() == 1 ? "" : "s")+" fetched in " + (System.currentTimeMillis() - t0) + " ms");
println(printWriter, remoteItems.size() + " resource"+(remoteItems.size() == 1 ? "" : "s")+" fetched in " + (System.currentTimeMillis() - t0) + " ms");
if(incremental){
catalog = contentCatalog.getDelta(remoteItems, resourceResolver, updateStrategy);
out.println(catalog.size() + " resource"+(catalog.size() == 1 ? "" : "s")+" modified");
println(printWriter, catalog.size() + " resource"+(catalog.size() == 1 ? "" : "s")+" modified");
} else {
catalog = remoteItems;
}
Expand All @@ -124,9 +131,9 @@
boolean modified = updateStrategy.isModified(item, targetResource);

if(targetResource == null || modified || !incremental) {
out.println(++count + "\t" + path);
println(printWriter, ++count + "\t" + path);
String msg = updateStrategy.getMessage(item, targetResource);
out.println("\t" + msg);
println(printWriter, "\t" + msg);
if(!dryRun) {
String reqPath = item.getContentUri() ;
JsonObject json = remoteInstance.getJson(reqPath);
Expand All @@ -137,18 +144,18 @@
if(targetResource != null && createVersion) {
String revisionId = contentSync.createVersion(targetResource);
if(revisionId != null) {
out.println("\tcreated revision: " + revisionId);
println(printWriter, "\tcreated revision: " + revisionId);
}
}
if(observationData != null){
session.getWorkspace().getObservationManager().setUserData(observationData);
}

out.println("\timporting data");
println(printWriter, "\timporting data");
contentSync.importData(item, sanitizedJson);

if(!binaryProperties.isEmpty()){
out.println("\tcopying " + binaryProperties.size() + " binary propert" + (binaryProperties.size() > 1 ? "ies" : "y"));
println(printWriter, "\tcopying " + binaryProperties.size() + " binary propert" + (binaryProperties.size() > 1 ? "ies" : "y"));
boolean contentResource = item.hasContentResource();
String basePath = path + (contentResource ? "/jcr:content" : "");
List<String> propertyPaths = binaryProperties.stream().map(p -> basePath + p).collect(Collectors.toList());
Expand All @@ -175,7 +182,7 @@
String eta = DurationFormatUtils.formatDurationWords(estimatedTime, true, true);
String etaMsg = pct +"%, ETA: " + eta;
t00 = System.currentTimeMillis();
out.println(etaMsg);
println(printWriter, etaMsg);
}

updatedResources.add(path);
Expand All @@ -189,11 +196,11 @@
Collection<String> remotePaths = remoteItems.stream().map(c -> c.getPath()).collect(Collectors.toList());
Collection<String> localPaths = updateStrategy.getItems(slingRequest).stream().map(c -> c.getPath()).collect(Collectors.toList());
localPaths.removeAll(remotePaths);
out.println();
println(printWriter, "");
for(String path : localPaths){
Resource res = resourceResolver.getResource(path);
if(res != null){
out.println("deleting " + path);
println(printWriter, "deleting " + path);
if(!dryRun) {
if(res != null) {
resourceResolver.delete(res);
Expand All @@ -203,27 +210,29 @@
}
}

out.println();
println(printWriter, "");
for(String parentPath : sortedNodes){
Node targetNode = resourceResolver.getResource(parentPath).adaptTo(Node.class);
out.println("sorting child nodes of " + targetNode.getPath() );
println(printWriter, "sorting child nodes of " + targetNode.getPath() );
contentSync.sort(targetNode);
}
session.save();

out.println();
out.println("sync-ed " + count + " resources, in " + (System.currentTimeMillis() - t0) + " ms");
println(printWriter, "");
println(printWriter, "sync-ed " + count + " resources, in " + (System.currentTimeMillis() - t0) + " ms");

if(!dryRun && workflowModel != null && !workflowModel.isEmpty()){
out.println();
println(printWriter, "");
long t1 = System.currentTimeMillis();

out.println("starting a " + workflowModel + " workflow for each processed item");
println(printWriter, "starting a " + workflowModel + " workflow for each processed item");
out.flush();
contentSync.runWorkflows(workflowModel, updatedResources);
out.println("started " + updatedResources.size() + " workflows, in " + (System.currentTimeMillis() - t1) + " ms");
println(printWriter, "started " + updatedResources.size() + " workflows, in " + (System.currentTimeMillis() - t1) + " ms");
}
if(!dryRun){
ConfigurationUtils.persistAuditLog(resourceResolver, root, count, tempWriter.toString());
}

} catch(Exception e){
if(e.getMessage() != null && e.getMessage().startsWith("Not a date string:")){
error(out, "It appears Sling GET Servlet on " + hostConfig.getHost() + " is configured to use the legacy ECMA date format.\n" +
Expand All @@ -239,6 +248,11 @@
</html>
<%!

void println(Writer out, String msg) throws IOException {
out.write(msg);
out.write('\n');
}

void error(JspWriter out, String msg) throws IOException {
out.print("<span class=\"error\">");
out.print(msg);
Expand All @@ -250,4 +264,5 @@
e.printStackTrace(new PrintWriter(out));
out.println("</span>");
}

%>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<!--
~ ACS AEM Commons
~
~ Copyright (C) 2013 - 2023 Adobe
~
~ 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.
-->
<section class="coral-Form coral-Form--vertical">

<table is="coral-table">
<colgroup>
<col is="coral-table-column"/>
<col is="coral-table-column"/>
<col is="coral-table-column"/>
<col is="coral-table-column"/>
<col is="coral-table-column"/>
<col is="coral-table-column"/>
</colgroup>
<thead is="coral-table-head">
<tr is="coral-table-row">
<th is="coral-table-headercell">Initiator</th>
<th is="coral-table-headercell">Start Time</th>
<th is="coral-table-headercell">Path</th>
<th is="coral-table-headercell">Resources Updated</th>
<th is="coral-table-headercell">Log</th>
</tr>
</thead>

<tbody is="coral-table-body" divider="cell">
<sly
data-sly-list="${resource.getChildren}">
<tr is="coral-table-row">
<td is="coral-table-cell">${item.jcr:createdBy}</td>
<td is="coral-table-cell">${'dd-MM-yyyy hh:mm:ss' @ format=item.jcr:created}</td>
<td is="coral-table-cell">${item.syncPath}</td>
<td is="coral-table-cell">${item.count}</td>
<td is="coral-table-cell">
<a title="view log in a new tab" href="${item.path @ context='unsafe'}/log" target="_blank"><coral-icon icon="download" size="XS"></coral-icon></a>
</td>
</tr>
</sly>
</tbody>

</table>

</section>
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
icon="gear"
text="Configure"
variant="primary"/>
<history
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/anchorbutton"
href="/apps/acs-commons/content/contentsync/history.html"
icon="stopwatch"
text="History"
variant="primary"/>
</secondary>
</actions>
<content
Expand Down
Loading
Loading