Skip to content
Draft
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
22 changes: 14 additions & 8 deletions src/main/java/com/contentstack/sdk/CSHttpConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,17 +268,21 @@ private Response<ResponseBody> pluginResponseImp(Request request, Response<Respo
}

void handleJSONArray() {
if (responseJSON.has("entries") && !responseJSON.optJSONArray("entries").isEmpty()) {
JSONArray finalEntries = responseJSON.optJSONArray("entries");
JSONArray entriesArray = responseJSON.optJSONArray("entries");
if (responseJSON.has("entries") && entriesArray != null && !entriesArray.isEmpty()) {
JSONArray finalEntries = entriesArray;
IntStream.range(0, finalEntries.length()).forEach(idx -> {
JSONObject objJSON = (JSONObject) finalEntries.get(idx);
handleJSONObject(finalEntries, objJSON, idx);
});
}
if (responseJSON.has("entry") && !responseJSON.optJSONObject("entry").isEmpty()) {
JSONObject entry = responseJSON.optJSONObject("entry");
if (!entry.isEmpty()) {
if (entry.has("uid") && entry.opt("uid").equals(this.config.livePreviewEntry.opt("uid"))) {
JSONObject entryObj = responseJSON.optJSONObject("entry");
if (responseJSON.has("entry") && entryObj != null && !entryObj.isEmpty()) {
JSONObject entry = entryObj;
if (!entry.isEmpty() && this.config.livePreviewEntry != null) {
Object entryUid = entry.opt("uid");
Object previewUid = this.config.livePreviewEntry.opt("uid");
if (entryUid != null && java.util.Objects.equals(entryUid, previewUid)) {
responseJSON = new JSONObject().put("entry", this.config.livePreviewEntry);
}
}
Expand All @@ -287,8 +291,10 @@ void handleJSONArray() {
}

void handleJSONObject(JSONArray arrayEntry, JSONObject jsonObj, int idx) {
if (!jsonObj.isEmpty()) {
if (jsonObj.has("uid") && jsonObj.opt("uid").equals(this.config.livePreviewEntry.opt("uid"))) {
if (!jsonObj.isEmpty() && this.config.livePreviewEntry != null) {
Object entryUid = jsonObj.opt("uid");
Object previewUid = this.config.livePreviewEntry.opt("uid");
if (entryUid != null && java.util.Objects.equals(entryUid, previewUid)) {
arrayEntry.put(idx, this.config.livePreviewEntry);
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/contentstack/sdk/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ protected Config setLivePreviewEntry(@NotNull JSONObject livePreviewEntry) {
return this;
}

protected void clearLivePreviewEntry() {
this.livePreviewEntry = null;
}

/**
* Sets preview token.
*
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/contentstack/sdk/Entry.java
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,14 @@ public void fetch(EntryResultCallBack callback) {
logger.log(Level.SEVERE, ErrorMessages.ENTRY_FETCH_FAILED, e);
}
}
Config config = contentType.stackInstance.config;
if (config.enableLivePreview && config.livePreviewEntry != null && !config.livePreviewEntry.isEmpty()
&& java.util.Objects.equals(config.livePreviewEntryUid, uid)
&& contentTypeUid != null && contentTypeUid.equalsIgnoreCase(config.livePreviewContentType)) {
this.configure(config.livePreviewEntry);
callback.onRequestFinish(ResponseType.NETWORK);
return;
}
String urlString = "content_types/" + contentTypeUid + "/entries/" + uid;
JSONObject urlQueries = new JSONObject();
urlQueries.put(ENVIRONMENT, headers.get(ENVIRONMENT));
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/contentstack/sdk/ErrorMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ private ErrorMessages() {

public static final String MISSING_PREVIEW_TOKEN = "Missing preview token for rest-preview.contentstack.com. Set the preview token in your configuration to use Live Preview.";
public static final String LIVE_PREVIEW_NOT_ENABLED = "Live Preview is not enabled in the configuration. Enable it and try again.";
public static final String LIVE_PREVIEW_HOST_NOT_ENABLED = "Live Preview host is not set. Call config.setLivePreviewHost(\"rest-preview.contentstack.com\") (or your preview host) before using Live Preview.";
public static final String EMBEDDED_ITEMS_NOT_INCLUDED = "Embedded items are not included in the entry. Call includeEmbeddedItems() and try again.";

// ========== OPERATION ERRORS ==========
Expand Down
Loading
Loading