Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*************************************************************************
*
* ADOBE CONFIDENTIAL
* __________________
*
* Copyright 2012 Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
**************************************************************************/
package com.adobe.cq.social.srp.internal;
import javax.jcr.InvalidItemStateException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.management.ServiceNotFoundException;
import com.day.text.Text;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.NonExistingResource;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceWrapper;
import org.osgi.util.tracker.ServiceTracker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.adobe.cq.social.srp.SocialResource;
import com.adobe.cq.social.srp.SocialResourceProvider;
import com.adobe.cq.social.srp.config.SRPConfigurationError;
import com.adobe.cq.social.srp.config.SRPConfigurationFactory;
import com.adobe.cq.social.srp.config.SocialResourceConfiguration;
import com.adobe.granite.security.user.UserProperties;
import com.adobe.granite.security.user.UserPropertiesManager;
import com.day.cq.wcm.webservicesupport.Configuration;
import java.io.UnsupportedEncodingException;
/**
* Static social utilities.
*/
public final class SocialResourceUtils {
public static String GRAVATAR_PREFIX = "https://www.gravatar.com/avatar/";
/**
* The path to the user-generated content.
*/
public static final String PATH_UGC = "/content/usergenerated";
/**
* The SRP prefix.
*/
public static final String ASI_UGC_PREFIX = PATH_UGC + "/asi";
/**
* Default avatar URL.
*/
public static final String DEFAULT_AVATAR = "/etc.clientlibs/settings/wcm/designs/default/resources/social/avatar.png";
public static final String CC_ASIPATH = "asipath";
public static final String CC_HOST_URL = "hosturl";
public static final String CC_REPORT_SUITE = "reportsuite";
public static final String CC_CONSUMER_KEY = "consumerkey";
public static final String CC_SECRET_KEY = "secret";
/**
* enum for avartar's size.
*/
public static enum AVATAR_SIZE {
/**
* Avartar's size for 32 and for 48.
*/
THIRTY_TWO(32), FOURTY_EIGHT(48), EIGHTY(80), THIRTY_FOUR(34);
/**
* Avartar's size.
*/
private final int size;
/**
* Set avartar's size for 32 and for 48.
* @param s The size for avatar
*/
AVATAR_SIZE(final int s) {
size = s;
}
@Override
public String toString() {
return String.valueOf(this.size);
}
}
/**
* Where config definitions are stored. TODO: Temporary This will change once the config location settles down.
*/
public static final String SRP_CONFIGURATION_CONF_ROOT = "/conf/global/settings/community/srpc/";
public static final String SRP_CONFIGURATION_ROOT = "/etc/socialconfig/srpc/";
public static final String SRP_DEFAULT_CONFIG_CONF_PATH = SRP_CONFIGURATION_CONF_ROOT + "defaultconfiguration";
public static final String SRP_DEFAULT_CONFIG_PATH = SRP_CONFIGURATION_ROOT + "defaultconfiguration";
private static final Logger LOG = LoggerFactory.getLogger(SocialResourceUtils.class);
private static ServiceTracker tracker;
/**
* Don't ever instantiate this class.
*/
private SocialResourceUtils() {
}
/**
* Checks a path and an action based on the passed in resolver.
* @param resolver the resource resolver to use to evaluate the permissions
* @param path the path to the resource to check
* @param action the action to check
* @return True if the resolver is allowed to take the requested action, otherwise false
*/
public static Boolean checkPermission(final ResourceResolver resolver, final String path, final String action) {
if (StringUtils.isEmpty(path)) {
return false;
}
try {
final Session userSession = resolver.adaptTo(Session.class);
return userSession.hasPermission(path, action);
} catch (final RepositoryException e) {
return checkPermission(resolver, StringUtils.substringBeforeLast(path, "/"), action);
}
}
/**
* Gets the first cloud storage provider. Note: Caller must have read access to configuration storage.
* @param resolver the resolver to use
* @return the configuration
* @deprecated No longer using cloud storage, will return the default configuration.
*/
@Deprecated
public static Configuration getFirstCloudStorageConfig(final ResourceResolver resolver) {
Configuration config = null;
// Stack trace to locate the origin.
if (LOG.isDebugEnabled()) {
LOG.debug("STATIC getDefaultStorageConfig called. Please use SocialUtils", new Throwable(
"getDefaultStorageConfig"));
}
// Note: Caller has to be in admin session
Resource configResource = resolver.getResource(SocialResourceUtils.SRP_DEFAULT_CONFIG_PATH);
if (configResource == null) {
configResource = resolver.getResource(SocialResourceUtils.SRP_DEFAULT_CONFIG_CONF_PATH);
}
SocialResourceConfiguration srConfig = null;
try {
if (configResource != null) {
LOG.debug("config resource is: {}", configResource.getPath());
try {
srConfig = getConfigFactory().createConfiguration(configResource);
} catch (final ServiceNotFoundException e) {
throw new SRPConfigurationError(e);
}
}
} catch (final SRPConfigurationError e) {
LOG.error("Could not build configuration: ", e);
}
if (srConfig != null) {
config = new com.adobe.cq.social.srp.internal.Configuration(srConfig);
}
if (srConfig != null) {
LOG.debug("getFirstCloudStorageConfig asipath is: {} ", srConfig.getAsiPath());
}
return config;
}
/**
* Returns the user properties denoted by the given userId. The user props are looked for using the
* provided resource resolver, so as to ensure that the user properties are only accessible to users having the
* necessary access rights on the requested user properties.
* @param resolver The {@link ResourceResolver}.
* @param userId The user id for which to retrieve the user properties.
* @return The {@link UserProperties} or null if not found.
*/
public static UserProperties getUserProperties(final ResourceResolver resolver, final String userId) {
UserProperties userProperties = null;
final UserPropertiesManager upm = resolver.adaptTo(UserPropertiesManager.class);
if (null != upm && null != userId) {
try {
userProperties = upm.getUserProperties(userId, "profile");
} catch (final RepositoryException e) {
LOG.warn("User cannot access the profile.", e);
}
}
return userProperties;
}
/**
* Returns the userProperties avatar URL or {@link SocialResourceUtils#DEFAULT_AVATAR} if profile has no avatar defined.
* @param userProperties The userProperties (may be null)
* @param absoluteDefaultAvatar The absolute default avatar
* @param size The avatar size (for example, 32x48)
* @return the avatar URL or the default one.
*/
public static String getAvatar(final UserProperties userProperties, final String absoluteDefaultAvatar,
final AVATAR_SIZE size) {
return buildAvatar(userProperties, absoluteDefaultAvatar, size.toString());
}
/**
* Returns the userProperties avatar URL or {@link SocialResourceUtils#DEFAULT_AVATAR} if profile has no avatar defined.
* @param userProperties The userProperties (may be null)
* @param absoluteDefaultAvatar The absolute default avatar
* @param size The avatar size (for example, 32)
* @return the avatar URL or the default one.
*/
public static String getAvatar(final UserProperties userProperties, final String absoluteDefaultAvatar,
final String size) {
return buildAvatar(userProperties, absoluteDefaultAvatar, size);
}
private static String buildAvatar(final UserProperties userProperties, final String absoluteDefaultAvatar,
final String size) {
String avatar = DEFAULT_AVATAR;
int iSize = AVATAR_SIZE.THIRTY_TWO.ordinal();
try {
iSize = Integer.parseInt(size);
} catch (final NumberFormatException e) {
LOG.warn("Bad size passed in, defaulting size.", e);
}
if (userProperties != null) {
try {
final Resource resource = userProperties.getResource(UserProperties.PHOTOS + "/primary/image");
if (null != resource) {
avatar = resource.getPath() + ".prof.thumbnail." + Integer.toString(iSize) + ".png";
} else {
final String primaryMail = userProperties.getProperty(UserProperties.EMAIL);
if (primaryMail != null && !"".equals(primaryMail)) {
final String gravatar = getGravatar(primaryMail, absoluteDefaultAvatar);
if (gravatar != null) {
avatar = gravatar;
}
}
}
} catch (final RepositoryException e) {
LOG.error("getAvatar: error getting avatar: ", e);
}
}
return avatar;
}
/**
* Returns the gravatar URL.
* @param email The email address
* @param absoluteDefaultAvatar The absolute default avatar
* @return the gravatar URL or the default one.
*/
private static String getGravatar(String email, final String absoluteDefaultAvatar) {
if (StringUtils.isBlank(email) || StringUtils.isBlank(absoluteDefaultAvatar)) {
return null;
}
try {
email = Text.md5(email, "utf-8");
} catch (final UnsupportedEncodingException e) {
// do nothing
}
final StringBuilder sb = new StringBuilder();
sb.append(GRAVATAR_PREFIX);
sb.append(email);
sb.append("?d=").append("mm");
sb.append("&s=32");
sb.append("&r=g");
return sb.toString();
}
/**
* Check if path is non JCR based. Assume that all alternate storage paths have prefix
* {@link com.adobe.cq.social.srp.utilities.internal.InternalSocialResourceUtilities#ASI_UGC_PREFIX}, which is enforced in the cloudconfig UI.
* @param path the path to the ugc resource
* @return true if path is of alternate storage type (non jcr)
*/
public static boolean isCloudUGC(final String path) {
return StringUtils.startsWith(path, ASI_UGC_PREFIX);
}
/**
* Check if a given resource is an instance of SocialResource.
* @param res the resource to check
* @return true if resource is a SocialResource
*/
public static boolean isSocialResource(final Resource res) {
if (res instanceof SocialResource) {
return true;
} else if (res instanceof ResourceWrapper) {
return isSocialResource(((ResourceWrapper) res).getResource());
} else {
return false;
}
}
/**
* Check if a resource is a wrapped resource, and unwrap until a SocialResource is found.
* @param res resource to unwrap
* @return an unwrap resource or null if no SocialResource found
*/
public static SocialResource getSocialResource(final Resource res) {
if (res == null) {
return null;
}
if (res instanceof SocialResource) {
return (SocialResource) res;
} else if (res instanceof ResourceWrapper) {
return getSocialResource(((ResourceWrapper) res).getResource());
} else if (res instanceof NonExistingResource) {
LOG.warn("Resource {} is a NonExistingResource, returning null", res);
return null;
} else {
LOG.debug("Resource {} is unknown resource type, returning null", res);
return null;
}
}
/**
* Check to see if the specified exception caused by a jcr InvalidItemStateException. This exception is thrown if
* any of the changes to be persisted conflicts with a change already persisted through another session and the
* implementation is such that this conflict can only be detected at save-time and therefore was not detected
* earlier, at change-time.
* @param e The exception
* @return if caused by InvalidItemStateException
*/
public static boolean causeByInvalidItemStateException(final Exception e) {
Throwable cause = e;
do {
if (cause instanceof InvalidItemStateException) {
return true;
}
cause = cause.getCause();
} while (cause != null);
return false;
}
private static SRPConfigurationFactory getConfigFactory() throws ServiceNotFoundException {
final Object factory = tracker.getService();
if (factory != null && factory instanceof SRPConfigurationFactory) {
return (SRPConfigurationFactory) factory;
} else {
throw new ServiceNotFoundException();
}
}
// @Override
// public void start(final BundleContext context) throws Exception {
// synchronized (SocialResourceUtils.class) {
// if (tracker == null) {
// tracker = new ServiceTracker(context, SRPConfigurationFactory.class.getName(), null);
// tracker.open();
// }
// }
// }
public static String getUniqueUGCResourceName(final ResourceResolver resourceResolver, final String ugcPath,
final SocialResourceProvider resourceProvider, final String nodeName) {
Resource newResource = resourceProvider.getResource(resourceResolver, ugcPath + "/" + nodeName);
if (newResource != null) {
String leafNodeName;
int i = 0;
do {
leafNodeName = nodeName + "_" + String.valueOf(i);
i++;
newResource = resourceProvider.getResource(resourceResolver, ugcPath + "/" + leafNodeName);
} while (newResource != null);
return leafNodeName;
}
return nodeName;
}
// @Override
// public synchronized void stop(final BundleContext context) throws Exception {
// synchronized (SocialResourceUtils.class) {
// if (tracker != null) {
// tracker.close();
// }
// }
// }
}