Improve favicon/logo response content type handling

master
Mohamed Rashd 3 years ago
parent 460675fcad
commit b8a0ffe7c0

@ -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();
}

@ -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);
}
/**

@ -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);

Loading…
Cancel
Save