Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some direct adaptions to aemobject #3324

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
*/
package com.adobe.acs.commons.models.injectors.impl;

import com.adobe.acs.commons.util.impl.ReflectionUtil;
import com.adobe.granite.asset.api.AssetManager;
import com.day.cq.commons.Externalizer;
import com.day.cq.search.QueryBuilder;
import com.day.cq.tagging.TagManager;
import com.day.cq.wcm.api.policies.ContentPolicyManager;
import org.apache.sling.xss.XSSAPI;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
Expand All @@ -36,27 +39,14 @@
import org.apache.sling.models.spi.DisposalCallbackRegistry;
import org.apache.sling.models.spi.Injector;
import org.osgi.framework.Constants;
import com.adobe.acs.commons.i18n.I18nProvider;
import com.adobe.acs.commons.models.injectors.annotation.AemObject;
import com.day.cq.i18n.I18n;

import javax.jcr.Session;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Type;
import java.util.Locale;

import static com.adobe.acs.commons.models.injectors.impl.InjectorUtils.getComponentContext;
import static com.adobe.acs.commons.models.injectors.impl.InjectorUtils.getCurrentDesign;
import static com.adobe.acs.commons.models.injectors.impl.InjectorUtils.getCurrentPage;
import static com.adobe.acs.commons.models.injectors.impl.InjectorUtils.getCurrentStyle;
import static com.adobe.acs.commons.models.injectors.impl.InjectorUtils.getDesigner;
import static com.adobe.acs.commons.models.injectors.impl.InjectorUtils.getPageManager;
import static com.adobe.acs.commons.models.injectors.impl.InjectorUtils.getResource;
import static com.adobe.acs.commons.models.injectors.impl.InjectorUtils.getResourceDesign;
import static com.adobe.acs.commons.models.injectors.impl.InjectorUtils.getResourcePage;
import static com.adobe.acs.commons.models.injectors.impl.InjectorUtils.getResourceResolver;
import static com.adobe.acs.commons.models.injectors.impl.InjectorUtils.getSession;
import static com.adobe.acs.commons.models.injectors.impl.InjectorUtils.getXssApi;
import static com.adobe.acs.commons.models.injectors.impl.InjectorUtils.*;
import static com.adobe.acs.commons.util.impl.ReflectionUtil.getClassOrGenericParam;

