-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unable to publish/preview new assets when quick publish from tools co…
…nfig page editor (#7) Co-authored-by: srikanth gurram <srikanth.gurram@aldi-sued.com> Co-authored-by: Stefan Seifert <stefanseifert@users.noreply.github.com>
- Loading branch information
1 parent
d430641
commit f0dea9a
Showing
8 changed files
with
324 additions
and
16 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
140 changes: 140 additions & 0 deletions
140
src/main/java/io/wcm/caconfig/extensions/references/impl/AssetRefereneDetector.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,140 @@ | ||
/* | ||
* #%L | ||
* wcm.io | ||
* %% | ||
* Copyright (C) 2024 wcm.io | ||
* %% | ||
* 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 io.wcm.caconfig.extensions.references.impl; | ||
|
||
import static com.day.cq.dam.api.DamConstants.MOUNTPOINT_ASSETS; | ||
|
||
import java.lang.reflect.Array; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import org.apache.sling.api.resource.Resource; | ||
import org.apache.sling.api.resource.ResourceResolver; | ||
import org.apache.sling.api.resource.ValueMap; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.day.cq.dam.api.Asset; | ||
import com.day.cq.wcm.api.Page; | ||
|
||
/** | ||
* Recursively scans all string and string array properties in all resources of the given configuration page | ||
* to check for asset references. | ||
*/ | ||
class AssetRefereneDetector { | ||
|
||
private final Page configPage; | ||
private final Resource configResource; | ||
private final ResourceResolver resourceResolver; | ||
private final List<Asset> assets = new ArrayList<>(); | ||
|
||
private static final Pattern ASSET_PATH = Pattern.compile("^" + MOUNTPOINT_ASSETS + "/.*$"); | ||
private static final Logger log = LoggerFactory.getLogger(AssetRefereneDetector.class); | ||
|
||
/** | ||
* @param configPage Configuration page (must have a content resource). | ||
*/ | ||
AssetRefereneDetector(@NotNull Page configPage) { | ||
this.configPage = configPage; | ||
this.configResource = configPage.getContentResource(); | ||
this.resourceResolver = configResource.getResourceResolver(); | ||
} | ||
|
||
/** | ||
* @return List of all assets referenced in the configuration page. | ||
*/ | ||
List<Asset> getReferencedAssets() { | ||
assets.clear(); | ||
findAssetReferencesRecursively(configResource); | ||
return assets; | ||
} | ||
|
||
/** | ||
* Recurse through all child resources of the given resource. | ||
* @param resource Resource | ||
*/ | ||
private void findAssetReferencesRecursively(@NotNull Resource resource) { | ||
findAssetReferences(resource); | ||
resource.getChildren().forEach(this::findAssetReferencesRecursively); | ||
} | ||
|
||
/** | ||
* Find asset references in all properties of the given resource. | ||
* @param resource Resource | ||
*/ | ||
private void findAssetReferences(@NotNull Resource resource) { | ||
ValueMap props = resource.getValueMap(); | ||
assets.addAll(props.values().stream() | ||
.flatMap(this::getAssetsIfAssetReference) | ||
.collect(Collectors.toList())); | ||
} | ||
|
||
/** | ||
* Checks if the value is string which might be asset reference, or an array containing a string asset reference. | ||
* @param value Value | ||
* @return Found referenced assets | ||
*/ | ||
private Stream<Asset> getAssetsIfAssetReference(@Nullable Object value) { | ||
List<Asset> result = new ArrayList<>(); | ||
if (value instanceof String) { | ||
getAssetIfAssetReference((String)value).ifPresent(result::add); | ||
} | ||
else if (value != null && value.getClass().isArray()) { | ||
int length = Array.getLength(value); | ||
for (int i = 0; i < length; i++) { | ||
Object itemValue = Array.get(value, i); | ||
if (itemValue instanceof String) { | ||
getAssetIfAssetReference((String)itemValue).ifPresent(result::add); | ||
} | ||
} | ||
} | ||
return result.stream(); | ||
} | ||
|
||
/** | ||
* Checks if the given string points to an asset. | ||
* @param value String value | ||
* @return Asset if string is a valid asset reference. | ||
*/ | ||
private Optional<Asset> getAssetIfAssetReference(@NotNull String value) { | ||
if (isAssetReference(value)) { | ||
Resource resource = resourceResolver.getResource(value); | ||
if (resource != null) { | ||
Asset asset = resource.adaptTo(Asset.class); | ||
if (asset != null) { | ||
log.trace("Found asset reference {} for resource {}", configPage.getPath(), resource.getPath()); | ||
return Optional.of(asset); | ||
} | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
static boolean isAssetReference(@NotNull String value) { | ||
return ASSET_PATH.matcher(value).matches(); | ||
} | ||
|
||
} |
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
97 changes: 97 additions & 0 deletions
97
src/test/java/io/wcm/caconfig/extensions/references/impl/AssetRefereneDetectorTest.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,97 @@ | ||
/* | ||
* #%L | ||
* wcm.io | ||
* %% | ||
* Copyright (C) 2024 wcm.io | ||
* %% | ||
* 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 io.wcm.caconfig.extensions.references.impl; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import com.day.cq.dam.api.Asset; | ||
import com.day.cq.wcm.api.Page; | ||
|
||
import io.wcm.testing.mock.aem.junit5.AemContext; | ||
import io.wcm.testing.mock.aem.junit5.AemContextExtension; | ||
import io.wcm.wcm.commons.contenttype.ContentType; | ||
|
||
@ExtendWith(AemContextExtension.class) | ||
class AssetRefereneDetectorTest { | ||
|
||
private static final String ASSET_1 = "/content/dam/asset1.jpg"; | ||
private static final String ASSET_2 = "/content/dam/asset2.jpg"; | ||
private static final String ASSET_3 = "/content/dam/asset3.jpg"; | ||
|
||
final AemContext context = new AemContext(); | ||
|
||
@BeforeEach | ||
void setUp() { | ||
context.create().asset(ASSET_1, 10, 10, ContentType.JPEG); | ||
context.create().asset(ASSET_2, 10, 10, ContentType.JPEG); | ||
context.create().asset(ASSET_3, 10, 10, ContentType.JPEG); | ||
} | ||
|
||
@Test | ||
void testNoReferences() { | ||
Page page = context.create().page("/content/test", null, | ||
"prop1", "value1", "prop2", 5); | ||
assertTrue(getReferences(page).isEmpty()); | ||
} | ||
|
||
@Test | ||
void testSimpleProperties() { | ||
Page page = context.create().page("/content/test", null, | ||
"prop1", "value1", "prop2", 5, | ||
"ref1", ASSET_1, "ref2", ASSET_2); | ||
assertEquals(Set.of(ASSET_1, ASSET_2), getReferences(page)); | ||
} | ||
|
||
@Test | ||
void testArrayProperty() { | ||
Page page = context.create().page("/content/test", null, | ||
"prop1", "value1", "prop2", 5, | ||
"ref", ASSET_1, "refs", new String[] { ASSET_2, ASSET_3 }); | ||
assertEquals(Set.of(ASSET_1, ASSET_2, ASSET_3), getReferences(page)); | ||
} | ||
|
||
@Test | ||
void testNested() { | ||
Page page = context.create().page("/content/test", null, | ||
"prop1", "value1", "prop2", 5, | ||
"ref", ASSET_1); | ||
context.create().resource(page, "sub1", | ||
"ref", ASSET_2); | ||
context.create().resource(page, "sub2/sub21/sub211", | ||
"ref", ASSET_3); | ||
assertEquals(Set.of(ASSET_1, ASSET_2, ASSET_3), getReferences(page)); | ||
} | ||
|
||
static Set<String> getReferences(@NotNull Page page) { | ||
return new AssetRefereneDetector(page).getReferencedAssets().stream() | ||
.map(Asset::getPath) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
} |
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 |
---|---|---|
|
@@ -26,4 +26,8 @@ | |
|
||
String key() default ""; | ||
|
||
String assetReference1() default ""; | ||
|
||
String assetReference2() default ""; | ||
|
||
} |
Oops, something went wrong.