Skip to content

Commit

Permalink
SLING-10096 - Allow the SlingHttpServletRequest in the IncludeGenerat…
Browse files Browse the repository at this point in the history
…or or Sling dynamic includes

Extend the IncludeGenerator interface with the Sling Request object to give more flexibility to custom implementators of the interface

Co-authored-by: Santiago Garcia Pimentel <santiago.pimentel@netcentric.biz>
  • Loading branch information
santiagozky and santiagoGPNC committed Apr 13, 2021
1 parent 07fc5b9 commit e2c70ea
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 26 deletions.
6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
</parent>

<artifactId>org.apache.sling.dynamic-include</artifactId>
<version>3.2.1-SNAPSHOT</version>
<version>3.3.0-SNAPSHOT</version>

<name>Apache Sling Dynamic Include</name>
<description>Dynamic Include filter for Apache Sling</description>
Expand Down Expand Up @@ -70,6 +70,10 @@
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.component.annotations</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.annotation.versioning</artifactId>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.servlets.annotations</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.osgi.service.metatype.annotations.AttributeDefinition;
import org.osgi.service.metatype.annotations.Designate;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;
import org.osgi.service.metatype.annotations.Option;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -66,17 +65,13 @@ public class Configuration {
@AttributeDefinition(name="Resource types", description="Filter will replace components with selected resource types", cardinality = Integer.MAX_VALUE)
String include$_$filter_config_resource$_$types() default "";

@AttributeDefinition(name = "Include type", description = "Type of generated include tags", options = {
@Option(label = "Apache SSI", value = "SSI"),
@Option(label = "ESI", value = "ESI"),
@Option(label = "Javascript", value = "JSI")
})
@AttributeDefinition(name = "Include type", description = "Type of generated include tags")
String include$_$filter_config_include$_$type() default "SSI";

@AttributeDefinition(name="Add comment", description = "Add comment to included components")
boolean include$_$filter_config_add__comment() default false;

@AttributeDefinition(name = "Filter selector", description = "Selector used to mark included resources")
@AttributeDefinition(name = "Filter selector", description = "Selector used to mark included resources. The Built in option are 'SSI','ESI' and 'JSI'")
String include$_$filter_config_selector() default "nocache";

@AttributeDefinition(name = "Extension", description = "Extension to append to virtual resources to make caching possible")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
Expand All @@ -39,7 +38,7 @@
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.dynamicinclude.generator.IncludeGenerator;
import org.apache.sling.dynamicinclude.api.IncludeGenerator;
import org.apache.sling.dynamicinclude.generator.IncludeGeneratorWhiteboard;
import org.apache.sling.dynamicinclude.impl.UrlBuilder;
import org.apache.sling.servlets.annotations.SlingServletFilter;
Expand Down Expand Up @@ -97,7 +96,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
// Only write the includes markup if the required, configurable request
// header is present
if (shouldWriteIncludes(config, slingRequest)) {
String include = generator.getInclude(url);
String include = generator.getInclude(slingRequest,url);
LOG.debug(include);
writer.append(include);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*-
* 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.dynamicinclude.api;

import org.apache.sling.api.SlingHttpServletRequest;

/**
* Include generator interface
*/
public interface IncludeGenerator {
String getType();

/**
* Returns the string used to include the resource.
* For example, this might be a Javascript code that retrieves a snippet of html,
* an Apache SSI Tag, or an Edge Side Include Tag.
* <p>
* This method receives the sling request and an Url that has already be normalized as followed:
* <ul>
* <li>The query string has been removed</li>
* <li>The Url has been mapped using the ResourceResolver</li>
* <li>The jcr:content paths have been encoded to _jcr_content.</li>
* </ul>
*
* @param request the Sling request object
* @param normalizedUrl the requested url, normalized
* @return a String used to include the resource
**/
String getInclude(SlingHttpServletRequest request, String normalizedUrl);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,7 @@
* under the License.
*/

package org.apache.sling.dynamicinclude.generator;
@Version("1.0.0")
package org.apache.sling.dynamicinclude.api;

/**
* Include generator interface
*/
public interface IncludeGenerator {
String getType();

String getInclude(String url);
}
import org.osgi.annotation.versioning.Version;
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

import org.apache.sling.dynamicinclude.api.IncludeGenerator;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
package org.apache.sling.dynamicinclude.generator.types;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.sling.dynamicinclude.generator.IncludeGenerator;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.dynamicinclude.api.IncludeGenerator;
import org.osgi.service.component.annotations.Component;

/**
Expand All @@ -36,7 +37,7 @@ public String getType() {
}

@Override
public String getInclude(String url) {
public String getInclude(SlingHttpServletRequest request, String url) {
StringBuffer buf = new StringBuffer();
buf.append("<esi:include src=\"");
buf.append(StringEscapeUtils.escapeHtml4(url));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
import java.net.URL;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.sling.dynamicinclude.generator.IncludeGenerator;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.dynamicinclude.api.IncludeGenerator;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
Expand Down Expand Up @@ -69,7 +70,7 @@ public String getType() {
}

@Override
public String getInclude(String url) {
public String getInclude(SlingHttpServletRequest request, String url) {
if (template == null) {
throw new IllegalStateException("JSI generator hasn't be initialized");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

package org.apache.sling.dynamicinclude.generator.types;

import org.apache.sling.dynamicinclude.generator.IncludeGenerator;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.dynamicinclude.api.IncludeGenerator;
import org.osgi.service.component.annotations.Component;

/**
Expand All @@ -35,7 +36,7 @@ public String getType() {
}

@Override
public String getInclude(String url) {
public String getInclude(SlingHttpServletRequest request, String url) {
StringBuffer buf = new StringBuffer();
buf.append("<!--#include virtual=\"");
buf.append(escapeForApache(url));
Expand Down

0 comments on commit e2c70ea

Please sign in to comment.