Skip to content

Commit d3c3183

Browse files
committed
feat: update asseturl for gql
1 parent 0ee7fe3 commit d3c3183

File tree

5 files changed

+256
-2
lines changed

5 files changed

+256
-2
lines changed

Changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Changelog
22

33
A brief description of what changes project contains
4+
## May 28, 2024
5+
6+
#### v1.2.10
7+
8+
-Enhancement: Update Asset url method added for GQL
49

510
## May 14, 2024
611

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.contentstack.sdk</groupId>
66
<artifactId>utils</artifactId>
7-
<version>1.2.9</version>
7+
<version>1.2.10</version>
88
<packaging>jar</packaging>
99
<name>Contentstack-utils</name>
1010
<description>Java Utils SDK for Contentstack Content Delivery API, Contentstack is a headless CMS</description>

src/main/java/com/contentstack/utils/Utils.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
import javax.validation.constraints.NotNull;
1313
import java.util.ArrayList;
14+
import java.util.HashMap;
15+
import java.util.Iterator;
16+
import java.util.Map;
1417
import java.util.Optional;
1518
import java.util.Set;
1619
import java.util.stream.StreamSupport;
@@ -248,7 +251,96 @@ public void render(@NotNull JSONArray jsonArray, @NotNull String[] keyPath, @Not
248251
jsonArray.forEach(jsonObj -> render((JSONObject) jsonObj, keyPath, renderObject));
249252
}
250253

254+
//update assetURL in json of GQL response
255+
public static void UpdateAssetURLForGQL(JSONObject entryJson) {
256+
Map<String, String> assetUrls = new HashMap<>();
257+
if (entryJson.has("data")) {
258+
JSONObject data = entryJson.optJSONObject("data");
259+
for (String key : data.keySet()) {
260+
Object value = data.get(key);
261+
if (value instanceof JSONObject) {
262+
JSONObject contentTypeObj = (JSONObject) value;
263+
Iterator<String> keys = contentTypeObj.keys();
264+
while (keys.hasNext()) {
265+
String mainKey = keys.next();
266+
Object embeddedItem = contentTypeObj.get(mainKey);
267+
if (embeddedItem instanceof JSONObject) {
268+
JSONObject jsonRteObj = (JSONObject) embeddedItem;
269+
if (jsonRteObj.has("embedded_itemsConnection")) {
270+
JSONObject embeddedConnection = jsonRteObj.getJSONObject("embedded_itemsConnection");
271+
if (embeddedConnection.has("edges")) {
272+
JSONArray embeddedItems = embeddedConnection.getJSONArray("edges");
273+
for (int i = 0; i < embeddedItems.length(); i++) {
274+
JSONObject item = embeddedItems.getJSONObject(i);
275+
if (item.has("node")) {
276+
JSONObject nodeList = item.getJSONObject("node");
277+
if (nodeList.has("url")) {
278+
String url = nodeList.getString("url");
279+
if (nodeList.has("system")) {
280+
JSONObject systemList = nodeList.getJSONObject("system");
281+
if ("sys_assets".equals(systemList.optString("content_type_uid")) && systemList.has("uid")) {
282+
String uid = systemList.getString("uid");
283+
assetUrls.put(uid, url);
284+
updateChildObjects(entryJson, assetUrls);
285+
}
286+
}
287+
}
288+
}
289+
}
290+
}
291+
}
292+
else
293+
{
294+
throw new IllegalArgumentException("_embedded_items not present in entry. Call includeEmbeddedItems() before fetching entry.");
295+
}
296+
}
297+
}
298+
}
299+
}
300+
}
301+
}
251302

303+
private static void updateChildObjects(JSONObject entryJson, Map<String, String> assetUrls) {
304+
if (entryJson.has("data")) {
305+
JSONObject dataObject = entryJson.getJSONObject("data");
306+
for (String key : dataObject.keySet()) {
307+
Object value = dataObject.get(key);
308+
if (value instanceof JSONObject) {
309+
JSONObject contentTypeObj = (JSONObject) value;
310+
Iterator<String> keys = contentTypeObj.keys();
311+
while (keys.hasNext()) {
312+
String mainKey = keys.next();
313+
Object embeddedItem = contentTypeObj.get(mainKey);
314+
if (embeddedItem instanceof JSONObject) {
315+
JSONObject jsonRteObj = (JSONObject) embeddedItem;
316+
if (jsonRteObj.has("json")) {
317+
JSONObject jsonValue = jsonRteObj.getJSONObject("json");
318+
if (jsonValue.has("children")) {
319+
JSONArray childrenArray = jsonValue.getJSONArray("children");
320+
updateChildrenArray(childrenArray, assetUrls);
321+
}
322+
}
323+
}
324+
}
325+
}
326+
}
327+
}
328+
}
329+
330+
private static void updateChildrenArray(JSONArray childrenArray, Map<String, String> assetUrls) {
331+
for (int i = 0; i < childrenArray.length(); i++) {
332+
JSONObject child = childrenArray.getJSONObject(i);
333+
if (child.has("attrs")) {
334+
JSONObject attrsObject = child.getJSONObject("attrs");
335+
if (attrsObject.has("asset-uid") && attrsObject.has("asset-link")) {
336+
String assetUid = attrsObject.getString("asset-uid");
337+
if (assetUrls.containsKey(assetUid)) {
338+
attrsObject.put("asset-link", assetUrls.get(assetUid));
339+
}
340+
}
341+
}
342+
}
343+
}
252344
}
253345

254346

src/test/java/com/contentstack/utils/UtilTests.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,14 @@ public void testCustomJSONRTE() {
164164
};
165165
Utils.jsonToHTML(rteObject, keyPath, new DefaultOptionClass());
166166
}
167-
167+
168+
@Test
169+
public void testUpdateAssetUrl() throws IOException{
170+
final String json = "src/test/resources/file.json";
171+
JSONObject localJsonObj1 = new ReadResource().readJson(json);
172+
Utils.UpdateAssetURLForGQL(localJsonObj1);
173+
// System.out.println(localJsonObj1);
174+
}
168175

169176
}
170177

src/test/resources/file.json

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
{
2+
"data": {
3+
"test": {
4+
"json_rte1": {
5+
"json": {
6+
"type": "doc",
7+
"attrs": {},
8+
"uid": "2fcad68bd7d34cd5a01c8ccdd915c7ae",
9+
"children": [
10+
{
11+
"uid": "a8c4ca25e7304707822eafcdb22536e7",
12+
"type": "reference",
13+
"attrs": {
14+
"display-type": "display",
15+
"asset-uid": "blt51f558a28ef555ca",
16+
"content-type-uid": "sys_assets",
17+
"asset-link": "https://images.***REMOVED***/v3/assets/***REMOVED***/blt51f558a28ef555ca/6643371882c344cb839ec1a8/ben.jpeg",
18+
"asset-name": "Designer.png",
19+
"asset-type": "image/jpeg",
20+
"type": "asset",
21+
"class-name": "embedded-asset",
22+
"alt": "Designer.png",
23+
"asset-alt": "Designer.png",
24+
"inline": false
25+
},
26+
"children": [
27+
{
28+
"text": ""
29+
}
30+
]
31+
},
32+
{
33+
"type": "p",
34+
"attrs": {},
35+
"uid": "795712911f514e7cad0a1f087800f2a8",
36+
"children": [
37+
{
38+
"text": ""
39+
}
40+
]
41+
}
42+
],
43+
"_version": 4
44+
},
45+
"embedded_itemsConnection": {
46+
"edges": [
47+
{
48+
"node": {
49+
"title": "Designer.png",
50+
"url": "https://images.***REMOVED***/v3/assets/***REMOVED***/blt51f558a28ef555ca/66545ff10258be81fc639ae5/Virginia_Woolf_Headshot.jpg",
51+
"system": {
52+
"uid": "blt51f558a28ef555ca",
53+
"content_type_uid": "sys_assets"
54+
}
55+
}
56+
}
57+
]
58+
}
59+
},
60+
"json_rte2": {
61+
"embedded_itemsConnection": {
62+
"edges": [
63+
{
64+
"node": {
65+
"title": "W._E._B._Du_Bois_Headshot.jpg",
66+
"url": "https://images.***REMOVED***/v3/assets/***REMOVED***/blt897c06ebf8c6353a/66545fe479abeeb60a3d4e35/Mark_Twain_Headshot.jpg",
67+
"system": {
68+
"content_type_uid": "sys_assets",
69+
"uid": "blt897c06ebf8c6353a"
70+
}
71+
}
72+
},
73+
{
74+
"node": {
75+
"title": "iot-icon.png",
76+
"url": "https://images.***REMOVED***/v3/assets/***REMOVED***/bltbd81ff2ed42f1317/6654501e466a1c9b3edecb21/iot-icon.png",
77+
"system": {
78+
"content_type_uid": "sys_assets",
79+
"uid": "bltbd81ff2ed42f1317"
80+
}
81+
}
82+
}
83+
]
84+
},
85+
"json": {
86+
"type": "doc",
87+
"uid": "fb4ae425afcd466a872f662596ccd852",
88+
"attrs": {},
89+
"children": [
90+
{
91+
"uid": "58a41f7b4c5340ad8bfea5ffb9c98dde",
92+
"type": "reference",
93+
"attrs": {
94+
"display-type": "display",
95+
"asset-uid": "blt897c06ebf8c6353a",
96+
"content-type-uid": "sys_assets",
97+
"asset-link": "https://images.***REMOVED***/v3/assets/***REMOVED***/blt897c06ebf8c6353a/6644340e015b1c2d2b56ca48/W._E._B._Du_Bois_Headshot.jpg",
98+
"asset-name": "W._E._B._Du_Bois_Headshot.jpg",
99+
"asset-type": "image/jpeg",
100+
"type": "asset",
101+
"class-name": "embedded-asset",
102+
"alt": "W._E._B._Du_Bois_Headshot.jpg",
103+
"asset-alt": "W._E._B._Du_Bois_Headshot.jpg",
104+
"inline": false
105+
},
106+
"children": [
107+
{
108+
"text": ""
109+
}
110+
]
111+
},
112+
{
113+
"type": "p",
114+
"attrs": {},
115+
"uid": "321122af10bd4e288b296c2ca87eb2a8",
116+
"children": [
117+
{
118+
"text": ""
119+
}
120+
]
121+
},
122+
{
123+
"uid": "91537fbd2f3c48cc939cdf737a392986",
124+
"type": "reference",
125+
"attrs": {
126+
"display-type": "display",
127+
"asset-uid": "bltbd81ff2ed42f1317",
128+
"content-type-uid": "sys_assets",
129+
"asset-link": "https://images.***REMOVED***/v3/assets/***REMOVED***/bltbd81ff2ed42f1317/664425e3a3f9df03dfc0e5e6/5216292.jpg",
130+
"asset-name": "iot-icon.png",
131+
"asset-type": "image/jpeg",
132+
"type": "asset",
133+
"class-name": "embedded-asset",
134+
"alt": "iot-icon.png",
135+
"asset-alt": "iot-icon.png",
136+
"inline": false
137+
},
138+
"children": [
139+
{
140+
"text": ""
141+
}
142+
]
143+
}
144+
],
145+
"_version": 4
146+
}
147+
}
148+
}
149+
}
150+
}

0 commit comments

Comments
 (0)