NextGenDynamicMediaMetadataUrlBuilder.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.metadata;

  21. import static io.wcm.handler.mediasource.ngdm.impl.NextGenDynamicMediaConfigService.PLACEHOLDER_ASSET_ID;

  22. import org.apache.commons.lang3.StringUtils;
  23. import org.jetbrains.annotations.NotNull;
  24. import org.jetbrains.annotations.Nullable;

  25. import io.wcm.handler.mediasource.ngdm.impl.NextGenDynamicMediaConfigService;
  26. import io.wcm.handler.mediasource.ngdm.impl.NextGenDynamicMediaReference;

  27. /**
  28.  * Builds URL to reference a asset metadata via NextGen Dynamic Media.
  29.  *
  30.  * <p>
  31.  * Example URL that might be build:
  32.  * https://host/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/metadata
  33.  * </p>
  34.  */
  35. final class NextGenDynamicMediaMetadataUrlBuilder {

  36.   private final NextGenDynamicMediaConfigService config;

  37.   /**
  38.    * @param config Config
  39.    */
  40.   NextGenDynamicMediaMetadataUrlBuilder(@NotNull NextGenDynamicMediaConfigService config) {
  41.     this.config = config;
  42.   }

  43.   /**
  44.    * Builds the URL for metadata.
  45.    * @return URL or null if invalid/not possible
  46.    */
  47.   public @Nullable String build(@NotNull NextGenDynamicMediaReference reference) {

  48.     // get parameters from nextgen dynamic media config for URL parameters
  49.     String repositoryId;
  50.     if (reference.isLocal()) {
  51.       repositoryId = config.getLocalAssetsRepositoryId();
  52.     }
  53.     else {
  54.       repositoryId = config.getRemoteAssetsRepositoryId();
  55.     }
  56.     String metadataPath = config.getAssetMetadataPath();
  57.     if (StringUtils.isAnyEmpty(repositoryId, metadataPath)) {
  58.       return null;
  59.     }

  60.     // replace placeholders in delivery path
  61.     metadataPath = StringUtils.replace(metadataPath, PLACEHOLDER_ASSET_ID, reference.getAssetId());

  62.     // build URL
  63.     StringBuilder url = new StringBuilder();
  64.     if (StringUtils.startsWith(repositoryId, "localhost:")) {
  65.       // switch to HTTP for unit tests/local testing
  66.       url.append("http");
  67.     }
  68.     else {
  69.       url.append("https");
  70.     }
  71.     url.append("://")
  72.         .append(repositoryId)
  73.         .append(metadataPath);
  74.     return url.toString();
  75.   }

  76. }