Last active
March 28, 2016 17:43
-
-
Save nateyolles/a400d401573380c07766 to your computer and use it in GitHub Desktop.
AEM com.adobe.granite.confmgr.Conf example of using a Service Account
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
package com.nateyolles.aem.slashconf.core.models; | |
import java.util.HashMap; | |
import java.util.Map; | |
import javax.annotation.PostConstruct; | |
import javax.inject.Inject; | |
import org.apache.sling.api.SlingHttpServletRequest; | |
import org.apache.sling.api.resource.LoginException; | |
import org.apache.sling.api.resource.Resource; | |
import org.apache.sling.api.resource.ResourceResolver; | |
import org.apache.sling.api.resource.ResourceResolverFactory; | |
import org.apache.sling.api.resource.ValueMap; | |
import org.apache.sling.models.annotations.Model; | |
import com.adobe.granite.confmgr.Conf; | |
import com.adobe.granite.confmgr.ConfMgr; | |
import com.day.cq.wcm.api.Page; | |
/** | |
* AEM com.adobe.granite.confmgr.Conf example of getting a configuration while | |
* respecting permissions by using a Resource Resolver and a Service Account. | |
*/ | |
@Model(adaptables = SlingHttpServletRequest.class) | |
public class ConfServiceAccountModel { | |
@Inject | |
private Page currentPage; | |
@Inject | |
private ConfMgr confMgr; | |
@Inject | |
private ResourceResolverFactory resolverFactory; | |
@PostConstruct | |
protected void init() { | |
final Map<String, Object> serviceParams = new HashMap<String, Object>(); | |
serviceParams.put(ResourceResolverFactory.SUBSERVICE, "readService"); | |
ResourceResolver serviceResolver = null; | |
try { | |
serviceResolver = resolverFactory.getServiceResourceResolver(serviceParams); | |
final Conf conf = confMgr.getConf(currentPage.adaptTo(Resource.class), serviceResolver); | |
final ValueMap fooSettings = conf.getItem("socialmedia/foo"); | |
final String name = fooSettings.get("name", String.class); | |
} catch (LoginException e) { | |
// TODO Auto-generated catch block | |
} finally { | |
if (serviceResolver != null && serviceResolver.isLive()) { | |
serviceResolver.close(); | |
serviceResolver = null; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment