View Javadoc
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  
22  import static io.wcm.handler.mediasource.ngdm.impl.NextGenDynamicMediaConfigService.PLACEHOLDER_ASSET_ID;
23  
24  import org.apache.commons.lang3.StringUtils;
25  import org.jetbrains.annotations.NotNull;
26  import org.jetbrains.annotations.Nullable;
27  
28  import io.wcm.handler.mediasource.ngdm.impl.NextGenDynamicMediaConfigService;
29  import io.wcm.handler.mediasource.ngdm.impl.NextGenDynamicMediaReference;
30  
31  /**
32   * Builds URL to reference a asset metadata via NextGen Dynamic Media.
33   *
34   * <p>
35   * Example URL that might be build:
36   * https://host/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/metadata
37   * </p>
38   */
39  final class NextGenDynamicMediaMetadataUrlBuilder {
40  
41    private final NextGenDynamicMediaConfigService config;
42  
43    /**
44     * @param config Config
45     */
46    NextGenDynamicMediaMetadataUrlBuilder(@NotNull NextGenDynamicMediaConfigService config) {
47      this.config = config;
48    }
49  
50    /**
51     * Builds the URL for metadata.
52     * @return URL or null if invalid/not possible
53     */
54    public @Nullable String build(@NotNull NextGenDynamicMediaReference reference) {
55  
56      // get parameters from nextgen dynamic media config for URL parameters
57      String repositoryId;
58      if (reference.isLocal()) {
59        repositoryId = config.getLocalAssetsRepositoryId();
60      }
61      else {
62        repositoryId = config.getRemoteAssetsRepositoryId();
63      }
64      String metadataPath = config.getAssetMetadataPath();
65      if (StringUtils.isAnyEmpty(repositoryId, metadataPath)) {
66        return null;
67      }
68  
69      // replace placeholders in delivery path
70      metadataPath = StringUtils.replace(metadataPath, PLACEHOLDER_ASSET_ID, reference.getAssetId());
71  
72      // build URL
73      StringBuilder url = new StringBuilder();
74      if (StringUtils.startsWith(repositoryId, "localhost:")) {
75        // switch to HTTP for unit tests/local testing
76        url.append("http");
77      }
78      else {
79        url.append("https");
80      }
81      url.append("://")
82          .append(repositoryId)
83          .append(metadataPath);
84      return url.toString();
85    }
86  
87  }