Improve favicon/logo response content type handling

issue-9736
Mohamed Rashd 3 years ago
parent 460675fcad
commit b8a0ffe7c0

@ -19,6 +19,7 @@
package org.wso2.carbon.device.mgt.common; package org.wso2.carbon.device.mgt.common;
public class FileResponse { public class FileResponse {
private static final String DEFAULT_MIME_TYPE = "application/octet-stream";
private byte[] fileContent; private byte[] fileContent;
private String mimeType; private String mimeType;
@ -53,10 +54,13 @@ public class FileResponse {
GIF; GIF;
public String mimeType() { public String mimeType() {
return "application/octet-stream"; return DEFAULT_MIME_TYPE;
} }
public static String mimeTypeOf(String extension) { public static String mimeTypeOf(String extension) {
if (extension.isEmpty()) {
return DEFAULT_MIME_TYPE;
}
ImageExtension imageExtension = ImageExtension.valueOf(extension.toUpperCase()); ImageExtension imageExtension = ImageExtension.valueOf(extension.toUpperCase());
return imageExtension.mimeType(); return imageExtension.mimeType();
} }

@ -99,22 +99,7 @@ public class WhiteLabelManagementServiceImpl implements WhiteLabelManagementServ
if (image.getImageLocationType() == WhiteLabelImage.ImageLocationType.URL) { if (image.getImageLocationType() == WhiteLabelImage.ImageLocationType.URL) {
return getImageFileResponseFromUrl(image.getImageLocation()); return getImageFileResponseFromUrl(image.getImageLocation());
} }
return getImageFileResponseFromStorage(image, imageName); return WhiteLabelStorageUtil.getWhiteLabelImageStream(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;
} }
/** /**

@ -17,10 +17,12 @@
package org.wso2.carbon.device.mgt.core.metadata.mgt.util; 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.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.Base64File; 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.MetadataManagementException;
import org.wso2.carbon.device.mgt.common.exceptions.NotFoundException; import org.wso2.carbon.device.mgt.common.exceptions.NotFoundException;
import org.wso2.carbon.device.mgt.common.metadata.mgt.WhiteLabelImage; import org.wso2.carbon.device.mgt.common.metadata.mgt.WhiteLabelImage;
@ -126,8 +128,9 @@ public class WhiteLabelStorageUtil {
* @param imageName (i.e: LOGO) * @param imageName (i.e: LOGO)
* @return white label image input stream * @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 { throws MetadataManagementException, NotFoundException {
FileResponse fileResponse = new FileResponse();
String fullPathToFile = getPathToImage(image, imageName); String fullPathToFile = getPathToImage(image, imageName);
try { try {
InputStream imageStream = StorageManagementUtil.getInputStream(fullPathToFile); InputStream imageStream = StorageManagementUtil.getInputStream(fullPathToFile);
@ -136,7 +139,12 @@ public class WhiteLabelStorageUtil {
log.error(msg); log.error(msg);
throw new NotFoundException(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) { } catch (IOException e) {
String msg = "Error occurred when accessing the file in file path: " + fullPathToFile; String msg = "Error occurred when accessing the file in file path: " + fullPathToFile;
log.error(msg, e); log.error(msg, e);

Loading…
Cancel
Save