mirror of
https://repository.entgra.net/community/device-mgt-core.git
synced 2025-10-06 02:01:45 +00:00
Fix favicon/logo response content type
This commit is contained in:
parent
768950bbb1
commit
460675fcad
@ -87,9 +87,7 @@ public interface WhiteLabelService {
|
||||
|
||||
@GET
|
||||
@Path("/favicon")
|
||||
@Produces(MediaType.APPLICATION_OCTET_STREAM)
|
||||
@ApiOperation(
|
||||
produces = MediaType.APPLICATION_OCTET_STREAM,
|
||||
httpMethod = HTTPConstants.HEADER_GET,
|
||||
value = "Get whitelabel favicon",
|
||||
notes = "Get whitelabel favicon for the tenant of the logged in user",
|
||||
@ -123,9 +121,7 @@ public interface WhiteLabelService {
|
||||
|
||||
@GET
|
||||
@Path("/logo")
|
||||
@Produces(MediaType.APPLICATION_OCTET_STREAM)
|
||||
@ApiOperation(
|
||||
produces = MediaType.APPLICATION_OCTET_STREAM,
|
||||
httpMethod = HTTPConstants.HEADER_GET,
|
||||
value = "Get whitelabel logo",
|
||||
notes = "Get whitelabel logo for the tenant of the logged in user",
|
||||
|
||||
@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.jaxrs.service.impl;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
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.WhiteLabelTheme;
|
||||
@ -52,8 +53,8 @@ public class WhiteLabelServiceImpl implements WhiteLabelService {
|
||||
@Path("/favicon")
|
||||
public Response getWhiteLabelFavicon() {
|
||||
try {
|
||||
byte[] fileContent = DeviceMgtAPIUtils.getWhiteLabelManagementService().getWhiteLabelFavicon();
|
||||
return sendFileStream(fileContent);
|
||||
FileResponse fileResponse = DeviceMgtAPIUtils.getWhiteLabelManagementService().getWhiteLabelFavicon();
|
||||
return sendFileStream(fileResponse);
|
||||
} catch (NotFoundException e) {
|
||||
String msg = "Favicon white label image cannot be found in the system. Updating the whitelabel theme might" +
|
||||
"help restore it";
|
||||
@ -71,8 +72,8 @@ public class WhiteLabelServiceImpl implements WhiteLabelService {
|
||||
@Path("/logo")
|
||||
public Response getWhiteLabelLogo() {
|
||||
try {
|
||||
byte[] fileContent = DeviceMgtAPIUtils.getWhiteLabelManagementService().getWhiteLabelLogo();
|
||||
return sendFileStream(fileContent);
|
||||
FileResponse fileResponse = DeviceMgtAPIUtils.getWhiteLabelManagementService().getWhiteLabelLogo();
|
||||
return sendFileStream(fileResponse);
|
||||
} catch (NotFoundException e) {
|
||||
String msg = "Logo white label image cannot be found in the system. Updating the whitelabel theme might" +
|
||||
"help restore it";
|
||||
@ -131,14 +132,14 @@ public class WhiteLabelServiceImpl implements WhiteLabelService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Useful to send files as application/octet-stream responses
|
||||
* Useful to send file responses
|
||||
*/
|
||||
private Response sendFileStream(byte[] content) {
|
||||
try (ByteArrayInputStream binaryDuplicate = new ByteArrayInputStream(content)) {
|
||||
private Response sendFileStream(FileResponse fileResponse) {
|
||||
try (ByteArrayInputStream binaryDuplicate = new ByteArrayInputStream(fileResponse.getFileContent())) {
|
||||
Response.ResponseBuilder response = Response
|
||||
.ok(binaryDuplicate, MediaType.APPLICATION_OCTET_STREAM);
|
||||
.ok(binaryDuplicate, fileResponse.getMimeType());
|
||||
response.status(Response.Status.OK);
|
||||
response.header("Content-Length", content.length);
|
||||
response.header("Content-Length", fileResponse.getFileContent().length);
|
||||
return response.build();
|
||||
} catch (IOException e) {
|
||||
String msg = "Error occurred while creating input stream from buffer array. ";
|
||||
|
||||
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (c) 2022, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.wso2.carbon.device.mgt.common;
|
||||
|
||||
public class FileResponse {
|
||||
private byte[] fileContent;
|
||||
private String mimeType;
|
||||
|
||||
private String name;
|
||||
|
||||
public byte[] getFileContent() {
|
||||
return fileContent;
|
||||
}
|
||||
|
||||
public void setFileContent(byte[] fileContent) {
|
||||
this.fileContent = fileContent;
|
||||
}
|
||||
|
||||
public String getMimeType() {
|
||||
return mimeType;
|
||||
}
|
||||
|
||||
public void setMimeType(String mimeType) {
|
||||
this.mimeType = mimeType;
|
||||
}
|
||||
|
||||
public enum ImageExtension {
|
||||
SVG() {
|
||||
@Override
|
||||
public String mimeType() {
|
||||
return "image/svg+xml";
|
||||
}
|
||||
},
|
||||
PNG,
|
||||
JPG,
|
||||
JPEG,
|
||||
GIF;
|
||||
|
||||
public String mimeType() {
|
||||
return "application/octet-stream";
|
||||
}
|
||||
|
||||
public static String mimeTypeOf(String extension) {
|
||||
ImageExtension imageExtension = ImageExtension.valueOf(extension.toUpperCase());
|
||||
return imageExtension.mimeType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.name().toLowerCase();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@ -18,6 +18,7 @@
|
||||
|
||||
package org.wso2.carbon.device.mgt.common.metadata.mgt;
|
||||
|
||||
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;
|
||||
|
||||
@ -32,7 +33,7 @@ public interface WhiteLabelManagementService {
|
||||
* @throws MetadataManagementException if error occurred while retrieving favicon
|
||||
* @throws NotFoundException if favicon is not found
|
||||
*/
|
||||
byte[] getWhiteLabelFavicon() throws
|
||||
FileResponse getWhiteLabelFavicon() throws
|
||||
MetadataManagementException, NotFoundException;
|
||||
|
||||
/**
|
||||
@ -41,7 +42,7 @@ public interface WhiteLabelManagementService {
|
||||
* @throws MetadataManagementException if error occurred while retrieving logo
|
||||
* @throws NotFoundException if logo is not found
|
||||
*/
|
||||
byte[] getWhiteLabelLogo() throws
|
||||
FileResponse getWhiteLabelLogo() throws
|
||||
MetadataManagementException, NotFoundException;
|
||||
|
||||
/**
|
||||
|
||||
@ -88,7 +88,7 @@ public class FileUtil {
|
||||
String suffix = generateDuplicateFileNameSuffix(fileNameCount);
|
||||
String fileNameWithoutExtension = extractFileNameWithoutExtension(fileName);
|
||||
String fileNameWithSuffix = fileNameWithoutExtension + suffix;
|
||||
fileNameWithSuffix = fileNameWithSuffix + '.' + extractFileExtension(fileName);
|
||||
fileNameWithSuffix = fileNameWithSuffix + '.' + extractFileExtensionFileName(fileName);
|
||||
return fileNameWithSuffix;
|
||||
}
|
||||
|
||||
@ -111,13 +111,27 @@ public class FileUtil {
|
||||
return suffix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Use to extract file extension from file path
|
||||
*
|
||||
* @param filePath path of the file
|
||||
* @return extension of the file
|
||||
*/
|
||||
public static String extractFileExtensionFromFilePath(String filePath) {
|
||||
File file = new File(filePath);
|
||||
return extractFileExtensionFileName(file.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Use to extract file extension from file name
|
||||
*
|
||||
* @param fileName name of the file
|
||||
* @return extension of the file
|
||||
*/
|
||||
private static String extractFileExtension(String fileName) {
|
||||
public static String extractFileExtensionFileName(String fileName) {
|
||||
if (!fileName.contains(".")) {
|
||||
return "";
|
||||
}
|
||||
return fileName.substring(fileName.lastIndexOf('.') + 1);
|
||||
}
|
||||
|
||||
|
||||
@ -28,6 +28,7 @@ import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
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.exceptions.TransactionManagementException;
|
||||
@ -37,6 +38,8 @@ import org.wso2.carbon.device.mgt.common.metadata.mgt.WhiteLabelImageRequestPayl
|
||||
import org.wso2.carbon.device.mgt.common.metadata.mgt.WhiteLabelManagementService;
|
||||
import org.wso2.carbon.device.mgt.common.metadata.mgt.WhiteLabelTheme;
|
||||
import org.wso2.carbon.device.mgt.common.metadata.mgt.WhiteLabelThemeCreateRequest;
|
||||
import org.wso2.carbon.device.mgt.core.common.util.FileUtil;
|
||||
import org.wso2.carbon.device.mgt.core.common.util.HttpUtil;
|
||||
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
|
||||
import org.wso2.carbon.device.mgt.core.config.metadata.mgt.MetaDataConfiguration;
|
||||
import org.wso2.carbon.device.mgt.core.config.metadata.mgt.whitelabel.WhiteLabelConfiguration;
|
||||
@ -46,7 +49,6 @@ import org.wso2.carbon.device.mgt.core.metadata.mgt.dao.MetadataManagementDAOExc
|
||||
import org.wso2.carbon.device.mgt.core.metadata.mgt.dao.MetadataManagementDAOFactory;
|
||||
import org.wso2.carbon.device.mgt.core.metadata.mgt.dao.util.MetadataConstants;
|
||||
import org.wso2.carbon.device.mgt.core.metadata.mgt.util.WhiteLabelStorageUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@ -66,10 +68,10 @@ public class WhiteLabelManagementServiceImpl implements WhiteLabelManagementServ
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getWhiteLabelFavicon() throws MetadataManagementException, NotFoundException {
|
||||
public FileResponse getWhiteLabelFavicon() throws MetadataManagementException, NotFoundException {
|
||||
try {
|
||||
WhiteLabelTheme whiteLabelTheme = getWhiteLabelTheme();
|
||||
return getImageContent(whiteLabelTheme.getFaviconImage(), WhiteLabelImage.ImageName.FAVICON);
|
||||
return getImageFileResponse(whiteLabelTheme.getFaviconImage(), WhiteLabelImage.ImageName.FAVICON);
|
||||
} catch (IOException e) {
|
||||
String msg = "Error occurred while getting byte content of favicon";
|
||||
log.error(msg, e);
|
||||
@ -78,10 +80,10 @@ public class WhiteLabelManagementServiceImpl implements WhiteLabelManagementServ
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getWhiteLabelLogo() throws MetadataManagementException, NotFoundException {
|
||||
public FileResponse getWhiteLabelLogo() throws MetadataManagementException, NotFoundException {
|
||||
try {
|
||||
WhiteLabelTheme whiteLabelTheme = getWhiteLabelTheme();
|
||||
return getImageContent(whiteLabelTheme.getLogoImage(), WhiteLabelImage.ImageName.LOGO);
|
||||
return getImageFileResponse(whiteLabelTheme.getLogoImage(), WhiteLabelImage.ImageName.LOGO);
|
||||
} catch (IOException e) {
|
||||
String msg = "Error occurred while getting byte content of logo";
|
||||
log.error(msg, e);
|
||||
@ -90,21 +92,36 @@ public class WhiteLabelManagementServiceImpl implements WhiteLabelManagementServ
|
||||
}
|
||||
|
||||
/**
|
||||
* Useful to get white label image file byte content for provided {@link WhiteLabelImage.ImageName}
|
||||
* Useful to get white label image file response for provided {@link WhiteLabelImage.ImageName}
|
||||
*/
|
||||
private byte[] getImageContent(WhiteLabelImage image, WhiteLabelImage.ImageName imageName) throws
|
||||
private FileResponse getImageFileResponse(WhiteLabelImage image, WhiteLabelImage.ImageName imageName) throws
|
||||
IOException, MetadataManagementException, NotFoundException {
|
||||
if (image.getImageLocationType() == WhiteLabelImage.ImageLocationType.URL) {
|
||||
return getImageStreamFromUrl(image.getImageLocation());
|
||||
return getImageFileResponseFromUrl(image.getImageLocation());
|
||||
}
|
||||
InputStream fileStream = WhiteLabelStorageUtil.getWhiteLabelImageStream(image, imageName);
|
||||
return IOUtils.toByteArray(fileStream);
|
||||
return getImageFileResponseFromStorage(image, imageName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Useful to get white label image file byte content from provided url
|
||||
* Useful to get white label image file response from provided image info
|
||||
*/
|
||||
private byte[] getImageStreamFromUrl(String url) throws IOException, NotFoundException {
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Useful to get white label image file response from provided url
|
||||
*/
|
||||
private FileResponse getImageFileResponseFromUrl(String url) throws IOException, NotFoundException {
|
||||
FileResponse fileResponse = new FileResponse();
|
||||
try(CloseableHttpClient client = HttpClients.createDefault()) {
|
||||
HttpGet imageGetRequest = new HttpGet(url);
|
||||
HttpResponse response = client.execute(imageGetRequest);
|
||||
@ -114,7 +131,11 @@ public class WhiteLabelManagementServiceImpl implements WhiteLabelManagementServ
|
||||
log.error(msg);
|
||||
throw new NotFoundException(msg);
|
||||
}
|
||||
return IOUtils.toByteArray(imageStream);
|
||||
byte[] fileContent = IOUtils.toByteArray(imageStream);
|
||||
fileResponse.setFileContent(fileContent);
|
||||
String mimeType = HttpUtil.getContentType(response);
|
||||
fileResponse.setMimeType(mimeType);
|
||||
return fileResponse;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user