Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce repository access #2447

Merged
merged 8 commits into from
Feb 27, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ 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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be moved outside of internal so that it can also be used in our proxy classes?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, although this would be an extension of the public API, and so I would like to have it at least discussed here. Will raise that point when this PR has been merged.


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

import org.jetbrains.annotations.Nullable;


/**
* Resolve a value lazily
*
* @param <T>
*/

public class LazyValue<T> {

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

public LazyValue(Supplier<T> supplier) {
this.supplier = supplier;
}

@Nullable
public T get() {
if (value == null) {
value = Optional.ofNullable(supplier.get());
}
return value.get();
joerghoh marked this conversation as resolved.
Show resolved Hide resolved
}




}
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