Skip to content

Commit

Permalink
Content policy injector (Adobe-Consulting-Services#3305)
Browse files Browse the repository at this point in the history
* Add content policy injector / via provider

---------

Co-authored-by: Niek Raaijmakers <raaijmak@adobe.com>
Co-authored-by: david g <davidjgonzalez@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 2, 2024
1 parent 9b66e71 commit 4105c3c
Show file tree
Hide file tree
Showing 6 changed files with 331 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*-
* #%L
* ACS AEM Commons Bundle
* %%
* Copyright (C) 2013 - 2024 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.models.injectors.annotation;

import org.apache.sling.models.annotations.Source;
import org.apache.sling.models.annotations.injectorspecific.InjectionStrategy;
import org.apache.sling.models.spi.injectorspecific.InjectAnnotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@InjectAnnotation
@Source(ContentPolicyValue.SOURCE)
public @interface ContentPolicyValue {

/**
* Source value used for this annotation.
* @see Source
*/
String SOURCE = "design";

String name() default "";

/**
* Whether to use the page policy (true) or the component policy (false).
* @return
*/
boolean usePagePolicy() default false;

InjectionStrategy injectionStrategy() default InjectionStrategy.DEFAULT;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*-
* #%L
* ACS AEM Commons Bundle
* %%
* Copyright (C) 2013 - 2024 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.models.injectors.annotation.impl;

import com.adobe.acs.commons.models.injectors.annotation.ContentPolicyValue;
import org.apache.sling.models.annotations.injectorspecific.InjectionStrategy;
import org.apache.sling.models.spi.injectorspecific.AbstractInjectAnnotationProcessor2;
import org.apache.sling.models.spi.injectorspecific.InjectAnnotationProcessor2;
import org.apache.sling.models.spi.injectorspecific.StaticInjectAnnotationProcessorFactory;
import org.osgi.service.component.annotations.Component;

import java.lang.reflect.AnnotatedElement;
import java.util.Optional;

import static org.apache.commons.lang3.StringUtils.isBlank;

@Component(service = StaticInjectAnnotationProcessorFactory.class)
public class ContentPolicyValueAnnotationProcessorFactory implements StaticInjectAnnotationProcessorFactory {
@Override
public InjectAnnotationProcessor2 createAnnotationProcessor(AnnotatedElement element) {
return Optional.ofNullable(element.getAnnotation(ContentPolicyValue.class)).map(ContentPolicyValueAnnotationProcessorFactory.ContentPolicyPropertyAnnotationProcessor::new).orElse(null);
}

private class ContentPolicyPropertyAnnotationProcessor extends AbstractInjectAnnotationProcessor2 {
private final ContentPolicyValue annotation;

public ContentPolicyPropertyAnnotationProcessor(ContentPolicyValue annotation) {
this.annotation = annotation;
}

@Override
public InjectionStrategy getInjectionStrategy() {
return annotation.injectionStrategy();
}

@Override
public String getName() {
if (isBlank(annotation.name())) {
return null;
}
return annotation.name();
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*-
* #%L
* ACS AEM Commons Bundle
* %%
* Copyright (C) 2013 - 2024 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.models.injectors.impl;

import com.adobe.acs.commons.models.injectors.annotation.ContentPolicyValue;
import com.adobe.acs.commons.util.impl.ReflectionUtil;
import com.day.cq.wcm.api.designer.Designer;
import com.day.cq.wcm.api.designer.Style;
import com.day.cq.wcm.api.policies.ContentPolicy;
import com.day.cq.wcm.api.policies.ContentPolicyManager;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.scripting.SlingBindings;
import org.apache.sling.models.spi.DisposalCallbackRegistry;
import org.apache.sling.models.spi.Injector;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Type;

import static com.adobe.acs.commons.models.injectors.impl.InjectorUtils.*;
import static com.adobe.acs.commons.util.impl.ReflectionUtil.convertValueMapValue;

@Component(
property = {
Constants.SERVICE_RANKING + ":Integer=5500"
},
service = Injector.class
)
public class ContentPolicyValueInjector implements Injector {

@NotNull
@Override
public String getName() {
return ContentPolicyValue.SOURCE;
}

@Nullable
@Override
public Object getValue(Object adaptable, String name, Type declaredType, AnnotatedElement element,
DisposalCallbackRegistry callbackRegistry) {
ContentPolicy policy = getContentPolicy(adaptable);

if(policy != null){
return convertValueMapValue(policy.getProperties(), name, declaredType);
}

return null;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@
import com.day.cq.wcm.api.designer.Design;
import com.day.cq.wcm.api.designer.Designer;
import com.day.cq.wcm.api.designer.Style;
import com.day.cq.wcm.api.policies.ContentPolicy;
import com.day.cq.wcm.api.policies.ContentPolicyManager;
import com.day.cq.wcm.commons.WCMUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.scripting.SlingBindings;
import org.apache.sling.xss.XSSAPI;

import javax.jcr.Session;
import javax.servlet.ServletRequest;

/**
* Common methods for the injectors
Expand All @@ -54,6 +58,29 @@ public static Resource getResource(Object adaptable) {
return null;
}

public static ContentPolicy getContentPolicy(Object adaptable){
ResourceResolver resourceResolver = getResourceResolver(adaptable);
Resource resource = getResource(adaptable);

if (resourceResolver != null && resource != null) {

ContentPolicyManager manager = resourceResolver.adaptTo(ContentPolicyManager.class);

if(manager == null){
return null;
}

final ContentPolicy policy;
if(adaptable instanceof SlingHttpServletRequest){
SlingHttpServletRequest request = (SlingHttpServletRequest) adaptable;
return manager.getPolicy(resource, request);
}else{
return manager.getPolicy(resource);
}
}
return null;
}

public static ResourceResolver getResourceResolver(Object adaptable) {
if (adaptable instanceof SlingHttpServletRequest) {
return ((SlingHttpServletRequest) adaptable).getResourceResolver();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*-
* #%L
* ACS AEM Commons Bundle
* %%
* Copyright (C) 2013 - 2024 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.models.via.annotations;

import org.apache.sling.models.annotations.ViaProviderType;

public interface ContentPolicyViaType extends ViaProviderType {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*-
* #%L
* ACS AEM Commons Bundle
* %%
* Copyright (C) 2013 - 2024 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.models.via.impl;

import com.adobe.acs.commons.models.via.annotations.ContentPolicyViaType;
import com.day.cq.wcm.api.policies.ContentPolicy;
import com.day.cq.wcm.api.policies.ContentPolicyManager;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.ViaProviderType;
import org.apache.sling.models.spi.ViaProvider;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.adobe.acs.commons.models.injectors.impl.InjectorUtils.*;

@Component(service = ViaProvider.class)
public class ContentPolicyPropertiesViaProvider implements ViaProvider {

public static final String VIA_COMPONENT = "component";
public static final String VIA_RESOURCE_PAGE = "resourcePage";
public static final String VIA_CURRENT_PAGE = "currentPage";

private static final Logger logger = LoggerFactory.getLogger(ContentPolicyPropertiesViaProvider.class);


@Override
public Class<? extends ViaProviderType> getType() {
return ContentPolicyViaType.class;
}

@Override
public Object getAdaptable(Object original, String value) {


try{
ResourceResolver resourceResolver = getResourceResolver(original);
final Resource resource;

if(StringUtils.equals(value, VIA_CURRENT_PAGE)){
resource = getCurrentPage(original).getContentResource();
}else if(StringUtils.equals(value, VIA_RESOURCE_PAGE)){
resource = getResourcePage(original).getContentResource();
}else{
resource = getResource(original);
}

if (resourceResolver != null && resource != null) {

ContentPolicyManager manager = resourceResolver.adaptTo(ContentPolicyManager.class);

if(manager == null){
return null;
}

final ContentPolicy policy = manager.getPolicy(resource);
if(policy != null){
return policy.getProperties();
}
}

}catch(NullPointerException ex){
logger.error("Error while getting content policy properties", ex);
}

return null;

}
}

0 comments on commit 4105c3c

Please sign in to comment.