SubscriptionManagerImpl.java 6.88 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.JSONArray
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.mcm.campaign.profile.impl;

import com.day.cq.mcm.campaign.ACConnectorException;
import com.day.cq.mcm.campaign.CallResults;
import com.day.cq.mcm.campaign.CampaignCredentials;
import com.day.cq.mcm.campaign.ConfigurationException;
import com.day.cq.mcm.campaign.GenericCampaignConnector;
import com.day.cq.mcm.campaign.profile.Subscription;
import com.day.cq.mcm.campaign.profile.Subscriptions;
import com.day.cq.mcm.campaign.profile.SubscriptionsManager;
import com.day.cq.mcm.campaign.profile.impl.SubscriptionImpl;
import com.day.cq.mcm.campaign.profile.impl.SubscriptionsImpl;
import com.day.cq.wcm.webservicesupport.Configuration;
import java.io.IOException;
import java.util.HashMap;
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.JSONArray;
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 SubscriptionManagerImpl
implements SubscriptionsManager {
    private static final int BATCH_SIZE = 100;
    @Reference
    private GenericCampaignConnector connector;
    private final Logger log;

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

    private String getJSON(Resource page, int offset, int limit) 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("searchTerm", "");
        parameters.put("startLine", Integer.toString(offset));
        parameters.put("lineCount", Integer.toString(limit));
        CallResults results = null;
        try {
            results = this.connector.callFunction("amcGetServices.jssp", parameters, credentials);
            json = results.bodyAsString();
        }
        catch (IOException ioe) {
            throw new ACConnectorException("Could not determine response body.", ioe);
        }
        finally {
            if (results != null) {
                results.destroy();
            }
        }
        return json;
    }

    private String getNextJSON(Resource page, String nextPath) throws ACConnectorException {
        String json;
        Configuration config = this.connector.getWebserviceConfig(page);
        CampaignCredentials credentials = this.connector.retrieveCredentials(config);
        int qsPos = nextPath.indexOf("?");
        String path = nextPath;
        String queryString = null;
        if (qsPos > 0) {
            path = nextPath.substring(0, qsPos);
            queryString = nextPath.substring(qsPos + 1);
        }
        CallResults results = null;
        try {
            results = this.connector.callGenericWithBasicAuth(path, queryString, credentials);
            json = results.bodyAsString();
        }
        catch (IOException ioe) {
            throw new ACConnectorException("Could not determine response body.", ioe);
        }
        finally {
            if (results != null) {
                results.destroy();
            }
        }
        return json;
    }

    @Override
    public Subscriptions retrieve(Resource page) throws ACConnectorException {
        SubscriptionsImpl subscriptions = new SubscriptionsImpl();
        boolean isDone = false;
        int offset = 0;
        int batchSize = 100;
        boolean isPaging = true;
        boolean isFirst = true;
        String nextUrl = null;
        while (!isDone) {
            String jsonStr = isPaging ? this.getJSON(page, offset, batchSize) : this.getNextJSON(page, nextUrl);
            try {
                JSONObject json = new JSONObject(jsonStr);
                JSONArray services = json.has("services") ? json.getJSONArray("services") : json.getJSONArray("content");
                int serviceCnt = services.length();
                for (int s = 0; s < serviceCnt; ++s) {
                    JSONObject service = services.getJSONObject(s);
                    String name = service.getString("name");
                    String label = service.getString("label");
                    subscriptions.add(new SubscriptionImpl(name, label));
                }
                if (isFirst) {
                    if (json.has("next")) {
                        isPaging = false;
                    }
                    isFirst = false;
                }
                if (isPaging) {
                    isDone = serviceCnt < batchSize;
                    offset += serviceCnt;
                    continue;
                }
                if (!json.has("next") || json.isNull("next")) {
                    isDone = true;
                    continue;
                }
                JSONObject next = json.getJSONObject("next");
                if (next.has("href")) {
                    nextUrl = next.getString("href");
                }
                if (nextUrl != null) continue;
                isDone = true;
                continue;
            }
            catch (JSONException je) {
                throw new ACConnectorException("Invalid JSON", (Throwable)je);
            }
        }
        return subscriptions;
    }

    protected void bindConnector(GenericCampaignConnector genericCampaignConnector) {
        this.connector = genericCampaignConnector;
    }

    protected void unbindConnector(GenericCampaignConnector genericCampaignConnector) {
        if (this.connector == genericCampaignConnector) {
            this.connector = null;
        }
    }
}