You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by dk...@apache.org on 2018/05/02 21:28:39 UTC
[sling-org-apache-sling-scripting-jsp-taglib] branch master
updated: Resolved SLING-7643 - Adding support for CA Configs in JSP TagLib
This is an automated email from the ASF dual-hosted git repository.
dklco pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-jsp-taglib.git
The following commit(s) were added to refs/heads/master by this push:
new 5a26807 Resolved SLING-7643 - Adding support for CA Configs in JSP TagLib
5a26807 is described below
commit 5a268075a0196312bfe4656c238786f995cb99c7
Author: Dan Klco <dk...@apache.org>
AuthorDate: Wed May 2 17:28:35 2018 -0400
Resolved SLING-7643 - Adding support for CA Configs in JSP TagLib
---
pom.xml | 7 +-
.../sling/scripting/jsp/taglib/AbstractCATag.java | 124 +++++++++++++++++++++
.../jsp/taglib/GetCAConfigResourceTag.java | 51 +++++++++
.../jsp/taglib/GetCAConfigResourcesTag.java | 54 +++++++++
.../sling/scripting/jsp/taglib/GetResourceTag.java | 5 -
.../sling/scripting/jsp/taglib/SlingFunctions.java | 55 +++++++--
src/main/resources/META-INF/sling.tld | 94 +++++++++++++++-
7 files changed, 373 insertions(+), 17 deletions(-)
diff --git a/pom.xml b/pom.xml
index a00859c..d0e0144 100644
--- a/pom.xml
+++ b/pom.xml
@@ -114,6 +114,11 @@
<scope>compile</scope>
</dependency>
<dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.caconfig.api</artifactId>
+ <version>1.0.0</version>
+ </dependency>
+ <dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi.core</artifactId>
</dependency>
@@ -126,7 +131,7 @@
<dependency>
<groupId>org.owasp.esapi</groupId>
<artifactId>esapi</artifactId>
- <version>2.1.0</version>
+ <version>2.1.0.1</version>
<scope>compile</scope>
</dependency>
diff --git a/src/main/java/org/apache/sling/scripting/jsp/taglib/AbstractCATag.java b/src/main/java/org/apache/sling/scripting/jsp/taglib/AbstractCATag.java
new file mode 100644
index 0000000..2c69638
--- /dev/null
+++ b/src/main/java/org/apache/sling/scripting/jsp/taglib/AbstractCATag.java
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.sling.scripting.jsp.taglib;
+
+import javax.servlet.jsp.tagext.TagSupport;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.scripting.SlingBindings;
+import org.apache.sling.api.scripting.SlingScriptHelper;
+import org.apache.sling.caconfig.resource.ConfigurationResourceResolver;
+
+/**
+ * Abstract class for Tags for interactiving with Context-Aware Configuration
+ * resources to extend.
+ */
+public abstract class AbstractCATag extends TagSupport {
+
+ /** Serialization UID */
+ private static final long serialVersionUID = -3083138119694952836L;
+
+ /** The bucket. */
+ private String bucket;
+
+ /** The name. */
+ private String name;
+
+ /** The resource for which to get the configuration. */
+ private Resource resource;
+
+ /** The var. */
+ private String var;
+
+ /**
+ * @return the bucket
+ */
+ public String getBucket() {
+ return bucket;
+ }
+
+ /**
+ * @return the name
+ */
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * @return the resource
+ */
+ public Resource getResource() {
+ return resource;
+ }
+
+ /**
+ * Method for retrieving the ConfigurationResourceResolver from the page
+ * context.
+ *
+ * @return the resource resolver
+ */
+ protected ConfigurationResourceResolver getConfigurationResourceResolver() {
+ final SlingBindings bindings = (SlingBindings) pageContext.getRequest()
+ .getAttribute(SlingBindings.class.getName());
+ final SlingScriptHelper scriptHelper = bindings.getSling();
+ final ConfigurationResourceResolver resolver = scriptHelper.getService(ConfigurationResourceResolver.class);
+ return resolver;
+ }
+
+ /**
+ * Gets the variable name to which to save the list of children.
+ *
+ * @return the variable name
+ */
+ public String getVar() {
+ return var;
+ }
+
+ /**
+ * @param bucket
+ * the bucket to set
+ */
+ public void setBucket(String bucket) {
+ this.bucket = bucket;
+ }
+
+ /**
+ * @param name
+ * the name to set
+ */
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ /**
+ * @param resource
+ * the resource to set
+ */
+ public void setResource(Resource resource) {
+ this.resource = resource;
+ }
+
+ /**
+ * Sets the variable name to which to save the list of children.
+ *
+ * @param var
+ * the variable name
+ */
+ public void setVar(String var) {
+ this.var = var;
+ }
+}
diff --git a/src/main/java/org/apache/sling/scripting/jsp/taglib/GetCAConfigResourceTag.java b/src/main/java/org/apache/sling/scripting/jsp/taglib/GetCAConfigResourceTag.java
new file mode 100644
index 0000000..dfa4925
--- /dev/null
+++ b/src/main/java/org/apache/sling/scripting/jsp/taglib/GetCAConfigResourceTag.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.sling.scripting.jsp.taglib;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.caconfig.resource.ConfigurationResourceResolver;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Tag for retrieving Context-Aware Configuration resources for a specified
+ * resource, bucket and name.
+ */
+public class GetCAConfigResourceTag extends AbstractCATag {
+
+ /** Serialization UID */
+ private static final long serialVersionUID = -8191004532757820167L;
+
+ /** The Constant log. */
+ private static final Logger log = LoggerFactory.getLogger(GetCAConfigResourceTag.class);
+
+ @Override
+ public int doEndTag() {
+ log.trace("doEndTag");
+
+ ConfigurationResourceResolver caResourceResolver = this.getConfigurationResourceResolver();
+
+ log.debug("Finding configuration with {}/{} for {}", getBucket(), getName(), getResource());
+ Resource config = caResourceResolver.getResource(getResource(), getBucket(), getName());
+
+ log.debug("Saving {} to variable {}", config, getVar());
+ pageContext.setAttribute(getVar(), config);
+
+ return EVAL_PAGE;
+ }
+
+}
diff --git a/src/main/java/org/apache/sling/scripting/jsp/taglib/GetCAConfigResourcesTag.java b/src/main/java/org/apache/sling/scripting/jsp/taglib/GetCAConfigResourcesTag.java
new file mode 100644
index 0000000..d40e357
--- /dev/null
+++ b/src/main/java/org/apache/sling/scripting/jsp/taglib/GetCAConfigResourcesTag.java
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.sling.scripting.jsp.taglib;
+
+import java.util.Iterator;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.caconfig.resource.ConfigurationResourceResolver;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Tag for retrieving Context-Aware Configuration resource collection for a
+ * specified resource, bucket and name.
+ */
+public class GetCAConfigResourcesTag extends AbstractCATag {
+
+ /** Serialization UID */
+ private static final long serialVersionUID = -8191004532757820167L;
+
+ /** The Constant log. */
+ private static final Logger log = LoggerFactory.getLogger(GetCAConfigResourceTag.class);
+
+
+ @Override
+ public int doEndTag() {
+ log.trace("doEndTag");
+
+ ConfigurationResourceResolver caResourceResolver = this.getConfigurationResourceResolver();
+
+ log.debug("Finding configuration with {}/{} for {}", getBucket(), getName(), getResource());
+ Iterator<Resource> config = caResourceResolver.getResourceCollection(getResource(), getBucket(), getName()).iterator();
+
+ log.debug("Saving {} to variable {}", config, getVar());
+ pageContext.setAttribute(getVar(), config);
+
+ return EVAL_PAGE;
+ }
+
+}
diff --git a/src/main/java/org/apache/sling/scripting/jsp/taglib/GetResourceTag.java b/src/main/java/org/apache/sling/scripting/jsp/taglib/GetResourceTag.java
index 7251259..37ce6f8 100644
--- a/src/main/java/org/apache/sling/scripting/jsp/taglib/GetResourceTag.java
+++ b/src/main/java/org/apache/sling/scripting/jsp/taglib/GetResourceTag.java
@@ -84,11 +84,6 @@ public class GetResourceTag extends TagSupport {
return base;
}
- /*
- * (non-Javadoc)
- *
- * @see javax.servlet.jsp.tagext.TagSupport#doEndTag()
- */
/**
* Get the path of the resource to retrieve.
*
diff --git a/src/main/java/org/apache/sling/scripting/jsp/taglib/SlingFunctions.java b/src/main/java/org/apache/sling/scripting/jsp/taglib/SlingFunctions.java
index 8e66c26..e4d79cc 100644
--- a/src/main/java/org/apache/sling/scripting/jsp/taglib/SlingFunctions.java
+++ b/src/main/java/org/apache/sling/scripting/jsp/taglib/SlingFunctions.java
@@ -27,6 +27,7 @@ import org.apache.sling.api.adapter.Adaptable;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.caconfig.resource.ConfigurationResourceResolver;
import org.apache.sling.scripting.jsp.taglib.helpers.XSSSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -132,8 +133,44 @@ public class SlingFunctions {
}
/**
- * Function for retrieving all of the parent resources of a specified
- * resource, returning them in hierarchy order.
+ * Method for retrieving the CA Config resource for a specified resource
+ *
+ * @param resource
+ * the resource for which to retrieve the CA Config resource
+ * @param bucket
+ * the bucket name of the configuration to retrieve
+ * @param name
+ * the configuration name to retrieve
+ * @return the config resource
+ */
+ public static final Resource getCAConfigResource(Resource resource, String bucket, String name) {
+ log.trace("getCAConfigResource");
+ ConfigurationResourceResolver caResourceResolver = resource.getResourceResolver()
+ .adaptTo(ConfigurationResourceResolver.class);
+ return caResourceResolver.getResource(resource, bucket, name);
+ }
+
+ /**
+ * Method for retrieving the CA Config resources for a specified resource
+ *
+ * @param resource
+ * the resource for which to retrieve the CA Config resources
+ * @param bucket
+ * the bucket name of the configuration to retrieve
+ * @param name
+ * the configuration name to retrieve
+ * @return the config resources
+ */
+ public static final Iterator<Resource> getCAConfigResources(Resource resource, String bucket, String name) {
+ log.trace("getCAConfigResource");
+ ConfigurationResourceResolver caResourceResolver = resource.getResourceResolver()
+ .adaptTo(ConfigurationResourceResolver.class);
+ return caResourceResolver.getResourceCollection(resource, bucket, name).iterator();
+ }
+
+ /**
+ * Function for retrieving all of the parent resources of a specified resource,
+ * returning them in hierarchy order.
*
* @param current
* the current resource for which to retrieve the parents
@@ -156,8 +193,8 @@ public class SlingFunctions {
}
Collections.reverse(parents);
- int depth = Integer.parseInt(startDepth,10);
- if(depth <= parents.size()){
+ int depth = Integer.parseInt(startDepth, 10);
+ if (depth <= parents.size()) {
parents = parents.subList(depth, parents.size());
} else {
parents.clear();
@@ -208,8 +245,8 @@ public class SlingFunctions {
}
/**
- * Gets the value of the specified key from the ValueMap and either coerses
- * the value into the specified type or uses the specified type as a default
+ * Gets the value of the specified key from the ValueMap and either coerses the
+ * value into the specified type or uses the specified type as a default
* depending on the parameter passed in.
*
* @param properties
@@ -217,8 +254,7 @@ public class SlingFunctions {
* @param key
* the key for the value to retrieve
* @param defaultOrType
- * either the default value or the class to which to coerce the
- * value
+ * either the default value or the class to which to coerce the value
* @return
*/
@SuppressWarnings("unchecked")
@@ -243,8 +279,7 @@ public class SlingFunctions {
}
/**
- * Method for allowing the invocation of the Sling Resource listChildren
- * method.
+ * Method for allowing the invocation of the Sling Resource listChildren method.
*
* @param resource
* the resource of which to list the children
diff --git a/src/main/resources/META-INF/sling.tld b/src/main/resources/META-INF/sling.tld
index 878c91b..3c5a0fc 100644
--- a/src/main/resources/META-INF/sling.tld
+++ b/src/main/resources/META-INF/sling.tld
@@ -533,6 +533,98 @@
<tag>
<description>
+ Retrieves Context-Aware Configuration resource for a specified resource, bucket and name.
+ </description>
+ <name>getCAConfigResource</name>
+ <tag-class>
+ org.apache.sling.scripting.jsp.taglib.GetCAConfigResourceTag
+ </tag-class>
+ <tei-class>
+ org.apache.sling.scripting.jsp.taglib.tei.ResourceVariableTEI
+ </tei-class>
+ <body-content>empty</body-content>
+ <attribute>
+ <description>
+ The name of the variable to which to save the CA config resource.
+ </description>
+ <name>var</name>
+ <required>false</required>
+ <rtexprvalue>true</rtexprvalue>
+ </attribute>
+ <attribute>
+ <description>
+ The resource for which to retrieve CA Config
+ </description>
+ <name>resource</name>
+ <required>true</required>
+ <rtexprvalue>true</rtexprvalue>
+ </attribute>
+ <attribute>
+ <description>
+ The bucket name to retrieve for the config
+ </description>
+ <name>bucket</name>
+ <required>false</required>
+ <rtexprvalue>true</rtexprvalue>
+ </attribute>
+ <attribute>
+ <description>
+ The config name to retrieve
+ </description>
+ <name>name</name>
+ <required>false</required>
+ <rtexprvalue>true</rtexprvalue>
+ </attribute>
+ </tag>
+
+ <tag>
+ <description>
+ Retrieves Context-Aware Configuration resources for a specified resource, bucket and name.
+ </description>
+ <name>getCAConfigResources</name>
+ <tag-class>
+ org.apache.sling.scripting.jsp.taglib.GetCAConfigResourcesTag
+ </tag-class>
+ <tei-class>
+ org.apache.sling.scripting.jsp.taglib.tei.ResourceIteratorVariableTEI
+ </tei-class>
+ <body-content>empty</body-content>
+ <attribute>
+ <description>
+ The name of the variable to which to save the CA config resources
+ </description>
+ <name>var</name>
+ <required>false</required>
+ <rtexprvalue>true</rtexprvalue>
+ </attribute>
+ <attribute>
+ <description>
+ The resource for which to retrieve CA Configs
+ </description>
+ <name>resource</name>
+ <required>true</required>
+ <rtexprvalue>true</rtexprvalue>
+ </attribute>
+ <attribute>
+ <description>
+ The bucket name to retrieve for the configs
+ </description>
+ <name>bucket</name>
+ <required>false</required>
+ <rtexprvalue>true</rtexprvalue>
+ </attribute>
+ <attribute>
+ <description>
+ The config name to retrieve
+ </description>
+ <name>name</name>
+ <required>false</required>
+ <rtexprvalue>true</rtexprvalue>
+ </attribute>
+ </tag>
+
+ <tag>
+ <description>
Retrieves the parent of the resource or the absolute parent
at the level if specified.
</description>
@@ -630,7 +722,7 @@
<body-content>empty</body-content>
<attribute>
<description>
- The name of the variable to which to save the resource.
+ The name of the variable to which to save the value.
</description>
<name>var</name>
<required>false</required>
--
To stop receiving notification emails like this one, please contact
dklco@apache.org.