Skip to content

Commit

Permalink
Merge branch 'main' into issue/2526
Browse files Browse the repository at this point in the history
  • Loading branch information
LSantha authored Feb 29, 2024
2 parents e46ea2a + e333b37 commit 9dd8bb5
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ Copyright 2023 Adobe
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
package com.adobe.cq.wcm.core.components.internal;

import java.util.Optional;
import java.util.function.Supplier;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;


/**
* Resolve a value lazily.
*
* In SlingModels, not all values computed in the @PostConstruct method are always
* used. That means that there might be values which are only consumed in certain
* circumstances, but which have overhead to calculate. In case this value is not used
* at all, this overhead is wasted time.
*
* Wrapping them into the LazyValue type will do the calculation of the value only
* when required.
*
*
*
* @param <T> the type of the value
*/

public class LazyValue<T> {

private Optional<T> value;
private Supplier<T> supplier;

/**
*
* @param supplier to provide the value
*/
public LazyValue(@NotNull Supplier<T> supplier) {
if (supplier == null) {
throw new IllegalArgumentException("supplier must not be null");
}
this.supplier = supplier;
}

@Nullable
public T get() {
if (value == null) {
value = Optional.ofNullable(supplier.get());
}
return value.orElse(null);
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import com.adobe.cq.export.json.ContainerExporter;
import com.adobe.cq.export.json.ExporterConstants;
import com.adobe.cq.export.json.SlingModelFilter;
import com.adobe.cq.wcm.core.components.internal.LazyValue;
import com.adobe.cq.wcm.core.components.internal.Utils;
import com.adobe.cq.wcm.core.components.commons.link.LinkManager;
import com.adobe.cq.wcm.core.components.models.Page;
Expand Down Expand Up @@ -99,16 +100,16 @@ public class PageImpl extends AbstractComponentImpl implements Page {
@Self
protected LinkManager linkManager;

protected String[] keywords = new String[0];
protected LazyValue<String[]> keywords;
protected String designPath;
protected String staticDesignPath;
protected String title;
protected String description;
protected String brandSlug;
protected LazyValue<String> brandSlug;

protected String[] clientLibCategories = new String[0];
protected Calendar lastModifiedDate;
protected String templateName;
protected LazyValue<String> templateName;

protected static final String DEFAULT_TEMPLATE_EDITOR_CLIENTLIB = "wcm.foundation.components.parsys.allowedcomponents";
protected static final String PN_CLIENTLIBS = "clientlibs";
Expand All @@ -129,27 +130,34 @@ protected void initModel() {
if (StringUtils.isBlank(title)) {
title = currentPage.getName();
}
Tag[] tags = currentPage.getTags();
keywords = new String[tags.length];
int index = 0;
for (Tag tag : tags) {
keywords[index++] = tag.getTitle(currentPage.getLanguage(false));
}
keywords = new LazyValue<>(() -> buildKeywords());
if (currentDesign != null) {
String designPath = currentDesign.getPath();
if (!Designer.DEFAULT_DESIGN_PATH.equals(designPath)) {
this.designPath = designPath;
if (resolver.getResource(designPath + "/static.css") != null) {
final Resource designResource = resolver.getResource(designPath);
if (designResource != null && designResource.getChild("static.css") != null) {
staticDesignPath = designPath + "/static.css";
}
loadFavicons(designPath);
loadFavicons(designResource);
}
}
populateClientlibCategories();
templateName = extractTemplateName();
brandSlug = Utils.getInheritedValue(currentPage, PN_BRANDSLUG);
templateName = new LazyValue<>(() -> extractTemplateName());
brandSlug = new LazyValue<>(() -> Utils.getInheritedValue(currentPage, PN_BRANDSLUG));
}

private String[] buildKeywords() {
Tag[] tags = currentPage.getTags();
String[] keywords = new String[tags.length];
int index = 0;
Locale language= currentPage.getLanguage(false);
for (Tag tag : tags) {
keywords[index++] = tag.getTitle(language);
}
return keywords;
}

protected String extractTemplateName() {
String templateName = null;
String templatePath = pageProperties.get(NameConstants.PN_TEMPLATE, String.class);
Expand Down Expand Up @@ -179,7 +187,12 @@ public Calendar getLastModifiedDate() {
@Override
@JsonIgnore
public String[] getKeywords() {
return Arrays.copyOf(keywords, keywords.length);
String[] kw = keywords.get();
if (kw != null) {
return Arrays.copyOf(kw, kw.length);
} else {
return new String[0];
}
}

@Override
Expand Down Expand Up @@ -211,12 +224,12 @@ public String getDescription() {

@Override
public String getBrandSlug() {
return brandSlug;
return brandSlug.get();
}

@Override
public String getTemplateName() {
return templateName;
return templateName.get();
}

@Override
Expand Down Expand Up @@ -290,21 +303,20 @@ private <T> Map<String, T> getChildModels(@NotNull SlingHttpServletRequest sling
return itemWrappers;
}

protected void loadFavicons(String designPath) {
favicons.put(PN_FAVICON_ICO, getFaviconPath(designPath, FN_FAVICON_ICO));
favicons.put(PN_FAVICON_PNG, getFaviconPath(designPath, FN_FAVICON_PNG));
favicons.put(PN_TOUCH_ICON_120, getFaviconPath(designPath, FN_TOUCH_ICON_120));
favicons.put(PN_TOUCH_ICON_152, getFaviconPath(designPath, FN_TOUCH_ICON_152));
favicons.put(PN_TOUCH_ICON_60, getFaviconPath(designPath, FN_TOUCH_ICON_60));
favicons.put(PN_TOUCH_ICON_76, getFaviconPath(designPath, FN_TOUCH_ICON_76));
protected void loadFavicons(@Nullable Resource designResource) {
favicons.put(PN_FAVICON_ICO, getFaviconPath(designResource, FN_FAVICON_ICO));
favicons.put(PN_FAVICON_PNG, getFaviconPath(designResource, FN_FAVICON_PNG));
favicons.put(PN_TOUCH_ICON_120, getFaviconPath(designResource, FN_TOUCH_ICON_120));
favicons.put(PN_TOUCH_ICON_152, getFaviconPath(designResource, FN_TOUCH_ICON_152));
favicons.put(PN_TOUCH_ICON_60, getFaviconPath(designResource, FN_TOUCH_ICON_60));
favicons.put(PN_TOUCH_ICON_76, getFaviconPath(designResource, FN_TOUCH_ICON_76));
}

protected String getFaviconPath(String designPath, String faviconName) {
String path = designPath + "/" + faviconName;
if (resolver.getResource(path) == null) {
return null;
}
return path;
protected String getFaviconPath(@Nullable Resource designResource, String faviconName) {
if (designResource != null && designResource.getChild(faviconName) != null) {
return designResource.getPath() + "/" + faviconName;
}
return null;
}

protected void populateClientlibCategories() {
Expand Down Expand Up @@ -344,7 +356,7 @@ protected final PageData getComponentData() {
Optional.ofNullable(pageProperties.get(JcrConstants.JCR_CREATED, Calendar.class))
.map(Calendar::getTime)
.orElse(null)))
.withTags(() -> Arrays.copyOf(this.keywords, this.keywords.length))
.withTags(() -> getKeywords())
.withDescription(() -> this.pageProperties.get(NameConstants.PN_DESCRIPTION, String.class))
.withTemplatePath(() -> Optional.ofNullable(this.currentPage.getTemplate())
.map(Template::getPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import com.adobe.cq.wcm.core.components.config.HtmlPageItemConfig;
import com.adobe.cq.wcm.core.components.config.HtmlPageItemsConfig;
import com.adobe.cq.wcm.core.components.commons.link.LinkManager;
import com.adobe.cq.wcm.core.components.internal.LazyValue;
import com.adobe.cq.wcm.core.components.internal.models.v1.RedirectItemImpl;
import com.adobe.cq.wcm.core.components.models.HtmlPageItem;
import com.adobe.cq.wcm.core.components.models.NavigationItem;
Expand Down Expand Up @@ -157,7 +158,7 @@ public class PageImpl extends com.adobe.cq.wcm.core.components.internal.models.v
* The proxy path of the first client library listed in the style under the
* &quot;{@value Page#PN_APP_RESOURCES_CLIENTLIB}&quot; property.
*/
private String appResourcesPath;
private LazyValue<String> appResourcesPath;

/**
* The redirect target as a NavigationItem.
Expand All @@ -182,14 +183,14 @@ public class PageImpl extends com.adobe.cq.wcm.core.components.internal.models.v
@PostConstruct
protected void initModel() {
super.initModel();
this.appResourcesPath = Optional.ofNullable(currentStyle)
this.appResourcesPath = new LazyValue<>(() -> Optional.ofNullable(currentStyle)
.map(style -> style.get(PN_APP_RESOURCES_CLIENTLIB, String.class))
.map(resourcesClientLibrary -> htmlLibraryManager.getLibraries(new String[]{resourcesClientLibrary}, LibraryType.CSS, true, false))
.map(Collection::stream)
.orElse(Stream.empty())
.findFirst()
.map(this::getProxyPath)
.orElse(null);
.orElse(null));
}

protected NavigationItem newRedirectItem(@NotNull String redirectTarget, @NotNull SlingHttpServletRequest request, @NotNull LinkManager linkManager) {
Expand All @@ -216,7 +217,7 @@ private String getProxyPath(ClientLibrary lib) {
}

@Override
protected void loadFavicons(String designPath) {
protected void loadFavicons(Resource designPath) {
}

@Override
Expand Down Expand Up @@ -255,7 +256,7 @@ public String[] getClientLibCategoriesJsHead() {

@Override
public String getAppResourcesPath() {
return appResourcesPath;
return appResourcesPath.get();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
<columns
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/numberfield"
defaultValue=""
fieldDescription="Number of columns in the grid"
fieldLabel="Columns"
min="1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
<columns
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/numberfield"
defaultValue=""
fieldDescription="Number of columns in the grid"
fieldLabel="Columns"
min="1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
<field
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/numberfield"
defaultValue="{Long}1280"
min="{Long}0"
name="./allowedRenditionWidths"
required="{Boolean}true"/>
Expand All @@ -47,8 +46,7 @@
typeHint="Long"
required="{Boolean}true"
min="{Long}0"
max="{Long}100"
defaultValue="{Long}82" />
max="{Long}100"/>
<disableLazyLoading
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/checkbox"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@
<field
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/numberfield"
defaultValue="{Long}1280"
min="{Long}0"
name="./allowedRenditionWidths"
required="{Boolean}true"/>
Expand All @@ -152,8 +151,7 @@
typeHint="Long"
required="{Boolean}true"
min="{Long}0"
max="{Long}100"
defaultValue="{Long}82" />
max="{Long}100"/>
</items>
</content>
</items>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@
name="./resizeWidth"
typeHint="Long"
required="{Boolean}false"
defaultValue="{Long}1280"
min="{Long}1"
max="{Long}3840"/>
<heading
Expand All @@ -143,7 +142,6 @@
<field
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/numberfield"
defaultValue="{Long}1280"
min="{Long}0"
name="./allowedRenditionWidths"
required="{Boolean}true"/>
Expand All @@ -170,8 +168,7 @@
typeHint="Long"
required="{Boolean}false"
min="{Long}0"
max="{Long}100"
defaultValue="{Long}82" />
max="{Long}100" />
</items>
<granite:data
jcr:primaryType="nt:unstructured"
Expand Down

0 comments on commit 9dd8bb5

Please sign in to comment.