diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/FileResponse.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/FileResponse.java index d2b29e34b9f..45ee74e020b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/FileResponse.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/FileResponse.java @@ -19,6 +19,7 @@ package org.wso2.carbon.device.mgt.common; public class FileResponse { + private static final String DEFAULT_MIME_TYPE = "application/octet-stream"; private byte[] fileContent; private String mimeType; @@ -53,10 +54,13 @@ public class FileResponse { GIF; public String mimeType() { - return "application/octet-stream"; + return DEFAULT_MIME_TYPE; } public static String mimeTypeOf(String extension) { + if (extension.isEmpty()) { + return DEFAULT_MIME_TYPE; + } ImageExtension imageExtension = ImageExtension.valueOf(extension.toUpperCase()); return imageExtension.mimeType(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/metadata/mgt/WhiteLabelManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/metadata/mgt/WhiteLabelManagementServiceImpl.java index 6dfbda673b6..73095e36900 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/metadata/mgt/WhiteLabelManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/metadata/mgt/WhiteLabelManagementServiceImpl.java @@ -99,22 +99,7 @@ public class WhiteLabelManagementServiceImpl implements WhiteLabelManagementServ if (image.getImageLocationType() == WhiteLabelImage.ImageLocationType.URL) { return getImageFileResponseFromUrl(image.getImageLocation()); } - return getImageFileResponseFromStorage(image, imageName); - } - - /** - * Useful to get white label image file response from provided image info - */ - private FileResponse getImageFileResponseFromStorage(WhiteLabelImage image, WhiteLabelImage.ImageName imageName) - throws IOException, NotFoundException, MetadataManagementException { - FileResponse fileResponse = new FileResponse(); - InputStream fileStream = WhiteLabelStorageUtil.getWhiteLabelImageStream(image, imageName); - byte[] fileContent = IOUtils.toByteArray(fileStream); - String fileExtension = FileUtil.extractFileExtensionFromFilePath(image.getImageLocation()); - String mimeType = FileResponse.ImageExtension.mimeTypeOf(fileExtension); - fileResponse.setMimeType(mimeType); - fileResponse.setFileContent(fileContent); - return fileResponse; + return WhiteLabelStorageUtil.getWhiteLabelImageStream(image, imageName); } /** diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/metadata/mgt/util/WhiteLabelStorageUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/metadata/mgt/util/WhiteLabelStorageUtil.java index 26e2b23ce1d..341e1130fef 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/metadata/mgt/util/WhiteLabelStorageUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/metadata/mgt/util/WhiteLabelStorageUtil.java @@ -17,10 +17,12 @@ package org.wso2.carbon.device.mgt.core.metadata.mgt.util; +import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.Base64File; +import org.wso2.carbon.device.mgt.common.FileResponse; import org.wso2.carbon.device.mgt.common.exceptions.MetadataManagementException; import org.wso2.carbon.device.mgt.common.exceptions.NotFoundException; import org.wso2.carbon.device.mgt.common.metadata.mgt.WhiteLabelImage; @@ -126,8 +128,9 @@ public class WhiteLabelStorageUtil { * @param imageName (i.e: LOGO) * @return white label image input stream */ - public static InputStream getWhiteLabelImageStream(WhiteLabelImage image, WhiteLabelImage.ImageName imageName) + public static FileResponse getWhiteLabelImageStream(WhiteLabelImage image, WhiteLabelImage.ImageName imageName) throws MetadataManagementException, NotFoundException { + FileResponse fileResponse = new FileResponse(); String fullPathToFile = getPathToImage(image, imageName); try { InputStream imageStream = StorageManagementUtil.getInputStream(fullPathToFile); @@ -136,7 +139,12 @@ public class WhiteLabelStorageUtil { log.error(msg); throw new NotFoundException(msg); } - return imageStream; + byte[] fileContent = IOUtils.toByteArray(imageStream); + String fileExtension = FileUtil.extractFileExtensionFromFilePath(image.getImageLocation()); + String mimeType = FileResponse.ImageExtension.mimeTypeOf(fileExtension); + fileResponse.setMimeType(mimeType); + fileResponse.setFileContent(fileContent); + return fileResponse; } catch (IOException e) { String msg = "Error occurred when accessing the file in file path: " + fullPathToFile; log.error(msg, e);