MCMUtil.java 7.18 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.security.Authorizable
 *  com.day.cq.security.User
 *  com.day.cq.security.UserManager
 *  com.day.cq.tagging.Tag
 *  com.day.cq.wcm.api.Page
 *  javax.jcr.RepositoryException
 *  org.apache.jackrabbit.api.security.user.Authorizable
 *  org.apache.jackrabbit.api.security.user.User
 *  org.apache.jackrabbit.api.security.user.UserManager
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.commons.json.JSONArray
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.apache.sling.commons.json.io.JSONWriter
 */
package com.day.cq.mcm.api;

import com.day.cq.tagging.Tag;
import com.day.cq.wcm.api.Page;
import java.security.Principal;
import java.util.Map;
import java.util.Set;
import javax.jcr.RepositoryException;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.commons.json.io.JSONWriter;

public class MCMUtil {
    public static String removePossibleJcrContent(String path) {
        if (path.endsWith("/")) {
            path = path.substring(0, path.length() - 1);
        }
        if (path.endsWith("/jcr:content")) {
            path = path.substring(0, path.length() - "/jcr:content".length());
        }
        return path;
    }

    public static <T> void writeMapAsJsonObject(JSONWriter out, Map<String, T> m) throws JSONException {
        out.object();
        for (String key : m.keySet()) {
            out.key(key).value(m.get(key));
        }
        out.endObject();
    }

    public static void addPossibleTagsToValues(Page aPage, Map<String, Object> values) throws JSONException {
        Tag[] tags = aPage.getTags();
        if (tags != null && tags.length > 0) {
            JSONArray tagsArray = new JSONArray();
            for (int i2 = 0; i2 < tags.length; ++i2) {
                JSONObject tag = new JSONObject();
                tag.put("title", (Object)tags[i2].getTitle());
                tag.put("id", (Object)tags[i2].getTagID());
                tagsArray.put((Object)tag);
            }
            values.put("tags", (Object)tagsArray);
        }
    }

    public static void addPossibleSegmentsToValues(ValueMap content, Map<String, Object> values, ResourceResolver resourceResolver) throws JSONException {
        if (content.containsKey((Object)"cq:segments")) {
            JSONArray segArray = new JSONArray();
            if (content.get((Object)"cq:segments") instanceof String) {
                JSONObject seg = new JSONObject();
                seg.put("title", (Object)MCMUtil.getSegmentName(resourceResolver, content.get((Object)"cq:segments").toString()));
                seg.put("path", (Object)content.get((Object)"cq:segments").toString());
                segArray.put((Object)seg);
            } else {
                for (String segment : (String[])content.get("cq:segments", (Object)new String[0])) {
                    JSONObject seg = new JSONObject();
                    seg.put("title", (Object)MCMUtil.getSegmentName(resourceResolver, segment));
                    seg.put("path", (Object)segment);
                    segArray.put((Object)seg);
                }
            }
            values.put("segments", (Object)segArray);
        }
    }

    public static String getSegmentName(ResourceResolver resolver, String path) {
        Resource resource = resolver.getResource(path);
        if (resource == null || !resource.getResourceType().equals("cq:Page")) {
            return path;
        }
        Page page = (Page)resource.adaptTo(Page.class);
        return page.getTitle();
    }

    @Deprecated
    public static com.day.cq.security.User getUser(ResourceResolver rr, String userId) {
        com.day.cq.security.User retval = null;
        com.day.cq.security.UserManager userManager = (com.day.cq.security.UserManager)rr.adaptTo(com.day.cq.security.UserManager.class);
        if (userManager == null) {
            throw new RuntimeException("Could not adapt resource resolver to UserManager.");
        }
        com.day.cq.security.Authorizable authorizable = userManager.get(userId);
        if (authorizable != null && authorizable.isUser()) {
            retval = (com.day.cq.security.User)authorizable;
        }
        return retval;
    }

    public static User getAuthorizedUser(ResourceResolver rr, String userId) {
        User retval = null;
        UserManager userManager = (UserManager)rr.adaptTo(UserManager.class);
        if (userManager == null) {
            throw new RuntimeException("Could not adapt resource resolver to UserManager.");
        }
        Object authorizable = null;
        try {
            userManager.getAuthorizable(userId);
        }
        catch (RepositoryException e) {
            throw new RuntimeException((Throwable)e);
        }
        if (authorizable != null && !authorizable.isGroup()) {
            retval = authorizable;
        }
        return retval;
    }

    public static String getUserId(ResourceResolver rr, String userId) {
        String id = null;
        UserManager userManager = (UserManager)rr.adaptTo(UserManager.class);
        if (userManager == null) {
            throw new RuntimeException("Could not adapt resource resolver to UserManager.");
        }
        Object authorizable = null;
        try {
            userManager.getAuthorizable(userId);
        }
        catch (RepositoryException e) {
            throw new RuntimeException((Throwable)e);
        }
        if (authorizable != null && !authorizable.isGroup()) {
            try {
                id = authorizable.getPrincipal().getName();
            }
            catch (RepositoryException e) {
                throw new RuntimeException("Could not fetch name for the given UserId : " + userId);
            }
        }
        return id;
    }

    public static boolean isAnExperience(Resource resource) {
        if (resource != null) {
            return ResourceUtil.isA((Resource)resource, (String)"cq/personalization/components/teaserpage") || ResourceUtil.isA((Resource)resource, (String)"cq/personalization/components/offerpage") || ResourceUtil.isA((Resource)resource, (String)"mcm/components/newsletter/page") || ResourceUtil.isA((Resource)resource, (String)"mcm/campaign/components/campaign_newsletterpage") || ResourceUtil.isA((Resource)resource, (String)"wcm/designimporter/components/importerpage") || ResourceUtil.isA((Resource)resource, (String)"commerce/components/voucher") || ResourceUtil.isA((Resource)resource, (String)"commerce/components/promotion") || ResourceUtil.isA((Resource)resource, (String)"cq/personalization/components/experiencepage");
        }
        return false;
    }
}