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;
21  
22  import java.util.regex.Matcher;
23  import java.util.regex.Pattern;
24  
25  import org.apache.commons.lang3.StringUtils;
26  import org.jetbrains.annotations.NotNull;
27  import org.jetbrains.annotations.Nullable;
28  
29  /**
30   * Parses and validates Next Generation Dynamic Media references.
31   * <p>
32   * Example: <code>/urn:aaid:aem:12345678-abcd-abcd-abcd-abcd12345678/my-image.jpg</code>
33   * </p>
34   */
35  public final class NextGenDynamicMediaReference {
36  
37    private static final Pattern REFERENCE_PATTERN = Pattern.compile("^/(urn:[^/]+)/([^/]+)$");
38    private static final String ASSET_ID_PREFIX = "urn:";
39  
40    private final String assetId;
41    private final String fileName;
42  
43    /**
44     * @param assetId Asset ID (has to start with "urn:")
45     * @param fileName File name
46     */
47    public NextGenDynamicMediaReference(@NotNull String assetId, @NotNull String fileName) {
48      if (!StringUtils.startsWith(assetId, ASSET_ID_PREFIX)) {
49        throw new IllegalArgumentException("Asset ID must start with '" + ASSET_ID_PREFIX + "'");
50      }
51      this.assetId = assetId;
52      this.fileName = fileName;
53    }
54  
55    /**
56     * @return Asset ID
57     */
58    public @NotNull String getAssetId() {
59      return assetId;
60    }
61  
62    /**
63     * @return File name
64     */
65    public @NotNull String getFileName() {
66      return fileName;
67    }
68  
69    /**
70     * @return Reference
71     */
72    public @NotNull String toReference() {
73      return "/" + assetId + "/" + fileName;
74    }
75  
76    /**
77     * Parses a next generation dynamic media reference.
78     * @param reference Reference
79     * @return Parsed reference or null if reference is invalid
80     */
81    public static @Nullable NextGenDynamicMediaReference fromReference(@Nullable String reference) {
82      if (reference == null) {
83        return null;
84      }
85      Matcher matcher = REFERENCE_PATTERN.matcher(reference);
86      if (!matcher.matches()) {
87        return null;
88      }
89      String assetId = matcher.group(1);
90      String fileName = matcher.group(2);
91      return new NextGenDynamicMediaReference(assetId, fileName);
92    }
93  
94    /**
95     * Checks if given string is a valid next generation dynamic media reference.
96     * @param reference Reference
97     * @return true if reference is valid
98     */
99    public static boolean isReference(@Nullable String reference) {
100     return reference != null && REFERENCE_PATTERN.matcher(reference).matches();
101   }
102 
103   @Override
104   public String toString() {
105     return toReference();
106   }
107 
108 }