/**
Expand Down Expand Up @@ -129,8 +119,6 @@ public Object getValue(Object adaptable, String name, Type declaredType, Annotat
return getComponentContext(adaptable);
case PAGE_MANAGER:
return getPageManager(adaptable);
case TAG_MANAGER:
return getTagManager(adaptable);
case CURRENT_PAGE:
return getCurrentPage(adaptable);
case RESOURCE_PAGE:
Expand All @@ -149,19 +137,23 @@ public Object getValue(Object adaptable, String name, Type declaredType, Annotat
return resolveXssApi(adaptable);
case LOCALE:
return resolveLocale(adaptable);
case TAG_MANAGER:
return adaptFromResourceResolver(adaptable, TagManager.class);
case ASSET_MANAGER:
return adaptFromResourceResolver(adaptable, AssetManager.class);
case ASSET_MANAGER_OLD:
return adaptFromResourceResolver(adaptable, com.day.cq.dam.api.AssetManager.class);
case QUERY_BUILDER:
return adaptFromResourceResolver(adaptable, QueryBuilder.class);
case CONTENT_POLICY_MANAGER:
return adaptFromResourceResolver(adaptable,ContentPolicyManager.class);
case EXTERNALIZER:
return adaptFromResourceResolver(adaptable, Externalizer.class);
default:
return null;
}
}

private TagManager getTagManager(Object adaptable) {
final ResourceResolver resourceResolver = getResourceResolver(adaptable);

if(resourceResolver != null){
return resourceResolver.adaptTo(TagManager.class);
}
return null;
}

private Object resolveLocale(Object adaptable) {
final Page page = getResourcePage(adaptable);
Expand Down Expand Up @@ -204,6 +196,11 @@ private enum ObjectType {
SESSION,
LOCALE,
TAG_MANAGER,
QUERY_BUILDER,
CONTENT_POLICY_MANAGER,
ASSET_MANAGER,
ASSET_MANAGER_OLD,
EXTERNALIZER,
XSS_API;

private static final String RESOURCE_PAGE_STRING = "resourcePage";
Expand Down Expand Up @@ -235,6 +232,16 @@ public static ObjectType fromClassAndName(Class<?> classOrGenericParam, String n
return ObjectType.XSS_API;
} else if(classOrGenericParam.isAssignableFrom(Locale.class)){
return ObjectType.LOCALE;
} else if(classOrGenericParam.isAssignableFrom(AssetManager.class)){
return ObjectType.ASSET_MANAGER;
} else if(classOrGenericParam.isAssignableFrom(com.day.cq.dam.api.AssetManager.class)){
return ObjectType.ASSET_MANAGER_OLD;
} else if(classOrGenericParam.isAssignableFrom(QueryBuilder.class)){
return ObjectType.QUERY_BUILDER;
} else if(classOrGenericParam.isAssignableFrom(ContentPolicyManager.class)){
return ObjectType.CONTENT_POLICY_MANAGER;
} else if(classOrGenericParam.isAssignableFrom(Externalizer.class)){
return ObjectType.EXTERNALIZER;
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@
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 Down Expand Up @@ -81,6 +79,13 @@ public static ContentPolicy getContentPolicy(Object adaptable){
return null;
}

public static <T> T adaptFromResourceResolver(Object adaptable, Class<T> clazz){
ResourceResolver resourceResolver = getResourceResolver(adaptable);
if(resourceResolver != null){
return resourceResolver.adaptTo(clazz);
}
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
Expand Up @@ -28,7 +28,11 @@
import javax.inject.Inject;
import javax.jcr.Session;

import com.adobe.granite.asset.api.AssetManager;
import com.day.cq.commons.Externalizer;
import com.day.cq.search.QueryBuilder;
import com.day.cq.tagging.TagManager;
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;
Expand Down Expand Up @@ -62,6 +66,21 @@ public class AemObjectInjectorTest {
@Mock
private TagManager tagManager;

@Mock
private AssetManager assetManager;

@Mock
private com.day.cq.dam.api.AssetManager oldAssetManager;

@Mock
private QueryBuilder queryBuilder;

@Mock
private ContentPolicyManager contentPolicyManager;

@Mock
private Externalizer externalizer;

@Mock
private Page resourcePage;

Expand All @@ -80,6 +99,11 @@ public final void setUp() throws Exception {
context.registerService(PageManager.class,pageManager);
context.registerService(Designer.class,designer);
context.registerService(TagManager.class, tagManager);
context.registerService(AssetManager.class, assetManager);
context.registerService(com.day.cq.dam.api.AssetManager.class, oldAssetManager);
context.registerService(QueryBuilder.class, queryBuilder);
context.registerService(ContentPolicyManager.class, contentPolicyManager);
context.registerService(Externalizer.class, externalizer);
context.addModelsForClasses(TestResourceModel.class);


Expand All @@ -102,6 +126,12 @@ public final void testResourceInjection() {
assertNotNull(testResourceModel.getTagManager());
assertNotNull(testResourceModel.getDesigner());
assertNotNull(testResourceModel.getLocale());
assertNotNull(testResourceModel.getAssetManager());
assertNotNull(testResourceModel.getOldAssetManager());
assertNotNull(testResourceModel.getExternalizer());
assertNotNull(testResourceModel.getContentPolicyManager());
assertNotNull(testResourceModel.getQueryBuilder());

assertEquals(Locale.ENGLISH, testResourceModel.getLocale());

// TODO: Tests for the remaining injectable objects
Expand All @@ -120,6 +150,11 @@ public final void testSlingHttpServiceRequestInjection() {
assertNotNull(testResourceModel.getTagManager());
assertNotNull(testResourceModel.getDesigner());
assertNotNull(testResourceModel.getLocale());
assertNotNull(testResourceModel.getAssetManager());
assertNotNull(testResourceModel.getOldAssetManager());
assertNotNull(testResourceModel.getExternalizer());
assertNotNull(testResourceModel.getContentPolicyManager());
assertNotNull(testResourceModel.getQueryBuilder());
assertEquals(Locale.ENGLISH, testResourceModel.getLocale());
// TODO: Tests for the remaining injectable objects
}
Expand Down Expand Up @@ -163,6 +198,21 @@ public static class TestResourceModel {
@Inject @Optional
private Locale locale;

@Inject @Optional
private AssetManager assetManager;

@Inject @Optional
private com.day.cq.dam.api.AssetManager oldAssetManager;

@Inject @Optional
private Externalizer externalizer;

@Inject @Optional
private QueryBuilder queryBuilder;

@Inject @Optional
private ContentPolicyManager contentPolicyManager;

public Resource getResource() {
return resource;
}
Expand Down Expand Up @@ -218,5 +268,25 @@ public XSSAPI getXssApi() {
public Locale getLocale() {
return locale;
}

public AssetManager getAssetManager() {
return assetManager;
}

public com.day.cq.dam.api.AssetManager getOldAssetManager() {
return oldAssetManager;
}

public Externalizer getExternalizer() {
return externalizer;
}

public QueryBuilder getQueryBuilder() {
return queryBuilder;
}

public ContentPolicyManager getContentPolicyManager() {
return contentPolicyManager;
}
}
}
Loading