1717package io .cdap .plugin .servicenow .restapi ;
1818
1919import com .google .gson .Gson ;
20+ import com .google .gson .JsonElement ;
2021import com .google .gson .JsonObject ;
22+ import com .google .gson .stream .JsonReader ;
2123import io .cdap .plugin .servicenow .apiclient .ServiceNowAPIException ;
2224import io .cdap .plugin .servicenow .util .ServiceNowConstants ;
2325import org .apache .http .Header ;
26+ import org .apache .http .HttpEntity ;
2427import org .apache .http .HttpResponse ;
2528import org .apache .http .HttpStatus ;
2629import org .apache .http .util .EntityUtils ;
2730
2831import java .io .IOException ;
32+ import java .io .InputStream ;
33+ import java .io .InputStreamReader ;
34+ import java .nio .charset .StandardCharsets ;
2935import java .util .Arrays ;
3036import java .util .Collections ;
3137import java .util .HashMap ;
3238import java .util .HashSet ;
3339import java .util .List ;
3440import java .util .Map ;
41+ import java .util .Optional ;
3542import java .util .Set ;
3643import java .util .stream .Collectors ;
3744import javax .annotation .Nullable ;
@@ -46,9 +53,14 @@ public class RestAPIResponse {
4653 private static final Set <Integer > SUCCESS_CODES = new HashSet <>(Arrays .asList (HttpStatus .SC_CREATED ,
4754 HttpStatus .SC_OK ));
4855 private final Map <String , String > headers ;
49- private final String responseBody ;
56+ // Deprecated: storing full body as String can cause OOM
57+ @ Deprecated
58+ private String responseBody ;
5059 @ Nullable private final ServiceNowAPIException exception ;
5160
61+ // New: store InputStream for streaming consumption
62+ private InputStream inputStream ;
63+
5264 public RestAPIResponse (
5365 Map <String , String > headers ,
5466 @ Nullable String responseBody ,
@@ -58,6 +70,15 @@ public RestAPIResponse(
5870 this .exception = exception ;
5971 }
6072
73+ public RestAPIResponse (
74+ Map <String , String > headers ,
75+ InputStream inputStream ,
76+ @ Nullable ServiceNowAPIException exception ) {
77+ this .headers = headers ;
78+ this .inputStream = inputStream ;
79+ this .exception = exception ;
80+ }
81+
6182 /**
6283 * Parses HttpResponse into RestAPIResponse object when no errors occur.
6384 * Throws a {@link ServiceNowAPIException}.
@@ -80,17 +101,27 @@ public static RestAPIResponse parse(HttpResponse httpResponse, String... headerN
80101
81102 ServiceNowAPIException serviceNowAPIException = validateHttpResponse (httpResponse );
82103 if (serviceNowAPIException != null ) {
83- return new RestAPIResponse (headers , null , serviceNowAPIException );
104+ return new RestAPIResponse (headers , ( InputStream ) null , serviceNowAPIException );
84105 }
85106
86107 String responseBody = null ;
87108 try {
88109 responseBody = EntityUtils .toString (httpResponse .getEntity ());
89110 } catch (IOException e ) {
90- return new RestAPIResponse (headers , null , new ServiceNowAPIException (e , httpResponse ));
111+ return new RestAPIResponse (headers , (String ) null , new ServiceNowAPIException (e , httpResponse ));
112+ }
113+ // Instead of reading the entire entity, store the stream
114+ HttpEntity httpEntity = httpResponse .getEntity ();
115+ InputStream responseStream ;
116+ try {
117+ responseStream = (httpEntity != null ) ? httpEntity .getContent () : null ;
118+ } catch (IOException e ) {
119+ return new RestAPIResponse (headers , (InputStream ) null , new ServiceNowAPIException (e , httpResponse ));
91120 }
92121 serviceNowAPIException = validateRestApiResponse (httpResponse , responseBody );
93- return new RestAPIResponse (headers , responseBody , serviceNowAPIException );
122+ // return new RestAPIResponse(headers, responseBody, serviceNowAPIException);
123+ return new RestAPIResponse (headers , responseStream , serviceNowAPIException );
124+
94125 }
95126
96127 public static RestAPIResponse parse (HttpResponse httpResponse ) throws IOException {
@@ -131,6 +162,10 @@ public String getResponseBody() {
131162 return responseBody ;
132163 }
133164
165+ public InputStream getInputStream () {
166+ return inputStream ;
167+ }
168+
134169 @ Nullable
135170 public ServiceNowAPIException getException () {
136171 return exception ;
0 commit comments