2
2
3
3
import okhttp3 .Request ;
4
4
import okhttp3 .ResponseBody ;
5
+
5
6
import org .json .JSONArray ;
6
7
import org .json .JSONException ;
7
8
import org .json .JSONObject ;
9
+
8
10
import retrofit2 .Call ;
9
11
import retrofit2 .Response ;
10
12
11
13
import java .io .IOException ;
12
14
import java .io .UnsupportedEncodingException ;
13
15
import java .net .URLEncoder ;
16
+ import java .net .SocketTimeoutException ;
14
17
import java .nio .charset .StandardCharsets ;
15
18
import java .util .HashMap ;
16
19
import java .util .Iterator ;
19
22
import java .util .logging .Level ;
20
23
import java .util .logging .Logger ;
21
24
import 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 ;
22
29
23
30
import static com .contentstack .sdk .Constants .*;
24
31
@@ -185,6 +192,14 @@ public void send() {
185
192
}
186
193
}
187
194
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
+
188
203
private void getService (String requestUrl ) throws IOException {
189
204
190
205
this .headers .put (X_USER_AGENT_KEY , "contentstack-delivery-java/" + SDK_VERSION );
@@ -202,22 +217,41 @@ private void getService(String requestUrl) throws IOException {
202
217
requestUrl = request .url ().toString ();
203
218
}
204
219
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 ());
214
247
}
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 ());
219
254
}
220
-
221
255
}
222
256
223
257
private Request pluginRequestImp (String requestUrl ) {
@@ -261,7 +295,13 @@ void handleJSONObject(JSONArray arrayEntry, JSONObject jsonObj, int idx) {
261
295
}
262
296
263
297
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
+ }
265
305
responseJSON .put (ERROR_MESSAGE , responseJSON .optString (ERROR_MESSAGE ));
266
306
responseJSON .put (ERROR_CODE , responseJSON .optString (ERROR_CODE ));
267
307
responseJSON .put (ERRORS , responseJSON .optString (ERRORS ));
0 commit comments