Skip to content

Commit 744d2c6

Browse files
finito formatting
1 parent 38b8557 commit 744d2c6

1 file changed

Lines changed: 109 additions & 84 deletions

File tree

  • datamodel/openapi/openapi-core-apache/src/main/java/com/sap/cloud/sdk/services/openapi/apache/apiclient

datamodel/openapi/openapi-core-apache/src/main/java/com/sap/cloud/sdk/services/openapi/apache/apiclient/ApiClient.java

Lines changed: 109 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -351,89 +351,6 @@ private ContentType getContentType( @Nonnull final String headerValue )
351351
}
352352
}
353353

354-
/**
355-
* Serialize the given Java object into string according the given Content-Type (only JSON is supported for now).
356-
*
357-
* @param obj
358-
* Object
359-
* @param contentType
360-
* Content type
361-
* @param formParams
362-
* Form parameters
363-
* @return Object
364-
* @throws OpenApiRequestException
365-
* API exception
366-
*/
367-
@Nonnull
368-
private HttpEntity serialize(
369-
@Nullable final Object obj,
370-
@Nonnull final Map<String, Object> formParams,
371-
@Nonnull final ContentType contentType,
372-
@Nonnull final Map<String, String> headerParams )
373-
throws OpenApiRequestException
374-
{
375-
final String mimeType = contentType.getMimeType();
376-
if( isJsonMime(mimeType) ) {
377-
if( "gzip".equals(headerParams.get("Content-Encoding")) ) {
378-
val outputStream = new ByteArrayOutputStream();
379-
try( val gzip = new GZIPOutputStream(outputStream) ) {
380-
gzip.write(objectMapper.writeValueAsBytes(obj));
381-
}
382-
catch( IOException e ) {
383-
throw new OpenApiRequestException(e);
384-
}
385-
return new ByteArrayEntity(outputStream.toByteArray(), contentType.withCharset(StandardCharsets.UTF_8));
386-
} else {
387-
try {
388-
return new StringEntity(
389-
objectMapper.writeValueAsString(obj),
390-
contentType.withCharset(StandardCharsets.UTF_8));
391-
}
392-
catch( JsonProcessingException e ) {
393-
throw new OpenApiRequestException(e);
394-
}
395-
}
396-
} else if( mimeType.equals(ContentType.MULTIPART_FORM_DATA.getMimeType()) ) {
397-
final MultipartEntityBuilder multiPartBuilder = MultipartEntityBuilder.create();
398-
for( final Entry<String, Object> paramEntry : formParams.entrySet() ) {
399-
final Object value = paramEntry.getValue();
400-
if( value instanceof File file ) {
401-
multiPartBuilder.addBinaryBody(paramEntry.getKey(), file);
402-
} else if( value instanceof byte[] byteArray ) {
403-
multiPartBuilder.addBinaryBody(paramEntry.getKey(), byteArray);
404-
} else {
405-
final Charset charset = contentType.getCharset();
406-
if( charset != null ) {
407-
final ContentType customContentType =
408-
ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), charset);
409-
multiPartBuilder
410-
.addTextBody(
411-
paramEntry.getKey(),
412-
parameterToString(paramEntry.getValue()),
413-
customContentType);
414-
} else {
415-
multiPartBuilder.addTextBody(paramEntry.getKey(), parameterToString(paramEntry.getValue()));
416-
}
417-
}
418-
}
419-
return multiPartBuilder.build();
420-
} else if( mimeType.equals(ContentType.APPLICATION_FORM_URLENCODED.getMimeType()) ) {
421-
final List<NameValuePair> formValues = new ArrayList<>();
422-
for( final Entry<String, Object> paramEntry : formParams.entrySet() ) {
423-
formValues.add(new BasicNameValuePair(paramEntry.getKey(), parameterToString(paramEntry.getValue())));
424-
}
425-
return new UrlEncodedFormEntity(formValues, contentType.getCharset());
426-
} else {
427-
// Handle files with unknown content type
428-
if( obj instanceof File file ) {
429-
return new FileEntity(file, contentType);
430-
} else if( obj instanceof byte[] byteArray ) {
431-
return new ByteArrayEntity(byteArray, contentType);
432-
}
433-
throw new OpenApiRequestException("Serialization for content type '" + contentType + "' not supported");
434-
}
435-
}
436-
437354
/**
438355
* Build full URL by concatenating base URL, the given sub path and query parameters.
439356
*
@@ -575,7 +492,7 @@ public <T> T invokeAPI(
575492
if( body != null || !formParams.isEmpty() ) {
576493
if( isBodyAllowed(Method.valueOf(method)) ) {
577494
// Add entity if we have content and a valid method
578-
builder.setEntity(serialize(body, formParams, contentTypeObj, headerParams));
495+
builder.setEntity(serialize(body, formParams, contentTypeObj, headerParams.get("Content-Encoding")));
579496
} else {
580497
throw new OpenApiRequestException("method " + method + " does not support a request body");
581498
}
@@ -593,4 +510,112 @@ public <T> T invokeAPI(
593510
throw new OpenApiRequestException(e);
594511
}
595512
}
513+
514+
/**
515+
* Serialize the given Java object into string according the given Content-Type (only JSON is supported for now).
516+
*
517+
* @param body
518+
* Object
519+
* @param contentType
520+
* Content type
521+
* @param formParams
522+
* Form parameters
523+
* @return Object
524+
* @throws OpenApiRequestException
525+
* API exception
526+
*/
527+
@Nonnull
528+
private HttpEntity serialize(
529+
@Nullable final Object body,
530+
@Nonnull final Map<String, Object> formParams,
531+
@Nonnull final ContentType contentType,
532+
@Nonnull final String contentEncoding )
533+
throws OpenApiRequestException
534+
{
535+
final String mimeType = contentType.getMimeType();
536+
if( isJsonMime(mimeType) ) {
537+
return serializeJson(body, contentType, contentEncoding);
538+
} else if( mimeType.equals(ContentType.MULTIPART_FORM_DATA.getMimeType()) ) {
539+
return serializeMultipart(formParams, contentType);
540+
} else if( mimeType.equals(ContentType.APPLICATION_FORM_URLENCODED.getMimeType()) ) {
541+
return serializeFormUrlEncoded(formParams, contentType);
542+
} else if( body instanceof File file ) {
543+
return new FileEntity(file, contentType);
544+
} else if( body instanceof byte[] byteArray ) {
545+
return new ByteArrayEntity(byteArray, contentType);
546+
}
547+
throw new OpenApiRequestException("Serialization for content type '" + contentType + "' not supported");
548+
}
549+
550+
@Nonnull
551+
private HttpEntity serializeJson(
552+
@Nullable final Object body,
553+
@Nonnull final ContentType contentType,
554+
@Nonnull final String contentEncoding )
555+
throws OpenApiRequestException
556+
{
557+
if( "gzip".equals(contentEncoding) ) {
558+
val outputStream = new ByteArrayOutputStream();
559+
try( val gzip = new GZIPOutputStream(outputStream) ) {
560+
gzip.write(objectMapper.writeValueAsBytes(body));
561+
}
562+
catch( IOException e ) {
563+
throw new OpenApiRequestException(e);
564+
}
565+
return new ByteArrayEntity(outputStream.toByteArray(), contentType.withCharset(StandardCharsets.UTF_8));
566+
}
567+
try {
568+
return new StringEntity(
569+
objectMapper.writeValueAsString(body),
570+
contentType.withCharset(StandardCharsets.UTF_8));
571+
}
572+
catch( JsonProcessingException e ) {
573+
throw new OpenApiRequestException(e);
574+
}
575+
}
576+
577+
@Nonnull
578+
private
579+
HttpEntity
580+
serializeMultipart( @Nonnull final Map<String, Object> formParams, @Nonnull final ContentType contentType )
581+
{
582+
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
583+
for( final Entry<String, Object> entry : formParams.entrySet() ) {
584+
final Object value = entry.getValue();
585+
if( value instanceof File file ) {
586+
builder.addBinaryBody(entry.getKey(), file);
587+
} else if( value instanceof byte[] byteArray ) {
588+
builder.addBinaryBody(entry.getKey(), byteArray);
589+
} else {
590+
addMultipartTextField(builder, entry, contentType);
591+
}
592+
}
593+
return builder.build();
594+
}
595+
596+
private void addMultipartTextField(
597+
@Nonnull final MultipartEntityBuilder builder,
598+
@Nonnull final Entry<String, Object> entry,
599+
@Nonnull final ContentType contentType )
600+
{
601+
final Charset charset = contentType.getCharset();
602+
if( charset != null ) {
603+
final ContentType textContentType = ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), charset);
604+
builder.addTextBody(entry.getKey(), parameterToString(entry.getValue()), textContentType);
605+
} else {
606+
builder.addTextBody(entry.getKey(), parameterToString(entry.getValue()));
607+
}
608+
}
609+
610+
@Nonnull
611+
private
612+
HttpEntity
613+
serializeFormUrlEncoded( @Nonnull final Map<String, Object> formParams, @Nonnull final ContentType contentType )
614+
{
615+
final List<NameValuePair> formValues = new ArrayList<>();
616+
for( final Entry<String, Object> entry : formParams.entrySet() ) {
617+
formValues.add(new BasicNameValuePair(entry.getKey(), parameterToString(entry.getValue())));
618+
}
619+
return new UrlEncodedFormEntity(formValues, contentType.getCharset());
620+
}
596621
}

0 commit comments

Comments
 (0)