From 6020b3fb6530ce9064eb5f33f0c20d54dfe4f401 Mon Sep 17 00:00:00 2001 From: sinthuja Date: Tue, 13 Jun 2017 18:15:58 +0530 Subject: [PATCH] Adding platform manager first API. --- .../device/application/mgt/api/APIUtil.java | 6 +- .../api/services/PlatformManagementAPI.java | 88 +++++++++++++++++-- .../impl/PlatformManagementAPIImpl.java | 41 +++++++++ .../application/mgt/common/Platform.java | 4 + .../mgt/common/services/PlatformManager.java | 2 + .../pom.xml | 3 +- .../application/mgt/core/dao/PlatformDAO.java | 2 + .../mgt/core/dao/impl/PlatformDAOImpl.java | 2 +- .../mgt/core/impl/PlatformManagerImpl.java | 32 +++++++ 9 files changed, 168 insertions(+), 12 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/APIUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/APIUtil.java index 03e36661788..5ffc2ccda5d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/APIUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/APIUtil.java @@ -77,8 +77,12 @@ public class APIUtil { } public static Response getResponse(ApplicationManagementException ex, Response.Status status) { + return getResponse(ex.getMessage(), status); + } + + public static Response getResponse(String message, Response.Status status) { ErrorResponse errorMessage = new ErrorResponse(); - errorMessage.setMessage(ex.getMessage()); + errorMessage.setMessage(message); if (status == null) { status = Response.Status.INTERNAL_SERVER_ERROR; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/PlatformManagementAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/PlatformManagementAPI.java index 3cc5761f6ef..7741ccf6570 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/PlatformManagementAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/PlatformManagementAPI.java @@ -18,7 +18,6 @@ package org.wso2.carbon.device.application.mgt.api.services;/* import io.swagger.annotations.*; import org.wso2.carbon.device.application.mgt.api.beans.ErrorResponse; -import org.wso2.carbon.device.application.mgt.common.ApplicationList; import org.wso2.carbon.device.application.mgt.common.Platform; import javax.validation.constraints.Size; @@ -30,11 +29,11 @@ import javax.ws.rs.core.Response; "such as get all the available platform for a tenant, etc.") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) +@Path("/platforms") public interface PlatformManagementAPI { public final static String SCOPE = "scope"; @GET - @Path("platforms") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @ApiOperation( @@ -42,11 +41,11 @@ public interface PlatformManagementAPI { produces = MediaType.APPLICATION_JSON, httpMethod = "GET", value = "get all platforms", - notes = "This will get all applications", - tags = "Application Management", + notes = "This will get all platforms that is visible for tenants", + tags = "Platform Management", extensions = { @Extension(properties = { - @ExtensionProperty(name = SCOPE, value = "perm:get-platforms") + @ExtensionProperty(name = SCOPE, value = "perm:get-platform") }) } ) @@ -57,10 +56,6 @@ public interface PlatformManagementAPI { message = "OK. \n Successfully got platforms list.", response = Platform.class, responseContainer = "List"), - @ApiResponse( - code = 304, - message = "Not Modified. \n " + - "Empty body because the client already has the latest version of the requested resource."), @ApiResponse( code = 500, message = "Internal Server Error. \n Error occurred while getting the platform list.", @@ -79,4 +74,79 @@ public interface PlatformManagementAPI { @Size(max = 45) String status ); + + @GET + @Path("/{code}") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "get platform", + notes = "This will get application which was registered with {code}", + tags = "Platform Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:get-platform") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully got requested platform.", + response = Platform.class), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while getting the platform.", + response = ErrorResponse.class) + }) + Response getPlatform( + @ApiParam( + name = "code", + required = true) + @PathParam("code") + @Size(max = 45) + String code + ); + + @POST + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "POST", + value = "Add Platform", + notes = "This will a platform for the tenant space", + tags = "Platform Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:add-platform") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully added the platform"), + @ApiResponse( + code = 400, + message = "Bad Request. \n Invalid request parameters passed."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while getting the platform list.", + response = ErrorResponse.class) + }) + Response addPlatform( + @ApiParam( + name = "platform", + value = "The payload of the platform", + required = true) + Platform platform + ); + } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/PlatformManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/PlatformManagementAPIImpl.java index 5448b2f878d..401a82d0220 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/PlatformManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/PlatformManagementAPIImpl.java @@ -17,6 +17,7 @@ */ package org.wso2.carbon.device.application.mgt.api.services.impl; +import io.swagger.annotations.ApiParam; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; @@ -24,7 +25,12 @@ import org.wso2.carbon.device.application.mgt.api.APIUtil; import org.wso2.carbon.device.application.mgt.api.services.PlatformManagementAPI; import org.wso2.carbon.device.application.mgt.common.Platform; import org.wso2.carbon.device.application.mgt.common.exception.PlatformManagementException; +import org.wso2.carbon.device.application.mgt.core.exception.PlatformManagementDAOException; +import javax.validation.constraints.Size; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; import java.util.ArrayList; @@ -38,6 +44,7 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI { private static Log log = LogFactory.getLog(PlatformManagementAPIImpl.class); + @Override public Response getPlatforms(@QueryParam("status") String status) { String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true); @@ -73,4 +80,38 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI { return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } } + + @GET + @Override + @Path("/{code}") + public Response getPlatform(@PathParam("code") String code) { + String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true); + try { + Platform platform = APIUtil.getPlatformManager().getPlatform(tenantDomain, code); + return Response.status(Response.Status.OK).entity(platform).build(); + } catch (PlatformManagementDAOException e) { + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } catch (PlatformManagementException e) { + return APIUtil.getResponse(e, Response.Status.NOT_FOUND); + } + } + + @Override + public Response addPlatform(Platform platform) { + String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true); + try { + if (platform != null) { + if (platform.validate()) { + APIUtil.getPlatformManager().register(tenantDomain, platform); + return Response.status(Response.Status.CREATED).build(); + } else { + return APIUtil.getResponse("Invalid payload! Platform code and names are mandatory fields!", Response.Status.BAD_REQUEST); + } + } else { + return APIUtil.getResponse("Invalid payload! Platform needs to be passed as payload!", Response.Status.BAD_REQUEST); + } + } catch (PlatformManagementException e) { + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Platform.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Platform.java index b58410138d8..4f4d08b88c7 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Platform.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Platform.java @@ -165,6 +165,10 @@ public class Platform implements Cloneable { this.defaultTenantMapping = defaultTenantMapping; } + public boolean validate(){ + return !(name == null || code == null); + } + public static class Property implements Cloneable { private String name; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/PlatformManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/PlatformManager.java index 05f538586de..96850548408 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/PlatformManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/PlatformManager.java @@ -33,6 +33,8 @@ public interface PlatformManager { List getPlatforms(String tenantDomain) throws PlatformManagementException; + Platform getPlatform(String tenantDomain, String code) throws PlatformManagementException; + void register(String tenantDomain, Platform platform) throws PlatformManagementException; void unregister(String tenantDomain, String platformCode, boolean isFileBased) throws PlatformManagementException; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/pom.xml b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/pom.xml index cb64c11f244..1d74027d97f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/pom.xml +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/pom.xml @@ -72,7 +72,8 @@ org.wso2.carbon.device.mgt.core.*, org.wso2.carbon.device.mgt.common.*, org.apache.axis2.*, - org.wso2.carbon.user.core.* + org.wso2.carbon.user.core.*, + org.wso2.carbon.user.api.* !org.wso2.carbon.device.application.mgt.core.internal.*, diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/PlatformDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/PlatformDAO.java index 65a1c4ca91e..a569d5061bb 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/PlatformDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/PlatformDAO.java @@ -35,4 +35,6 @@ public interface PlatformDAO { List getPlatforms(String tenantDomain) throws PlatformManagementDAOException; + Platform getPlatform(String tenantDomain, String platformCode) throws PlatformManagementDAOException; + } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/PlatformDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/PlatformDAOImpl.java index baef15102ef..db0b8803629 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/PlatformDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/PlatformDAOImpl.java @@ -264,7 +264,7 @@ public class PlatformDAOImpl implements PlatformDAO { } } - private Platform getPlatform(String tenantDomain, String platformCode) throws PlatformManagementDAOException { + public Platform getPlatform(String tenantDomain, String platformCode) throws PlatformManagementDAOException { String platformQuery = "SELECT * FROM (SELECT * FROM APPM_PLATFORM WHERE (TENANT_DOMAIN=? AND CODE=?) OR (IS_SHARED = TRUE AND CODE=?)) PLATFORM " + "LEFT JOIN APPM_PLATFORM_PROPERTIES PROPS ON PLATFORM.ID = PROPS.PLATFORM_ID"; try { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/PlatformManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/PlatformManagerImpl.java index 4bec94ad5c4..cf4c468feff 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/PlatformManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/PlatformManagerImpl.java @@ -70,6 +70,38 @@ public class PlatformManagerImpl implements PlatformManager { return platforms; } + @Override + public Platform getPlatform(String tenantDomain, String code) throws PlatformManagementException { + Platform platform = getPlatformFromInMemory(tenantDomain, code); + if (platform == null) { + platform = DAOFactory.getPlatformDAO().getPlatform(tenantDomain, code); + } + if (platform != null) { + return new Platform(platform); + } + throw new PlatformManagementException("No platform was found for tenant - "+ tenantDomain+" with Platform code - "+ code); + } + + private Platform getPlatformFromInMemory(String tenantDomain, String code) { + Map platformMap = this.inMemoryStore.get(tenantDomain); + if (platformMap != null) { + Platform platform = platformMap.get(code); + if (platform != null) { + return platform; + } + } + if (!tenantDomain.equalsIgnoreCase(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)) { + platformMap = this.inMemoryStore.get(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); + if (platformMap != null) { + Platform platform = platformMap.get(code); + if (platform != null && platform.isShared()) { + return platform; + } + } + } + return null; + } + @Override public synchronized void register(String tenantDomain, Platform platform) throws PlatformManagementException { if (platform.isShared() && !tenantDomain.equals(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)) {