PagedHttpIterator.java 3.5 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.crypto.CryptoException
 *  javax.jcr.RepositoryException
 *  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.client;

import com.adobe.cq.aam.client.TestData;
import com.adobe.cq.aam.client.spi.AudienceManagerAccessDenied;
import com.adobe.granite.crypto.CryptoException;
import java.io.IOException;
import java.util.Iterator;
import javax.jcr.RepositoryException;
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 abstract class PagedHttpIterator<T>
implements Iterator<T> {
    private static final Logger LOGGER = LoggerFactory.getLogger(PagedHttpIterator.class);
    private int current = 0;
    private int total = -1;
    private JSONArray list;
    private int pos = 0;
    private int page = 0;
    private String offlineTestSuite;

    public PagedHttpIterator(String offlineSuite) {
        this.offlineTestSuite = offlineSuite;
    }

    protected abstract T getElement(JSONObject var1) throws JSONException;

    protected abstract JSONObject getJSONPageObject(int var1) throws CryptoException, IOException, RepositoryException, AudienceManagerAccessDenied;

    @Override
    public boolean hasNext() {
        try {
            if (this.loadList()) {
                return this.current < this.total;
            }
            return false;
        }
        catch (Exception e) {
            LOGGER.debug(e.getMessage(), (Throwable)e);
            return false;
        }
    }

    @Override
    public T next() {
        try {
            if (this.loadList()) {
                if (this.pos >= this.list.length()) {
                    ++this.page;
                    this.list = null;
                    if (!this.loadList()) {
                        return null;
                    }
                    this.pos = 0;
                }
                ++this.pos;
                ++this.current;
                return this.getElement(this.list.getJSONObject(this.pos - 1));
            }
            return null;
        }
        catch (Exception e) {
            LOGGER.debug(e.getMessage(), (Throwable)e);
            return null;
        }
    }

    private boolean loadList() throws IOException, JSONException, CryptoException, RepositoryException, AudienceManagerAccessDenied {
        if (this.list == null) {
            if (this.offlineTestSuite != null) {
                JSONObject object = new JSONObject(TestData.loadTestData(this.offlineTestSuite + "_folder_page" + this.page + ".json"));
                this.total = object.getInt("total");
                this.list = object.getJSONArray("list");
                return true;
            }
            JSONObject object = this.getJSONPageObject(this.page);
            if (object != null && object.has("total") && object.has("list")) {
                this.total = object.getInt("total");
                this.list = object.getJSONArray("list");
                LOGGER.debug("Loaded page {} at {} of {} ", new Object[]{this.page, this.current, this.total});
                return true;
            }
            return false;
        }
        return true;
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException();
    }
}