Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog

A brief description of what changes project contains
## May 28, 2024

#### v1.2.10

-Enhancement: Update Asset url method added for GQL

## May 14, 2024

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.contentstack.sdk</groupId>
<artifactId>utils</artifactId>
<version>1.2.9</version>
<version>1.2.10</version>
<packaging>jar</packaging>
<name>Contentstack-utils</name>
<description>Java Utils SDK for Contentstack Content Delivery API, Contentstack is a headless CMS</description>
Expand Down
92 changes: 92 additions & 0 deletions src/main/java/com/contentstack/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.StreamSupport;
Expand Down Expand Up @@ -248,7 +251,96 @@ public void render(@NotNull JSONArray jsonArray, @NotNull String[] keyPath, @Not
jsonArray.forEach(jsonObj -> render((JSONObject) jsonObj, keyPath, renderObject));
}

//update assetURL in json of GQL response
public static void UpdateAssetURLForGQL(JSONObject entryJson) {
Map<String, String> assetUrls = new HashMap<>();
if (entryJson.has("data")) {
JSONObject data = entryJson.optJSONObject("data");
for (String key : data.keySet()) {
Object value = data.get(key);
if (value instanceof JSONObject) {
JSONObject contentTypeObj = (JSONObject) value;
Iterator<String> keys = contentTypeObj.keys();
while (keys.hasNext()) {
String mainKey = keys.next();
Object embeddedItem = contentTypeObj.get(mainKey);
if (embeddedItem instanceof JSONObject) {
JSONObject jsonRteObj = (JSONObject) embeddedItem;
if (jsonRteObj.has("embedded_itemsConnection")) {
JSONObject embeddedConnection = jsonRteObj.getJSONObject("embedded_itemsConnection");
if (embeddedConnection.has("edges")) {
JSONArray embeddedItems = embeddedConnection.getJSONArray("edges");
for (int i = 0; i < embeddedItems.length(); i++) {
JSONObject item = embeddedItems.getJSONObject(i);
if (item.has("node")) {
JSONObject nodeList = item.getJSONObject("node");
if (nodeList.has("url")) {
String url = nodeList.getString("url");
if (nodeList.has("system")) {
JSONObject systemList = nodeList.getJSONObject("system");
if ("sys_assets".equals(systemList.optString("content_type_uid")) && systemList.has("uid")) {
String uid = systemList.getString("uid");
assetUrls.put(uid, url);
updateChildObjects(entryJson, assetUrls);
}
}
}
}
}
}
}
else
{
throw new IllegalArgumentException("_embedded_items not present in entry. Call includeEmbeddedItems() before fetching entry.");
}
}
}
}
}
}
}

private static void updateChildObjects(JSONObject entryJson, Map<String, String> assetUrls) {
if (entryJson.has("data")) {
JSONObject dataObject = entryJson.getJSONObject("data");
for (String key : dataObject.keySet()) {
Object value = dataObject.get(key);
if (value instanceof JSONObject) {
JSONObject contentTypeObj = (JSONObject) value;
Iterator<String> keys = contentTypeObj.keys();
while (keys.hasNext()) {
String mainKey = keys.next();
Object embeddedItem = contentTypeObj.get(mainKey);
if (embeddedItem instanceof JSONObject) {
JSONObject jsonRteObj = (JSONObject) embeddedItem;
if (jsonRteObj.has("json")) {
JSONObject jsonValue = jsonRteObj.getJSONObject("json");
if (jsonValue.has("children")) {
JSONArray childrenArray = jsonValue.getJSONArray("children");
updateChildrenArray(childrenArray, assetUrls);
}
}
}
}
}
}
}
}

private static void updateChildrenArray(JSONArray childrenArray, Map<String, String> assetUrls) {
for (int i = 0; i < childrenArray.length(); i++) {
JSONObject child = childrenArray.getJSONObject(i);
if (child.has("attrs")) {
JSONObject attrsObject = child.getJSONObject("attrs");
if (attrsObject.has("asset-uid") && attrsObject.has("asset-link")) {
String assetUid = attrsObject.getString("asset-uid");
if (assetUrls.containsKey(assetUid)) {
attrsObject.put("asset-link", assetUrls.get(assetUid));
}
}
}
}
}
}


9 changes: 8 additions & 1 deletion src/test/java/com/contentstack/utils/UtilTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,14 @@ public void testCustomJSONRTE() {
};
Utils.jsonToHTML(rteObject, keyPath, new DefaultOptionClass());
}


