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   * <p>
34   * Example URL that might be build:
35   * https://host/adobe/assets/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/metadata
36   * </p>
37   */
38  final class NextGenDynamicMediaMetadataUrlBuilder {
39  
40    private final NextGenDynamicMediaConfigService config;
41  
42    /**
43     * @param config Config
44     */
45    NextGenDynamicMediaMetadataUrlBuilder(@NotNull NextGenDynamicMediaConfigService config) {
46      this.config = config;
47    }
48  
49    /**
50     * Builds the URL for metadata.
51     * @return URL or null if invalid/not possible
52     */
53    public @Nullable String build(@NotNull NextGenDynamicMediaReference reference) {
54  
55      // get parameters from nextgen dynamic media config for URL parameters
56      String repositoryId = config.getRepositoryId();
57      String metadataPath = config.getAssetMetadataPath();
58      if (StringUtils.isAnyEmpty(repositoryId, metadataPath)) {
59        return null;
60      }
61  
62      // replace placeholders in delivery path
63      metadataPath = StringUtils.replace(metadataPath, PLACEHOLDER_ASSET_ID, reference.getAssetId());
64  
65      // build URL
66      StringBuilder url = new StringBuilder();
67      if (StringUtils.startsWith(repositoryId, "localhost:")) {
68        // switch to HTTP for unit tests/local testing
69        url.append("http");
70      }
71      else {
72        url.append("https");
73      }
74      url.append("://")
75          .append(repositoryId)
76          .append(metadataPath);
77      return url.toString();
78    }
79  
80  }