NextGenDynamicMediaConfigServiceImpl.java

  1. /*
  2.  * #%L
  3.  * wcm.io
  4.  * %%
  5.  * Copyright (C) 2024 wcm.io
  6.  * %%
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *      http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  * #L%
  19.  */
  20. package io.wcm.handler.mediasource.ngdm.impl;

  21. import org.apache.commons.lang3.StringUtils;
  22. import org.jetbrains.annotations.Nullable;
  23. import org.osgi.service.component.annotations.Activate;
  24. import org.osgi.service.component.annotations.Component;
  25. import org.osgi.service.component.annotations.ConfigurationPolicy;
  26. import org.osgi.service.component.annotations.Reference;
  27. import org.osgi.service.component.annotations.ReferenceCardinality;
  28. import org.osgi.service.component.annotations.ReferencePolicy;
  29. import org.osgi.service.component.annotations.ReferencePolicyOption;
  30. import org.osgi.service.metatype.annotations.AttributeDefinition;
  31. import org.osgi.service.metatype.annotations.Designate;
  32. import org.osgi.service.metatype.annotations.ObjectClassDefinition;
  33. import org.slf4j.Logger;
  34. import org.slf4j.LoggerFactory;

  35. import com.adobe.cq.ui.wcm.commons.config.NextGenDynamicMediaConfig;

  36. /**
  37.  * Wraps access to NextGenDynamicMediaConfig - which is deployed but not accessible on AEM 6.5.
  38.  */
  39. @Component(service = NextGenDynamicMediaConfigService.class, immediate = true, configurationPolicy = ConfigurationPolicy.REQUIRE)
  40. @Designate(ocd = NextGenDynamicMediaConfigServiceImpl.Config.class)
  41. public class NextGenDynamicMediaConfigServiceImpl implements NextGenDynamicMediaConfigService {

  42.   @ObjectClassDefinition(
  43.       name = "wcm.io Media Handler Dynamic Media with OpenAPI Support",
  44.       description = "Support for Dynamic Media with OpenAPI.")
  45.   @interface Config {

  46.     @AttributeDefinition(
  47.         name = "Remote Assets",
  48.         description = "Enable Dynamic Media with OpenAPI for remote assets.")
  49.     boolean enabledRemoteAssets() default false;

  50.     @AttributeDefinition(
  51.         name = "Local Assets",
  52.         description = "Enable Next Dynamic Media with OpenAPI for local assets in this AEMaaCS instance.")
  53.     boolean enabledLocalAssets() default false;

  54.     @AttributeDefinition(
  55.         name = "Repository ID for Local Assets",
  56.         description = "Dynamic Media with OpenAPI Delivery host name for local assets. Mandatory if local assets is enabled.")
  57.     String localAssetsRepositoryId();

  58.     @AttributeDefinition(
  59.         name = "Image Delivery Base Path",
  60.         description = "Base path with placeholders to deliver image renditions. "
  61.             + "Placeholders: " + PLACEHOLDER_ASSET_ID + ", " + PLACEHOLDER_SEO_NAME + ", " + PLACEHOLDER_FORMAT + ". "
  62.             + "If not set, the default value from the NextGenDynamicMediaConfig service will be used.")
  63.     String imageDeliveryBasePath() default ADOBE_ASSETS_PREFIX + PLACEHOLDER_ASSET_ID + "/as/"
  64.         + PLACEHOLDER_SEO_NAME + "." + PLACEHOLDER_FORMAT;

  65.     @AttributeDefinition(
  66.         name = "Asset Original Binary Delivery Path",
  67.         description = "Base path with placeholders to deliver asset original binaries. "
  68.             + "Placeholders: " + PLACEHOLDER_ASSET_ID + ", " + PLACEHOLDER_SEO_NAME + ". "
  69.             + "If not set, the default value from the NextGenDynamicMediaConfig service will be used.")
  70.     String assetOriginalBinaryDeliveryPath() default ADOBE_ASSETS_PREFIX + PLACEHOLDER_ASSET_ID + "/original/as/"
  71.         + PLACEHOLDER_SEO_NAME;

  72.     @AttributeDefinition(
  73.         name = "Asset Metadata Path",
  74.         description = "Base path to get asset metadata. "
  75.             + "Placeholder: " + PLACEHOLDER_ASSET_ID + ". "
  76.             + "If not set, the default value from the NextGenDynamicMediaConfig service will be used.")
  77.     String assetMetadataPath() default ADOBE_ASSETS_PREFIX + PLACEHOLDER_ASSET_ID + "/metadata";

  78.     @AttributeDefinition(
  79.         name = "Default image width/height",
  80.         description = "Default width/height (longest edge) when requesting image renditions without explicit dimension.")
  81.     long imageWidthHeightDefault() default 2048;

  82.   }

  83.   private static final String ADOBE_ASSETS_PREFIX = "/adobe/assets/";
  84.   private static final Logger log = LoggerFactory.getLogger(NextGenDynamicMediaConfigServiceImpl.class);

  85.   private boolean enabledRemoteAssets;
  86.   private boolean enabledLocalAssets;
  87.   private String localAssetsRepositoryId;
  88.   private String imageDeliveryBasePath;
  89.   private String assetOriginalBinaryDeliveryPath;
  90.   private String assetMetadataPath;
  91.   private long imageWidthHeightDefault;

  92.   @Reference(policy = ReferencePolicy.STATIC, policyOption = ReferencePolicyOption.GREEDY, cardinality = ReferenceCardinality.OPTIONAL)
  93.   private NextGenDynamicMediaConfig nextGenDynamicMediaConfig;

  94.   @Activate
  95.   private void activate(Config config) {
  96.     enabledRemoteAssets = config.enabledRemoteAssets();
  97.     if (enabledRemoteAssets) {
  98.       if (nextGenDynamicMediaConfig == null) {
  99.         log.debug("NextGenDynamicMediaConfig service is not available, disable remote assets.");
  100.         enabledRemoteAssets = false;
  101.       }
  102.       else {
  103.         log.debug("NextGenDynamicMediaConfig: enabled={}, repositoryId={}, apiKey={}, env={}, imsClient={}",
  104.             nextGenDynamicMediaConfig.enabled(), nextGenDynamicMediaConfig.getRepositoryId(),
  105.             nextGenDynamicMediaConfig.getApiKey(), nextGenDynamicMediaConfig.getEnv(), nextGenDynamicMediaConfig.getImsClient());
  106.       }
  107.     }

  108.     imageDeliveryBasePath = StringUtils.defaultIfBlank(config.imageDeliveryBasePath(),
  109.         nextGenDynamicMediaConfig != null ? nextGenDynamicMediaConfig.getImageDeliveryBasePath() : null);
  110.     assetOriginalBinaryDeliveryPath = StringUtils.defaultIfBlank(config.assetOriginalBinaryDeliveryPath(),
  111.         nextGenDynamicMediaConfig != null ? nextGenDynamicMediaConfig.getAssetOriginalBinaryDeliveryPath() : null);
  112.     assetMetadataPath = StringUtils.defaultIfBlank(config.assetMetadataPath(),
  113.         nextGenDynamicMediaConfig != null ? nextGenDynamicMediaConfig.getAssetMetadataPath() : null);

  114.     enabledLocalAssets = config.enabledLocalAssets();
  115.     localAssetsRepositoryId = config.localAssetsRepositoryId();
  116.     if (enabledLocalAssets && StringUtils.isBlank(localAssetsRepositoryId)) {
  117.       log.debug("localAssetsRepositoryId is not configured, disable local assets.");
  118.       enabledLocalAssets = false;
  119.     }

  120.     imageWidthHeightDefault = config.imageWidthHeightDefault();
  121.   }

  122.   @Override
  123.   public boolean isEnabledRemoteAssets() {
  124.     return enabledRemoteAssets && nextGenDynamicMediaConfig != null && nextGenDynamicMediaConfig.enabled();
  125.   }

  126.   @Override
  127.   public boolean isEnabledLocalAssets() {
  128.     return enabledLocalAssets;
  129.   }

  130.   @Override
  131.   public @Nullable String getAssetSelectorsJsUrl() {
  132.     return nextGenDynamicMediaConfig != null ? nextGenDynamicMediaConfig.getAssetSelectorsJsUrl() : null;
  133.   }

  134.   @Override
  135.   public @Nullable String getImageDeliveryBasePath() {
  136.     return imageDeliveryBasePath;
  137.   }

  138.   @Override
  139.   public @Nullable String getVideoDeliveryPath() {
  140.     return nextGenDynamicMediaConfig != null ? nextGenDynamicMediaConfig.getVideoDeliveryPath() : null;
  141.   }

  142.   @Override
  143.   public @Nullable String getAssetOriginalBinaryDeliveryPath() {
  144.     return assetOriginalBinaryDeliveryPath;
  145.   }

  146.   @Override
  147.   public @Nullable String getAssetMetadataPath() {
  148.     return assetMetadataPath;
  149.   }

  150.   @Override
  151.   public @Nullable String getRemoteAssetsRepositoryId() {
  152.     return nextGenDynamicMediaConfig != null ? nextGenDynamicMediaConfig.getRepositoryId() : null;
  153.   }

  154.   @Override
  155.   public @Nullable String getLocalAssetsRepositoryId() {
  156.     return localAssetsRepositoryId;
  157.   }

  158.   @Override
  159.   public @Nullable String getApiKey() {
  160.     return nextGenDynamicMediaConfig != null ? nextGenDynamicMediaConfig.getApiKey() : null;
  161.   }

  162.   @Override
  163.   public @Nullable String getEnv() {
  164.     return nextGenDynamicMediaConfig != null ? nextGenDynamicMediaConfig.getEnv() : null;
  165.   }

  166.   @Override
  167.   public @Nullable String getImsClient() {
  168.     return nextGenDynamicMediaConfig != null ? nextGenDynamicMediaConfig.getImsClient() : null;
  169.   }

  170.   @Override
  171.   public long getImageWidthHeightDefault() {
  172.     return imageWidthHeightDefault;
  173.   }

  174. }