
com.day.cq.mailer.commons.ProfileVariableLookup Maven / Gradle / Ivy
/*
* Copyright 1997-2010 Day Management AG
* Barfuesserplatz 6, 4001 Basel, Switzerland
* All Rights Reserved.
*
* This software is the confidential and proprietary information of
* Day Management AG, ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Day.
*/
package com.day.cq.mailer.commons;
import com.adobe.granite.security.user.UserProperties;
import com.day.cq.security.profile.Profile;
import org.apache.commons.lang.text.StrLookup;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jcr.RepositoryException;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import static java.beans.Introspector.IGNORE_ALL_BEANINFO;
import static java.beans.Introspector.getBeanInfo;
/**
* Implements a {@link org.apache.commons.lang.text.StrLookup StrLookup} that is
* looking up variables from a
* {@link org.apache.jackrabbit.api.security.user.Authorizable's UserProperties}.
*
* Additional variables can bee added via a {@link #put(String, String)}.
* If the key exists in the UserProperties, the value is overwritten with the newly set
* @see StrLookup
* @since 5.4
*/
public class ProfileVariableLookup extends StrLookup {
public final static String AUTHORIZABLE_ID = "authorizableID";
private final static Logger log = LoggerFactory.getLogger(ProfileVariableLookup.class);
private final Map overlay;
private Profile profile;
private final Map map;
private final Authorizable authorizable;
private final UserProperties userProperties;
/**
*
* @param profile The profile of the authorizable.
* @throws IntrospectionException when not being able to map a string class name to a Class object
*
* @deprecated
* Use {@link #ProfileVariableLookup(org.apache.jackrabbit.api.security.user.Authorizable, com.adobe.granite.security.user.UserProperties, java.util.Map)}
*/
public ProfileVariableLookup(Profile profile) throws IntrospectionException {
this(profile, new HashMap());
}
/**
*Use {@link #ProfileVariableLookup(org.apache.jackrabbit.api.security.user.Authorizable, com.adobe.granite.security.user.UserProperties, java.util.Map)}
*/
@Deprecated
public ProfileVariableLookup(Profile profile, Map overlays) throws IntrospectionException {
this.map = null;
this.authorizable = null;
this.overlay = null;
this.userProperties = null;
log.warn("Deprected. Use ProfileVariableLookup(Authorizable, UserProperties, Map) instead");
}
/**
*
* @param authorizable The authorizable for which variables need to be looked up.
* @param userProperties The properties of the authorizable.
* @param overlays
* @throws IntrospectionException when not being able to map a string class name to a Class object
*/
public ProfileVariableLookup(Authorizable authorizable, UserProperties userProperties, Map overlays) throws IntrospectionException {
this.authorizable = authorizable;
try {
if (authorizable != null && authorizable.getID() != null ) {
overlays.put("userUUID", authorizable.getID());
}
} catch (RepositoryException e) {
log.warn("Could not read authorizable ID to replace 'userUUID' in email", e);
}
this.map = new HashMap();
this.userProperties = userProperties;
//note this lines release the dependency to a commons.bean
for (PropertyDescriptor propDesc : getBeanInfo(authorizable.getClass(), IGNORE_ALL_BEANINFO).getPropertyDescriptors()) {
if (propDesc.getPropertyType().isAssignableFrom(String.class)) {
map.put(propDesc.getName(), propDesc.getReadMethod());
}
}
this.overlay = overlays;
}
/**
* {@inheritDoc}
*/
@Override
public String lookup(String variable) {
if (overlay.containsKey(variable)) {
return overlay.get(variable);
}
//add the id to the variable as there is no getter.
if (AUTHORIZABLE_ID.equals(variable)) {
if(profile != null)
return profile.getAuthorizable().getID();
else if(userProperties != null)
return userProperties.getAuthorizableID();
}
if (profile != null && profile.containsKey(variable)) {
return profile.get(variable, String.class);
}
if (authorizable != null ) {
try {
if(authorizable.hasProperty(variable) )
return authorizable.getProperty(variable)[0].getString();
} catch (Exception e) {
log.error(e.getMessage());
}
}
if(userProperties != null){
try {
if(userProperties.getProperty(variable) != null){
return userProperties.getProperty(variable);
}
} catch (RepositoryException e) {
log.error(e.getMessage());
}
}
if(map.containsKey(variable)) {
try {
return (String) map.get(variable).invoke(profile);
} catch (IllegalAccessException e) {
log.warn("Failed to access Profile Method for {}:{}",
variable, e);
} catch (InvocationTargetException e) {
log.warn("Failed to access Profile Method for {}:{}",
variable, e);
}
}
return null;
}
/**
* Adds or overwrites a mapping of this replacer.
* @param var name of the variable to map
* @param replace the value of the variable
*/
public void put(String var, String replace) {
overlay.put(var, replace);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy