66import org .jetbrains .annotations .NotNull ;
77import org .json .JSONArray ;
88import org .json .JSONObject ;
9+
10+ import com .contentstack .sdk .Constants .REQUEST_CONTROLLER ;
11+
912import retrofit2 .Response ;
1013import 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