@@ -615,19 +615,22 @@ class EcpWebService {
615
615
616
616
// Execute request
617
617
try (Response response = client. newCall(requestBuilder. build()). execute()) {
618
- okhttp3.MediaType respContentType = response. body(). contentType()
619
- String subType = respContentType. subtype()?. toLowerCase()
618
+
619
+ ResponseBody responseBody = response. body()
620
+ okhttp3.MediaType respContentType = responseBody. contentType()
621
+ String mediaType = respContentType. toString()?. toLowerCase()
620
622
def responseData
621
- String responseBody = response . body() . string()
622
- switch (subType) {
623
- case " json" :
623
+ if (isTextBased(mediaType)) {
624
+ responseData = responseBody . string()
625
+ if (mediaType . contains( ' json' )) {
624
626
ObjectMapper objectMapper = new ObjectMapper ()
625
- responseData = objectMapper. readValue(responseBody, Object )
626
- break
627
- case " text" :
628
- default :
629
- responseData = responseBody
630
- break
627
+ responseData = objectMapper. readValue(responseData, Object )
628
+ }
629
+ } else {
630
+ // This is only used for the /document/createThumbnail endpoint. Because the thumbnail is small we
631
+ // can get away with reading the entire response into memory. If dealing with large files
632
+ // we might have to remove the try with resources and handle the stream manually.
633
+ responseData = responseBody. bytes()
631
634
}
632
635
633
636
result. statusCode = result. status = response. code()
@@ -641,6 +644,18 @@ class EcpWebService {
641
644
return result
642
645
}
643
646
647
+ private static boolean isTextBased (String contentType ) {
648
+ if (! contentType) {
649
+ log. error(" Missing content type" )
650
+ return false
651
+ }
652
+ String contentTypeLower = contentType. toLowerCase()
653
+ return contentTypeLower. contains(' json' ) ||
654
+ contentTypeLower. contains(' text' ) ||
655
+ contentTypeLower. contains(' xml' ) ||
656
+ contentTypeLower. contains(' graphql' )
657
+ }
658
+
644
659
/**
645
660
* Forwards a HTTP multipart/form-data request to ecodata.
646
661
* @param url the URL to forward to.
@@ -660,7 +675,7 @@ class EcpWebService {
660
675
}
661
676
662
677
Map postMultipart (String url , Map params , File file , String contentType , String originalFilename , String fileParamName = ' files' , boolean useToken = false , boolean userToken = false ) {
663
- RequestBody fileBody = RequestBody . create(okhttp3.MediaType . parse(contentType), file )
678
+ RequestBody fileBody = RequestBody . create(file, okhttp3.MediaType . parse(contentType))
664
679
postMultipart(url, params, fileBody, contentType, originalFilename, fileParamName, useToken, userToken)
665
680
}
666
681
0 commit comments