Skip to content

Commit

Permalink
Merge pull request #168 from contentstack/staging
Browse files Browse the repository at this point in the history
DX | 03-03-2025 | Release
  • Loading branch information
harshithad0703 authored Mar 4, 2025
2 parents 5e09340 + 0e0a0de commit 19068dd
Show file tree
Hide file tree
Showing 19 changed files with 393 additions and 78 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# CHANGELOG

## v2.0.3

### Date: 3-March-2025

- Added skip limit methods for Assets
- Resolved a bug
- Github issue fixed

## v2.0.2

### Date: 5-December-2024
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2012 - 2024 Contentstack
Copyright (c) 2012 - 2025 Contentstack

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.contentstack.sdk</groupId>
<artifactId>java</artifactId>
<version>2.0.2</version>
<version>2.0.3</version>
<packaging>jar</packaging>
<name>contentstack-java</name>
<description>Java SDK for Contentstack Content Delivery API</description>
Expand Down
104 changes: 103 additions & 1 deletion src/main/java/com/contentstack/sdk/AssetLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,101 @@ 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) {
urlQueries.put(paramKey, paramValue);
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(urlQueries.has(paramKey)){
urlQueries.remove(paramKey);
}
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,
* &#34;skip&#34; 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, &#34;
* limit&#34; 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.
*
Expand Down Expand Up @@ -180,6 +275,10 @@ public void getResultObject(List<Object> objects, JSONObject jsonObject, boolean

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;
Expand All @@ -193,7 +292,10 @@ public void getResultObject(List<Object> objects, JSONObject jsonObject, boolean
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);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/contentstack/sdk/AssetModel.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.contentstack.sdk;

import java.util.LinkedHashMap;
import org.json.JSONArray;
import org.json.JSONObject;


/**
* The type Asset model.
*/
Expand All @@ -25,11 +27,10 @@ class AssetModel {
* @param isArray the is array
*/
public AssetModel(JSONObject response, boolean isArray) {

if (isArray) {
json = response;
} else {
json = response.optJSONObject("asset");
json = new JSONObject((LinkedHashMap<?, ?>) response.get("asset"));
}

if (json != null) {
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/com/contentstack/sdk/AssetsModel.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.contentstack.sdk;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

/**
* The type Assets model.
*/
Expand All @@ -19,7 +19,12 @@ class AssetsModel {
* @param response the response
*/
public AssetsModel(JSONObject response) {
JSONArray listResponse = response != null && response.has("assets") ? response.optJSONArray("assets") : null;
JSONArray listResponse = null;
Object rawAssets = response.get("assets"); // Get assets
if (rawAssets instanceof List) { // Check if it's an ArrayList
List<?> assetsList = (List<?>) rawAssets;
listResponse = new JSONArray(assetsList); // Convert to JSONArray
}
if (listResponse != null) {
listResponse.forEach(model -> {
JSONObject modelObj = (JSONObject) model;
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/contentstack/sdk/CSConnectionRequest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.contentstack.sdk;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import org.json.JSONObject;


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

Expand Down Expand Up @@ -128,7 +128,8 @@ public void onRequestFinished(CSHttpConnection request) {
EntriesModel model = new EntriesModel(jsonResponse);
notifyClass.getResultObject(model.objectList, jsonResponse, true);
} else if (request.getController().equalsIgnoreCase(Constants.FETCHENTRY)) {
EntryModel model = new EntryModel(jsonResponse);
JSONObject jsonModel = new JSONObject((LinkedHashMap<?, ?>) jsonResponse.get("entry"));
EntryModel model = new EntryModel(jsonModel);
entryInstance.resultJson = model.jsonObject;
entryInstance.title = model.title;
entryInstance.url = model.url;
Expand Down
48 changes: 28 additions & 20 deletions src/main/java/com/contentstack/sdk/CSHttpConnection.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
package com.contentstack.sdk;

import okhttp3.Request;
import okhttp3.ResponseBody;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import retrofit2.Call;
import retrofit2.Response;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.type.MapType;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.net.SocketTimeoutException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -22,10 +15,16 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.IntStream;
import com.fasterxml.jackson.databind.ObjectMapper; // Jackson for JSON parsing
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.type.MapType;
import okhttp3.Request;
import okhttp3.ResponseBody;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import retrofit2.Call;
import retrofit2.Response;




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

Expand Down Expand Up @@ -230,7 +229,7 @@ private void getService(String requestUrl) throws IOException {
MapType type = mapper.getTypeFactory().constructMapType(LinkedHashMap.class, String.class,
Object.class);
Map<String, Object> responseMap = mapper.readValue(response.body().string(), type);

// Use the custom method to create an ordered JSONObject
responseJSON = createOrderedJSONObject(responseMap);
if (this.config.livePreviewEntry != null && !this.config.livePreviewEntry.isEmpty()) {
Expand Down Expand Up @@ -295,17 +294,26 @@ void handleJSONObject(JSONArray arrayEntry, JSONObject jsonObj, int idx) {
}

void setError(String errResp) {

if (errResp == null || errResp.trim().isEmpty()) {
errResp = "Unexpected error: No response received from server.";
}
try {
responseJSON = new JSONObject(errResp);
} catch (JSONException e) {
// If errResp is not valid JSON, create a new JSONObject with the error message
responseJSON = new JSONObject();
responseJSON.put(ERROR_MESSAGE, errResp);
}
responseJSON.put(ERROR_MESSAGE, responseJSON.optString(ERROR_MESSAGE));
responseJSON.put(ERROR_CODE, responseJSON.optString(ERROR_CODE));
responseJSON.put(ERRORS, responseJSON.optString(ERRORS));
int errCode = Integer.parseInt(responseJSON.optString(ERROR_CODE));
responseJSON.put(ERROR_MESSAGE, responseJSON.optString(ERROR_MESSAGE, "An unknown error occurred."));
responseJSON.put(ERROR_CODE, responseJSON.optString(ERROR_CODE, "0"));
responseJSON.put(ERRORS, responseJSON.optString(ERRORS, "No additional error details available."));
int errCode = 0;
try {
errCode = Integer.parseInt(responseJSON.optString(ERROR_CODE, "0"));
} catch (NumberFormatException e) {
// Default error code remains 0 if parsing fails
}
connectionRequest.onRequestFailed(responseJSON, errCode, callBackObject);
}

Expand Down
27 changes: 21 additions & 6 deletions src/main/java/com/contentstack/sdk/ContentTypesModel.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.contentstack.sdk;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;



/**
* The ContentTypesModel that contains content type response
*/
Expand All @@ -12,16 +17,26 @@ public class ContentTypesModel {
private JSONArray responseJSONArray = new JSONArray();

public void setJSON(JSONObject responseJSON) {

if (responseJSON != null) {
String ctKey = "content_type";
if (responseJSON.has(ctKey) && responseJSON.opt(ctKey) instanceof JSONObject) {
this.response = responseJSON.optJSONObject(ctKey);
if (responseJSON.has(ctKey) && responseJSON.opt(ctKey) instanceof LinkedHashMap) {
this.response = new JSONObject((LinkedHashMap<?, ?>) responseJSON.get(ctKey));
}
String ctListKey = "content_types";
if (responseJSON.has(ctListKey) && responseJSON.opt(ctListKey) instanceof JSONArray) {
this.response = responseJSON.optJSONArray(ctListKey);
this.responseJSONArray = (JSONArray) this.response;
if (responseJSON.has(ctListKey) && responseJSON.opt(ctListKey) instanceof ArrayList) {
ArrayList<LinkedHashMap<?, ?>> contentTypes = (ArrayList) responseJSON.get(ctListKey);
List<Object> objectList = new ArrayList<>();
if (!contentTypes.isEmpty()) {
contentTypes.forEach(model -> {
if (model instanceof LinkedHashMap) {
// Convert LinkedHashMap to JSONObject
JSONObject jsonModel = new JSONObject((LinkedHashMap<?, ?>) model);
objectList.add(jsonModel);
}
});
}
this.response = new JSONArray(objectList);
this.responseJSONArray = new JSONArray(objectList);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/contentstack/sdk/EntryModel.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.contentstack.sdk;

import java.util.HashMap;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

class EntryModel {

Expand Down Expand Up @@ -40,6 +40,7 @@ class EntryModel {

public EntryModel(JSONObject response) {
this.jsonObject = response;

if (this.jsonObject.has(ENTRY_KEY)) {
this.jsonObject = jsonObject.optJSONObject(ENTRY_KEY);
}
Expand All @@ -59,7 +60,6 @@ public EntryModel(JSONObject response) {
if (this.jsonObject.has("description")) {
this.description = this.jsonObject.opt("description");
}

this.images = (JSONArray) this.jsonObject.opt("images");
this.isDirectory = (Boolean) this.jsonObject.opt("is_dir");
this.updatedAt = (String) this.jsonObject.opt("updated_at");
Expand Down
Loading

0 comments on commit 19068dd

Please sign in to comment.