-
Notifications
You must be signed in to change notification settings - Fork 607
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a dispatcher filter that calculates the ttl header based on a pag… (
#2465) * Add a dispatcher filter that calculates the ttl header based on a page property * Change the logic so that it can work both on pages and custom resources * Make it possible to use sling http filters instead of the http whiteboard Co-authored-by: Roy Teeuwen <roy.teeuwen@kbc.be>
- Loading branch information
1 parent
14d769c
commit 5bad843
Showing
8 changed files
with
187 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
...va/com/adobe/acs/commons/http/headers/impl/PropertyBasedDispatcherMaxAgeHeaderFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
* #%L | ||
* ACS AEM Commons Bundle | ||
* %% | ||
* Copyright (C) 2013 - 2020 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. | ||
* #L% | ||
*/ | ||
|
||
package com.adobe.acs.commons.http.headers.impl; | ||
|
||
import com.day.cq.commons.PathInfo; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.apache.felix.scr.annotations.Component; | ||
import org.apache.felix.scr.annotations.ConfigurationPolicy; | ||
import org.apache.felix.scr.annotations.Properties; | ||
import org.apache.felix.scr.annotations.Property; | ||
import org.apache.jackrabbit.JcrConstants; | ||
import org.apache.sling.api.SlingHttpServletRequest; | ||
import org.apache.sling.api.resource.Resource; | ||
import org.apache.sling.api.resource.ResourceResolver; | ||
import org.apache.sling.commons.osgi.PropertiesUtil; | ||
import org.osgi.service.cm.ConfigurationException; | ||
import org.osgi.service.component.ComponentContext; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import java.util.Dictionary; | ||
|
||
import static com.adobe.acs.commons.http.headers.impl.AbstractDispatcherCacheHeaderFilter.PROP_DISPATCHER_FILTER_ENGINE; | ||
import static com.adobe.acs.commons.http.headers.impl.AbstractDispatcherCacheHeaderFilter.PROP_DISPATCHER_FILTER_ENGINE_SLING; | ||
|
||
@Component( | ||
label = "ACS AEM Commons - Dispacher Cache Control Header Property Based - Max Age", | ||
description = "Adds a Cache-Control max-age header to content based on property value to enable Dispatcher TTL support.", | ||
metatype = true, | ||
configurationFactory = true, | ||
policy = ConfigurationPolicy.REQUIRE) | ||
@Properties({ | ||
@Property( | ||
name = "webconsole.configurationFactory.nameHint", | ||
value = "Property name: {property.name}. Fallback Max Age: {max.age} ", | ||
propertyPrivate = true), | ||
@Property( | ||
name = PROP_DISPATCHER_FILTER_ENGINE, | ||
value = PROP_DISPATCHER_FILTER_ENGINE_SLING, | ||
propertyPrivate = true) | ||
}) | ||
public class PropertyBasedDispatcherMaxAgeHeaderFilter extends DispatcherMaxAgeHeaderFilter { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(PropertyBasedDispatcherMaxAgeHeaderFilter.class); | ||
|
||
@Property(label = "Property name", | ||
description = "Property to check on how long you want to cache this request") | ||
public static final String PROP_PROPERTY_NAME = "property.name"; | ||
|
||
@Property(label = "Inherit property value", | ||
description = "Property value to skip this filter and inherit another lower service ranking dispatcher ttl filter") | ||
public static final String PROP_INHERIT_PROPERTY_VALUE = "inherit.property.value"; | ||
|
||
private String propertyName; | ||
private String inheritPropertyValue; | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
protected boolean accepts(final HttpServletRequest request) { | ||
if (!super.accepts(request)) { | ||
log.debug("Not accepting request because it is not coming from the dispatcher."); | ||
return false; | ||
} | ||
if (request instanceof SlingHttpServletRequest) { | ||
SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request; | ||
Resource resource = getResource(request, slingRequest.getResourceResolver()); | ||
if (resource == null) { | ||
log.debug("Could not find resource for request, not accepting"); | ||
return false; | ||
} | ||
String headerValue = resource.getValueMap().get(propertyName, String.class); | ||
if (!StringUtils.isBlank(headerValue) && !inheritPropertyValue.equals(headerValue)) { | ||
log.debug("Found a max age header value for request {} that is not the inherit value, accepting", resource.getPath()); | ||
return true; | ||
} | ||
log.debug("PResource property is blank or INHERIT, not taking this filter "); | ||
return false; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
protected String getHeaderValue(HttpServletRequest request) { | ||
if (request instanceof SlingHttpServletRequest) { | ||
SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request; | ||
Resource resource = getResource(request, slingRequest.getResourceResolver()); | ||
if (resource != null) { | ||
String headerValue = resource.getValueMap().get(propertyName, String.class); | ||
return HEADER_PREFIX + headerValue; | ||
} | ||
} | ||
log.debug("An error occurred, falling back to the default max age value of this filter"); | ||
return super.getHeaderValue(request); | ||
} | ||
|
||
private Resource getResource(HttpServletRequest request, ResourceResolver resourceResolver) { | ||
PathInfo pathInfo = new PathInfo(request.getRequestURI()); | ||
Resource reqResource = resourceResolver.getResource(pathInfo.getResourcePath()); | ||
if (reqResource != null) { | ||
if (reqResource.isResourceType("cq:Page")) { | ||
return reqResource.getChild(JcrConstants.JCR_CONTENT); | ||
} | ||
return reqResource; | ||
} | ||
return null; | ||
} | ||
|
||
@SuppressWarnings("squid:S1149") | ||
protected final void doActivate(ComponentContext context) throws Exception { | ||
super.doActivate(context); | ||
Dictionary<?, ?> properties = context.getProperties(); | ||
|
||
propertyName = PropertiesUtil.toString(properties.get(PROP_PROPERTY_NAME), null); | ||
if (propertyName == null) { | ||
throw new ConfigurationException(PROP_PROPERTY_NAME, "Property name should be specified."); | ||
} | ||
inheritPropertyValue = PropertiesUtil.toString(properties.get(PROP_INHERIT_PROPERTY_VALUE), "INHERIT"); | ||
} | ||
|
||
public String toString() { | ||
return this.getClass().getName() + "[property-name:" + propertyName + ",fallback-max-age:" + super.getHeaderValue(null) + "]"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters