Skip to content

Commit

Permalink
Merge pull request #119 from contentstack/next
Browse files Browse the repository at this point in the history
DX-566| Enhancement: Update asset url
  • Loading branch information
reeshika-h authored May 17, 2024
2 parents 0f0807f + 11ecb8c commit 18c0cd4
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## v1.14.1

### Date: 17-May-2024

-Enhancement: Update Asset url method added

## v1.14.0

### Date: 13-May-2024
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>1.14.0</version>
<version>1.14.1</version>
<packaging>jar</packaging>
<name>contentstack-java</name>
<description>Java SDK for Contentstack Content Delivery API</description>
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/com/contentstack/sdk/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONObject;
import retrofit2.Response;
import retrofit2.Retrofit;
Expand Down Expand Up @@ -552,5 +553,57 @@ public enum PublishType {
ENTRY_PUBLISHED,
ENTRY_UNPUBLISHED
}
public void updateAssetUrl(Entry entry) {
JSONObject entryJson = entry.toJSON();
// Check if entry consists of _embedded_items object
if (!entryJson.has("_embedded_items")) {
throw new IllegalArgumentException("_embedded_items not present in entry. Call includeEmbeddedItems() before fetching entry.");
}
// Get _embedded_items as a JSONObject
JSONObject embeddedItems = entryJson.getJSONObject("_embedded_items");
Iterator<String> keys = embeddedItems.keys();
Map<String, String> assetUrls = new HashMap<>();
while (keys.hasNext()) {
String key = keys.next();
Object embeddedItem = embeddedItems.get(key);
if (embeddedItem instanceof JSONArray) {
JSONArray itemList = (JSONArray) embeddedItem;
for (int i = 0; i < itemList.length(); i++) {
JSONObject item = itemList.getJSONObject(i);
if ("sys_assets".equals(item.getString("_content_type_uid")) && item.has("filename")) {
String url = item.getString("url");
String uid = item.getString("uid");
assetUrls.put(uid,url);
}
}
}
}
updateChildObjects(entryJson, assetUrls);
}
private void updateChildObjects(JSONObject entryJson, Map<String, String> assetUrls) {
Iterator<String> mainKeys = entryJson.keys();
while (mainKeys.hasNext()) {
String key = mainKeys.next();
Object childObj = entryJson.get(key);
if(childObj instanceof JSONObject)
{ JSONObject mainKey = (JSONObject) childObj;
if (mainKey.has("children")) {
JSONArray mainList = mainKey.getJSONArray("children");
for (int i = 0; i < mainList.length(); i++) {
JSONObject list = mainList.getJSONObject(i);
if (list.has("attrs") ) {
JSONObject childList = list.getJSONObject("attrs");
if(childList.has("asset-uid") && childList.has("asset-link")){
String assetUid = childList.getString("asset-uid");
if (assetUrls.containsKey(assetUid)) {
childList.put("asset-link", assetUrls.get(assetUid));
}
}
}
}
}
}
}
}

}
16 changes: 16 additions & 0 deletions src/test/java/com/contentstack/sdk/TestStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class TestStack {
Stack stack = Credentials.getStack();
protected String paginationToken;
private final Logger logger = Logger.getLogger(TestStack.class.getName());
private String entryUid = Credentials.ENTRY_UID;
private String CONTENT_TYPE = Credentials.CONTENT_TYPE;


@Test
Expand Down Expand Up @@ -384,5 +386,19 @@ public void onCompletion(SyncStack response, Error error) {
}
});
}
@Test
@Disabled
@Order(43)
void testAsseturlupdate() throws IllegalAccessException {
Entry entry = stack.contentType(CONTENT_TYPE).entry(entryUid).includeEmbeddedItems();
entry.fetch(new EntryResultCallBack() {
@Override
public void onCompletion(ResponseType responseType, Error error) {
stack.updateAssetUrl(entry);
Assertions.assertEquals(entryUid, entry.getUid());
Assertions.assertTrue(entry.params.has("include_embedded_items[]"));
}
});
}

}

0 comments on commit 18c0cd4

Please sign in to comment.