Adding platform manager first API.

feature/appm-store/pbac
sinthuja 8 years ago
parent fe26352ec9
commit 6020b3fb65

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

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

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

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

@ -33,6 +33,8 @@ public interface PlatformManager {
List<Platform> 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;

@ -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.*
</Import-Package>
<Export-Package>
!org.wso2.carbon.device.application.mgt.core.internal.*,

@ -35,4 +35,6 @@ public interface PlatformDAO {
List<Platform> getPlatforms(String tenantDomain) throws PlatformManagementDAOException;
Platform getPlatform(String tenantDomain, String platformCode) throws PlatformManagementDAOException;
}

@ -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 {

@ -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<String, Platform> 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)) {

Loading…
Cancel
Save