MetaDataRetrieverImpl.java 6.49 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.ReferenceCardinality
 *  org.apache.felix.scr.annotations.ReferencePolicy
 *  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.MetaDataExtender;
import com.adobe.cq.mcm.campaign.profile.MetaDataNode;
import com.adobe.cq.mcm.campaign.profile.MetaDataRetriever;
import com.adobe.cq.mcm.campaign.profile.Options;
import com.adobe.cq.mcm.campaign.profile.impl.AbstractMetaDataNode;
import com.adobe.cq.mcm.campaign.profile.impl.MetaDataNodeImpl;
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.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
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 MetaDataRetrieverImpl
implements MetaDataRetriever {
    @Reference
    private CampaignConnector connector;
    @Reference(policy=ReferencePolicy.DYNAMIC, cardinality=ReferenceCardinality.OPTIONAL_UNARY)
    private volatile MetaDataExtender extender;
    private final Logger log;

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

    private String getJSON(Resource page) 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 templateId = (String)values.get("acTemplateId", String.class);
        if (templateId == null) {
            throw new ConfigurationException("Missing template ID on page.");
        }
        HashMap<String, String> parameters = new HashMap<String, String>(4);
        parameters.put("delivery", templateId);
        CallResults results = null;
        try {
            results = this.connector.callFunction("amcGetDeliveryMetadata.jssp", parameters, credentials);
            json = results.bodyAsString();
            if (this.extender != null) {
                JSONObject root = new JSONObject(json);
                this.extender.extend(root);
                json = root.toString();
            }
        }
        catch (JSONException je) {
            throw new ACConnectorException("Could not process JSON.", (Throwable)je);
        }
        catch (IOException ioe) {
            throw new ACConnectorException("Could not determine response body.", ioe);
        }
        finally {
            if (results != null) {
                results.destroy();
            }
        }
        return json;
    }

    private JSONObject handleCompatibility(JSONObject root) throws JSONException {
        if (!root.has("schema")) {
            return root;
        }
        JSONObject schema = root.getJSONObject("schema");
        JSONObject content = root.getJSONObject("content");
        JSONObject newMetaData = new JSONObject();
        Iterator schemaIt = schema.keys();
        while (schemaIt.hasNext()) {
            String key = (String)schemaIt.next();
            JSONObject toProcess = schema.getJSONObject(key);
            newMetaData.put(key, (Object)toProcess);
            toProcess.put("content", content.get(key));
        }
        return newMetaData;
    }

    private void parseMetaData(JSONObject json, MetaDataNode metaData) throws JSONException {
        Iterator keys = json.keys();
        while (keys.hasNext()) {
            String key = (String)keys.next();
            JSONObject obj = json.getJSONObject(key);
            MetaDataNodeImpl node = new MetaDataNodeImpl(obj, key, metaData);
            metaData.add(node);
            if (!obj.has("content")) continue;
            this.parseMetaData(obj.getJSONObject("content"), node);
        }
    }

    @Override
    public MetaDataNode retrieve(Resource page) throws ACConnectorException {
        String jsonStr = this.getJSON(page);
        MetaDataRoot metaData = new MetaDataRoot();
        try {
            JSONObject json = new JSONObject(jsonStr);
            json = this.handleCompatibility(json);
            this.parseMetaData(json, metaData);
        }
        catch (JSONException je) {
            throw new ACConnectorException("Could not parse JSON response from Adobe Campaign server.", (Throwable)je);
        }
        return metaData;
    }

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

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

    protected void bindExtender(MetaDataExtender metaDataExtender) {
        this.extender = metaDataExtender;
    }

    protected void unbindExtender(MetaDataExtender metaDataExtender) {
        if (this.extender == metaDataExtender) {
            this.extender = null;
        }
    }

    private static class MetaDataRoot
    extends AbstractMetaDataNode {
        public MetaDataRoot() {
            super(null, null, null, null, null);
        }
    }

}