22
33import okhttp3 .Request ;
44import okhttp3 .ResponseBody ;
5+
56import org .json .JSONArray ;
67import org .json .JSONException ;
78import org .json .JSONObject ;
9+
810import retrofit2 .Call ;
911import retrofit2 .Response ;
1012
1113import java .io .IOException ;
1214import java .io .UnsupportedEncodingException ;
1315import java .net .URLEncoder ;
16+ import java .net .SocketTimeoutException ;
1417import java .nio .charset .StandardCharsets ;
1518import java .util .HashMap ;
1619import java .util .Iterator ;
1922import java .util .logging .Level ;
2023import java .util .logging .Logger ;
2124import java .util .stream .IntStream ;
25+ import com .fasterxml .jackson .databind .ObjectMapper ; // Jackson for JSON parsing
26+ import com .fasterxml .jackson .databind .json .JsonMapper ;
27+ import com .fasterxml .jackson .databind .node .ObjectNode ;
28+ import com .fasterxml .jackson .databind .type .MapType ;
2229
2330import static com .contentstack .sdk .Constants .*;
2431
@@ -185,6 +192,14 @@ public void send() {
185192 }
186193 }
187194
195+ private JSONObject createOrderedJSONObject (Map <String , Object > map ) {
196+ JSONObject json = new JSONObject ();
197+ for (Map .Entry <String , Object > entry : map .entrySet ()) {
198+ json .put (entry .getKey (), entry .getValue ());
199+ }
200+ return json ;
201+ }
202+
188203 private void getService (String requestUrl ) throws IOException {
189204
190205 this .headers .put (X_USER_AGENT_KEY , "contentstack-delivery-java/" + SDK_VERSION );
@@ -202,22 +217,41 @@ private void getService(String requestUrl) throws IOException {
202217 requestUrl = request .url ().toString ();
203218 }
204219
205- Response <ResponseBody > response = this .service .getRequest (requestUrl , this .headers ).execute ();
206- if (response .isSuccessful ()) {
207- assert response .body () != null ;
208- if (request != null ) {
209- response = pluginResponseImp (request , response );
210- }
211- responseJSON = new JSONObject (response .body ().string ());
212- if (this .config .livePreviewEntry != null && !this .config .livePreviewEntry .isEmpty ()) {
213- handleJSONArray ();
220+ try {
221+ Response <ResponseBody > response = this .service .getRequest (requestUrl , this .headers ).execute ();
222+ if (response .isSuccessful ()) {
223+ assert response .body () != null ;
224+ if (request != null ) {
225+ response = pluginResponseImp (request , response );
226+ }
227+ try {
228+ // Use Jackson to parse the JSON while preserving order
229+ ObjectMapper mapper = JsonMapper .builder ().build ();
230+ MapType type = mapper .getTypeFactory ().constructMapType (LinkedHashMap .class , String .class ,
231+ Object .class );
232+ Map <String , Object > responseMap = mapper .readValue (response .body ().string (), type );
233+
234+ // Use the custom method to create an ordered JSONObject
235+ responseJSON = createOrderedJSONObject (responseMap );
236+ if (this .config .livePreviewEntry != null && !this .config .livePreviewEntry .isEmpty ()) {
237+ handleJSONArray ();
238+ }
239+ connectionRequest .onRequestFinished (CSHttpConnection .this );
240+ } catch (JSONException e ) {
241+ // Handle non-JSON response
242+ setError ("Invalid JSON response" );
243+ }
244+ } else {
245+ assert response .errorBody () != null ;
246+ setError (response .errorBody ().string ());
214247 }
215- connectionRequest .onRequestFinished (CSHttpConnection .this );
216- } else {
217- assert response .errorBody () != null ;
218- setError (response .errorBody ().string ());
248+ } catch (SocketTimeoutException e ) {
249+ // Handle timeout
250+ setError ("Request timed out: " + e .getMessage ());
251+ } catch (IOException e ) {
252+ // Handle other IO exceptions
253+ setError ("IO error occurred: " + e .getMessage ());
219254 }
220-
221255 }
222256
223257 private Request pluginRequestImp (String requestUrl ) {
@@ -261,7 +295,13 @@ void handleJSONObject(JSONArray arrayEntry, JSONObject jsonObj, int idx) {
261295 }
262296
263297 void setError (String errResp ) {
264- responseJSON = new JSONObject (errResp ); // Parse error string to JSONObject
298+ try {
299+ responseJSON = new JSONObject (errResp );
300+ } catch (JSONException e ) {
301+ // If errResp is not valid JSON, create a new JSONObject with the error message
302+ responseJSON = new JSONObject ();
303+ responseJSON .put (ERROR_MESSAGE , errResp );
304+ }
265305 responseJSON .put (ERROR_MESSAGE , responseJSON .optString (ERROR_MESSAGE ));
266306 responseJSON .put (ERROR_CODE , responseJSON .optString (ERROR_CODE ));
267307 responseJSON .put (ERRORS , responseJSON .optString (ERRORS ));
0 commit comments