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

#3443 Content Sync: don't drill down into content tree if recursion is off #3452

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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])
- #3443 - Content Sync: don't drill down into content tree if recursion is off

### Added

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.8.1-SNAPSHOT</version>
<version>6.9.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.8.1-SNAPSHOT</version>
<version>6.9.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.8.1-SNAPSHOT</version>
<version>6.9.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.8.1-SNAPSHOT</version>
<version>6.9.0-SNAPSHOT</version>
</parent>

<!-- ====================================================================== -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
import java.util.stream.Collectors;


/**
* The ContentCatalog class provides methods to fetch and process content catalogs
* from a remote instance.
*/
public class ContentCatalog {

private RemoteInstance remoteInstance;
Expand All @@ -46,12 +50,48 @@ public ContentCatalog(RemoteInstance remoteInstance, String catalogServlet) {
this.catalogServlet = catalogServlet;
}

/**
* @deprecated use {@link #getFetchURI(String, String, boolean)}
*/
@Deprecated
public URI getFetchURI(String path, String updateStrategy) throws URISyntaxException {
return remoteInstance.toURI(catalogServlet, "root", path, "strategy", updateStrategy);
return getFetchURI(path, updateStrategy, true);
}

/**
* Gets the URI to fetch the catalog.
*
* @param path the path to fetch the catalog for
* @param updateStrategy the update strategy to use
* @param recursive whether to fetch recursively
* @return the URI to fetch the catalog
* @throws URISyntaxException if the URI syntax is incorrect
*/
public URI getFetchURI(String path, String updateStrategy, boolean recursive) throws URISyntaxException {
return remoteInstance.toURI(catalogServlet, "root", path, "strategy",
updateStrategy, "recursive", String.valueOf(recursive));
}

/**
* @deprecated use {@link #fetch(String, String, boolean)}
*/
@Deprecated
public List<CatalogItem> fetch(String path, String updateStrategy) throws IOException, URISyntaxException {
URI uri = getFetchURI(path, updateStrategy);
return fetch(path, updateStrategy, true);
}

/**
* Fetches the catalog items from the remote instance.
*
* @param path the path to fetch the catalog for
* @param updateStrategy the update strategy to use
* @param recursive whether to fetch recursively
* @return a list of catalog items
* @throws IOException if an I/O error occurs
* @throws URISyntaxException if the URI syntax is incorrect
*/
public List<CatalogItem> fetch(String path, String updateStrategy, boolean recursive) throws IOException, URISyntaxException {
URI uri = getFetchURI(path, updateStrategy, recursive);

String json = remoteInstance.getString(uri);

Expand All @@ -70,6 +110,14 @@ public List<CatalogItem> fetch(String path, String updateStrategy) throws IOExce
.collect(Collectors.toList());
}

/**
* Gets the delta between the catalog items and the resources in the resource resolver.
*
* @param catalog the list of catalog items
* @param resourceResolver the resource resolver to check against
* @param updateStrategy the update strategy to use
* @return a list of catalog items that are modified or not present in the resource resolver
*/
public List<CatalogItem> getDelta(List<CatalogItem> catalog, ResourceResolver resourceResolver, UpdateStrategy updateStrategy) {
List<CatalogItem> lst = new ArrayList<>();
for(CatalogItem item : catalog){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,33 +54,50 @@ public class LastModifiedStrategy implements UpdateStrategy {
@Reference
private ServletResolver servletResolver;

/**
* The ContentCatalog class provides methods to fetch and process content catalogs
* from a remote instance.
*/
@Override
public List<CatalogItem> getItems(SlingHttpServletRequest request) {
String rootPath = request.getParameter("root");
if (rootPath == null) {
throw new IllegalArgumentException("root request parameter is required");
}
boolean recursive = "true".equals(request.getParameter("recursive"));
boolean nonRecursive = "false".equals(request.getParameter("recursive"));

Resource root = request.getResourceResolver().getResource(rootPath);
if (root == null) {
return Collections.emptyList();
}
List<CatalogItem> items = new ArrayList<>();
new AbstractResourceVisitor() {
@Override
public void visit(Resource res) {
if ((!recursive && !res.getPath().equals(root.getPath())) || !accepts(res)) {
return;
if (nonRecursive) {
JsonObjectBuilder json = Json.createObjectBuilder();
writeMetadata(json, root, request);
items.add(new CatalogItem(json.build()));
} else {
new AbstractResourceVisitor() {
@Override
public void visit(Resource res) {
if (!accepts(res)) {
return;
}
JsonObjectBuilder json = Json.createObjectBuilder();
writeMetadata(json, res, request);
items.add(new CatalogItem(json.build()));
}
JsonObjectBuilder json = Json.createObjectBuilder();
writeMetadata(json, res, request);
items.add(new CatalogItem(json.build()));
}
}.accept(root);
}.accept(root);
}
return items;
}

/**
* Checks if the remote resource is modified compared to the local resource.
*
* @param remoteResource the remote catalog item
* @param localResource the local resource
* @return true if the remote resource is modified, false otherwise
*/
@Override
public boolean isModified(CatalogItem remoteResource, Resource localResource) {
LastModifiedInfo remoteLastModified = getLastModified(remoteResource);
Expand All @@ -89,6 +106,13 @@ public boolean isModified(CatalogItem remoteResource, Resource localResource) {
return remoteLastModified.getLastModified() > localLastModified.getLastModified();
}

/**
* Generates a message indicating the modification status of the resource.
*
* @param remoteResource the remote catalog item
* @param localResource the local resource
* @return a message indicating the modification status
*/
@Override
@SuppressWarnings("squid:S2583")
public String getMessage(CatalogItem remoteResource, Resource localResource) {
Expand Down
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.2.0")
@org.osgi.annotation.versioning.Version("1.3.0")
package com.adobe.acs.commons.contentsync;
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ public void testLastModifiedNA() {
assertFalse(updateStrategy.isModified(new CatalogItem(catalogItem), pageResource));
}

/**
* isModified() returns true if the resource is newer
*/
@Test
public void testPageModified() {
String pagePath = "/content/wknd/page";
Expand Down Expand Up @@ -200,8 +203,7 @@ public void testCustomExporter() {
assertEquals(customExporter, item.getCustomExporter());
}

@Test
public void testRecursive() {
void sync(String ... requestParams) {
doAnswer(invocation -> {
GenericServlet servlet = mock(GenericServlet.class);
doReturn(REDIRECT_SERVLET).when(servlet).getServletName();
Expand All @@ -213,28 +215,35 @@ public void testRecursive() {
context.create().page("/content/wknd/en/home");
MockSlingHttpServletRequest request = context.request();

request.addRequestParameter("root", "/content/wknd");
request.addRequestParameter("recursive", "true");
for(int i = 0; i < requestParams.length; i += 2){
request.addRequestParameter(requestParams[i], requestParams[i + 1]);
}
}

List<CatalogItem> items = updateStrategy.getItems(request);
@Test
public void testRecursive() {
sync("root", "/content/wknd",
"recursive", "true");

List<CatalogItem> items = updateStrategy.getItems(context.request());
assertEquals(3, items.size());
}

@Test
public void testNonRecursive() {
doAnswer(invocation -> {
GenericServlet servlet = mock(GenericServlet.class);
doReturn(REDIRECT_SERVLET).when(servlet).getServletName();
return servlet;
}).when(servletResolver).resolveServlet(any(SlingHttpServletRequest.class));
public void testRecursiveDefault() {
sync("root", "/content/wknd");

context.create().page("/content/wknd");
context.create().page("/content/wknd/en");
context.create().page("/content/wknd/en/home");
MockSlingHttpServletRequest request = context.request();
request.addRequestParameter("root", "/content/wknd");
List<CatalogItem> items = updateStrategy.getItems(context.request());
assertEquals(3, items.size());
}

List<CatalogItem> items = updateStrategy.getItems(request);
@Test
public void testNonRecursive() {
sync("root", "/content/wknd",
"recursive", "false");

List<CatalogItem> items = updateStrategy.getItems(context.request());
assertEquals(1, items.size());
}

}
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.8.1-SNAPSHOT</version>
<version>6.9.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.8.1-SNAPSHOT</version>
<version>6.9.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.8.1-SNAPSHOT</version>
<version>6.9.0-SNAPSHOT</version>
</parent>

<!-- ====================================================================== -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
boolean incremental = request.getParameter("incremental") != null;
boolean createVersion = request.getParameter("createVersion") != null;
boolean delete = request.getParameter("delete") != null;
boolean recursive = request.getParameter("recursive") != null;

ValueMap generalSettings = ConfigurationUtils.getSettingsResource(resourceResolver).getValueMap();

Expand All @@ -96,10 +97,10 @@
ContentSync contentSync = new ContentSync(remoteInstance, resourceResolver, importer);
ContentCatalog contentCatalog = new ContentCatalog(remoteInstance, catalogServlet);

println(printWriter, "building catalog from " + contentCatalog.getFetchURI(root, strategyPid) );
println(printWriter, "building catalog from " + contentCatalog.getFetchURI(root, strategyPid, recursive) );
out.flush();
List<CatalogItem> catalog;
List<CatalogItem> remoteItems = contentCatalog.fetch(root, strategyPid);
List<CatalogItem> remoteItems = contentCatalog.fetch(root, strategyPid, recursive);
long t0 = System.currentTimeMillis();
println(printWriter, remoteItems.size() + " resource"+(remoteItems.size() == 1 ? "" : "s")+" fetched in " + (System.currentTimeMillis() - t0) + " ms");
if(incremental){
Expand Down
2 changes: 1 addition & 1 deletion ui.config/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.8.1-SNAPSHOT</version>
<version>6.9.0-SNAPSHOT</version>
</parent>

<!-- ====================================================================== -->
Expand Down
2 changes: 1 addition & 1 deletion ui.content/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.8.1-SNAPSHOT</version>
<version>6.9.0-SNAPSHOT</version>
</parent>

<!-- ====================================================================== -->
Expand Down
Loading