RequestIncludedLibraries.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.wcm.ui.clientlibs.components;

  21. import java.util.HashSet;
  22. import java.util.List;
  23. import java.util.Set;
  24. import java.util.function.Function;

  25. import org.apache.commons.lang3.BooleanUtils;
  26. import org.apache.sling.api.SlingHttpServletRequest;
  27. import org.jetbrains.annotations.Nullable;

  28. import com.adobe.granite.ui.clientlibs.HtmlLibraryManager;
  29. import com.drew.lang.annotations.NotNull;

  30. class RequestIncludedLibraries {

  31.   /**
  32.    * Request attributes that is also used by Granite UI clientlib manager to store the (raw) library
  33.    * paths that are already included in the current request.
  34.    */
  35.   private static final String RA_INCLUDED_LIBRARY_PATHS = HtmlLibraryManager.class.getName() + ".included";

  36.   private final SlingHttpServletRequest request;
  37.   private final boolean allowMultipleIncludes;

  38.   RequestIncludedLibraries(@NotNull SlingHttpServletRequest request,
  39.       @Nullable Object allowMultipleIncludes) {
  40.     this.request = request;
  41.     this.allowMultipleIncludes = toBoolean(allowMultipleIncludes);
  42.   }

  43.   /**
  44.    * Gets set of library paths from request attribute. If not set, attribute is initialized with an empty set.
  45.    * @return Set of library paths attached to current request
  46.    */
  47.   @SuppressWarnings("unchecked")
  48.   private @NotNull Set<String> getLibaryPathsSetFromRequest() {
  49.     Set<String> libraryPaths = (Set<String>)request.getAttribute(RA_INCLUDED_LIBRARY_PATHS);
  50.     if (libraryPaths == null) {
  51.       libraryPaths = new HashSet<>();
  52.       request.setAttribute(RA_INCLUDED_LIBRARY_PATHS, libraryPaths);
  53.     }
  54.     return libraryPaths;
  55.   }

  56.   /**
  57.    * @param libraryPath Library path
  58.    * @return true if given library was already included in current request.
  59.    */
  60.   boolean isInlucded(@NotNull String libraryPath) {
  61.     return getLibaryPathsSetFromRequest().contains(libraryPath);
  62.   }

  63.   /**
  64.    * Store library path as included in current request.
  65.    * @param libraryPath Library path
  66.    */
  67.   void storeIncluded(@NotNull String libraryPath) {
  68.     getLibaryPathsSetFromRequest().add(libraryPath);
  69.   }

  70.   /**
  71.    * Builds the markup for all given HTML libraries that are not already included in the current request.
  72.    * @param libraryPaths Library paths
  73.    * @param htmlTagBuilderFactory Factory to create HTML tag builders
  74.    * @return Markup
  75.    */
  76.   String buildMarkupIgnoringDuplicateLibraries(@NotNull List<String> libraryPaths,
  77.       @NotNull Function<String, HtmlTagBuilder> htmlTagBuilderFactory) {
  78.     StringBuilder markup = new StringBuilder();
  79.     for (String libraryPath : libraryPaths) {
  80.       // ignore libraries that are already included
  81.       if (!allowMultipleIncludes && isInlucded(libraryPath)) {
  82.         continue;
  83.       }
  84.       // build markup for library
  85.       markup.append(htmlTagBuilderFactory.apply(libraryPath).build());
  86.       // mark library as included
  87.       storeIncluded(libraryPath);
  88.     }
  89.     return markup.toString();
  90.   }

  91.   private static boolean toBoolean(Object value) {
  92.     if (value instanceof Boolean) {
  93.       return (Boolean)value;
  94.     }
  95.     else if (value instanceof String) {
  96.       return BooleanUtils.toBoolean((String)value);
  97.     }
  98.     return false;
  99.   }

  100. }