@Test
public void testUpdateAssetUrl() throws IOException{
final String json = "src/test/resources/file.json";
JSONObject localJsonObj1 = new ReadResource().readJson(json);
Utils.UpdateAssetURLForGQL(localJsonObj1);
// System.out.println(localJsonObj1);
}

}

Expand Down
150 changes: 150 additions & 0 deletions src/test/resources/file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
{
"data": {
"test": {
"json_rte1": {
"json": {
"type": "doc",
"attrs": {},
"uid": "2fcad68bd7d34cd5a01c8ccdd915c7ae",
"children": [
{
"uid": "a8c4ca25e7304707822eafcdb22536e7",
"type": "reference",
"attrs": {
"display-type": "display",
"asset-uid": "blt51f558a28ef555ca",
"content-type-uid": "sys_assets",
"asset-link": "https://images.contentstack.io/v3/assets/blt6fc934a068135ff9/blt51f558a28ef555ca/6643371882c344cb839ec1a8/ben.jpeg",
"asset-name": "Designer.png",
"asset-type": "image/jpeg",
"type": "asset",
"class-name": "embedded-asset",
"alt": "Designer.png",
"asset-alt": "Designer.png",
"inline": false
},
"children": [
{
"text": ""
}
]
},
{
"type": "p",
"attrs": {},
"uid": "795712911f514e7cad0a1f087800f2a8",
"children": [
{
"text": ""
}
]
}
],
"_version": 4
},
"embedded_itemsConnection": {
"edges": [
{
"node": {
"title": "Designer.png",
"url": "https://images.contentstack.io/v3/assets/blt6fc934a068135ff9/blt51f558a28ef555ca/66545ff10258be81fc639ae5/Virginia_Woolf_Headshot.jpg",
"system": {
"uid": "blt51f558a28ef555ca",
"content_type_uid": "sys_assets"
}
}
}
]
}
},
"json_rte2": {
"embedded_itemsConnection": {
"edges": [
{
"node": {
"title": "W._E._B._Du_Bois_Headshot.jpg",
"url": "https://images.contentstack.io/v3/assets/blt6fc934a068135ff9/blt897c06ebf8c6353a/66545fe479abeeb60a3d4e35/Mark_Twain_Headshot.jpg",
"system": {
"content_type_uid": "sys_assets",
"uid": "blt897c06ebf8c6353a"
}
}
},
{
"node": {
"title": "iot-icon.png",
"url": "https://images.contentstack.io/v3/assets/blt6fc934a068135ff9/bltbd81ff2ed42f1317/6654501e466a1c9b3edecb21/iot-icon.png",
"system": {
"content_type_uid": "sys_assets",
"uid": "bltbd81ff2ed42f1317"
}
}
}
]
},
"json": {
"type": "doc",
"uid": "fb4ae425afcd466a872f662596ccd852",
"attrs": {},
"children": [
{
"uid": "58a41f7b4c5340ad8bfea5ffb9c98dde",
"type": "reference",
"attrs": {
"display-type": "display",
"asset-uid": "blt897c06ebf8c6353a",
"content-type-uid": "sys_assets",
"asset-link": "https://images.contentstack.io/v3/assets/blt6fc934a068135ff9/blt897c06ebf8c6353a/6644340e015b1c2d2b56ca48/W._E._B._Du_Bois_Headshot.jpg",
"asset-name": "W._E._B._Du_Bois_Headshot.jpg",
"asset-type": "image/jpeg",
"type": "asset",
"class-name": "embedded-asset",
"alt": "W._E._B._Du_Bois_Headshot.jpg",
"asset-alt": "W._E._B._Du_Bois_Headshot.jpg",
"inline": false
},
"children": [
{
"text": ""
}
]
},
{
"type": "p",
"attrs": {},
"uid": "321122af10bd4e288b296c2ca87eb2a8",
"children": [
{
"text": ""
}
]
},
{
"uid": "91537fbd2f3c48cc939cdf737a392986",
"type": "reference",
"attrs": {
"display-type": "display",
"asset-uid": "bltbd81ff2ed42f1317",
"content-type-uid": "sys_assets",
"asset-link": "https://images.contentstack.io/v3/assets/blt6fc934a068135ff9/bltbd81ff2ed42f1317/664425e3a3f9df03dfc0e5e6/5216292.jpg",
"asset-name": "iot-icon.png",
"asset-type": "image/jpeg",
"type": "asset",
"class-name": "embedded-asset",
"alt": "iot-icon.png",
"asset-alt": "iot-icon.png",
"inline": false
},
"children": [
{
"text": ""
}
]
}
],
"_version": 4
}
}
}
}
}