Skip to content

Commit 19068dd

Browse files
Merge pull request #168 from contentstack/staging
DX | 03-03-2025 | Release
2 parents 5e09340 + 0e0a0de commit 19068dd

19 files changed

+393
-78
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# CHANGELOG
22

3+
## v2.0.3
4+
5+
### Date: 3-March-2025
6+
7+
- Added skip limit methods for Assets
8+
- Resolved a bug
9+
- Github issue fixed
10+
311
## v2.0.2
412

513
### Date: 5-December-2024

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2012 - 2024 Contentstack
3+
Copyright (c) 2012 - 2025 Contentstack
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<modelVersion>4.0.0</modelVersion>
66
<groupId>com.contentstack.sdk</groupId>
77
<artifactId>java</artifactId>
8-
<version>2.0.2</version>
8+
<version>2.0.3</version>
99
<packaging>jar</packaging>
1010
<name>contentstack-java</name>
1111
<description>Java SDK for Contentstack Content Delivery API</description>

src/main/java/com/contentstack/sdk/AssetLibrary.java

+103-1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,101 @@ public int getCount() {
133133
return count;
134134
}
135135

136+
/**
137+
* Add param assetlibrary.
138+
*
139+
* @param paramKey the param key
140+
* @param paramValue the param value
141+
* @return the assetlibrary
142+
*
143+
* <br>
144+
* <br>
145+
* <b>Example :</b><br>
146+
*
147+
* <pre class="prettyprint">
148+
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
149+
* AssetLibrary assetLibObject = stack.assetlibrary();
150+
* assetLibObject.addParam();
151+
* </pre>
152+
*/
153+
public AssetLibrary addParam(@NotNull String paramKey, @NotNull Object paramValue) {
154+
urlQueries.put(paramKey, paramValue);
155+
return this;
156+
}
157+
158+
/**
159+
* Remove param key assetlibrary.
160+
*
161+
* @param paramKey the param key
162+
* @return the assetlibrary
163+
*
164+
* <br>
165+
* <br>
166+
* <b>Example :</b><br>
167+
*
168+
* <pre class="prettyprint">
169+
* Stack stack = Contentstack.stack("apiKey", "deliveryToken", "environment");
170+
* AssetLibrary assetLibObject = stack.assetlibrary();
171+
* assetLibObject.removeParam(paramKey);
172+
* </pre>
173+
*/
174+
public AssetLibrary removeParam(@NotNull String paramKey){
175+
if(urlQueries.has(paramKey)){
176+
urlQueries.remove(paramKey);
177+
}
178+
return this;
179+
}
180+
181+
182+
183+
/**
184+
* The number of objects to skip before returning any.
185+
*
186+
* @param number No of objects to skip from returned objects
187+
* @return {@link Query} object, so you can chain this call.
188+
* <p>
189+
* <b> Note: </b> The skip parameter can be used for pagination,
190+
* &#34;skip&#34; specifies the number of objects to skip in the response. <br>
191+
*
192+
* <br>
193+
* <br>
194+
* <b>Example :</b><br>
195+
*
196+
* <pre class="prettyprint">
197+
* Stack stack = Contentstack.stack( "apiKey", "deliveryToken", "environment");
198+
* AssetLibrary assetLibObject = stack.assetlibrary.skip(4);<br>
199+
* </pre>
200+
*/
201+
202+
public AssetLibrary skip (@NotNull int number) {
203+
urlQueries.put("skip",number);
204+
return this;
205+
}
206+
207+
/**
208+
* A limit on the number of objects to return.
209+
*
210+
* @param number No of objects to limit.
211+
* @return {@link Query} object, so you can chain this call.
212+
* <p>
213+
* <b> Note:</b> The limit parameter can be used for pagination, &#34;
214+
* limit&#34; specifies the number of objects to limit to in the response. <br>
215+
*
216+
* <br>
217+
* <br>
218+
* <b>Example :</b><br>
219+
*
220+
* <pre class="prettyprint">
221+
* Stack stack = Contentstack.stack( "apiKey", "deliveryToken", "environment");
222+
* AssetLibrary assetLibObject = stack.assetlibrary.limit(4);<br>
223+
* </pre>
224+
*/
225+
226+
public AssetLibrary limit (@NotNull int number) {
227+
urlQueries.put("limit", number);
228+
return this;
229+
}
230+
136231
/**
137232
* Fetch all.
138233
*
@@ -180,6 +275,10 @@ public void getResultObject(List<Object> objects, JSONObject jsonObject, boolean
180275

181276
List<Asset> assets = new ArrayList<>();
182277

278+
// if (objects == null || objects.isEmpty()) {
279+
// System.out.println("Objects list is null or empty");
280+
// }
281+
183282
if (objects != null && !objects.isEmpty()) {
184283
for (Object object : objects) {
185284
AssetModel model = (AssetModel) object;
@@ -193,7 +292,10 @@ public void getResultObject(List<Object> objects, JSONObject jsonObject, boolean
193292
asset.setTags(model.tags);
194293
assets.add(asset);
195294
}
196-
}
295+
}
296+
// else {
297+
// System.out.println("Object is not an instance of AssetModel");
298+
// }
197299

198300
if (callback != null) {
199301
callback.onRequestFinish(ResponseType.NETWORK, assets);

src/main/java/com/contentstack/sdk/AssetModel.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.contentstack.sdk;
22

3+
import java.util.LinkedHashMap;
34
import org.json.JSONArray;
45
import org.json.JSONObject;
56

7+
68
/**
79
* The type Asset model.
810
*/
@@ -25,11 +27,10 @@ class AssetModel {
2527
* @param isArray the is array
2628
*/
2729
public AssetModel(JSONObject response, boolean isArray) {
28-
2930
if (isArray) {
3031
json = response;
3132
} else {
32-
json = response.optJSONObject("asset");
33+
json = new JSONObject((LinkedHashMap<?, ?>) response.get("asset"));
3334
}
3435

3536
if (json != null) {

src/main/java/com/contentstack/sdk/AssetsModel.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.contentstack.sdk;
22

3-
import org.json.JSONArray;
4-
import org.json.JSONObject;
5-
63
import java.util.ArrayList;
74
import java.util.List;
85

6+
import org.json.JSONArray;
7+
import org.json.JSONObject;
8+
99
/**
1010
* The type Assets model.
1111
*/
@@ -19,7 +19,12 @@ class AssetsModel {
1919
* @param response the response
2020
*/
2121
public AssetsModel(JSONObject response) {
22-
JSONArray listResponse = response != null && response.has("assets") ? response.optJSONArray("assets") : null;
22+
JSONArray listResponse = null;
23+
Object rawAssets = response.get("assets"); // Get assets
24+
if (rawAssets instanceof List) { // Check if it's an ArrayList
25+
List<?> assetsList = (List<?>) rawAssets;
26+
listResponse = new JSONArray(assetsList); // Convert to JSONArray
27+
}
2328
if (listResponse != null) {
2429
listResponse.forEach(model -> {
2530
JSONObject modelObj = (JSONObject) model;

src/main/java/com/contentstack/sdk/CSConnectionRequest.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.contentstack.sdk;
22

3-
import org.json.JSONObject;
4-
53
import java.util.HashMap;
64
import java.util.LinkedHashMap;
75
import java.util.List;
6+
import org.json.JSONObject;
7+
88

99
import static com.contentstack.sdk.Constants.*;
1010

@@ -128,7 +128,8 @@ public void onRequestFinished(CSHttpConnection request) {
128128
EntriesModel model = new EntriesModel(jsonResponse);
129129
notifyClass.getResultObject(model.objectList, jsonResponse, true);
130130
} else if (request.getController().equalsIgnoreCase(Constants.FETCHENTRY)) {
131-
EntryModel model = new EntryModel(jsonResponse);
131+
JSONObject jsonModel = new JSONObject((LinkedHashMap<?, ?>) jsonResponse.get("entry"));
132+
EntryModel model = new EntryModel(jsonModel);
132133
entryInstance.resultJson = model.jsonObject;
133134
entryInstance.title = model.title;
134135
entryInstance.url = model.url;

src/main/java/com/contentstack/sdk/CSHttpConnection.java

+28-20
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
package com.contentstack.sdk;
22

3-
import okhttp3.Request;
4-
import okhttp3.ResponseBody;
5-
6-
import org.json.JSONArray;
7-
import org.json.JSONException;
8-
import org.json.JSONObject;
9-
10-
import retrofit2.Call;
11-
import retrofit2.Response;
12-
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.databind.json.JsonMapper;
5+
import com.fasterxml.jackson.databind.type.MapType;
136
import java.io.IOException;
147
import java.io.UnsupportedEncodingException;
15-
import java.net.URLEncoder;
168
import java.net.SocketTimeoutException;
9+
import java.net.URLEncoder;
1710
import java.nio.charset.StandardCharsets;
1811
import java.util.HashMap;
1912
import java.util.Iterator;
@@ -22,10 +15,16 @@
2215
import java.util.logging.Level;
2316
import java.util.logging.Logger;
2417
import java.util.stream.IntStream;
25-
import com.fasterxml.jackson.databind.ObjectMapper; // Jackson for JSON parsing
26-
import com.fasterxml.jackson.databind.json.JsonMapper;
27-
import com.fasterxml.jackson.databind.node.ObjectNode;
28-
import com.fasterxml.jackson.databind.type.MapType;
18+
import okhttp3.Request;
19+
import okhttp3.ResponseBody;
20+
import org.json.JSONArray;
21+
import org.json.JSONException;
22+
import org.json.JSONObject;
23+
import retrofit2.Call;
24+
import retrofit2.Response;
25+
26+
27+
2928

3029
import static com.contentstack.sdk.Constants.*;
3130

@@ -230,7 +229,7 @@ private void getService(String requestUrl) throws IOException {
230229
MapType type = mapper.getTypeFactory().constructMapType(LinkedHashMap.class, String.class,
231230
Object.class);
232231
Map<String, Object> responseMap = mapper.readValue(response.body().string(), type);
233-
232+
234233
// Use the custom method to create an ordered JSONObject
235234
responseJSON = createOrderedJSONObject(responseMap);
236235
if (this.config.livePreviewEntry != null && !this.config.livePreviewEntry.isEmpty()) {
@@ -295,17 +294,26 @@ void handleJSONObject(JSONArray arrayEntry, JSONObject jsonObj, int idx) {
295294
}
296295

297296
void setError(String errResp) {
297+
298+
if (errResp == null || errResp.trim().isEmpty()) {
299+
errResp = "Unexpected error: No response received from server.";
300+
}
298301
try {
299302
responseJSON = new JSONObject(errResp);
300303
} catch (JSONException e) {
301304
// If errResp is not valid JSON, create a new JSONObject with the error message
302305
responseJSON = new JSONObject();
303306
responseJSON.put(ERROR_MESSAGE, errResp);
304307
}
305-
responseJSON.put(ERROR_MESSAGE, responseJSON.optString(ERROR_MESSAGE));
306-
responseJSON.put(ERROR_CODE, responseJSON.optString(ERROR_CODE));
307-
responseJSON.put(ERRORS, responseJSON.optString(ERRORS));
308-
int errCode = Integer.parseInt(responseJSON.optString(ERROR_CODE));
308+
responseJSON.put(ERROR_MESSAGE, responseJSON.optString(ERROR_MESSAGE, "An unknown error occurred."));
309+
responseJSON.put(ERROR_CODE, responseJSON.optString(ERROR_CODE, "0"));
310+
responseJSON.put(ERRORS, responseJSON.optString(ERRORS, "No additional error details available."));
311+
int errCode = 0;
312+
try {
313+
errCode = Integer.parseInt(responseJSON.optString(ERROR_CODE, "0"));
314+
} catch (NumberFormatException e) {
315+
// Default error code remains 0 if parsing fails
316+
}
309317
connectionRequest.onRequestFailed(responseJSON, errCode, callBackObject);
310318
}
311319

src/main/java/com/contentstack/sdk/ContentTypesModel.java

+21-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package com.contentstack.sdk;
22

3+
import java.util.ArrayList;
4+
import java.util.LinkedHashMap;
5+
import java.util.List;
36
import org.json.JSONArray;
47
import org.json.JSONObject;
58

9+
10+
611
/**
712
* The ContentTypesModel that contains content type response
813
*/
@@ -12,16 +17,26 @@ public class ContentTypesModel {
1217
private JSONArray responseJSONArray = new JSONArray();
1318

1419
public void setJSON(JSONObject responseJSON) {
15-
1620
if (responseJSON != null) {
1721
String ctKey = "content_type";
18-
if (responseJSON.has(ctKey) && responseJSON.opt(ctKey) instanceof JSONObject) {
19-
this.response = responseJSON.optJSONObject(ctKey);
22+
if (responseJSON.has(ctKey) && responseJSON.opt(ctKey) instanceof LinkedHashMap) {
23+
this.response = new JSONObject((LinkedHashMap<?, ?>) responseJSON.get(ctKey));
2024
}
2125
String ctListKey = "content_types";
22-
if (responseJSON.has(ctListKey) && responseJSON.opt(ctListKey) instanceof JSONArray) {
23-
this.response = responseJSON.optJSONArray(ctListKey);
24-
this.responseJSONArray = (JSONArray) this.response;
26+
if (responseJSON.has(ctListKey) && responseJSON.opt(ctListKey) instanceof ArrayList) {
27+
ArrayList<LinkedHashMap<?, ?>> contentTypes = (ArrayList) responseJSON.get(ctListKey);
28+
List<Object> objectList = new ArrayList<>();
29+
if (!contentTypes.isEmpty()) {
30+
contentTypes.forEach(model -> {
31+
if (model instanceof LinkedHashMap) {
32+
// Convert LinkedHashMap to JSONObject
33+
JSONObject jsonModel = new JSONObject((LinkedHashMap<?, ?>) model);
34+
objectList.add(jsonModel);
35+
}
36+
});
37+
}
38+
this.response = new JSONArray(objectList);
39+
this.responseJSONArray = new JSONArray(objectList);
2540
}
2641
}
2742
}

src/main/java/com/contentstack/sdk/EntryModel.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.contentstack.sdk;
22

3+
import java.util.HashMap;
4+
import java.util.Map;
35
import org.json.JSONArray;
46
import org.json.JSONObject;
57

6-
import java.util.HashMap;
7-
import java.util.Map;
88

99
class EntryModel {
1010

@@ -40,6 +40,7 @@ class EntryModel {
4040

4141
public EntryModel(JSONObject response) {
4242
this.jsonObject = response;
43+
4344
if (this.jsonObject.has(ENTRY_KEY)) {
4445
this.jsonObject = jsonObject.optJSONObject(ENTRY_KEY);
4546
}
@@ -59,7 +60,6 @@ public EntryModel(JSONObject response) {
5960
if (this.jsonObject.has("description")) {
6061
this.description = this.jsonObject.opt("description");
6162
}
62-
6363
this.images = (JSONArray) this.jsonObject.opt("images");
6464
this.isDirectory = (Boolean) this.jsonObject.opt("is_dir");
6565
this.updatedAt = (String) this.jsonObject.opt("updated_at");

0 commit comments

Comments
 (0)