Skip to content

Commit ef29655

Browse files
authored
Merge pull request #132 from contentstack/next
Next
2 parents b806a0f + aa637ad commit ef29655

File tree

8 files changed

+123
-15
lines changed

8 files changed

+123
-15
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66

77
-Feature: Variants method introduced
88

9+
## v1.15.0
10+
11+
### Date: 04-Sep-2024
12+
13+
- Live Preview 2.0 support added
14+
915
## v1.14.2
1016

1117
### Date: 15-July-2024

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

+12
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

+27-1
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

src/test/java/com/contentstack/sdk/TestAsset.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public void onCompletion(ResponseType responseType, List<Asset> assets, Error er
3838
Asset model = assets.get(0);
3939
assetUid = model.getAssetUid();
4040
Assertions.assertTrue(model.getAssetUid().startsWith("blt"));
41-
Assertions.assertEquals("image/jpeg", model.getFileType());
42-
Assertions.assertEquals("1775299", model.getFileSize());
43-
Assertions.assertEquals("phoenix2.jpg", model.getFileName());
44-
Assertions.assertTrue(model.getUrl().endsWith("phoenix2.jpg"));
41+
Assertions.assertEquals("image/png", model.getFileType());
42+
Assertions.assertEquals("13006", model.getFileSize());
43+
Assertions.assertEquals("iot-icon.png", model.getFileName());
44+
Assertions.assertTrue(model.getUrl().endsWith("iot-icon.png"));
4545
Assertions.assertTrue(model.toJSON().has("created_at"));
4646
Assertions.assertTrue(model.getCreatedBy().startsWith("blt"));
4747
Assertions.assertEquals("gregory", model.getUpdateAt().getCalendarType());
@@ -60,10 +60,10 @@ void testNewAssetZOnlyForOrderByUid() {
6060
@Override
6161
public void onCompletion(ResponseType responseType, Error error) {
6262
Assertions.assertTrue(asset.getAssetUid().startsWith("blt"));
63-
Assertions.assertEquals("image/jpeg", asset.getFileType());
64-
Assertions.assertEquals("1775299", asset.getFileSize());
65-
Assertions.assertEquals("phoenix2.jpg", asset.getFileName());
66-
Assertions.assertTrue(asset.getUrl().endsWith("phoenix2.jpg"));
63+
Assertions.assertEquals("image/png", asset.getFileType());
64+
Assertions.assertEquals("13006", asset.getFileSize());
65+
Assertions.assertEquals("iot-icon.png", asset.getFileName());
66+
Assertions.assertTrue(asset.getUrl().endsWith("iot-icon.png"));
6767
Assertions.assertTrue(asset.toJSON().has("created_at"));
6868
Assertions.assertTrue(asset.getCreatedBy().startsWith("blt"));
6969
Assertions.assertEquals("gregory", asset.getUpdateAt().getCalendarType());

src/test/java/com/contentstack/sdk/TestAssetLibrary.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ void testNewAssetLibrary() {
2424
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
2525
Asset model = assets.get(0);
2626
Assertions.assertTrue(model.getAssetUid().startsWith("blt"));
27-
assertEquals("image/jpeg", model.getFileType());
28-
assertEquals("1775299", model.getFileSize());
29-
assertEquals("phoenix2.jpg", model.getFileName());
30-
Assertions.assertTrue(model.getUrl().endsWith("phoenix2.jpg"));
27+
assertEquals("image/png", model.getFileType());
28+
assertEquals("13006", model.getFileSize());
29+
assertEquals("iot-icon.png", model.getFileName());
30+
Assertions.assertTrue(model.getUrl().endsWith("iot-icon.png"));
3131
Assertions.assertTrue(model.toJSON().has("created_at"));
3232
Assertions.assertTrue(model.getCreatedBy().startsWith("blt"));
3333
assertEquals("gregory", model.getUpdateAt().getCalendarType());

src/test/java/com/contentstack/sdk/TestEntry.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public void onCompletion(ResponseType responseType, Error error) {
105105
@Test
106106
@Order(4)
107107
void entryCalling() {
108-
Assertions.assertEquals(6, entry.headers.size());
108+
Assertions.assertEquals(7, entry.headers.size());
109109
logger.info("passed...");
110110
}
111111

src/test/java/com/contentstack/sdk/TestLivePreview.java

+64
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
import org.junit.jupiter.api.Disabled;
66
import org.junit.jupiter.api.Test;
77

8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.assertThrows;
10+
11+
import java.io.IOException;
812
import java.util.HashMap;
913
import java.util.logging.Level;
1014
import java.util.logging.Logger;
@@ -26,6 +30,7 @@ public static void setUp() {
2630
config = new Config();
2731
}
2832

33+
2934
/**
3035
* Test config test.
3136
*/
@@ -158,5 +163,64 @@ void testCompleteLivePreviewInQuery() throws Exception {
158163
Assertions.assertNotNull(entry);
159164
}
160165

166+
@Test
167+
void testCompleteLivePreviewWithPreviewToken() throws IOException, IllegalAccessException {
168+
Config livePreviewConfig = new Config()
169+
.enableLivePreview(true)
170+
.setLivePreviewHost("rest-preview.contentstack.com")
171+
.setPreviewToken("preview_token");
172+
173+
Stack stack = Contentstack.stack("stackApiKey", "deliveryToken", "env1", livePreviewConfig);
174+
175+
HashMap<String, String> hashMap = new HashMap<>();
176+
hashMap.put("live_preview", "hash167673");
177+
hashMap.put("content_type_uid", "page");
178+
179+
stack.livePreviewQuery(hashMap);
180+
Entry entry = stack.contentType("page").entry("entry_uid");
181+
entry.fetch(null);
182+
Assertions.assertNotNull(entry);
183+
184+
}
185+
186+
@Test()
187+
void testLivePreviewWithoutPreviewToken() throws Exception {
188+
Config livePreviewEnablerConfig = new Config().enableLivePreview(true).setLivePreviewHost("rest-preview.contentstack.com")
189+
.setManagementToken("fake@token");
190+
Stack stack = Contentstack.stack("stackApiKey", "deliveryToken", "env1", livePreviewEnablerConfig);
191+
HashMap<String, String> hashMap = new HashMap<>();
192+
hashMap.put("live_preview", "hash167673");
193+
hashMap.put("content_type_uid", "page");
194+
195+
IllegalAccessError thrown = Assertions.assertThrows(IllegalAccessError.class, () -> {
196+
stack.livePreviewQuery(hashMap);
197+
}, "Expected livePreviewQuery to throw IllegalAccessError");
198+
199+
Assertions.assertTrue(thrown.getMessage().contains("Provide the Preview Token for the host rest-preview.contentstack.com"),
200+
"Exception message should mention that Preview Token is required");
201+
202+
logger.severe(thrown.getMessage());
203+
}
204+
205+
@Test
206+
void testLivePreviewDisabled() throws IllegalAccessException, IOException {
207+
Config config = new Config()
208+
.enableLivePreview(false)
209+
.setPreviewToken("preview_token");
210+
211+
Stack stack = Contentstack.stack("stackApiKey", "deliveryToken", "env1", config);
212+
213+
HashMap<String, String> hashMap = new HashMap<>();
214+
hashMap.put("live_preview", "hash167673");
215+
hashMap.put("content_type_uid", "page");
216+
217+
Exception exception = assertThrows(IllegalStateException.class, () -> {
218+
stack.livePreviewQuery(hashMap);
219+
});
220+
221+
// Optionally, you can check the message of the exception
222+
assertEquals("Live Preview is not enabled in Config", exception.getMessage(),
223+
"Expected exception message does not match");
224+
}
161225

162226
}

src/test/java/com/contentstack/sdk/TestStack.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ void testGetAllContentTypes() {
304304
@Override
305305
public void onCompletion(ContentTypesModel contentTypesModel, Error error) {
306306
assertTrue(contentTypesModel.getResponse() instanceof JSONArray);
307-
assertEquals(5, ((JSONArray) contentTypesModel.getResponse()).length());
307+
assertEquals(8, ((JSONArray) contentTypesModel.getResponse()).length());
308308
}
309309
});
310310
}

0 commit comments

Comments
 (0)