Skip to content

Commit

Permalink
Merge 3655545 into bd1438a
Browse files Browse the repository at this point in the history
  • Loading branch information
royteeuwen committed Feb 22, 2021
2 parents bd1438a + 3655545 commit 04aa9bc
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 19 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com)
<!-- Keep this up to date! After a release, change the tag name to the latest release -->
[unreleased changes details]: https://github.com/Adobe-Consulting-Services/acs-aem-commons/compare/acs-aem-commons-4.7.2...HEAD

### Added
- #2451 - Adding a new dispatcher cache control header filter based on the resource type of the page

### Fixed
- #2529 - Unable to find an implementation for interface acscommons.io.jsonwebtoken.io.Serializer using java.util.ServiceLoader

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@

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;
Expand Down Expand Up @@ -58,7 +55,7 @@
value = PROP_DISPATCHER_FILTER_ENGINE_SLING,
propertyPrivate = true)
})
public class PropertyBasedDispatcherMaxAgeHeaderFilter extends DispatcherMaxAgeHeaderFilter {
public class PropertyBasedDispatcherMaxAgeHeaderFilter extends ResourceBasedDispatcherMaxAgeHeaderFilter {

private static final Logger log = LoggerFactory.getLogger(PropertyBasedDispatcherMaxAgeHeaderFilter.class);

Expand All @@ -82,7 +79,7 @@ protected boolean accepts(final HttpServletRequest request) {
}
if (request instanceof SlingHttpServletRequest) {
SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
Resource resource = getResource(request, slingRequest.getResourceResolver());
Resource resource = getResource(slingRequest);
if (resource == null) {
log.debug("Could not find resource for request, not accepting");
return false;
Expand All @@ -92,7 +89,7 @@ protected boolean accepts(final HttpServletRequest request) {
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 ");
log.debug("Resource property is blank or INHERIT, not taking this filter ");
return false;
}
return false;
Expand All @@ -102,7 +99,7 @@ protected boolean accepts(final HttpServletRequest request) {
protected String getHeaderValue(HttpServletRequest request) {
if (request instanceof SlingHttpServletRequest) {
SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
Resource resource = getResource(request, slingRequest.getResourceResolver());
Resource resource = getResource(slingRequest);
if (resource != null) {
String headerValue = resource.getValueMap().get(propertyName, String.class);
return HEADER_PREFIX + headerValue;
Expand All @@ -112,18 +109,6 @@ protected String getHeaderValue(HttpServletRequest request) {
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* #%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 org.apache.jackrabbit.JcrConstants;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class ResourceBasedDispatcherMaxAgeHeaderFilter extends DispatcherMaxAgeHeaderFilter {

private static final Logger log = LoggerFactory.getLogger(ResourceBasedDispatcherMaxAgeHeaderFilter.class);

protected Resource getResource(SlingHttpServletRequest slingRequest) {
if (slingRequest.getResource().isResourceType("cq:Page")) {
log.trace("Found page resource, checking page content resource type");
return slingRequest.getResource().getChild(JcrConstants.JCR_CONTENT);
}
log.trace("Found non-page resource, checking request resource type");
return slingRequest.getResource();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* #%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 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.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
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.Arrays;
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 Resource Type Based - Max Age",
description = "Adds a Cache-Control max-age header to content based on resource type to enable Dispatcher TTL support.",
metatype = true,
configurationFactory = true,
policy = ConfigurationPolicy.REQUIRE)
@Properties({
@Property(
name = "webconsole.configurationFactory.nameHint",
value = "Max Age: {max.age} for Resource Types: [{resource.types}]",
propertyPrivate = true),
@Property(
name = PROP_DISPATCHER_FILTER_ENGINE,
value = PROP_DISPATCHER_FILTER_ENGINE_SLING,
propertyPrivate = true)
})
public class ResourceTypeBasedDispatcherMaxAgeHeaderFilter extends ResourceBasedDispatcherMaxAgeHeaderFilter {

private static final Logger log = LoggerFactory.getLogger(ResourceTypeBasedDispatcherMaxAgeHeaderFilter.class);

@Property(label = "Resource types",
description = "Resource types the page should have to use this filter.",
cardinality = Integer.MAX_VALUE)
public static final String PROP_RESOURCE_TYPES = "resource.types";

private String[] resourceTypes;

@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(slingRequest);
if (resource == null) {
log.debug("Could not find resource for request, not accepting");
return false;
} else {
return verifyResourceType(resource);
}
}
return false;
}

private boolean verifyResourceType(Resource resource) {
for (String resourceType : resourceTypes) {
if (resource.isResourceType(resourceType)) {
log.debug("Accepting request for resource: {} with resource type: {}.", resource.getPath(), resourceType);
return true;
}
}
return false;
}

@SuppressWarnings("squid:S1149")
protected final void doActivate(ComponentContext context) throws Exception {
super.doActivate(context);
Dictionary<?, ?> properties = context.getProperties();

resourceTypes = PropertiesUtil.toStringArray(properties.get(PROP_RESOURCE_TYPES));
if (resourceTypes == null || resourceTypes.length == 0) {
throw new ConfigurationException(PROP_RESOURCE_TYPES, "At least one resource type must be specified.");
}
}

public String toString() {
return this.getClass().getName() + "[resource-types:" + Arrays.asList(resourceTypes) + ",fallback-max-age:" + super.getHeaderValue(null) + "]";
}
}

0 comments on commit 04aa9bc

Please sign in to comment.