-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAssetLibrary.java
More file actions
359 lines (319 loc) · 10.6 KB
/
AssetLibrary.java
File metadata and controls
359 lines (319 loc) · 10.6 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package com.contentstack.sdk;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;
import java.util.*;
import java.util.logging.Logger;
import static com.contentstack.sdk.Constants.ENVIRONMENT;
/**
* The Asset library is used to get list of assets available in the stack, We can apply filters on the assets also. The
* Get all assets request fetches the list of all the assets of a particular stack. It returns the content of each asset
* in JSON format.
*/
public class AssetLibrary implements INotifyClass {
protected static final Logger logger = Logger.getLogger(AssetLibrary.class.getSimpleName());
protected final JSONObject urlQueries;
protected Stack stackInstance;
protected LinkedHashMap<String, Object> headers;
protected FetchAssetsCallback callback;
protected int count;
protected AssetLibrary() {
this.urlQueries = new JSONObject();
}
protected void setStackInstance(@NotNull Stack stack) {
this.stackInstance = stack;
this.headers = stack.headers;
}
//Sanitization of keys
private boolean isValidKey(String key) {
return key.matches("^[a-zA-Z0-9_.]+$");
}
//Sanitization of values
private boolean isValidValue(Object value) {
if(value instanceof String){
return ((String) value).matches("^[a-zA-Z0-9_.\\-\\s]+$");
}
return true;
}
//Sanitization of values list
private boolean isValidValueList(Object[] values) {
for (Object value : values) {
if (value instanceof String) {
if (!((String) value).matches("^[a-zA-Z0-9_.\\-\\s]+$")) {
return false;
}
}
}
return true;
}
/**
* Sets header.
*
* @param headerKey the header key
* @param headerValue the header value
*/
public void setHeader(@NotNull String headerKey, @NotNull String headerValue) {
this.headers.put(headerKey, headerValue);
}
/**
* Remove header.
*
* @param headerKey the header key
*/
public void removeHeader(@NotNull String headerKey) {
if (!headerKey.isEmpty()) {
this.headers.remove(headerKey);
}
}
/**
* Sort asset library.
*
* @param keyOrderBy the key order by
* @param orderby the orderby
* @return the asset library
*/
public AssetLibrary sort(String keyOrderBy, ORDERBY orderby) {
if (orderby == ORDERBY.ASCENDING) {
urlQueries.put("asc", keyOrderBy);
} else {
urlQueries.put("desc", keyOrderBy);
}
return this;
}
/**
* Include count asset library.
*
* @return the asset library
*/
public AssetLibrary includeCount() {
urlQueries.put("include_count", "true");
return this;
}
/**
* Include relative url asset library.
*
* @return the asset library
*/
public AssetLibrary includeRelativeUrl() {
urlQueries.put("relative_urls", "true");
return this;
}
/**
* Retrieve the published content of the fallback locale if an entry is not localized in specified locale
*
* @return {@link AssetLibrary} object, so you can chain this call. <br>
* <br>
* <b>Example :</b><br>
*
* <pre class="prettyprint">
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
* AssetLibrary assetLibObject = stack.assetLibrary();
* AssetLibrary.includeFallback();
* </pre>
*/
public AssetLibrary includeFallback() {
urlQueries.put("include_fallback", true);
return this;
}
/**
* Retrieve Metadata in the response
*
* @return {@link AssetLibrary} object, so you can chain this call. <br>
* <br>
* <b>Example :</b><br>
*
* <pre class="prettyprint">
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
* AssetLibrary assetLibObject = stack.includeOwner();
* AssetLibrary.includeMetadata();
* </pre>
*/
public AssetLibrary includeMetadata() {
urlQueries.put("include_metadata", true);
return this;
}
/**
* Gets count.
*
* @return the count
*/
public int getCount() {
return count;
}
/**
* Add param assetlibrary.
*
* @param paramKey the param key
* @param paramValue the param value
* @return the assetlibrary
*
* <br>
* <br>
* <b>Example :</b><br>
*
* <pre class="prettyprint">
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
* AssetLibrary assetLibObject = stack.assetlibrary();
* assetLibObject.addParam();
* </pre>
*/
public AssetLibrary addParam(@NotNull String paramKey, @NotNull Object paramValue) {
if (isValidKey(paramKey) && isValidValue(paramValue)) {
urlQueries.put(paramKey, paramValue);
} else {
logger.warning("Invalid key or value");
}
return this;
}
/**
* Remove param key assetlibrary.
*
* @param paramKey the param key
* @return the assetlibrary
*
* <br>
* <br>
* <b>Example :</b><br>
*
* <pre class="prettyprint">
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
* AssetLibrary assetLibObject = stack.assetlibrary();
* assetLibObject.removeParam(paramKey);
* </pre>
*/
public AssetLibrary removeParam(@NotNull String paramKey){
if(isValidKey(paramKey)) {
if(urlQueries.has(paramKey)){
urlQueries.remove(paramKey);
}
} else {
logger.warning("Invalid key");
}
return this;
}
/**
* The number of objects to skip before returning any.
*
* @param number No of objects to skip from returned objects
* @return {@link Query} object, so you can chain this call.
* <p>
* <b> Note: </b> The skip parameter can be used for pagination,
* "skip" specifies the number of objects to skip in the response. <br>
*
* <br>
* <br>
* <b>Example :</b><br>
*
* <pre class="prettyprint">
* Stack stack = Contentstack.stack( "apiKey", "deliveryToken", "environment");
* AssetLibrary assetLibObject = stack.assetlibrary.skip(4);<br>
* </pre>
*/
public AssetLibrary skip (@NotNull int number) {
urlQueries.put("skip",number);
return this;
}
/**
* A limit on the number of objects to return.
*
* @param number No of objects to limit.
* @return {@link Query} object, so you can chain this call.
* <p>
* <b> Note:</b> The limit parameter can be used for pagination, "
* limit" specifies the number of objects to limit to in the response. <br>
*
* <br>
* <br>
* <b>Example :</b><br>
*
* <pre class="prettyprint">
* Stack stack = Contentstack.stack( "apiKey", "deliveryToken", "environment");
* AssetLibrary assetLibObject = stack.assetlibrary.limit(4);<br>
* </pre>
*/
public AssetLibrary limit (@NotNull int number) {
urlQueries.put("limit", number);
return this;
}
/**
* Fetch all.
*
* @param callback the callback
*/
public void fetchAll(FetchAssetsCallback callback) {
this.callback = callback;
urlQueries.put(ENVIRONMENT, headers.get(ENVIRONMENT));
fetchFromNetwork("assets", urlQueries, headers, callback);
}
private void fetchFromNetwork(String url, JSONObject urlQueries, LinkedHashMap<String, Object> headers,
FetchAssetsCallback callback) {
if (callback != null) {
HashMap<String, Object> urlParams = getUrlParams(urlQueries);
new CSBackgroundTask(this, stackInstance, Constants.FETCHALLASSETS, url, headers, urlParams,
Constants.REQUEST_CONTROLLER.ASSETLIBRARY.toString(), callback);
}
}
private HashMap<String, Object> getUrlParams(JSONObject urlQueriesJSON) {
HashMap<String, Object> hashMap = new HashMap<>();
if (urlQueriesJSON != null && urlQueriesJSON.length() > 0) {
Iterator<String> iter = urlQueriesJSON.keys();
while (iter.hasNext()) {
String key = iter.next();
Object value = urlQueriesJSON.opt(key);
if(isValidKey(key) && isValidValue(value)) {
hashMap.put(key, value);
}
}
}
return hashMap;
}
@Override
public void getResult(Object object, String controller) {
logger.warning("No implementation required");
}
@Override
public void getResultObject(List<Object> objects, JSONObject jsonObject, boolean isSingleEntry) {
if (jsonObject != null && jsonObject.has("count")) {
count = jsonObject.optInt("count");
}
List<Asset> assets = new ArrayList<>();
// if (objects == null || objects.isEmpty()) {
// System.out.println("Objects list is null or empty");
// }
if (objects != null && !objects.isEmpty()) {
for (Object object : objects) {
AssetModel model = (AssetModel) object;
Asset asset = stackInstance.asset();
asset.contentType = model.contentType;
asset.fileSize = model.fileSize;
asset.uploadUrl = model.uploadUrl;
asset.fileName = model.fileName;
asset.json = model.json;
asset.assetUid = model.uploadedUid;
asset.setTags(model.tags);
assets.add(asset);
}
}
// else {
// System.out.println("Object is not an instance of AssetModel");
// }
if (callback != null) {
callback.onRequestFinish(ResponseType.NETWORK, assets);
}
}
/**
* The enum Orderby.
*/
public enum ORDERBY {
ASCENDING, DESCENDING
}
public AssetLibrary where(String key, String value) {
if(isValidKey(key) && isValidValue(value)){
JSONObject queryParams = new JSONObject();
queryParams.put(key,value);
urlQueries.put("query", queryParams);
} else {
throw new IllegalArgumentException("Invalid key or value");
}
return this;
}
}