Skip to content

Commit 564ad8f

Browse files
committed
feat : Live preview 2.0 implementation
1 parent c101022 commit 564ad8f

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/main/java/com/contentstack/sdk/Config.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class Config {
2626
protected JSONObject livePreviewEntry = null;
2727
protected ContentstackRegion region = ContentstackRegion.US;
2828
protected String managementToken;
29+
protected String previewToken;
2930
protected String branch;
3031
protected Proxy proxy = null;
3132
protected String[] earlyAccess = null;
@@ -181,6 +182,17 @@ protected Config setLivePreviewEntry(@NotNull JSONObject livePreviewEntry) {
181182
return this;
182183
}
183184

185+
/**
186+
* Sets preview token.
187+
*
188+
* @param previewToken the preview token
189+
* @return the preview token
190+
*/
191+
public Config setPreviewToken(@NotNull String previewToken){
192+
this.previewToken = previewToken;
193+
return this;
194+
}
195+
184196
/**
185197
* Sets management token.
186198
*

src/main/java/com/contentstack/sdk/Stack.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import org.jetbrains.annotations.NotNull;
77
import org.json.JSONArray;
88
import org.json.JSONObject;
9+
10+
import com.contentstack.sdk.Constants.REQUEST_CONTROLLER;
11+
912
import retrofit2.Response;
1013
import retrofit2.Retrofit;
1114

@@ -98,6 +101,15 @@ private void client(String endpoint) {
98101

99102
private void includeLivePreview() {
100103
if (config.enableLivePreview) {
104+
String urlLivePreview = config.livePreviewHost;
105+
if(config.region != null && !config.region.name().isEmpty()){
106+
if(config.region.name() == "US" ){
107+
config.livePreviewHost = urlLivePreview;
108+
}else{
109+
String regionPrefix = config.region.name().toLowerCase();
110+
config.livePreviewHost = regionPrefix + "-" + urlLivePreview;
111+
}
112+
}
101113
this.livePreviewEndpoint = "https://".concat(config.livePreviewHost).concat("/v3/content_types/");
102114
}
103115
}
@@ -125,6 +137,7 @@ private void includeLivePreview() {
125137
* @throws IOException IO Exception
126138
*/
127139
public Stack livePreviewQuery(Map<String, String> query) throws IOException {
140+
if(config.enableLivePreview){
128141
config.livePreviewHash = query.get(LIVE_PREVIEW);
129142
config.livePreviewEntryUid = query.get(ENTRY_UID);
130143
config.livePreviewContentType = query.get(CONTENT_TYPE_UID);
@@ -137,7 +150,17 @@ public Stack livePreviewQuery(Map<String, String> query) throws IOException {
137150
try {
138151
LinkedHashMap<String, Object> liveHeader = new LinkedHashMap<>();
139152
liveHeader.put("api_key", this.headers.get("api_key"));
140-
liveHeader.put("authorization", config.managementToken);
153+
154+
if(config.livePreviewHost.equals("rest-preview.contentstack.com"))
155+
{
156+
if(config.previewToken != null) {
157+
liveHeader.put("preview_token", config.previewToken);
158+
} else{
159+
throw new IllegalAccessError("Provide the Preview Token for the host rest-preview.contentstack.com");
160+
}
161+
} else {
162+
liveHeader.put("authorization", config.managementToken);
163+
}
141164
response = this.service.getRequest(livePreviewUrl, liveHeader).execute();
142165
} catch (IOException e) {
143166
throw new IllegalStateException("IO Exception while executing the Live Preview url");
@@ -150,6 +173,9 @@ public Stack livePreviewQuery(Map<String, String> query) throws IOException {
150173
config.setLivePreviewEntry(liveResponse.getJSONObject("entry"));
151174
}
152175
}
176+
} else {
177+
throw new IllegalStateException("Live Preview is not enabled in Config");
178+
}
153179
return this;
154180
}
155181

0 commit comments

Comments
 (0)