From 57ce5957e5722d6fbe1438f8ac4a7ff95281bd88 Mon Sep 17 00:00:00 2001 From: Stefan Seifert Date: Mon, 18 Nov 2024 14:49:36 +0100 Subject: [PATCH] use local repo url for local asset metadata calls --- .../impl/NextGenDynamicMediaReference.java | 7 +++++++ ...NextGenDynamicMediaMetadataUrlBuilder.java | 8 +++++++- ...namicMedia_LocalAssetWithMetadataTest.java | 20 +++++++++++-------- .../NextGenDynamicMediaReferenceTest.java | 2 ++ 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/main/java/io/wcm/handler/mediasource/ngdm/impl/NextGenDynamicMediaReference.java b/src/main/java/io/wcm/handler/mediasource/ngdm/impl/NextGenDynamicMediaReference.java index 13eec568..5691b2b8 100644 --- a/src/main/java/io/wcm/handler/mediasource/ngdm/impl/NextGenDynamicMediaReference.java +++ b/src/main/java/io/wcm/handler/mediasource/ngdm/impl/NextGenDynamicMediaReference.java @@ -91,6 +91,13 @@ public NextGenDynamicMediaReference(@NotNull String assetId, @NotNull String fil return asset; } + /** + * @return True if reference points to local asset. + */ + public boolean isLocal() { + return asset != null; + } + /** * @return Reference */ diff --git a/src/main/java/io/wcm/handler/mediasource/ngdm/impl/metadata/NextGenDynamicMediaMetadataUrlBuilder.java b/src/main/java/io/wcm/handler/mediasource/ngdm/impl/metadata/NextGenDynamicMediaMetadataUrlBuilder.java index 2492d4d4..1a1f23fe 100644 --- a/src/main/java/io/wcm/handler/mediasource/ngdm/impl/metadata/NextGenDynamicMediaMetadataUrlBuilder.java +++ b/src/main/java/io/wcm/handler/mediasource/ngdm/impl/metadata/NextGenDynamicMediaMetadataUrlBuilder.java @@ -53,7 +53,13 @@ final class NextGenDynamicMediaMetadataUrlBuilder { public @Nullable String build(@NotNull NextGenDynamicMediaReference reference) { // get parameters from nextgen dynamic media config for URL parameters - String repositoryId = config.getRemoteAssetsRepositoryId(); + String repositoryId; + if (reference.isLocal()) { + repositoryId = config.getLocalAssetsRepositoryId(); + } + else { + repositoryId = config.getRemoteAssetsRepositoryId(); + } String metadataPath = config.getAssetMetadataPath(); if (StringUtils.isAnyEmpty(repositoryId, metadataPath)) { return null; diff --git a/src/test/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMedia_LocalAssetWithMetadataTest.java b/src/test/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMedia_LocalAssetWithMetadataTest.java index 4b262c82..f0d92736 100644 --- a/src/test/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMedia_LocalAssetWithMetadataTest.java +++ b/src/test/java/io/wcm/handler/mediasource/ngdm/NextGenDynamicMedia_LocalAssetWithMetadataTest.java @@ -73,6 +73,7 @@ class NextGenDynamicMedia_LocalAssetWithMetadataTest { private final AemContext context = AppAemContext.newAemContext(); private MockNextGenDynamicMediaConfig nextGenDynamicMediaConfig; + private NextGenDynamicMediaConfigServiceImpl nextGenDynamicMediaConfigService; private MediaHandler mediaHandler; private Resource resource; @@ -81,10 +82,10 @@ class NextGenDynamicMedia_LocalAssetWithMetadataTest { void setUp(WireMockRuntimeInfo wmRuntimeInfo) { nextGenDynamicMediaConfig = context.registerInjectActivateService(MockNextGenDynamicMediaConfig.class); nextGenDynamicMediaConfig.setEnabled(true); - nextGenDynamicMediaConfig.setRepositoryId("localhost:" + wmRuntimeInfo.getHttpPort()); - context.registerInjectActivateService(NextGenDynamicMediaConfigServiceImpl.class, + nextGenDynamicMediaConfig.setRepositoryId("remoterepo1"); + nextGenDynamicMediaConfigService = context.registerInjectActivateService(NextGenDynamicMediaConfigServiceImpl.class, "enabledLocalAssets", true, - "localAssetsRepositoryId", "repo1"); + "localAssetsRepositoryId", "localhost:" + wmRuntimeInfo.getHttpPort()); context.registerInjectActivateService(NextGenDynamicMediaMetadataServiceImpl.class, "enabled", true); @@ -114,14 +115,16 @@ void testLocalAsset() { Rendition rendition = media.getRendition(); UriTemplate uriTemplateScaleWidth = rendition.getUriTemplate(UriTemplateType.SCALE_WIDTH); - assertEquals("https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/my-image.jpg?preferwebp=true&quality=85&width={width}", + assertEquals("https://" + nextGenDynamicMediaConfigService.getLocalAssetsRepositoryId() + + "/adobe/assets/" + SAMPLE_ASSET_ID + "/as/my-image.jpg?preferwebp=true&quality=85&width={width}", uriTemplateScaleWidth.getUriTemplate()); assertEquals(UriTemplateType.SCALE_WIDTH, uriTemplateScaleWidth.getType()); assertEquals(1200, uriTemplateScaleWidth.getMaxWidth()); assertEquals(800, uriTemplateScaleWidth.getMaxHeight()); UriTemplate uriTemplateScaleHeight = rendition.getUriTemplate(UriTemplateType.SCALE_HEIGHT); - assertEquals("https://repo1/adobe/assets/" + SAMPLE_ASSET_ID + "/as/my-image.jpg?height={height}&preferwebp=true&quality=85", + assertEquals("https://" + nextGenDynamicMediaConfigService.getLocalAssetsRepositoryId() + + "/adobe/assets/" + SAMPLE_ASSET_ID + "/as/my-image.jpg?height={height}&preferwebp=true&quality=85", uriTemplateScaleHeight.getUriTemplate()); assertEquals(UriTemplateType.SCALE_HEIGHT, uriTemplateScaleHeight.getType()); assertEquals(1200, uriTemplateScaleHeight.getMaxWidth()); @@ -218,12 +221,13 @@ void testLocalAsset_NoUUID() { assertEquals(MediaInvalidReason.MEDIA_REFERENCE_INVALID, media.getMediaInvalidReason()); } - private static void assertUrl(Media media, String urlParams, String extension) { + private void assertUrl(Media media, String urlParams, String extension) { assertEquals(buildUrl(urlParams, extension), media.getUrl()); } - private static String buildUrl(String urlParams, String extension) { - return "https://repo1/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/my-image." + private String buildUrl(String urlParams, String extension) { + return "https://" + nextGenDynamicMediaConfigService.getLocalAssetsRepositoryId() + + "/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/as/my-image." + extension + "?" + urlParams; } diff --git a/src/test/java/io/wcm/handler/mediasource/ngdm/impl/NextGenDynamicMediaReferenceTest.java b/src/test/java/io/wcm/handler/mediasource/ngdm/impl/NextGenDynamicMediaReferenceTest.java index 366c4a29..b130fe59 100644 --- a/src/test/java/io/wcm/handler/mediasource/ngdm/impl/NextGenDynamicMediaReferenceTest.java +++ b/src/test/java/io/wcm/handler/mediasource/ngdm/impl/NextGenDynamicMediaReferenceTest.java @@ -58,6 +58,7 @@ void testFromReference() { assertEquals(SAMPLE_FILENAME, underTest.getFileName()); assertEquals(SAMPLE_REFERENCE, underTest.toReference()); assertNull(underTest.getAsset()); + assertFalse(underTest.isLocal()); assertEquals(SAMPLE_REFERENCE, underTest.toString()); } @@ -119,6 +120,7 @@ void testFromDamAssetReference_AssetWithUUID_Approved() { assertEquals(SAMPLE_FILENAME, underTest.getFileName()); assertEquals(SAMPLE_REFERENCE, underTest.toReference()); assertEquals(asset, underTest.getAsset()); + assertTrue(underTest.isLocal()); assertEquals(SAMPLE_REFERENCE, underTest.toString()); }