This is an automated email from the ASF dual-hosted git repository.

aleks pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/fineract.git


The following commit(s) were added to refs/heads/develop by this push:
     new a3ab467  Fix flaky GET {entity}/{entityId}/images Content-Type bug 
(FINERACT-1265)
a3ab467 is described below

commit a3ab467278c57fb654f20810d0ccddd985d586dc
Author: Michael Vorburger <[email protected]>
AuthorDate: Sat Nov 21 17:46:32 2020 +0100

    Fix flaky GET {entity}/{entityId}/images Content-Type bug (FINERACT-1265)
---
 .../documentmanagement/api/ImagesApiResource.java  | 63 +++++++++-------------
 .../self/client/api/SelfClientsApiResource.java    |  2 +-
 2 files changed, 26 insertions(+), 39 deletions(-)

diff --git 
a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/documentmanagement/api/ImagesApiResource.java
 
b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/documentmanagement/api/ImagesApiResource.java
index ed8a5b4..4806ba7 100644
--- 
a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/documentmanagement/api/ImagesApiResource.java
+++ 
b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/documentmanagement/api/ImagesApiResource.java
@@ -82,8 +82,8 @@ public class ImagesApiResource {
      * Upload images through multi-part form upload
      */
     @POST
-    @Consumes({ MediaType.MULTIPART_FORM_DATA })
-    @Produces({ MediaType.APPLICATION_JSON })
+    @Consumes(MediaType.MULTIPART_FORM_DATA)
+    @Produces(MediaType.APPLICATION_JSON)
     public String addNewClientImage(@PathParam("entity") final String 
entityName, @PathParam("entityId") final Long entityId,
             @HeaderParam("Content-Length") final Long fileSize, 
@FormDataParam("file") final InputStream inputStream,
             @FormDataParam("file") final FormDataContentDisposition 
fileDetails, @FormDataParam("file") final FormDataBodyPart bodyPart) {
@@ -106,7 +106,7 @@ public class ImagesApiResource {
      */
     @POST
     @Consumes({ MediaType.TEXT_PLAIN, MediaType.TEXT_HTML, 
MediaType.APPLICATION_JSON })
-    @Produces({ MediaType.APPLICATION_JSON })
+    @Produces(MediaType.APPLICATION_JSON)
     public String addNewClientImage(@PathParam("entity") final String 
entityName, @PathParam("entityId") final Long entityId,
             final String jsonRequestBody) {
         validateEntityTypeforImage(entityName);
@@ -119,14 +119,16 @@ public class ImagesApiResource {
     }
 
     /**
-     * Returns a base 64 encoded client image Data URI
+     * Returns a images, either as Base64 encoded text/plain or as inline or 
attachment with image MIME type as
+     * Content-Type.
      */
     @GET
-    @Consumes({ MediaType.TEXT_PLAIN, MediaType.TEXT_HTML, 
MediaType.APPLICATION_JSON })
-    @Produces({ MediaType.TEXT_PLAIN })
+    @Consumes(MediaType.APPLICATION_JSON)
+    // FINERACT-1265: Do NOT specify @Produces(TEXT_PLAIN) here - it may 
actually not (if it calls the next methods it's
+    // octet-stream)
     public Response retrieveImage(@PathParam("entity") final String 
entityName, @PathParam("entityId") final Long entityId,
             @QueryParam("maxWidth") final Integer maxWidth, 
@QueryParam("maxHeight") final Integer maxHeight,
-            @QueryParam("output") final String output) {
+            @QueryParam("output") final String output, @HeaderParam("Accept") 
String acceptHeader) {
         validateEntityTypeforImage(entityName);
         if 
(EntityTypeForImages.CLIENTS.toString().equalsIgnoreCase(entityName)) {
             
this.context.authenticatedUser().validateHasReadPermission("CLIENTIMAGE");
@@ -134,12 +136,18 @@ public class ImagesApiResource {
             
this.context.authenticatedUser().validateHasReadPermission("STAFFIMAGE");
         }
 
-        if (output != null && (output.equals("octet") || 
output.equals("inline_octet"))) {
-            return downloadClientImage(entityName, entityId, maxWidth, 
maxHeight, output);
-        }
-
         final FileData imageData = 
this.imageReadPlatformService.retrieveImage(entityName, entityId);
+        final FileData resizedImage = imageResizer.resize(imageData, maxWidth, 
maxHeight);
+
+        // If client wants (Accept header) octet-stream, or output="octet" or 
"inline_octet", then send that instead of
+        // text
+        if ("application/octet-stream".equalsIgnoreCase(acceptHeader)
+                || (output != null && (output.equals("octet") || 
output.equals("inline_octet")))) {
+            return ContentResources.fileDataToResponse(resizedImage, 
resizedImage.name() + ImageFileExtension.JPEG,
+                    "inline_octet".equals(output) ? "inline" : "attachment");
+        }
 
+        // Else return response with Base64 encoded
         // TODO: Need a better way of determining image type
         String imageDataURISuffix = 
ContentRepositoryUtils.ImageDataURIsuffix.JPEG.getValue();
         if (StringUtils.endsWith(imageData.name(), 
ContentRepositoryUtils.ImageFileExtension.GIF.getValue())) {
@@ -148,42 +156,22 @@ public class ImagesApiResource {
             imageDataURISuffix = 
ContentRepositoryUtils.ImageDataURIsuffix.PNG.getValue();
         }
 
-        FileData resizedImage = imageResizer.resize(imageData, maxWidth, 
maxHeight);
         try {
             byte[] resizedImageBytes = resizedImage.getByteSource().read();
             final String clientImageAsBase64Text = imageDataURISuffix + 
Base64.getMimeEncoder().encodeToString(resizedImageBytes);
-            return Response.ok(clientImageAsBase64Text).build();
+            return Response.ok(clientImageAsBase64Text, 
MediaType.TEXT_PLAIN_TYPE).build();
         } catch (IOException e) {
             throw new ContentManagementException(imageData.name(), 
e.getMessage(), e);
         }
     }
 
-    @GET
-    @Consumes({ MediaType.APPLICATION_JSON })
-    @Produces({ MediaType.APPLICATION_OCTET_STREAM })
-    public Response downloadClientImage(@PathParam("entity") final String 
entityName, @PathParam("entityId") final Long entityId,
-            @QueryParam("maxWidth") final Integer maxWidth, 
@QueryParam("maxHeight") final Integer maxHeight,
-            @QueryParam("output") String output) {
-        validateEntityTypeforImage(entityName);
-        if 
(EntityTypeForImages.CLIENTS.toString().equalsIgnoreCase(entityName)) {
-            
this.context.authenticatedUser().validateHasReadPermission("CLIENTIMAGE");
-        } else if 
(EntityTypeForImages.STAFF.toString().equalsIgnoreCase(entityName)) {
-            
this.context.authenticatedUser().validateHasReadPermission("STAFFIMAGE");
-        }
-
-        final FileData imageData = 
this.imageReadPlatformService.retrieveImage(entityName, entityId);
-        final FileData resizedImage = imageResizer.resize(imageData, maxWidth, 
maxHeight);
-        return ContentResources.fileDataToResponse(resizedImage, 
resizedImage.name() + ImageFileExtension.JPEG,
-                "inline_octet".equals(output) ? "inline" : "attachment");
-    }
-
     /**
      * This method is added only for consistency with other URL patterns and 
for maintaining consistency of usage of the
      * HTTP "verb" at the client side
      */
     @PUT
-    @Consumes({ MediaType.MULTIPART_FORM_DATA })
-    @Produces({ MediaType.APPLICATION_JSON })
+    @Consumes(MediaType.MULTIPART_FORM_DATA)
+    @Produces(MediaType.APPLICATION_JSON)
     public String updateClientImage(@PathParam("entity") final String 
entityName, @PathParam("entityId") final Long entityId,
             @HeaderParam("Content-Length") final Long fileSize, 
@FormDataParam("file") final InputStream inputStream,
             @FormDataParam("file") final FormDataContentDisposition 
fileDetails, @FormDataParam("file") final FormDataBodyPart bodyPart) {
@@ -198,15 +186,15 @@ public class ImagesApiResource {
      */
     @PUT
     @Consumes({ MediaType.TEXT_PLAIN, MediaType.TEXT_HTML, 
MediaType.APPLICATION_JSON })
-    @Produces({ MediaType.APPLICATION_JSON })
+    @Produces(MediaType.APPLICATION_JSON)
     public String updateClientImage(@PathParam("entity") final String 
entityName, @PathParam("entityId") final Long entityId,
             final String jsonRequestBody) {
         return addNewClientImage(entityName, entityId, jsonRequestBody);
     }
 
     @DELETE
-    @Consumes({ MediaType.APPLICATION_JSON })
-    @Produces({ MediaType.APPLICATION_JSON })
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces(MediaType.APPLICATION_JSON)
     public String deleteClientImage(@PathParam("entity") final String 
entityName, @PathParam("entityId") final Long entityId) {
         validateEntityTypeforImage(entityName);
         this.imageWritePlatformService.deleteImage(entityName, entityId);
@@ -238,5 +226,4 @@ public class ImagesApiResource {
         }
         return false;
     }
-
 }
diff --git 
a/fineract-provider/src/main/java/org/apache/fineract/portfolio/self/client/api/SelfClientsApiResource.java
 
b/fineract-provider/src/main/java/org/apache/fineract/portfolio/self/client/api/SelfClientsApiResource.java
index 6dd3af7..4354b21 100644
--- 
a/fineract-provider/src/main/java/org/apache/fineract/portfolio/self/client/api/SelfClientsApiResource.java
+++ 
b/fineract-provider/src/main/java/org/apache/fineract/portfolio/self/client/api/SelfClientsApiResource.java
@@ -163,7 +163,7 @@ public class SelfClientsApiResource {
 
         validateAppuserClientsMapping(clientId);
 
-        return this.imagesApiResource.retrieveImage("clients", clientId, 
maxWidth, maxHeight, output);
+        return this.imagesApiResource.retrieveImage("clients", clientId, 
maxWidth, maxHeight, output, MediaType.TEXT_PLAIN);
     }
 
     @GET

Reply via email to