forked from community/device-mgt-core
Make whitelabel favion and logo api public Refactor whitelabeling feature in separate classesissue-9736
parent
b894d371fb
commit
7907a1c5ff
@ -0,0 +1,289 @@
|
||||
/*
|
||||
* Copyright (c) 2020, 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.jaxrs.service.api;
|
||||
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import io.swagger.annotations.ApiParam;
|
||||
import io.swagger.annotations.ApiResponse;
|
||||
import io.swagger.annotations.ApiResponses;
|
||||
import io.swagger.annotations.Extension;
|
||||
import io.swagger.annotations.ExtensionProperty;
|
||||
import io.swagger.annotations.Info;
|
||||
import io.swagger.annotations.ResponseHeader;
|
||||
import io.swagger.annotations.SwaggerDefinition;
|
||||
import io.swagger.annotations.Tag;
|
||||
import org.apache.axis2.transport.http.HTTPConstants;
|
||||
import org.wso2.carbon.apimgt.annotations.api.Scope;
|
||||
import org.wso2.carbon.apimgt.annotations.api.Scopes;
|
||||
import org.wso2.carbon.device.mgt.common.metadata.mgt.Metadata;
|
||||
import org.wso2.carbon.device.mgt.common.metadata.mgt.WhiteLabelThemeCreateRequest;
|
||||
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
|
||||
import org.wso2.carbon.device.mgt.jaxrs.beans.MetadataList;
|
||||
import org.wso2.carbon.device.mgt.jaxrs.util.Constants;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.DELETE;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
/**
|
||||
* Metadata related REST-API implementation.
|
||||
*/
|
||||
@SwaggerDefinition(
|
||||
info = @Info(
|
||||
version = "1.0.0",
|
||||
title = "Whitelabel Service",
|
||||
extensions = {
|
||||
@Extension(properties = {
|
||||
@ExtensionProperty(name = "name", value = "WhiteLabelManagement"),
|
||||
@ExtensionProperty(name = "context", value = "/api/device-mgt/v1.0/whitelabel"),
|
||||
})
|
||||
}
|
||||
),
|
||||
tags = {
|
||||
@Tag(name = "device_management")
|
||||
}
|
||||
)
|
||||
@Scopes(
|
||||
scopes = {
|
||||
@Scope(
|
||||
name = "View Whitelabel",
|
||||
description = "View whitelabel details",
|
||||
key = "perm:whitelabel:view",
|
||||
roles = {"Internal/devicemgt-user"},
|
||||
permissions = {"/device-mgt/whitelabel/view"}
|
||||
),
|
||||
@Scope(
|
||||
name = "Update Whitelabel",
|
||||
description = "Updating whitelabel",
|
||||
key = "perm:whitelabel:update",
|
||||
roles = {"Internal/devicemgt-user"},
|
||||
permissions = {"/device-mgt/whitelabel/update"}
|
||||
),
|
||||
}
|
||||
)
|
||||
@Api(value = "Whitelabel Management")
|
||||
@Path("/whitelabel")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
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",
|
||||
tags = "Tenant Metadata Management"
|
||||
)
|
||||
@ApiResponses(
|
||||
value = {
|
||||
@ApiResponse(
|
||||
code = 200,
|
||||
message = "OK. \n Successfully retrieved white label favicon.",
|
||||
response = Metadata.class,
|
||||
responseHeaders = {
|
||||
@ResponseHeader(
|
||||
name = "Content-Type",
|
||||
description = "The content type of the body"),
|
||||
@ResponseHeader(
|
||||
name = "ETag",
|
||||
description = "Entity Tag of the response resource.\n" +
|
||||
"Used by caches, or in conditional requests."),
|
||||
@ResponseHeader(
|
||||
name = "Last-Modified",
|
||||
description = "Date and time the resource was last modified.\n" +
|
||||
"Used by caches, or in conditional requests."),
|
||||
}),
|
||||
@ApiResponse(
|
||||
code = 500,
|
||||
message = "Internal Server Error. " +
|
||||
"\n Server error occurred while getting white label artifact.",
|
||||
response = ErrorResponse.class)
|
||||
})
|
||||
Response getWhiteLabelFavicon();
|
||||
|
||||
@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",
|
||||
tags = "Tenant Metadata Management"
|
||||
)
|
||||
@ApiResponses(
|
||||
value = {
|
||||
@ApiResponse(
|
||||
code = 200,
|
||||
message = "OK. \n Successfully retrieved white label logo.",
|
||||
response = Metadata.class,
|
||||
responseHeaders = {
|
||||
@ResponseHeader(
|
||||
name = "Content-Type",
|
||||
description = "The content type of the body"),
|
||||
@ResponseHeader(
|
||||
name = "ETag",
|
||||
description = "Entity Tag of the response resource.\n" +
|
||||
"Used by caches, or in conditional requests."),
|
||||
@ResponseHeader(
|
||||
name = "Last-Modified",
|
||||
description = "Date and time the resource was last modified.\n" +
|
||||
"Used by caches, or in conditional requests."),
|
||||
}),
|
||||
@ApiResponse(
|
||||
code = 500,
|
||||
message = "Internal Server Error. " +
|
||||
"\n Server error occurred while getting white label artifact.",
|
||||
response = ErrorResponse.class)
|
||||
})
|
||||
Response getWhiteLabelLogo();
|
||||
|
||||
@PUT
|
||||
@ApiOperation(
|
||||
produces = MediaType.APPLICATION_JSON,
|
||||
httpMethod = HTTPConstants.HEADER_POST,
|
||||
value = "Create whitelabel for tenant",
|
||||
notes = "Create whitelabel for the tenant of the logged in user",
|
||||
tags = "Tenant Metadata Management",
|
||||
extensions = {
|
||||
@Extension(properties = {
|
||||
@ExtensionProperty(name = Constants.SCOPE, value = "perm:whitelabel:update")
|
||||
})
|
||||
}
|
||||
)
|
||||
@ApiResponses(
|
||||
value = {
|
||||
@ApiResponse(
|
||||
code = 200,
|
||||
message = "OK. \n Successfully created white label theme.",
|
||||
response = Metadata.class,
|
||||
responseHeaders = {
|
||||
@ResponseHeader(
|
||||
name = "Content-Type",
|
||||
description = "The content type of the body"),
|
||||
@ResponseHeader(
|
||||
name = "ETag",
|
||||
description = "Entity Tag of the response resource.\n" +
|
||||
"Used by caches, or in conditional requests."),
|
||||
@ResponseHeader(
|
||||
name = "Last-Modified",
|
||||
description = "Date and time the resource was last modified.\n" +
|
||||
"Used by caches, or in conditional requests."),
|
||||
}),
|
||||
@ApiResponse(
|
||||
code = 500,
|
||||
message = "Internal Server Error. " +
|
||||
"\n Server error occurred while creating white label theme.",
|
||||
response = ErrorResponse.class)
|
||||
})
|
||||
Response createWhiteLabelTheme(WhiteLabelThemeCreateRequest whiteLabelThemeCreateRequest);
|
||||
|
||||
@GET
|
||||
@ApiOperation(
|
||||
produces = MediaType.APPLICATION_JSON,
|
||||
httpMethod = HTTPConstants.HEADER_POST,
|
||||
value = "Get whitelabel for tenant",
|
||||
notes = "Get whitelabel for the tenant of the logged in user",
|
||||
tags = "Tenant Metadata Management",
|
||||
extensions = {
|
||||
@Extension(properties = {
|
||||
@ExtensionProperty(name = Constants.SCOPE, value = "perm:whitelabel:view")
|
||||
})
|
||||
}
|
||||
)
|
||||
@ApiResponses(
|
||||
value = {
|
||||
@ApiResponse(
|
||||
code = 200,
|
||||
message = "OK. \n Successfully fetched white label theme.",
|
||||
response = Metadata.class,
|
||||
responseHeaders = {
|
||||
@ResponseHeader(
|
||||
name = "Content-Type",
|
||||
description = "The content type of the body"),
|
||||
@ResponseHeader(
|
||||
name = "ETag",
|
||||
description = "Entity Tag of the response resource.\n" +
|
||||
"Used by caches, or in conditional requests."),
|
||||
@ResponseHeader(
|
||||
name = "Last-Modified",
|
||||
description = "Date and time the resource was last modified.\n" +
|
||||
"Used by caches, or in conditional requests."),
|
||||
}),
|
||||
@ApiResponse(
|
||||
code = 500,
|
||||
message = "Internal Server Error. " +
|
||||
"\n Server error occurred while fetching white label theme.",
|
||||
response = ErrorResponse.class)
|
||||
})
|
||||
Response getWhiteLabelTheme();
|
||||
|
||||
@PUT
|
||||
@Path("/reset")
|
||||
@ApiOperation(
|
||||
produces = MediaType.APPLICATION_JSON,
|
||||
httpMethod = HTTPConstants.HEADER_POST,
|
||||
value = "Reset whitelabel for tenant",
|
||||
notes = "Reset whitelabel to default for the tenant of the logged in user",
|
||||
tags = "Tenant Metadata Management",
|
||||
extensions = {
|
||||
@Extension(properties = {
|
||||
@ExtensionProperty(name = Constants.SCOPE, value = "perm:whitelabel:update")
|
||||
})
|
||||
}
|
||||
)
|
||||
@ApiResponses(
|
||||
value = {
|
||||
@ApiResponse(
|
||||
code = 200,
|
||||
message = "OK. \n Successfully fetched white label theme.",
|
||||
response = Metadata.class,
|
||||
responseHeaders = {
|
||||
@ResponseHeader(
|
||||
name = "Content-Type",
|
||||
description = "The content type of the body"),
|
||||
@ResponseHeader(
|
||||
name = "ETag",
|
||||
description = "Entity Tag of the response resource.\n" +
|
||||
"Used by caches, or in conditional requests."),
|
||||
@ResponseHeader(
|
||||
name = "Last-Modified",
|
||||
description = "Date and time the resource was last modified.\n" +
|
||||
"Used by caches, or in conditional requests."),
|
||||
}),
|
||||
@ApiResponse(
|
||||
code = 500,
|
||||
message = "Internal Server Error. " +
|
||||
"\n Server error occurred while deleting white label theme.",
|
||||
response = ErrorResponse.class)
|
||||
})
|
||||
Response resetWhiteLabel();
|
||||
}
|
@ -0,0 +1,172 @@
|
||||
/*
|
||||
* Copyright (c) 2020, 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.jaxrs.service.impl;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.device.mgt.common.PaginationRequest;
|
||||
import org.wso2.carbon.device.mgt.common.PaginationResult;
|
||||
import org.wso2.carbon.device.mgt.common.exceptions.MetadataKeyAlreadyExistsException;
|
||||
import org.wso2.carbon.device.mgt.common.exceptions.MetadataKeyNotFoundException;
|
||||
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.Metadata;
|
||||
import org.wso2.carbon.device.mgt.common.metadata.mgt.MetadataManagementService;
|
||||
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.jaxrs.beans.MetadataList;
|
||||
import org.wso2.carbon.device.mgt.jaxrs.service.api.MetadataService;
|
||||
import org.wso2.carbon.device.mgt.jaxrs.service.api.WhiteLabelService;
|
||||
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil;
|
||||
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.DELETE;
|
||||
import javax.ws.rs.DefaultValue;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* This is the service class for metadata management.
|
||||
*/
|
||||
@Path("/whitelabel")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public class WhiteLabelServiceImpl implements WhiteLabelService {
|
||||
|
||||
private static final Log log = LogFactory.getLog(WhiteLabelServiceImpl.class);
|
||||
|
||||
@GET
|
||||
@Override
|
||||
@Path("/favicon")
|
||||
public Response getWhiteLabelFavicon() {
|
||||
try {
|
||||
byte[] fileContent = DeviceMgtAPIUtils.getWhiteLabelManagementService().getWhiteLabelFavicon();
|
||||
return sendFileStream(fileContent);
|
||||
} catch (NotFoundException e) {
|
||||
String msg = "Couldn't find such a whitelabel artifact.";
|
||||
log.error(msg, e);
|
||||
return Response.status(Response.Status.NOT_FOUND).entity(msg).build();
|
||||
} catch (MetadataManagementException e) {
|
||||
String msg = "Error occurred while creating whitelabel for tenant";
|
||||
log.error(msg, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
||||
} catch (IOException e) {
|
||||
String msg = "Error occurred while getting the byte array of the artifact file. ";
|
||||
log.error(msg, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
||||
}
|
||||
}
|
||||
|
||||
@GET
|
||||
@Override
|
||||
@Path("/logo")
|
||||
public Response getWhiteLabelLogo() {
|
||||
try {
|
||||
byte[] fileContent = DeviceMgtAPIUtils.getWhiteLabelManagementService().getWhiteLabelLogo();
|
||||
return sendFileStream(fileContent);
|
||||
} catch (NotFoundException e) {
|
||||
String msg = "Couldn't find such a whitelabel artifact.";
|
||||
log.error(msg, e);
|
||||
return Response.status(Response.Status.NOT_FOUND).entity(msg).build();
|
||||
} catch (MetadataManagementException e) {
|
||||
String msg = "Error occurred while creating whitelabel for tenant";
|
||||
log.error(msg, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
||||
} catch (IOException e) {
|
||||
String msg = "Error occurred while getting the byte array of the artifact file. ";
|
||||
log.error(msg, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
||||
}
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Override
|
||||
public Response createWhiteLabelTheme(WhiteLabelThemeCreateRequest whiteLabelThemeCreateRequest) {
|
||||
RequestValidationUtil.validateWhiteLabelTheme(whiteLabelThemeCreateRequest);
|
||||
try {
|
||||
WhiteLabelTheme createdWhiteLabelTheme = DeviceMgtAPIUtils.getWhiteLabelManagementService().updateWhiteLabelTheme(whiteLabelThemeCreateRequest);
|
||||
return Response.status(Response.Status.CREATED).entity(createdWhiteLabelTheme).build();
|
||||
} catch (MetadataManagementException e) {
|
||||
String msg = "Error occurred while creating whitelabel for tenant";
|
||||
log.error(msg, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
||||
}
|
||||
}
|
||||
|
||||
@GET
|
||||
@Override
|
||||
public Response getWhiteLabelTheme() {
|
||||
try {
|
||||
WhiteLabelTheme whiteLabelTheme = DeviceMgtAPIUtils.getWhiteLabelManagementService().getWhiteLabelTheme();
|
||||
return Response.status(Response.Status.CREATED).entity(whiteLabelTheme).build();
|
||||
} catch (MetadataManagementException e) {
|
||||
String msg = "Error occurred while deleting whitelabel for tenant";
|
||||
log.error(msg, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
||||
} catch (NotFoundException e) {
|
||||
String msg = "Not white label theme configured for this tenant";
|
||||
log.error(msg, e);
|
||||
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
|
||||
}
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Override
|
||||
@Path("/reset")
|
||||
public Response resetWhiteLabel() {
|
||||
try {
|
||||
DeviceMgtAPIUtils.getWhiteLabelManagementService().resetToDefaultWhiteLabelTheme();
|
||||
return Response.status(Response.Status.CREATED).entity("White label theme deleted successfully.").build();
|
||||
} catch (MetadataManagementException e) {
|
||||
String msg = "Error occurred while resetting whitelabel for tenant";
|
||||
log.error(msg, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Useful to send files as application/octet-stream responses
|
||||
*/
|
||||
private Response sendFileStream(byte[] content) throws IOException {
|
||||
try (ByteArrayInputStream binaryDuplicate = new ByteArrayInputStream(content)) {
|
||||
Response.ResponseBuilder response = Response
|
||||
.ok(binaryDuplicate, MediaType.APPLICATION_OCTET_STREAM);
|
||||
response.status(Response.Status.OK);
|
||||
// response.header("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
|
||||
response.header("Content-Length", content.length);
|
||||
return response.build();
|
||||
} catch (IOException e) {
|
||||
String msg = "Error occurred while creating input stream from buffer array. ";
|
||||
log.error(msg, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package org.wso2.carbon.device.mgt.common.exceptions;
|
||||
|
||||
|
||||
/*
|
||||
* Copyright (c) 2022, Entgra (pvt) Ltd. (http://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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* This exception will be thrown when the requested application or platform not found.
|
||||
*/
|
||||
public class NotFoundException extends Exception {
|
||||
|
||||
public NotFoundException(String message, Throwable throwable) {
|
||||
super(message, throwable);
|
||||
}
|
||||
|
||||
public NotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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.metadata.mgt;
|
||||
|
||||
public class WhiteLabelArtifactPath {
|
||||
private String faviconPath;
|
||||
private String logoPath;
|
||||
|
||||
public WhiteLabelArtifactPath() {
|
||||
|
||||
}
|
||||
|
||||
public WhiteLabelArtifactPath(String faviconPath, String logoPath) {
|
||||
this.faviconPath = faviconPath;
|
||||
this.logoPath = logoPath;
|
||||
}
|
||||
|
||||
public String getFaviconPath() {
|
||||
return faviconPath;
|
||||
}
|
||||
|
||||
public void setFaviconPath(String faviconPath) {
|
||||
this.faviconPath = faviconPath;
|
||||
}
|
||||
|
||||
public String getLogoPath() {
|
||||
return logoPath;
|
||||
}
|
||||
|
||||
public void setLogoPath(String logoPath) {
|
||||
this.logoPath = logoPath;
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package org.wso2.carbon.device.mgt.common.metadata.mgt;
|
||||
|
||||
public class WhiteLabelImage {
|
||||
private ImageLocationType imageLocationType;
|
||||
private String imageLocation;
|
||||
|
||||
public ImageLocationType getImageLocationType() {
|
||||
return imageLocationType;
|
||||
}
|
||||
|
||||
public void setImageLocationType(ImageLocationType imageLocationType) {
|
||||
this.imageLocationType = imageLocationType;
|
||||
}
|
||||
|
||||
public String getImageLocation() {
|
||||
return imageLocation;
|
||||
}
|
||||
|
||||
public void setImageLocation(String imageLocation) {
|
||||
this.imageLocation = imageLocation;
|
||||
}
|
||||
|
||||
public enum ImageName {
|
||||
FAVICON,
|
||||
LOGO;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name().toLowerCase();
|
||||
}
|
||||
}
|
||||
|
||||
public enum ImageLocationType {
|
||||
URL,
|
||||
CUSTOM_FILE,
|
||||
DEFAULT_FILE
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package org.wso2.carbon.device.mgt.common.metadata.mgt;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import org.wso2.carbon.device.mgt.common.Base64File;
|
||||
|
||||
public class WhiteLabelImageRequestPayload {
|
||||
private ImageType imageType;
|
||||
private JsonElement image;
|
||||
|
||||
public ImageType getImageType() {
|
||||
return imageType;
|
||||
}
|
||||
|
||||
public void setImageType(ImageType imageType) {
|
||||
this.imageType = imageType;
|
||||
}
|
||||
|
||||
public JsonElement getImage() {
|
||||
return image;
|
||||
}
|
||||
|
||||
public Base64File getImageAsBase64File() {
|
||||
if (imageType != ImageType.BASE64) {
|
||||
throw new IllegalStateException("Cannot convert image with Image type of " + imageType + " to base64.");
|
||||
}
|
||||
return new Gson().fromJson(image, Base64File.class);
|
||||
}
|
||||
|
||||
public String getImageAsUrl() {
|
||||
if (imageType != ImageType.URL) {
|
||||
throw new IllegalStateException("Cannot convert image with Image type of " + imageType + " to image url string.");
|
||||
}
|
||||
return new Gson().fromJson(image, String.class);
|
||||
}
|
||||
|
||||
public void setImage(JsonElement image) {
|
||||
this.image = image;
|
||||
}
|
||||
|
||||
public enum ImageType {
|
||||
URL,
|
||||
BASE64;
|
||||
|
||||
public WhiteLabelImage.ImageLocationType getDTOImageLocationType() {
|
||||
if (this == URL) {
|
||||
return WhiteLabelImage.ImageLocationType.URL;
|
||||
}
|
||||
return WhiteLabelImage.ImageLocationType.CUSTOM_FILE;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (c) 2020, 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.metadata.mgt;
|
||||
|
||||
import org.wso2.carbon.device.mgt.common.PaginationRequest;
|
||||
import org.wso2.carbon.device.mgt.common.PaginationResult;
|
||||
import org.wso2.carbon.device.mgt.common.exceptions.MetadataKeyAlreadyExistsException;
|
||||
import org.wso2.carbon.device.mgt.common.exceptions.MetadataKeyNotFoundException;
|
||||
import org.wso2.carbon.device.mgt.common.exceptions.MetadataManagementException;
|
||||
import org.wso2.carbon.device.mgt.common.exceptions.NotFoundException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Defines the contract of MetadataManagementService.
|
||||
*/
|
||||
public interface WhiteLabelManagementService {
|
||||
|
||||
byte[] getWhiteLabelFavicon() throws
|
||||
MetadataManagementException, NotFoundException, IOException;
|
||||
byte[] getWhiteLabelLogo() throws
|
||||
MetadataManagementException, NotFoundException, IOException;
|
||||
|
||||
void addDefaultWhiteLabelThemeIfNotExist() throws MetadataManagementException;
|
||||
|
||||
void resetToDefaultWhiteLabelTheme() throws MetadataManagementException;
|
||||
|
||||
WhiteLabelTheme updateWhiteLabelTheme(WhiteLabelThemeCreateRequest createWhiteLabelTheme)
|
||||
throws MetadataManagementException;
|
||||
|
||||
WhiteLabelTheme getWhiteLabelTheme() throws MetadataManagementException, NotFoundException;
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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.metadata.mgt;
|
||||
|
||||
public class WhiteLabelTheme {
|
||||
private WhiteLabelImage faviconImage;
|
||||
private WhiteLabelImage logoImage;
|
||||
private String footerText;
|
||||
|
||||
public String getFooterText() {
|
||||
return footerText;
|
||||
}
|
||||
|
||||
public void setFooterText(String footerText) {
|
||||
this.footerText = footerText;
|
||||
}
|
||||
|
||||
public WhiteLabelImage getFaviconImage() {
|
||||
return faviconImage;
|
||||
}
|
||||
|
||||
public void setFaviconImage(WhiteLabelImage faviconImage) {
|
||||
this.faviconImage = faviconImage;
|
||||
}
|
||||
|
||||
public WhiteLabelImage getLogoImage() {
|
||||
return logoImage;
|
||||
}
|
||||
|
||||
public void setLogoImage(WhiteLabelImage logoImage) {
|
||||
this.logoImage = logoImage;
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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.metadata.mgt;
|
||||
|
||||
public class WhiteLabelThemeCreateRequest {
|
||||
private WhiteLabelImageRequestPayload favicon;
|
||||
private WhiteLabelImageRequestPayload logo;
|
||||
private String footerText;
|
||||
|
||||
public WhiteLabelImageRequestPayload getFavicon() {
|
||||
return favicon;
|
||||
}
|
||||
|
||||
public void setFavicon(WhiteLabelImageRequestPayload favicon) {
|
||||
this.favicon = favicon;
|
||||
}
|
||||
|
||||
public WhiteLabelImageRequestPayload getLogo() {
|
||||
return logo;
|
||||
}
|
||||
|
||||
public void setLogo(WhiteLabelImageRequestPayload logo) {
|
||||
this.logo = logo;
|
||||
}
|
||||
|
||||
public String getFooterText() {
|
||||
return footerText;
|
||||
}
|
||||
|
||||
public void setFooterText(String footerText) {
|
||||
this.footerText = footerText;
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
/* Copyright (c) 2019, 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.core.common.exception;
|
||||
|
||||
/**
|
||||
* Represents the exception thrown during storing and retrieving the artifacts.
|
||||
*/
|
||||
public class StorageManagementException extends Exception {
|
||||
public StorageManagementException(String message, Throwable ex) {
|
||||
super(message, ex);
|
||||
}
|
||||
|
||||
public StorageManagementException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,18 @@
|
||||
package org.wso2.carbon.device.mgt.core.config.tenant.whitelabel;
|
||||
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
@XmlRootElement(name = "MetaDataConfiguration")
|
||||
public class MetaDataConfiguration {
|
||||
private WhiteLabelConfiguration whiteLabelConfiguration;
|
||||
|
||||
@XmlElement(name = "WhiteLabelConfiguration", required = true)
|
||||
public WhiteLabelConfiguration getWhiteLabelConfiguration() {
|
||||
return whiteLabelConfiguration;
|
||||
}
|
||||
|
||||
public void setWhiteLabelConfiguration(WhiteLabelConfiguration whiteLabelConfiguration) {
|
||||
this.whiteLabelConfiguration = whiteLabelConfiguration;
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package org.wso2.carbon.device.mgt.core.config.tenant.whitelabel;
|
||||
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
@XmlRootElement(name = "WhiteLabelConfiguration")
|
||||
public class WhiteLabelConfiguration {
|
||||
private String footerText;
|
||||
private WhiteLabelImages whiteLabelImages;
|
||||
|
||||
@XmlElement(name = "FooterText", required = true)
|
||||
public String getFooterText() {
|
||||
return footerText;
|
||||
}
|
||||
|
||||
public void setFooterText(String footerText) {
|
||||
this.footerText = footerText;
|
||||
}
|
||||
|
||||
@XmlElement(name = "WhiteLabelImages", required = true)
|
||||
public WhiteLabelImages getWhiteLabelImages() {
|
||||
return whiteLabelImages;
|
||||
}
|
||||
|
||||
public void setWhiteLabelImages(WhiteLabelImages whiteLabelImages) {
|
||||
this.whiteLabelImages = whiteLabelImages;
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package org.wso2.carbon.device.mgt.core.config.tenant.whitelabel;
|
||||
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
@XmlRootElement(name = "WhiteLabelImages")
|
||||
public class WhiteLabelImages {
|
||||
|
||||
private String storagePath;
|
||||
private String defaultImagesLocation;
|
||||
private String defaultFaviconName;
|
||||
private String defaultLogoName;
|
||||
|
||||
@XmlElement(name = "StoragePath", required = true)
|
||||
public String getStoragePath() {
|
||||
return storagePath;
|
||||
}
|
||||
|
||||
public void setStoragePath(String storagePath) {
|
||||
this.storagePath = storagePath;
|
||||
}
|
||||
|
||||
@XmlElement(name = "DefaultFaviconName", required = true)
|
||||
public String getDefaultFaviconName() {
|
||||
return defaultFaviconName;
|
||||
}
|
||||
|
||||
public void setDefaultFaviconName(String defaultFaviconName) {
|
||||
this.defaultFaviconName = defaultFaviconName;
|
||||
}
|
||||
|
||||
@XmlElement(name = "DefaultLogoName", required = true)
|
||||
public String getDefaultLogoName() {
|
||||
return defaultLogoName;
|
||||
}
|
||||
|
||||
public void setDefaultLogoName(String defaultLogoName) {
|
||||
this.defaultLogoName = defaultLogoName;
|
||||
}
|
||||
|
||||
@XmlElement(name = "DefaultImagesLocation", required = true)
|
||||
public String getDefaultImagesLocation() {
|
||||
return defaultImagesLocation;
|
||||
}
|
||||
|
||||
public void setDefaultImagesLocation(String defaultImagesLocation) {
|
||||
this.defaultImagesLocation = defaultImagesLocation;
|
||||
}
|
||||
}
|
@ -0,0 +1,313 @@
|
||||
/*
|
||||
* Copyright (c) 2020, 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.core.metadata.mgt;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
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.exceptions.MetadataManagementException;
|
||||
import org.wso2.carbon.device.mgt.common.exceptions.NotFoundException;
|
||||
import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException;
|
||||
import org.wso2.carbon.device.mgt.common.metadata.mgt.Metadata;
|
||||
import org.wso2.carbon.device.mgt.common.metadata.mgt.WhiteLabelImage;
|
||||
import org.wso2.carbon.device.mgt.common.metadata.mgt.WhiteLabelImageRequestPayload;
|
||||
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.config.DeviceConfigurationManager;
|
||||
import org.wso2.carbon.device.mgt.core.config.tenant.whitelabel.MetaDataConfiguration;
|
||||
import org.wso2.carbon.device.mgt.core.config.tenant.whitelabel.WhiteLabelConfiguration;
|
||||
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
|
||||
import org.wso2.carbon.device.mgt.core.metadata.mgt.dao.MetadataDAO;
|
||||
import org.wso2.carbon.device.mgt.core.metadata.mgt.dao.MetadataManagementDAOException;
|
||||
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;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* This class implements the MetadataManagementService.
|
||||
*/
|
||||
public class WhiteLabelManagementServiceImpl implements WhiteLabelManagementService {
|
||||
|
||||
private static final Log log = LogFactory.getLog(WhiteLabelManagementServiceImpl.class);
|
||||
|
||||
private final MetadataDAO metadataDAO;
|
||||
|
||||
public WhiteLabelManagementServiceImpl() {
|
||||
this.metadataDAO = MetadataManagementDAOFactory.getMetadataDAO();
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getWhiteLabelFavicon() throws MetadataManagementException, NotFoundException, IOException {
|
||||
WhiteLabelTheme whiteLabelTheme = getWhiteLabelTheme();
|
||||
return getImageContent(whiteLabelTheme.getFaviconImage(), WhiteLabelImage.ImageName.FAVICON);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getWhiteLabelLogo() throws MetadataManagementException, NotFoundException, IOException {
|
||||
WhiteLabelTheme whiteLabelTheme = getWhiteLabelTheme();
|
||||
return getImageContent(whiteLabelTheme.getLogoImage(), WhiteLabelImage.ImageName.LOGO);
|
||||
}
|
||||
|
||||
private byte[] getImageContent(WhiteLabelImage image, WhiteLabelImage.ImageName imageName) throws
|
||||
IOException, MetadataManagementException, NotFoundException {
|
||||
if (image.getImageLocationType() == WhiteLabelImage.ImageLocationType.URL) {
|
||||
return getImageStreamFromUrl(image.getImageLocation());
|
||||
}
|
||||
InputStream fileStream = WhiteLabelStorageUtil.getWhiteLabelImageStream(image, imageName);
|
||||
return IOUtils.toByteArray(fileStream);
|
||||
}
|
||||
|
||||
private byte[] getImageStreamFromUrl(String url) throws IOException, NotFoundException {
|
||||
try(CloseableHttpClient client = HttpClients.createDefault()) {
|
||||
HttpGet imageGetRequest = new HttpGet(url);
|
||||
HttpResponse response = client.execute(imageGetRequest);
|
||||
InputStream imageStream = response.getEntity().getContent();
|
||||
if (imageStream == null) {
|
||||
String msg = "Failed to retrieve the image from url: " + url;
|
||||
log.error(msg);
|
||||
throw new NotFoundException(msg);
|
||||
}
|
||||
return IOUtils.toByteArray(imageStream);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void addDefaultWhiteLabelThemeIfNotExist() throws MetadataManagementException {
|
||||
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
|
||||
try {
|
||||
MetadataManagementDAOFactory.beginTransaction();
|
||||
if (!metadataDAO.isExist(tenantId, MetadataConstants.WHITELABEL_META_KEY)) {
|
||||
WhiteLabelTheme whiteLabelTheme = getDefaultWhiteLabelTheme();
|
||||
Metadata metadata = constructWhiteLabelThemeMetadata(whiteLabelTheme);
|
||||
metadataDAO.addMetadata(tenantId, metadata);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("White label metadata entry has inserted successfully");
|
||||
}
|
||||
}
|
||||
MetadataManagementDAOFactory.commitTransaction();
|
||||
} catch (MetadataManagementDAOException e) {
|
||||
MetadataManagementDAOFactory.rollbackTransaction();
|
||||
String msg = "Error occurred while inserting default whitelabel metadata entry.";
|
||||
log.error(msg, e);
|
||||
throw new MetadataManagementException(msg, e);
|
||||
} catch (TransactionManagementException e) {
|
||||
String msg = "Error occurred while opening a connection to the data source";
|
||||
log.error(msg, e);
|
||||
throw new MetadataManagementException(msg, e);
|
||||
} finally {
|
||||
MetadataManagementDAOFactory.closeConnection();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetToDefaultWhiteLabelTheme() throws MetadataManagementException {
|
||||
WhiteLabelTheme whiteLabelTheme = getDefaultWhiteLabelTheme();
|
||||
Metadata metadata = constructWhiteLabelThemeMetadata(whiteLabelTheme);
|
||||
DeviceManagementDataHolder.getInstance().getMetadataManagementService().updateMetadata(metadata);
|
||||
}
|
||||
|
||||
private WhiteLabelTheme getDefaultWhiteLabelTheme() {
|
||||
String footerText = getDefaultFooterText();
|
||||
WhiteLabelImage favicon = constructDefaultFaviconImage();
|
||||
WhiteLabelImage logo = constructDefaultLogoImage();
|
||||
WhiteLabelTheme defaultTheme = new WhiteLabelTheme();
|
||||
defaultTheme.setFooterText(footerText);
|
||||
defaultTheme.setLogoImage(logo);
|
||||
defaultTheme.setFaviconImage(favicon);
|
||||
return defaultTheme;
|
||||
}
|
||||
|
||||
private String getDefaultFooterText() {
|
||||
MetaDataConfiguration metaDataConfiguration = DeviceConfigurationManager.getInstance().
|
||||
getDeviceManagementConfig().getMetaDataConfiguration();
|
||||
WhiteLabelConfiguration whiteLabelConfiguration = metaDataConfiguration.getWhiteLabelConfiguration();
|
||||
return whiteLabelConfiguration.getFooterText();
|
||||
}
|
||||
|
||||
private WhiteLabelImage constructDefaultFaviconImage() {
|
||||
MetaDataConfiguration metaDataConfiguration = DeviceConfigurationManager.getInstance().
|
||||
getDeviceManagementConfig().getMetaDataConfiguration();
|
||||
WhiteLabelConfiguration whiteLabelConfiguration = metaDataConfiguration.getWhiteLabelConfiguration();
|
||||
WhiteLabelImage favicon = new WhiteLabelImage();
|
||||
favicon.setImageLocation(whiteLabelConfiguration.getWhiteLabelImages().getDefaultFaviconName());
|
||||
setDefaultWhiteLabelImageCommonProperties(favicon);
|
||||
return favicon;
|
||||
}
|
||||
|
||||
private WhiteLabelImage constructDefaultLogoImage() {
|
||||
MetaDataConfiguration metaDataConfiguration = DeviceConfigurationManager.getInstance().
|
||||
getDeviceManagementConfig().getMetaDataConfiguration();
|
||||
WhiteLabelConfiguration whiteLabelConfiguration = metaDataConfiguration.getWhiteLabelConfiguration();
|
||||
WhiteLabelImage logo = new WhiteLabelImage();
|
||||
logo.setImageLocation(whiteLabelConfiguration.getWhiteLabelImages().getDefaultLogoName());
|
||||
setDefaultWhiteLabelImageCommonProperties(logo);
|
||||
return logo;
|
||||
}
|
||||
|
||||
private void setDefaultWhiteLabelImageCommonProperties(WhiteLabelImage image) {
|
||||
image.setImageLocationType(WhiteLabelImage.ImageLocationType.DEFAULT_FILE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WhiteLabelTheme updateWhiteLabelTheme(WhiteLabelThemeCreateRequest createWhiteLabelTheme)
|
||||
throws MetadataManagementException {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Creating Metadata : [" + createWhiteLabelTheme.toString() + "]");
|
||||
}
|
||||
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
|
||||
File existingFaviconImage = null;
|
||||
File existingLogoImage = null;
|
||||
try {
|
||||
WhiteLabelTheme theme = getWhiteLabelTheme();
|
||||
if (theme.getFaviconImage().getImageLocationType() == WhiteLabelImage.ImageLocationType.CUSTOM_FILE) {
|
||||
existingFaviconImage = WhiteLabelStorageUtil.getWhiteLabelImageFile(theme.getFaviconImage(), WhiteLabelImage.ImageName.FAVICON);
|
||||
}
|
||||
if (theme.getFaviconImage().getImageLocationType() == WhiteLabelImage.ImageLocationType.CUSTOM_FILE) {
|
||||
existingLogoImage = WhiteLabelStorageUtil.getWhiteLabelImageFile(theme.getFaviconImage(), WhiteLabelImage.ImageName.FAVICON);
|
||||
}
|
||||
storeWhiteLabelImageIfRequired(createWhiteLabelTheme.getFavicon(), WhiteLabelImage.ImageName.FAVICON, tenantId);
|
||||
storeWhiteLabelImageIfRequired(createWhiteLabelTheme.getLogo(), WhiteLabelImage.ImageName.LOGO, tenantId);
|
||||
WhiteLabelTheme whiteLabelTheme = constructWhiteLabelTheme(createWhiteLabelTheme);
|
||||
Metadata metadataWhiteLabelTheme = constructWhiteLabelThemeMetadata(whiteLabelTheme);
|
||||
|
||||
MetadataManagementDAOFactory.beginTransaction();
|
||||
metadataDAO.updateMetadata(tenantId, metadataWhiteLabelTheme);
|
||||
MetadataManagementDAOFactory.commitTransaction();
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Metadata entry created successfully. " + createWhiteLabelTheme.toString());
|
||||
}
|
||||
return whiteLabelTheme;
|
||||
} catch (MetadataManagementDAOException e) {
|
||||
MetadataManagementDAOFactory.rollbackTransaction();
|
||||
restoreWhiteLabelImages(existingFaviconImage, existingLogoImage, tenantId);
|
||||
String msg = "Error occurred while creating the metadata entry. " + createWhiteLabelTheme.toString();
|
||||
log.error(msg, e);
|
||||
throw new MetadataManagementException(msg, e);
|
||||
} catch (TransactionManagementException e) {
|
||||
restoreWhiteLabelImages(existingFaviconImage, existingLogoImage, tenantId);
|
||||
String msg = "Error occurred while opening a connection to the data source";
|
||||
log.error(msg, e);
|
||||
throw new MetadataManagementException("Error occurred while creating metadata record", e);
|
||||
} catch (NotFoundException e) {
|
||||
String msg = "Error occurred while retrieving existing white label theme";
|
||||
log.error(msg, e);
|
||||
throw new MetadataManagementException(msg, e);
|
||||
} finally {
|
||||
MetadataManagementDAOFactory.closeConnection();
|
||||
}
|
||||
}
|
||||
|
||||
private void restoreWhiteLabelImages(File existingFavicon, File existingLogo, int tenantId)
|
||||
throws MetadataManagementException {
|
||||
WhiteLabelStorageUtil.deleteWhiteLabelImageForTenantIfExists(tenantId);
|
||||
if (existingFavicon != null) {
|
||||
WhiteLabelStorageUtil.storeWhiteLabelImage(existingFavicon, WhiteLabelImage.ImageName.FAVICON, tenantId);
|
||||
}
|
||||
if (existingLogo != null) {
|
||||
WhiteLabelStorageUtil.storeWhiteLabelImage(existingLogo, WhiteLabelImage.ImageName.LOGO, tenantId);
|
||||
}
|
||||
}
|
||||
|
||||
private void storeWhiteLabelImageIfRequired(WhiteLabelImageRequestPayload whiteLabelImage,
|
||||
WhiteLabelImage.ImageName imageName, int tenantId)
|
||||
throws MetadataManagementException {
|
||||
if (whiteLabelImage.getImageType() == WhiteLabelImageRequestPayload.ImageType.BASE64) {
|
||||
Base64File imageBase64 = new Gson().fromJson(whiteLabelImage.getImage(), Base64File.class);
|
||||
WhiteLabelStorageUtil.updateWhiteLabelImage(imageBase64, imageName, tenantId);
|
||||
}
|
||||
}
|
||||
|
||||
private WhiteLabelTheme constructWhiteLabelTheme(WhiteLabelThemeCreateRequest whiteLabelThemeCreateRequest) {
|
||||
WhiteLabelTheme whiteLabelTheme = new WhiteLabelTheme();
|
||||
WhiteLabelImageRequestPayload faviconPayload = whiteLabelThemeCreateRequest.getFavicon();
|
||||
WhiteLabelImageRequestPayload logoPayload = whiteLabelThemeCreateRequest.getLogo();
|
||||
WhiteLabelImage faviconImage = constructWhiteLabelImageDTO(faviconPayload);
|
||||
WhiteLabelImage logoImage = constructWhiteLabelImageDTO(logoPayload);
|
||||
whiteLabelTheme.setFaviconImage(faviconImage);
|
||||
whiteLabelTheme.setLogoImage(logoImage);
|
||||
whiteLabelTheme.setFooterText(whiteLabelThemeCreateRequest.getFooterText());
|
||||
return whiteLabelTheme;
|
||||
}
|
||||
|
||||
private WhiteLabelImage constructWhiteLabelImageDTO(WhiteLabelImageRequestPayload image) {
|
||||
WhiteLabelImage imageResponse = new WhiteLabelImage();
|
||||
WhiteLabelImage.ImageLocationType imageLocationType = image.getImageType().getDTOImageLocationType();
|
||||
imageResponse.setImageLocationType(imageLocationType);
|
||||
String imageLocation;
|
||||
if (image.getImageType() == WhiteLabelImageRequestPayload.ImageType.BASE64) {
|
||||
Base64File imageBase64 = image.getImageAsBase64File();
|
||||
imageLocation = imageBase64.getName();
|
||||
} else {
|
||||
imageLocation = image.getImageAsUrl();
|
||||
}
|
||||
imageResponse.setImageLocation(imageLocation);
|
||||
return imageResponse;
|
||||
}
|
||||
|
||||
private Metadata constructWhiteLabelThemeMetadata(WhiteLabelTheme whiteLabelTheme) {
|
||||
String whiteLabelThemeJsonString = new Gson().toJson(whiteLabelTheme);
|
||||
Metadata metadata = new Metadata();
|
||||
metadata.setMetaKey(MetadataConstants.WHITELABEL_META_KEY);
|
||||
metadata.setMetaValue(whiteLabelThemeJsonString);
|
||||
return metadata;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WhiteLabelTheme getWhiteLabelTheme() throws MetadataManagementException, NotFoundException {
|
||||
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Retrieving whitelabel theme for tenant: " + tenantId);
|
||||
}
|
||||
try {
|
||||
MetadataManagementDAOFactory.openConnection();
|
||||
Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.WHITELABEL_META_KEY);
|
||||
if (metadata == null) {
|
||||
String msg = "Whitelabel theme not found for tenant: " + tenantId;
|
||||
log.debug(msg);
|
||||
throw new NotFoundException(msg);
|
||||
}
|
||||
return new Gson().fromJson(metadata.getMetaValue(), WhiteLabelTheme.class);
|
||||
} catch (MetadataManagementDAOException e) {
|
||||
String msg = "Error occurred while retrieving white label theme for tenant:" + tenantId;
|
||||
log.error(msg, e);
|
||||
throw new MetadataManagementException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while opening a connection to the data source";
|
||||
log.error(msg, e);
|
||||
throw new MetadataManagementException(msg, e);
|
||||
} finally {
|
||||
MetadataManagementDAOFactory.closeConnection();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
/* 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.core.metadata.mgt.dao.util;
|
||||
|
||||
public class MetadataConstants {
|
||||
public static final String WHITELABEL_META_KEY = "whitelabel";
|
||||
}
|
@ -0,0 +1,162 @@
|
||||
/* 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.core.metadata.mgt.util;
|
||||
|
||||
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.exceptions.MetadataManagementException;
|
||||
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.core.common.exception.StorageManagementException;
|
||||
import org.wso2.carbon.device.mgt.core.common.util.FileUtil;
|
||||
import org.wso2.carbon.device.mgt.core.common.util.StorageManagementUtil;
|
||||
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
|
||||
import org.wso2.carbon.device.mgt.core.config.tenant.whitelabel.MetaDataConfiguration;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* This class contains the default concrete implementation of ApplicationStorage Management.
|
||||
*/
|
||||
public class WhiteLabelStorageUtil {
|
||||
private static final Log log = LogFactory.getLog(WhiteLabelStorageUtil.class);
|
||||
private static final MetaDataConfiguration metadataConfig;
|
||||
private static final String STORAGE_PATH;
|
||||
|
||||
static {
|
||||
metadataConfig = DeviceConfigurationManager.getInstance().getDeviceManagementConfig().getMetaDataConfiguration();
|
||||
if (metadataConfig == null) {
|
||||
throw new RuntimeException("Meta configuration is not found in cdm-config.xml");
|
||||
}
|
||||
STORAGE_PATH = metadataConfig.getWhiteLabelConfiguration().getWhiteLabelImages().getStoragePath();
|
||||
}
|
||||
|
||||
public static void storeWhiteLabelImage(Base64File image, WhiteLabelImage.ImageName imageName, int tenantId)
|
||||
throws MetadataManagementException {
|
||||
String storedLocation;
|
||||
|
||||
try {
|
||||
String imageStoringBaseDirPath = STORAGE_PATH + File.separator + tenantId;
|
||||
StorageManagementUtil.createArtifactDirectory(imageStoringBaseDirPath);
|
||||
String storingDir = imageStoringBaseDirPath + File.separator + imageName;
|
||||
StorageManagementUtil.createArtifactDirectory(storingDir);
|
||||
storedLocation = storingDir + File.separator + image.getName();
|
||||
StorageManagementUtil.saveFile(image, storedLocation);
|
||||
} catch (IOException e) {
|
||||
String msg = "IO Exception occurred while saving whitelabel artifacts for the tenant " + tenantId;
|
||||
log.error(msg, e);
|
||||
throw new MetadataManagementException(msg, e);
|
||||
} catch (StorageManagementException e) {
|
||||
String msg = "Error occurred while uploading white label image artifacts";
|
||||
log.error(msg, e);
|
||||
throw new MetadataManagementException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void storeWhiteLabelImage(File file, WhiteLabelImage.ImageName imageName, int tenantId) throws
|
||||
MetadataManagementException {
|
||||
try {
|
||||
storeWhiteLabelImage(FileUtil.fileToBase64File(file), imageName, tenantId);
|
||||
} catch (IOException e) {
|
||||
String msg = "Error occurred when converting provided File object to Base64File class";
|
||||
log.error(msg);
|
||||
throw new MetadataManagementException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void updateWhiteLabelImage(Base64File image, WhiteLabelImage.ImageName imageName, int tenantId)
|
||||
throws MetadataManagementException {
|
||||
deleteWhiteLabelImageIfExists(imageName, tenantId);
|
||||
storeWhiteLabelImage(image, imageName, tenantId);
|
||||
}
|
||||
|
||||
public static File getWhiteLabelImageFile(WhiteLabelImage image, WhiteLabelImage.ImageName imageName)
|
||||
throws MetadataManagementException, NotFoundException {
|
||||
String fullPathToImage = getPathToImage(image, imageName);
|
||||
return new File(fullPathToImage);
|
||||
}
|
||||
|
||||
public static InputStream getWhiteLabelImageStream(WhiteLabelImage image, WhiteLabelImage.ImageName imageName)
|
||||
throws MetadataManagementException, NotFoundException {
|
||||
String fullPathToFile = getPathToImage(image, imageName);
|
||||
try {
|
||||
InputStream imageStream = StorageManagementUtil.getInputStream(fullPathToFile);
|
||||
if (imageStream == null) {
|
||||
String msg = "Failed to get the " + imageName + " image with the file name: " + fullPathToFile;
|
||||
log.error(msg);
|
||||
throw new NotFoundException(msg);
|
||||
}
|
||||
return imageStream;
|
||||
} catch (IOException e) {
|
||||
String msg = "Error occurred when accessing the file in file path: " + fullPathToFile;
|
||||
log.error(msg, e);
|
||||
throw new MetadataManagementException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
private static String getPathToImage(WhiteLabelImage image, WhiteLabelImage.ImageName imageName)
|
||||
throws MetadataManagementException {
|
||||
WhiteLabelImage.ImageLocationType imageLocationType = image.getImageLocationType();
|
||||
if (imageLocationType == WhiteLabelImage.ImageLocationType.URL) {
|
||||
String msg = "White label images of URL type is not stored, hence it doesn't have a path in file system.";
|
||||
log.error(msg);
|
||||
throw new MetadataManagementException(msg);
|
||||
}
|
||||
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
|
||||
String fileName = image.getImageLocation();
|
||||
String filePath = String.valueOf(tenantId);
|
||||
if (imageLocationType == WhiteLabelImage.ImageLocationType.DEFAULT_FILE) {
|
||||
filePath = metadataConfig.getWhiteLabelConfiguration().getWhiteLabelImages().getDefaultImagesLocation();
|
||||
}
|
||||
return STORAGE_PATH + File.separator + filePath + File.separator + imageName + File.separator + fileName;
|
||||
}
|
||||
|
||||
/***
|
||||
* This method is responsible to delete artifact file which is located in the artifact path.
|
||||
*/
|
||||
public static void deleteWhiteLabelImageIfExists(WhiteLabelImage.ImageName imageName, int tenantId) throws MetadataManagementException {
|
||||
String artifactPath = STORAGE_PATH + File.separator + tenantId + File.separator + imageName;
|
||||
File artifact = new File(artifactPath);
|
||||
if (artifact.exists()) {
|
||||
try {
|
||||
StorageManagementUtil.delete(artifact);
|
||||
} catch (IOException e) {
|
||||
throw new MetadataManagementException("Error occurred while deleting whitelabel artifacts", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/***
|
||||
* This method is responsible to delete artifact file which is located in the artifact path.
|
||||
*/
|
||||
public static void deleteWhiteLabelImageForTenantIfExists(int tenantId) throws MetadataManagementException {
|
||||
String artifactPath = STORAGE_PATH + File.separator + tenantId;
|
||||
File artifact = new File(artifactPath);
|
||||
if (artifact.exists()) {
|
||||
try {
|
||||
StorageManagementUtil.delete(artifact);
|
||||
} catch (IOException e) {
|
||||
throw new MetadataManagementException("Error occurred while deleting whitelabel artifacts", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue