ProfileRetrieverImpl.java 6.08 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.webservicesupport.Configuration
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mcm.campaign.profile.impl;

import com.adobe.cq.mcm.campaign.profile.MetaDataNode;
import com.adobe.cq.mcm.campaign.profile.Profile;
import com.adobe.cq.mcm.campaign.profile.ProfileRetriever;
import com.adobe.cq.mcm.campaign.profile.impl.ProfileImpl;
import com.day.cq.mcm.campaign.ACConnectorException;
import com.day.cq.mcm.campaign.CallResults;
import com.day.cq.mcm.campaign.CampaignConnector;
import com.day.cq.mcm.campaign.CampaignCredentials;
import com.day.cq.mcm.campaign.ConfigurationException;
import com.day.cq.wcm.webservicesupport.Configuration;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service
@Component(metatype=0)
public class ProfileRetrieverImpl
implements ProfileRetriever {
    @Reference
    private CampaignConnector connector;
    private final Logger log;

    public ProfileRetrieverImpl() {
        this.log = LoggerFactory.getLogger(this.getClass());
    }

    private String getJSON(Resource page, String encryptedPK) throws ACConnectorException {
        String json;
        Resource resource = page.getChild("jcr:content");
        if (resource == null) {
            throw new ConfigurationException("Missing 'jcr:content' child node (path: " + page.getPath() + ").");
        }
        Configuration config = this.connector.getWebserviceConfig(page);
        CampaignCredentials credentials = this.connector.retrieveCredentials(config);
        ValueMap values = ResourceUtil.getValueMap((Resource)resource);
        String mappingId = (String)values.get("acMapping", String.class);
        if (mappingId == null) {
            throw new ConfigurationException("Missing delivery mapping on page.");
        }
        HashMap<String, String> parameters = new HashMap<String, String>(4);
        parameters.put("mapping", mappingId);
        parameters.put("resource", mappingId);
        parameters.put("encryptedPK", encryptedPK);
        CallResults results = null;
        try {
            this.log.debug("Requesting profile from Adobe Campaign ,,,");
            results = this.connector.callFunction("amcGetProfile.jssp", parameters, credentials);
            json = results.bodyAsString();
            this.log.debug("Retrieved successfully; JSON is: {}", (Object)json);
        }
        catch (IOException ioe) {
            throw new ACConnectorException("Could not determine response body.", ioe);
        }
        finally {
            if (results != null) {
                results.destroy();
            }
        }
        return json;
    }

    private void parseJSON(Profile profile, JSONObject jsonObj, MetaDataNode metaData) throws ACConnectorException {
        try {
            Iterator keys = jsonObj.keys();
            while (keys.hasNext()) {
                String key = (String)keys.next();
                Object value = jsonObj.get(key);
                if (value instanceof JSONObject) {
                    JSONObject objValue = (JSONObject)value;
                    MetaDataNode child = metaData.getChildByPath(key);
                    if (child != null) {
                        this.parseJSON(profile, objValue, child);
                        continue;
                    }
                    this.log.debug("Profile does not match meta data; no child '" + key + "' " + "available on path '" + metaData.getPath() + "'\n" + "profile: " + profile.toString() + "\n" + "meta data: " + metaData.toString());
                    continue;
                }
                MetaDataNode data = metaData.getChildByPath(key);
                if (data != null) {
                    profile.setValue(data, jsonObj.get(key));
                    continue;
                }
                this.log.debug("Profile does not match meta data; no child '" + key + "' " + "available on path '" + metaData.getPath() + "'\n" + "profile: " + profile.toString() + "\n" + "meta data: " + metaData.toString());
            }
        }
        catch (JSONException je) {
            throw new ACConnectorException("Could not parse JSON", (Throwable)je);
        }
    }

    private Profile parseJSON(String json, MetaDataNode metaData) throws ACConnectorException {
        try {
            ProfileImpl profile = new ProfileImpl();
            JSONObject jsonObject = new JSONObject(json);
            MetaDataNode child = metaData.getChildByPath("recipient");
            metaData = child != null ? child : metaData;
            this.parseJSON(profile, jsonObject, metaData);
            return profile;
        }
        catch (JSONException je) {
            throw new ACConnectorException("Could not parse JSON", (Throwable)je);
        }
    }

    @Override
    public Profile load(Resource page, String encryptedPK, MetaDataNode metaData) throws ACConnectorException {
        String json = this.getJSON(page, encryptedPK);
        return this.parseJSON(json, metaData);
    }

    protected void bindConnector(CampaignConnector campaignConnector) {
        this.connector = campaignConnector;
    }

    protected void unbindConnector(CampaignConnector campaignConnector) {
        if (this.connector == campaignConnector) {
            this.connector = null;
        }
    }
}