55package com .sap .cloud .sdk .services .openapi .apache .apiclient ;
66
77import static com .sap .cloud .sdk .services .openapi .apache .apiclient .DefaultApiResponseHandler .isJsonMime ;
8+ import static org .apache .hc .core5 .http .HttpHeaders .CONTENT_ENCODING ;
89
10+ import java .io .ByteArrayOutputStream ;
911import java .io .File ;
12+ import java .io .IOException ;
1013import java .nio .charset .Charset ;
1114import java .nio .charset .StandardCharsets ;
1215import java .nio .charset .UnsupportedCharsetException ;
1316import java .util .ArrayList ;
1417import java .util .List ;
1518import java .util .Map ;
1619import java .util .Set ;
20+ import java .util .zip .GZIPOutputStream ;
1721
1822import javax .annotation .Nonnull ;
1923import javax .annotation .Nullable ;
@@ -95,7 +99,7 @@ static ClassicRequestBuilder buildClassicRequest(
9599
96100 final String url = buildUrl (basePath , path , queryParams , collectionQueryParams , urlQueryDeepObject );
97101 final ContentType contentTypeObj = getContentType (contentType );
98- final HttpEntity entity = createEntity (method , body , formParams , contentTypeObj , objectMapper );
102+ final HttpEntity entity = createEntity (method , body , formParams , contentTypeObj , headerParams , objectMapper );
99103
100104 final ClassicRequestBuilder builder = ClassicRequestBuilder .create (method );
101105 builder .setUri (url );
@@ -122,12 +126,13 @@ private static HttpEntity createEntity(
122126 @ Nullable final Object body ,
123127 @ Nonnull final Map <String , Object > formParams ,
124128 @ Nonnull final ContentType contentType ,
129+ @ Nonnull final Map <String , String > headerParams ,
125130 @ Nonnull final ObjectMapper objectMapper )
126131 throws OpenApiRequestException
127132 {
128133 if ( body != null || !formParams .isEmpty () ) {
129134 if ( isBodyAllowed (Method .valueOf (method )) ) {
130- return serialize (body , formParams , contentType , objectMapper );
135+ return serialize (body , formParams , contentType , headerParams , objectMapper );
131136 } else {
132137 throw new OpenApiRequestException ("method " + method + " does not support a request body" );
133138 }
@@ -159,19 +164,13 @@ private static HttpEntity serialize(
159164 @ Nullable final Object obj ,
160165 @ Nonnull final Map <String , Object > formParams ,
161166 @ Nonnull final ContentType contentType ,
167+ @ Nonnull final Map <String , String > headerParams ,
162168 @ Nonnull final ObjectMapper objectMapper )
163169 throws OpenApiRequestException
164170 {
165171 final String mimeType = contentType .getMimeType ();
166172 if ( isJsonMime (mimeType ) ) {
167- try {
168- return new StringEntity (
169- objectMapper .writeValueAsString (obj ),
170- contentType .withCharset (StandardCharsets .UTF_8 ));
171- }
172- catch ( final JsonProcessingException e ) {
173- throw new OpenApiRequestException (e );
174- }
173+ return serializeJson (obj , contentType , headerParams , objectMapper );
175174 } else if ( mimeType .equals (ContentType .MULTIPART_FORM_DATA .getMimeType ()) ) {
176175 final MultipartEntityBuilder multiPartBuilder = MultipartEntityBuilder .create ();
177176 for ( final Map .Entry <String , Object > paramEntry : formParams .entrySet () ) {
@@ -218,6 +217,41 @@ private static HttpEntity serialize(
218217 }
219218 }
220219
220+ /**
221+ * Serialize JSON body, with optional GZIP compression if Content-Encoding header is set to "gzip".
222+ */
223+ @ Nonnull
224+ private static HttpEntity serializeJson (
225+ @ Nullable final Object body ,
226+ @ Nonnull final ContentType contentType ,
227+ @ Nonnull final Map <String , String > headerParams ,
228+ @ Nonnull final ObjectMapper objectMapper )
229+ throws OpenApiRequestException
230+ {
231+ if ( "gzip" .equalsIgnoreCase (headerParams .get (CONTENT_ENCODING ))
232+ || "gzip" .equalsIgnoreCase (headerParams .get (CONTENT_ENCODING .toLowerCase ())) ) {
233+ final ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
234+ try ( final GZIPOutputStream gzip = new GZIPOutputStream (outputStream ) ) {
235+ gzip .write (objectMapper .writeValueAsBytes (body ));
236+ }
237+ catch ( final IOException e ) {
238+ throw new OpenApiRequestException ("Failed to GZIP compress request body" , e );
239+ }
240+ return new ByteArrayEntity (
241+ outputStream .toByteArray (),
242+ contentType .withCharset (StandardCharsets .UTF_8 ),
243+ "gzip" );
244+ }
245+ try {
246+ return new StringEntity (
247+ objectMapper .writeValueAsString (body ),
248+ contentType .withCharset (StandardCharsets .UTF_8 ));
249+ }
250+ catch ( final JsonProcessingException e ) {
251+ throw new OpenApiRequestException (e );
252+ }
253+ }
254+
221255 /**
222256 * Build full URL by concatenating base URL, the given sub path and query parameters.
223257 */
0 commit comments