PagedHttpIterator.java
3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* 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();
}
}