forked from Adobe-Consulting-Services/acs-aem-commons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Content Sync (Adobe-Consulting-Services#3151)
* new feature: content sync
- Loading branch information
1 parent
f10eef5
commit 86b0315
Showing
39 changed files
with
5,255 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
bundle/src/main/java/com/adobe/acs/commons/contentsync/CatalogItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/*- | ||
* #%L | ||
* ACS AEM Commons Bundle | ||
* %% | ||
* Copyright (C) 2013 - 2022 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. | ||
* #L% | ||
*/ | ||
package com.adobe.acs.commons.contentsync; | ||
|
||
|
||
import javax.json.JsonObject; | ||
|
||
import static org.apache.jackrabbit.JcrConstants.JCR_CONTENT; | ||
|
||
/** | ||
* A Json object describing a resource to sync. | ||
* | ||
* Required fields: | ||
* - path | ||
* - jcr:primaryType | ||
* - exportUri | ||
*/ | ||
public class CatalogItem { | ||
private final JsonObject object; | ||
|
||
public CatalogItem(JsonObject object){ | ||
this.object = object; | ||
} | ||
|
||
public String getPath(){ | ||
return object.getString("path"); | ||
} | ||
|
||
public String getPrimaryType(){ | ||
return object.getString("jcr:primaryType"); | ||
} | ||
|
||
public boolean hasContentResource(){ | ||
return getContentUri().endsWith("/" + JCR_CONTENT + ".infinity.json"); | ||
} | ||
|
||
public String getContentUri(){ | ||
return object.getString("exportUri"); | ||
} | ||
|
||
public String getString(String key){ | ||
return object.containsKey(key) ? object.getString(key) : null; | ||
} | ||
|
||
public long getLong(String key){ | ||
return object.containsKey(key) ? object.getJsonNumber(key).longValue() : 0L; | ||
} | ||
|
||
public String getCustomExporter(){ | ||
return object.containsKey("renderServlet") ? object.getString("renderServlet") : null; | ||
|
||
} | ||
|
||
public JsonObject getJsonObject(){ | ||
return object; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
bundle/src/main/java/com/adobe/acs/commons/contentsync/ConfigurationUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/*- | ||
* #%L | ||
* ACS AEM Commons Bundle | ||
* %% | ||
* Copyright (C) 2013 - 2022 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. | ||
* #L% | ||
*/ | ||
package com.adobe.acs.commons.contentsync; | ||
|
||
import com.adobe.acs.commons.contentsync.impl.LastModifiedStrategy; | ||
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.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE; | ||
import static org.apache.jackrabbit.JcrConstants.NT_UNSTRUCTURED; | ||
import static org.apache.sling.jcr.resource.api.JcrResourceConstants.NT_SLING_FOLDER; | ||
|
||
public class ConfigurationUtils { | ||
public static final String CONFIG_PATH = "/var/acs-commons/contentsync"; | ||
public static final String SETTINGS_PATH = CONFIG_PATH + "/settings"; | ||
public static final String HOSTS_PATH = CONFIG_PATH + "/hosts"; | ||
|
||
public static final String UPDATE_STRATEGY_KEY = "update-strategy"; | ||
public static final String EVENT_USER_DATA_KEY = "event-user-data"; | ||
|
||
private ConfigurationUtils(){ | ||
|
||
} | ||
|
||
public static Resource getSettingsResource(ResourceResolver resourceResolver) throws PersistenceException { | ||
Map<String, Object> resourceProperties = new HashMap<>(); | ||
resourceProperties.put(JCR_PRIMARYTYPE, NT_UNSTRUCTURED); | ||
resourceProperties.put(UPDATE_STRATEGY_KEY, LastModifiedStrategy.class.getName()); | ||
resourceProperties.put(EVENT_USER_DATA_KEY, "changedByPageManagerCopy"); | ||
return ResourceUtil.getOrCreateResource(resourceResolver, SETTINGS_PATH, resourceProperties, NT_SLING_FOLDER, true); | ||
} | ||
|
||
public static Resource getHostsResource(ResourceResolver resourceResolver) throws PersistenceException { | ||
Map<String, Object> resourceProperties = new HashMap<>(); | ||
resourceProperties.put(JCR_PRIMARYTYPE, NT_UNSTRUCTURED); | ||
return ResourceUtil.getOrCreateResource(resourceResolver, HOSTS_PATH, resourceProperties, NT_SLING_FOLDER, true); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
bundle/src/main/java/com/adobe/acs/commons/contentsync/ContentCatalog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/*- | ||
* #%L | ||
* ACS AEM Commons Bundle | ||
* %% | ||
* Copyright (C) 2013 - 2022 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. | ||
* #L% | ||
*/ | ||
package com.adobe.acs.commons.contentsync; | ||
|
||
import org.apache.sling.api.resource.Resource; | ||
import org.apache.sling.api.resource.ResourceResolver; | ||
|
||
import javax.json.Json; | ||
import javax.json.JsonArray; | ||
import javax.json.JsonObject; | ||
import javax.json.JsonReader; | ||
import javax.json.JsonValue; | ||
import java.io.IOException; | ||
import java.io.StringReader; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
|
||
public class ContentCatalog { | ||
|
||
private RemoteInstance remoteInstance; | ||
private final String catalogServlet; | ||
|
||
public ContentCatalog(RemoteInstance remoteInstance, String catalogServlet) { | ||
this.remoteInstance = remoteInstance; | ||
this.catalogServlet = catalogServlet; | ||
} | ||
|
||
public URI getFetchURI(String path, String updateStrategy) throws URISyntaxException { | ||
return remoteInstance.toURI(catalogServlet, "root", path, "strategy", updateStrategy); | ||
} | ||
|
||
public List<CatalogItem> fetch(String path, String updateStrategy) throws IOException, URISyntaxException { | ||
URI uri = getFetchURI(path, updateStrategy); | ||
|
||
String json = remoteInstance.getString(uri); | ||
|
||
JsonObject response; | ||
try(JsonReader reader = Json.createReader(new StringReader(json))) { | ||
response = reader.readObject(); | ||
} | ||
if (!response.containsKey("resources")) { | ||
throw new IOException("Failed to fetch content catalog from " + uri + ", Response: " + json); | ||
} | ||
JsonArray catalog = response.getJsonArray("resources"); | ||
|
||
return catalog.stream() | ||
.map(JsonValue::asJsonObject) | ||
.map(CatalogItem::new) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public List<CatalogItem> getDelta(List<CatalogItem> catalog, ResourceResolver resourceResolver, UpdateStrategy updateStrategy) { | ||
List<CatalogItem> lst = new ArrayList<>(); | ||
for(CatalogItem item : catalog){ | ||
Resource resource = resourceResolver.getResource(item.getPath()); | ||
if(resource == null || updateStrategy.isModified(item, resource)){ | ||
lst.add(item); | ||
} | ||
} | ||
return lst; | ||
} | ||
|
||
} |
Oops, something went wrong.