JsonResponseSegmentIdList.java 3.61 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.commons.lang.StringUtils
 *  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.adobe.cq.aam.demdex;

import com.adobe.cq.aam.client.spi.AudienceManagerConfiguration;
import com.adobe.cq.aam.client.spi.SegmentService;
import com.adobe.cq.aam.demdex.SegmentListException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
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;

public class JsonResponseSegmentIdList
implements Iterable<String> {
    private static final Logger LOGGER = LoggerFactory.getLogger(JsonResponseSegmentIdList.class);
    private Iterable<String> items;

    public JsonResponseSegmentIdList(SegmentService segmentService, AudienceManagerConfiguration config, JSONObject json) throws SegmentListException {
        this.items = this.parse(json, config.getDestinationCookieMap());
        try {
            LOGGER.debug("Items is {} from {} ", this.items, (Object)json.toString(2));
        }
        catch (JSONException e) {
            LOGGER.debug(e.getMessage(), (Throwable)e);
        }
    }

    private Set<String> parse(JSONObject jsonObject, Map<String, String> integrationCookies) throws SegmentListException {
        HashSet<String> segmentIds;
        block7 : {
            segmentIds = new HashSet<String>();
            try {
                if (jsonObject.has("stuff")) {
                    JSONArray stuff = jsonObject.getJSONArray("stuff");
                    boolean found = false;
                    for (int i = 0; i < stuff.length(); ++i) {
                        JSONObject v = stuff.getJSONObject(i);
                        if (!v.has("cv") || !v.has("cn") || !v.has("dmn")) continue;
                        String cn = v.getString("cn");
                        String cv = v.getString("cv");
                        String domain = v.getString("dmn");
                        LOGGER.debug("Found Cookie ID {} {} ", (Object)domain, (Object)cn);
                        if (!domain.equals(integrationCookies.get(cn))) continue;
                        LOGGER.debug("Found Acceptable Cookie {} {} ", (Object)domain, (Object)cn);
                        found = true;
                        if (!cv.startsWith("segs=")) continue;
                        for (String sid : StringUtils.split((String)cv, (String)",")) {
                            if (sid.startsWith("segs=")) {
                                sid = sid.substring("segs=".length());
                            }
                            LOGGER.debug("Found Segment ID {} ", (Object)sid);
                            segmentIds.add(sid);
                        }
                    }
                    if (!found) {
                        throw new SegmentListException("Empty Stuff object found in response, no segments present");
                    }
                    break block7;
                }
                throw new SegmentListException("No Stuff object found in response, no segments present");
            }
            catch (JSONException e) {
                LOGGER.error(e.getMessage(), (Throwable)e);
            }
        }
        return segmentIds;
    }

    @Override
    public Iterator<String> iterator() {
        return this.items.iterator();
    }
}