Skip to content

Commit

Permalink
Resolved SLING-7643 - Adding support for CA Configs in JSP TagLib
Browse files Browse the repository at this point in the history
  • Loading branch information
klcodanr committed May 2, 2018
1 parent e349a7a commit 5a26807
Show file tree
Hide file tree
Showing 7 changed files with 373 additions and 17 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@
<!-- using compile scope to help IDEs -->
<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>
Expand All @@ -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>

Expand Down
124 changes: 124 additions & 0 deletions src/main/java/org/apache/sling/scripting/jsp/taglib/AbstractCATag.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,6 @@ public Resource getBase() {
return base;
}

/*
* (non-Javadoc)
*
* @see javax.servlet.jsp.tagext.TagSupport#doEndTag()
*/
/**
* Get the path of the resource to retrieve.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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;
Expand Down Expand Up @@ -132,8 +133,44 @@ public static final Resource getAbsoluteParent(Resource current, String level) {
}

/**
* 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
Expand All @@ -156,8 +193,8 @@ public static final Iterator<Resource> getParents(Resource current, String start
}
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();
Expand Down Expand Up @@ -208,17 +245,16 @@ public static final Resource getResource(ResourceResolver resolver, String path)
}

/**
* 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
* the ValueMap from which to retrieve the value
* @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")
Expand All @@ -243,8 +279,7 @@ public static final boolean hasChildren(Resource resource) {
}

/**
* 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
Expand Down

0 comments on commit 5a26807

Please sign in to comment.