-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAsset.java
More file actions
593 lines (557 loc) · 17.2 KB
/
Asset.java
File metadata and controls
593 lines (557 loc) · 17.2 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
package com.contentstack.sdk;
import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONObject;
import retrofit2.Retrofit;
import lombok.Getter;
import lombok.Setter;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.logging.Logger;
import static com.contentstack.sdk.Constants.ENVIRONMENT;
import static com.contentstack.sdk.Constants.parseDate;
/**
* <a href=
* "https://www.contentstack.com/docs/content-managers/working-with-assets/about-assets">Assets</a>
* refer to
* all the media files (images, videos, PDFs, audio files, and so on) uploaded
* in your Contentstack repository for
* future use. These files can be attached and used in multiple entries.
* <p>
* You can now pass the branch header in the API request to fetch or manage
* modules located within specific branches of
* the stack.
*
* @author Shailesh Mishra
* @version 1.0.0
* @since 01-11-2017
*/
@Getter
@Setter
public class Asset {
protected static final Logger logger = Logger.getLogger(Asset.class.getSimpleName());
protected final JSONObject urlQueries = new JSONObject();
protected String assetUid = null;
protected String contentType = null;
protected String fileSize = null;
protected String fileName = null;
protected String uploadUrl = null;
protected JSONObject json = null;
protected String[] tagsArray = null;
protected LinkedHashMap<String, Object> headers;
protected Stack stackInstance;
protected Retrofit retrofit;
protected Asset() {
this.headers = new LinkedHashMap<>();
}
protected Asset(@NotNull String assetUid) {
this.assetUid = assetUid;
this.headers = new LinkedHashMap<>();
}
protected void setStackInstance(@NotNull Stack stack) {
this.stackInstance = stack;
this.headers = stack.headers;
}
/**
* Configure asset.
*
* @param jsonObject the json object
* @return the asset
*/
public Asset configure(JSONObject jsonObject) {
AssetModel model;
model = new AssetModel(jsonObject, true);
this.contentType = model.contentType;
this.fileSize = model.fileSize;
this.uploadUrl = model.uploadUrl;
this.fileName = model.fileName;
this.json = model.json;
this.assetUid = model.uploadedUid;
this.setTags(model.tags);
return this;
}
/**
* Sets header.
*
* @param headerKey the header key
* @param headerValue the header value
*
* <br>
* <br>
* <b>Example :</b><br>
* <p>
* <code>
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment"); Asset asset =
* stack.asset(asset_uid); asset.setHeader();
* </code>
*/
public void setHeader(@NotNull String headerKey, @NotNull String headerValue) {
headers.put(headerKey, headerValue);
}
/**
* The function removes a header from a collection of headers based on a given
* key.
*
* @param headerKey The parameter "headerKey" is a String that represents the
* key of the header to
* be removed.
* <br>
* <b>Example :</b>
* <p>
* <code>
* Stack stack = Contentstack.stack("apiKey", "deliveryToken",
* "environment"); Asset asset =
* stack.asset(asset_uid); asset.removeHeader();
* <code>
*/
public void removeHeader(@NotNull String headerKey) {
headers.remove(headerKey);
}
protected void setUid(@NotNull String assetUid) {
if (!assetUid.isEmpty()) {
this.assetUid = assetUid;
}
}
/**
* Gets asset uid.
*
* @return the asset uid
*
* <br>
* <br>
* <b>Example :</b><br>
*
* <code>
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment"); Asset asset = stack.asset(asset_uid);
* asset.fetch(new FetchResultCallback() { @Override public void onCompletion(ResponseType responseType, Error
* error) { asset.getAssetUid(); } });
*
* </code>
*/
public String getAssetUid() {
return assetUid;
}
/**
* Gets file type.
*
* @return the file type
*
* <br>
* <br>
* <b>Example :</b><br>
*
* <pre class="prettyprint">
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
* Asset asset = stack.asset(asset_uid);
* asset.fetch(new FetchResultCallback() {
* @Override
* public void onCompletion(ResponseType responseType, Error error) {
* asset.getFileType();
* }
* });
* </pre>
*/
public String getFileType() {
return contentType;
}
/**
* Gets file size.
*
* @return the file size
*
* <br>
* <br>
* <b>Example :</b><br>
*
* <pre class="prettyprint">
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
* Asset asset = stack.asset(asset_uid);
* asset.fetch(new FetchResultCallback() {
* @Override
* public void onCompletion(ResponseType responseType, Error error) {
* asset.getFileSize();
* }
* });
*
* </pre>
*/
public String getFileSize() {
return fileSize;
}
/**
* Gets file name.
*
* @return the file name
*
* <br>
* <br>
* <b>Example :</b><br>
*
* <pre class="prettyprint">
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
* Asset asset = stack.asset(asset_uid);
* asset.fetch(new FetchResultCallback() {
* @Override
* public void onCompletion(ResponseType responseType, Error error) {
* asset.getFileName();
* }
* });
*
* </pre>
*/
public String getFileName() {
return fileName;
}
/**
* Gets url.
*
* @return the url
*
* <br>
* <br>
* <b>Example :</b><br>
*
* <pre class="prettyprint">
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
* Asset asset = stack.asset(asset_uid);
* asset.fetch(new FetchResultCallback() {
* @Override
* public void onCompletion(ResponseType responseType, Error error) {
* asset.getUrl();
* }
* });
*
* </pre>
*/
public String getUrl() {
return uploadUrl;
}
/**
* To json json object.
*
* @return the json object
*
* <br>
* <br>
* <b>Example :</b><br>
*
* <pre class="prettyprint">
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
* Asset asset = stack.asset(asset_uid);
* asset.fetch(new FetchResultCallback() {
* @Override
* public void onCompletion(ResponseType responseType, Error error) {
* asset.toJSON();
* }
* });
*
* </pre>
*/
public JSONObject toJSON() {
return json;
}
/**
* Gets create at.
*
* @return the create at
* <br>
* <b>Example :</b><br>
*
* <pre class="prettyprint">
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
* Asset asset = stack.asset(asset_uid);
* asset.fetch(new FetchResultCallback() {
* @Override
* public void onCompletion(ResponseType responseType, Error error) {
* asset.getCreateAt();
* }
* });
*
* </pre>
*/
public Calendar getCreateAt() {
return parseDate(json.optString("created_at"), null);
}
/**
* Gets created by.
*
* @return the created by
*
* <br>
* <br>
* <b>Example :</b><br>
*
* <pre class="prettyprint">
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
* Asset asset = stack.asset(asset_uid);
* asset.fetch(new FetchResultCallback() {
* @Override
* public void onCompletion(ResponseType responseType, Error error) {
* asset.getCreatedBy();
* }
* });
*
* </pre>
*/
public String getCreatedBy() {
return json.optString("created_by");
}
/**
* Gets update at.
*
* @return the update at
*
* <br>
* <br>
* <b>Example :</b><br>
*
* <pre class="prettyprint">
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
* Asset asset = stack.asset(asset_uid);
* asset.fetch(new FetchResultCallback() {
* @Override
* public void onCompletion(ResponseType responseType, Error error) {
* asset.getUpdateAt();
* }
* });
*
* </pre>
*/
public Calendar getUpdateAt() {
return parseDate(json.optString("updated_at"), null);
}
/**
* Gets updated by.
*
* @return the updated by
* <br>
* <br>
* <b>Example :</b><br>
*
* <pre class="prettyprint">
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
* Asset asset = stack.asset(asset_uid);
* asset.fetch(new FetchResultCallback() {
* @Override
* public void onCompletion(ResponseType responseType, Error error) {
* asset.getUpdatedBy();
* }
* });
*
* </pre>
*/
public String getUpdatedBy() {
return json.optString("updated_by");
}
/**
* Gets delete at.
*
* @return the delete at
*
* <br>
* <br>
* <b>Example :</b><br>
*
* <pre class="prettyprint">
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
* Asset asset = stack.asset(asset_uid);
* asset.fetch(new FetchResultCallback() {
* @Override
* public void onCompletion(ResponseType responseType, Error error) {
* asset.getDeleteAt();
* }
* });
*
* </pre>
*/
public Calendar getDeleteAt() {
return parseDate(json.optString("deleted_at"), null);
}
/**
* Gets deleted by.
*
* @return the deleted by
*
* <br>
* <br>
* <b>Example :</b><br>
*
* <pre class="prettyprint">
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
* Asset asset = stack.asset(asset_uid);
* asset.fetch(new FetchResultCallback() {
* @Override
* public void onCompletion(ResponseType responseType, Error error) {
* asset.getDeletedBy();
* }
* });
* </pre>
*/
public String getDeletedBy() {
return json.optString("deleted_by");
}
/**
* Get tags string [ ].
*
* @return the string [ ]
* <br>
* <br>
* <b>Example :</b><br>
*
* <pre class="prettyprint">
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
* Asset asset = stack.asset(asset_uid);
* asset.fetch(new FetchResultCallback() {
* @Override
* public void onCompletion(ResponseType responseType, Error error) {
* asset.getTags();
* }
* });
*
* </pre>
*/
public String[] getTags() {
return tagsArray;
}
protected Asset setTags(String[] tags) {
tagsArray = tags;
return this;
}
/**
* Include dimension asset.
*
* @return the asset
*
* <br>
* <br>
* <b>Example :</b><br>
*
* <pre class="prettyprint">
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
* Asset asset = stack.asset(asset_uid);
* asset.includeDimension();
* </pre>
*/
public Asset includeDimension() {
urlQueries.put("include_dimension", true);
return this;
}
/**
* Add param asset.
*
* @param paramKey the param key
* @param paramValue the param value
* @return the asset
*
* <br>
* <br>
* <b>Example :</b><br>
*
* <pre class="prettyprint">
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
* Asset asset = stack.asset(asset_uid);
* asset.addParam();
* </pre>
*/
public Asset addParam(@NotNull String paramKey, @NotNull String paramValue) {
urlQueries.put(paramKey, paramValue);
return this;
}
/**
* Include fallback asset.
*
* @return the asset
* <br>
* <br>
* <b>Example :</b><br>
*
* <pre class="prettyprint">
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
* Asset asset = stack.asset(asset_uid);
* asset.includeFallback();
* </pre>
*/
public Asset includeFallback() {
urlQueries.put("include_fallback", true);
return this;
}
/**
* Includes Branch in the asset response
*
* @return {@link Asset} object, so you can chain this call. <br>
*
* <br>
* <br>
* <b>Example :</b><br>
*
* <code>
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment"); Asset asset = stack.asset(asset_uid);
* asset.includeBranch();
* </code>
*/
public Asset includeBranch() {
urlQueries.put("include_branch", true);
return this;
}
/**
* Includes Metadata in the asset response
*
* @return {@link Asset} object, so you can chain this call. <br>
*
* <br>
* <br>
* <b>Example :</b><br>
*
* <code>
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment"); Asset asset = stack.asset(asset_uid);
* asset.includeMetadata();
* </code>
*/
public Asset includeMetadata() {
urlQueries.put("include_metadata", true);
return this;
}
public Asset assetFields(String... fields) {
if (fields != null && fields.length > 0) {
JSONArray array = new JSONArray();
for (String field : fields) {
array.put(field);
}
if (!array.isEmpty()) {
urlQueries.put("asset_fields[]", array);
}
}
return this;
}
/**
* Fetch.
*
* @param callback the callback
*/
public void fetch(FetchResultCallback callback) {
urlQueries.put(ENVIRONMENT, this.headers.get(ENVIRONMENT));
fetchFromNetwork("assets/" + assetUid, urlQueries, this.headers, callback);
}
private void fetchFromNetwork(String url, JSONObject urlQueries, LinkedHashMap<String, Object> headers,
FetchResultCallback callback) {
if (callback != null) {
HashMap<String, Object> urlParams = getUrlParams(urlQueries);
new CSBackgroundTask(this, stackInstance, Constants.FETCHASSETS, url, headers, urlParams,
Constants.REQUEST_CONTROLLER.ASSET.toString(), callback);
}
}
private HashMap<String, Object> getUrlParams(JSONObject urlQueriesJSON) {
HashMap<String, Object> hashMap = new HashMap<>();
if (urlQueriesJSON != null && urlQueriesJSON.length() > 0) {
Iterator<String> keyArrays = urlQueriesJSON.keys();
while (keyArrays.hasNext()) {
String key = keyArrays.next();
Object value = urlQueriesJSON.opt(key);
hashMap.put(key, value);
}
}
return hashMap;
}
}