Skip to content

Commit

Permalink
SLING-12011 - Using LazyBindings
Browse files Browse the repository at this point in the history
  • Loading branch information
klcodanr committed Aug 26, 2023
1 parent c0f38e9 commit c07f26c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javax.servlet.ServletRequest;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.scripting.LazyBindings;
import org.apache.sling.cms.CMSConstants;
import org.apache.sling.cms.CMSUtils;
import org.apache.sling.cms.ComponentConfiguration;
Expand All @@ -34,29 +35,57 @@
public class DefaultScriptBindingsValueProvider implements BindingsValuesProvider {

public static final String PN_CURRENT_PAGE = "currentPage";
public static final String PN_CURRENT_PAGE_EVALUATED = "org.apache.sling.cms.core.internal.DefaultScriptBindingsValueProvider_"
+ PN_CURRENT_PAGE + "_evaluated";

@Override
public void addBindings(Bindings bindings) {
Resource resource = (Resource) bindings.get("resource");
bindLazy((LazyBindings) bindings, resource);
}

private void bindLazy(LazyBindings bindings, Resource resource) {
bindings.put("properties", new LazyBindings.Supplier() {
@Override
public Object get() {
return resource.getValueMap();
}
});

bindings.put("componentConfiguration", new LazyBindings.Supplier() {
@Override
public Object get() {
return Optional.ofNullable(resource.adaptTo(ComponentConfiguration.class))
.map(ComponentConfiguration::getProperties).orElse(null);
}
});

bindings.put("properties", resource.getValueMap());

Optional.ofNullable(resource.adaptTo(ComponentConfiguration.class)).map(ComponentConfiguration::getProperties)
.ifPresent(p -> bindings.put("componentConfiguration", p));

Resource publishableParent = CMSUtils.findPublishableParent(resource);
if (publishableParent != null && CMSConstants.NT_PAGE.equals(publishableParent.getResourceType())) {
Optional.of(publishableParent.adaptTo(Page.class)).ifPresent(p -> {
bindings.put("page", p);
ServletRequest request = (ServletRequest) bindings.get("request");
if (request.getAttribute(PN_CURRENT_PAGE) == null) {
request.setAttribute(PN_CURRENT_PAGE, p);
bindings.put(PN_CURRENT_PAGE, p);
} else {
bindings.put(PN_CURRENT_PAGE, request.getAttribute(PN_CURRENT_PAGE));
bindings.put("page", new LazyBindings.Supplier() {
@Override
public Object get() {
Resource publishableParent = CMSUtils.findPublishableParent(resource);
if (publishableParent != null && CMSConstants.NT_PAGE.equals(publishableParent.getResourceType())) {
return publishableParent.adaptTo(Page.class);
}
});
return null;
}
});
ServletRequest request = (ServletRequest) bindings.get("request");
if (request.getAttribute(PN_CURRENT_PAGE_EVALUATED) == null) {
Resource publishableParent = CMSUtils.findPublishableParent(resource);
if (publishableParent != null && CMSConstants.NT_PAGE.equals(publishableParent.getResourceType())) {
Optional.of(publishableParent.adaptTo(Page.class)).ifPresent(p -> {
bindings.put("page", p);
if (request.getAttribute(PN_CURRENT_PAGE) == null) {
request.setAttribute(PN_CURRENT_PAGE, p);
bindings.put(PN_CURRENT_PAGE, p);
} else {
bindings.put(PN_CURRENT_PAGE, request.getAttribute(PN_CURRENT_PAGE));
}
});
}
request.setAttribute(PN_CURRENT_PAGE_EVALUATED, true);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import javax.script.Bindings;

import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.scripting.SlingBindings;
import org.apache.sling.cms.Page;
import org.apache.sling.cms.core.helpers.SlingCMSTestHelper;
import org.apache.sling.testing.mock.sling.junit.SlingContext;
Expand All @@ -43,7 +44,7 @@ public void init() {

@Test
public void testPageBinding() {
Bindings bindings = new MockBindings();
Bindings bindings = new SlingBindings();
context.currentResource("/content/apache/sling-apache-org/index");
bindings.put("resource", context.currentResource());
bindings.put("request", context.request());
Expand All @@ -66,7 +67,7 @@ public void testPageBinding() {

@Test
public void testSiteBinding() {
Bindings bindings = new MockBindings();
Bindings bindings = new SlingBindings();
context.currentResource("/content/apache/sling-apache-org");
bindings.put("resource", context.currentResource());
bindings.put("request", context.request());
Expand Down

0 comments on commit c07f26c

Please sign in to comment.