11package com .microsoft .graph .content ;
22
3+ import java .util .Set ;
4+
5+ import org .apache .http .Consts ;
6+ import org .apache .http .HttpEntity ;
7+ import org .apache .http .HttpResponse ;
8+ import org .apache .http .entity .ContentType ;
9+ import org .apache .http .entity .StringEntity ;
10+ import org .apache .http .message .BasicHttpResponse ;
11+ import org .json .simple .JSONArray ;
312import org .json .simple .JSONObject ;
413import org .json .simple .parser .JSONParser ;
514import org .json .simple .parser .ParseException ;
@@ -11,21 +20,48 @@ public class MSBatchResponseContent {
1120 public MSBatchResponseContent (String batchResponseData ) {
1221 JSONParser parser = new JSONParser ();
1322 try {
14- batchResponseObj = (JSONObject ) parser .parse (batchResponseData );
23+ if (batchResponseData != null )
24+ batchResponseObj = (JSONObject ) parser .parse (batchResponseData );
1525 }
1626 catch (ParseException e ) {
1727 }
1828 }
1929
20- public String getResponseById (String requestId ) {
21- if (batchResponseObj .get (requestId ) != null )
22- return batchResponseObj .get (requestId ).toString ();
23- return null ;
30+ public HttpResponse getResponseById (String requestId ) {
31+ if (batchResponseObj == null )
32+ return null ;
33+
34+ JSONArray responses = (JSONArray )batchResponseObj .get ("responses" );
35+ if (responses == null )
36+ return null ;
37+
38+ for (Object response : responses ) {
39+ JSONObject jsonresponse = (JSONObject )response ;
40+ String id = (String )jsonresponse .get ("id" );
41+ if (id .compareTo (requestId ) == 0 ) {
42+ HttpResponse httpresponse = new BasicHttpResponse (null , ((Long )jsonresponse .get ("status" )).intValue (), null );
43+ if (jsonresponse .get ("body" ) != null ) {
44+ HttpEntity entity = new StringEntity (jsonresponse .get ("body" ).toString (), ContentType .APPLICATION_JSON );
45+ httpresponse .setEntity (entity );
46+ }
47+ if (jsonresponse .get ("headers" ) != null ){
48+ JSONObject jsonheaders = (JSONObject )jsonresponse .get ("headers" );
49+ for (Object key : jsonheaders .keySet ()) {
50+ String strkey = (String )key ;
51+ String strvalue = (String )jsonheaders .get (strkey );
52+ httpresponse .setHeader (strkey , strvalue );
53+ }
54+ }
55+ return httpresponse ;
56+
57+ }
58+ }
59+ return null ;
2460 }
2561
2662 public String getResponses () {
2763 if (batchResponseObj != null )
28- return batchResponseObj .toJSONString ();
64+ return (( JSONArray ) batchResponseObj . get ( "responses" )) .toJSONString ();
2965 return null ;
3066 }
3167}
0 commit comments