forked from Adobe-Consulting-Services/acs-aem-commons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Content policy injector (Adobe-Consulting-Services#3305)
* 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
1 parent
9b66e71
commit 4105c3c
Showing
6 changed files
with
331 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...e/src/main/java/com/adobe/acs/commons/models/injectors/annotation/ContentPolicyValue.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,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; | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
...ommons/models/injectors/annotation/impl/ContentPolicyValueAnnotationProcessorFactory.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,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(); | ||
} | ||
|
||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...src/main/java/com/adobe/acs/commons/models/injectors/impl/ContentPolicyValueInjector.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,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; | ||
} | ||
|
||
|
||
} |
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
26 changes: 26 additions & 0 deletions
26
bundle/src/main/java/com/adobe/acs/commons/models/via/annotations/ContentPolicyViaType.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,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 { | ||
|
||
} |
88 changes: 88 additions & 0 deletions
88
...c/main/java/com/adobe/acs/commons/models/via/impl/ContentPolicyPropertiesViaProvider.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,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; | ||
|
||
} | ||
} |