partial fix on app update apis -2

feature/appm-store/pbac
inoshperera 6 years ago
parent 4e7f1e4047
commit 7dd7dc8d64

@ -76,6 +76,10 @@ public class Application {
value = "If unrestricted roles are defined then isRestricted value is true otherwise it is false") value = "If unrestricted roles are defined then isRestricted value is true otherwise it is false")
private boolean isRestricted; private boolean isRestricted;
@ApiModelProperty(name = "deviceTypeId",
value = "Id of the Related device type of the application",
example = "1, 2, 3")
private int deviceTypeId;
@ApiModelProperty(name = "deviceType", @ApiModelProperty(name = "deviceType",
value = "Related device type of the application", value = "Related device type of the application",
required = true, required = true,
@ -180,4 +184,12 @@ public class Application {
public void setDeviceType(String deviceType) { public void setDeviceType(String deviceType) {
this.deviceType = deviceType; this.deviceType = deviceType;
} }
public int getDeviceTypeId() {
return deviceTypeId;
}
public void setDeviceTypeId(int deviceTypeId) {
this.deviceTypeId = deviceTypeId;
}
} }

@ -25,7 +25,11 @@ import org.wso2.carbon.device.application.mgt.common.Filter;
import org.wso2.carbon.device.application.mgt.common.LifecycleState; import org.wso2.carbon.device.application.mgt.common.LifecycleState;
import org.wso2.carbon.device.application.mgt.common.UnrestrictedRole; import org.wso2.carbon.device.application.mgt.common.UnrestrictedRole;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException;
import org.wso2.carbon.device.application.mgt.common.exception.RequestValidatingException;
import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagementException;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import java.io.InputStream;
import java.util.List; import java.util.List;
/** /**
@ -90,10 +94,14 @@ public interface ApplicationManager {
* To get Application with the given Id. * To get Application with the given Id.
* *
* @param id id of the Application * @param id id of the Application
* @return the Application identified by the UUID * @param state state of the Application
* @param handleConnections Whether it is required to handle DB connections within(true), or if there are
* existing connection(false)
* @return the Application identified by the ID
* @throws ApplicationManagementException Application Management Exception. * @throws ApplicationManagementException Application Management Exception.
*/ */
Application getApplicationById(int id) throws ApplicationManagementException; Application getApplicationById(int id, String state, boolean handleConnections) throws
ApplicationManagementException;
/** /**
* To get an application associated with the release. * To get an application associated with the release.
@ -130,6 +138,16 @@ public interface ApplicationManager {
*/ */
List<ApplicationRelease> getReleases(int applicationId) throws ApplicationManagementException; List<ApplicationRelease> getReleases(int applicationId) throws ApplicationManagementException;
/**
* To get the release of a particular Application.
*
* @param applicationId ID of the Application.
* @param state state of the Application.
* @return the List of the Application releases related with the particular Application.
* @throws ApplicationManagementException Application Management Exception.
*/
List<ApplicationRelease> getReleaseInState(int applicationId, String state) throws ApplicationManagementException;
/** /**
* To get all the releases of a particular Application. * To get all the releases of a particular Application.
* *
@ -162,24 +180,33 @@ public interface ApplicationManager {
Application getApplicationIfAccessible(int applicationId) throws ApplicationManagementException; Application getApplicationIfAccessible(int applicationId) throws ApplicationManagementException;
/** /**
* Get the application release for given UUID if application release is exists and application id is valid one. * To update release images such as icons, banner and screenshots.
* *
* @param releaseUuid UUID of the Application Release. * @param appId ID of the Application
* @param uuid uuid of the Application
* @param iconFileStream icon file of the release
* @param bannerFileStream bannerFileStream of the release.
* @param attachments screenshot attachments of the release
* @return Updated Application Release.
* @throws ApplicationManagementException Application Management Exception. * @throws ApplicationManagementException Application Management Exception.
*/ */
ApplicationRelease getAppReleaseIfExists(int applicationId, String releaseUuid) throws ApplicationRelease updateApplicationImageArtifact(int appId, String uuid, InputStream iconFileStream, InputStream
ApplicationManagementException; bannerFileStream, List<InputStream> attachments)
throws ApplicationManagementException, ResourceManagementException;
/** /**
* To update with a new release for an Application. * To update release images.
* *
* @param appId ID of the Application * @param appId ID of the Application
* @param applicationRelease ApplicationRelease * @param uuid uuid of the Application
* @param binaryFile binaryFile of the release.
* @return Updated Application Release. * @return Updated Application Release.
* @throws ApplicationManagementException Application Management Exception. * @throws ApplicationManagementException Application Management Exception.
*/ */
ApplicationRelease updateRelease(int appId, ApplicationRelease applicationRelease) ApplicationRelease updateApplicationArtifact(int appId, String uuid, InputStream binaryFile)
throws ApplicationManagementException; throws ApplicationManagementException, ResourceManagementException, RequestValidatingException, DeviceManagementException;
/** /**
* To verify whether application release is acceptable to update or not. * To verify whether application release is acceptable to update or not.

@ -52,7 +52,7 @@ public interface ApplicationStorageManager {
* @param screenshots Input Streams of screenshots * @param screenshots Input Streams of screenshots
* @throws ResourceManagementException Resource Management Exception. * @throws ResourceManagementException Resource Management Exception.
*/ */
ApplicationRelease updateImageArtifacts(ApplicationRelease applicationRelease, InputStream iconFile, void updateImageArtifacts(ApplicationRelease applicationRelease, InputStream iconFile,
InputStream bannerFile, List<InputStream> screenshots) InputStream bannerFile, List<InputStream> screenshots)
throws ResourceManagementException, ApplicationManagementException; throws ResourceManagementException, ApplicationManagementException;

@ -142,15 +142,15 @@ public interface ApplicationManagementAPI {
); );
@GET @GET
@Path("/{appType}") @Path("/{appId}")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@ApiOperation( @ApiOperation(
consumes = MediaType.APPLICATION_JSON, consumes = MediaType.APPLICATION_JSON,
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
httpMethod = "GET", httpMethod = "GET",
value = "get the application of requesting application type", value = "get the application of requesting application id and state",
notes = "This will get the application identified by the application type and name, if exists", notes = "This will get the application identified by the application id and state, if exists",
tags = "Application Management", tags = "Application Management",
extensions = { extensions = {
@Extension(properties = { @Extension(properties = {
@ -177,7 +177,12 @@ public interface ApplicationManagementAPI {
name = "appId", name = "appId",
value = "application Id", value = "application Id",
required = true) required = true)
@PathParam("appId") int appId @PathParam("appId") int appId,
@ApiParam(
name = "state",
value = "state",
defaultValue = "PUBLISHED")
@QueryParam("state") String state
); );
@PUT @PUT
@ -323,10 +328,10 @@ public interface ApplicationManagementAPI {
@PathParam("appid") int applicationId @PathParam("appid") int applicationId
); );
@POST @PUT
@Path("/image-artifacts/{appId}/{uuid}") @Path("/image-artifacts/{appId}/{uuid}")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA) @Consumes("multipart/mixed")
@ApiOperation( @ApiOperation(
consumes = MediaType.MULTIPART_FORM_DATA, consumes = MediaType.MULTIPART_FORM_DATA,
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
@ -358,19 +363,47 @@ public interface ApplicationManagementAPI {
response = ErrorResponse.class) response = ErrorResponse.class)
}) })
Response updateApplicationImageArtifacts( Response updateApplicationImageArtifacts(
@ApiParam(name = "appId", value = "ID of the application", required = true) @ApiParam(
name = "appId",
value = "ID of the application",
required = true)
@PathParam("appId") int applicatioId, @PathParam("appId") int applicatioId,
@ApiParam(name = "uuid", value = "UUID of the application", required = true) @ApiParam(
name = "uuid",
value = "UUID of the application",
required = true)
@PathParam("uuid") String applicationUUID, @PathParam("uuid") String applicationUUID,
@ApiParam(
name = "icon",
value = "Icon of the uploading application",
required = true)
@Multipart(value = "icon") Attachment iconFile, @Multipart(value = "icon") Attachment iconFile,
@ApiParam(
name = "banner",
value = "Banner of the uploading application",
required = true)
@Multipart(value = "banner") Attachment bannerFile, @Multipart(value = "banner") Attachment bannerFile,
@Multipart(value = "screenshot") List<Attachment> screenshots @ApiParam(
name = "screenshot1",
value = "Screen Shots of the uploading application",
required = true)
@Multipart(value = "screenshot1") Attachment screenshot1,
@ApiParam(
name = "screenshot2",
value = "Screen Shots of the uploading application",
required = false)
@Multipart(value = "screenshot2") Attachment screenshot2,
@ApiParam(
name = "screenshot3",
value = "Screen Shots of the uploading application",
required = false)
@Multipart(value = "screenshot3") Attachment screenshot3
); );
@PUT @PUT
@Path("/app-artifacts/{deviceType}/{appType}/{appId}/{uuid}") @Path("/app-artifacts/{deviceType}/{appType}/{appId}/{uuid}")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA) @Consumes("multipart/mixed")
@ApiOperation( @ApiOperation(
consumes = MediaType.MULTIPART_FORM_DATA, consumes = MediaType.MULTIPART_FORM_DATA,
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,

@ -32,6 +32,7 @@ import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagemen
import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager;
import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager;
import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException; import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -90,10 +91,11 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
@Consumes("application/json") @Consumes("application/json")
@Path("/{appId}") @Path("/{appId}")
public Response getApplication( public Response getApplication(
@PathParam("appId") int appId) { @PathParam("appId") int appId,
@QueryParam("state") String state) {
ApplicationManager applicationManager = APIUtil.getApplicationManager(); ApplicationManager applicationManager = APIUtil.getApplicationManager();
try { try {
Application application = applicationManager.getApplicationById(appId); Application application = applicationManager.getApplicationById(appId, state, true);
if (application == null) { if (application == null) {
return Response.status(Response.Status.NOT_FOUND).entity return Response.status(Response.Status.NOT_FOUND).entity
("Application with application id: " + appId + " not found").build(); ("Application with application id: " + appId + " not found").build();
@ -198,22 +200,22 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
@Override @Override
@PUT @PUT
@Consumes("multipart/mixed")
@Produces(MediaType.APPLICATION_JSON)
@Path("/image-artifacts/{appId}/{uuid}") @Path("/image-artifacts/{appId}/{uuid}")
public Response updateApplicationImageArtifacts( public Response updateApplicationImageArtifacts(
@PathParam("appId") int appId, @PathParam("appId") int appId,
@PathParam("uuid") String applicationUuid, @PathParam("uuid") String applicationUuid,
@Multipart("icon") Attachment iconFile, @Multipart("icon") Attachment iconFile,
@Multipart("banner") Attachment bannerFile, @Multipart("banner") Attachment bannerFile,
@Multipart("screenshot") List<Attachment> attachmentList) { @Multipart("screenshot1") Attachment screenshot1,
@Multipart("screenshot2") Attachment screenshot2,
ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); @Multipart("screenshot3") Attachment screenshot3) {
ApplicationManager applicationManager = APIUtil.getApplicationManager();
ApplicationRelease applicationRelease;
try { try {
InputStream iconFileStream = null; InputStream iconFileStream = null;
InputStream bannerFileStream = null; InputStream bannerFileStream = null;
List<InputStream> attachments = new ArrayList<>(); List<InputStream> attachments = new ArrayList<>();;
if (iconFile != null) { if (iconFile != null) {
iconFileStream = iconFile.getDataHandler().getInputStream(); iconFileStream = iconFile.getDataHandler().getInputStream();
@ -221,29 +223,22 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
if (bannerFile != null) { if (bannerFile != null) {
bannerFileStream = bannerFile.getDataHandler().getInputStream(); bannerFileStream = bannerFile.getDataHandler().getInputStream();
} }
if (attachmentList != null && !attachmentList.isEmpty()) {
for (Attachment screenshot : attachmentList) { attachments.add(screenshot1.getDataHandler().getInputStream());
attachments.add(screenshot.getDataHandler().getInputStream()); if(screenshot2 != null) {
} attachments.add(screenshot2.getDataHandler().getInputStream());
} }
applicationRelease = applicationManager.getAppReleaseIfExists(appId, applicationUuid); if(screenshot3 != null) {
LifecycleState lifecycleState = applicationManager.getLifecycleState(appId, applicationRelease.getUuid()); attachments.add(screenshot3.getDataHandler().getInputStream());
if (AppLifecycleState.PUBLISHED.toString().equals(lifecycleState.getCurrentState()) ||
AppLifecycleState.DEPRECATED.toString().equals(lifecycleState.getCurrentState())) {
return Response.status(Response.Status.FORBIDDEN).entity("Can't Update the application release in " +
"PUBLISHED or DEPRECATED state. Hence please demote the application and update the application " +
"release").build();
} }
applicationRelease = applicationStorageManager ApplicationManager applicationManager = APIUtil.getApplicationManager();
.updateImageArtifacts(applicationRelease, iconFileStream, bannerFileStream, attachments); applicationManager.updateApplicationImageArtifact(appId,
applicationManager.updateRelease(appId, applicationRelease); applicationUuid, iconFileStream, bannerFileStream, attachments);
return Response.status(Response.Status.OK)
.entity("Successfully uploaded artifacts for the application " + applicationUuid).build(); return Response.status(Response.Status.OK).entity("Successfully uploaded artifacts for the application "
+ applicationUuid).build();
} catch (NotFoundException e) { } catch (NotFoundException e) {
String msg = log.error(e.getMessage(), e);
"Couldn't found application release details or storage details or lifecycle details. Application id: "
+ appId + " App release uuid: " + applicationUuid;
log.error(msg, e);
return APIUtil.getResponse(e, Response.Status.NOT_FOUND); return APIUtil.getResponse(e, Response.Status.NOT_FOUND);
} catch (ApplicationManagementException e) { } catch (ApplicationManagementException e) {
String msg = "Error occurred while updating the application."; String msg = "Error occurred while updating the application.";
@ -263,6 +258,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
@Override @Override
@PUT @PUT
@Consumes("multipart/mixed")
@Path("/app-artifacts/{deviceType}/{appType}/{appId}/{uuid}") @Path("/app-artifacts/{deviceType}/{appType}/{appId}/{uuid}")
public Response updateApplicationArtifact( public Response updateApplicationArtifact(
@PathParam("deviceType") String deviceType, @PathParam("deviceType") String deviceType,
@ -270,9 +266,6 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
@PathParam("appId") int applicationId, @PathParam("appId") int applicationId,
@PathParam("uuid") String applicationUuid, @PathParam("uuid") String applicationUuid,
@Multipart("binaryFile") Attachment binaryFile) { @Multipart("binaryFile") Attachment binaryFile) {
ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager();
ApplicationManager applicationManager = APIUtil.getApplicationManager();
ApplicationRelease applicationRelease;
try { try {
@ -280,16 +273,14 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
return APIUtil.getResponse("Uploading artifacts for the application is failed " + applicationUuid, return APIUtil.getResponse("Uploading artifacts for the application is failed " + applicationUuid,
Response.Status.BAD_REQUEST); Response.Status.BAD_REQUEST);
} }
applicationRelease = applicationManager.getAppReleaseIfExists(applicationId, applicationUuid); APIUtil.getApplicationManager().updateApplicationArtifact(applicationId, applicationUuid,
applicationRelease = applicationStorageManager binaryFile.getDataHandler().getInputStream());
.updateReleaseArtifacts(applicationRelease, appType, deviceType,
binaryFile.getDataHandler().getInputStream());
applicationManager.updateRelease(applicationId, applicationRelease);
return Response.status(Response.Status.OK) return Response.status(Response.Status.OK)
.entity("Successfully uploaded artifacts for the application release. UUID is " + applicationUuid).build(); .entity("Successfully uploaded artifacts for the application release. UUID is " + applicationUuid).build();
} catch (IOException e) { } catch (IOException e) {
String msg = String msg =
"Error occured while trying to read icon, banner files for the application release" + applicationUuid; "Error occurred while trying to read icon, banner files for the application release" +
applicationUuid;
log.error(msg); log.error(msg);
return APIUtil.getResponse(new ApplicationManagementException(msg, e), return APIUtil.getResponse(new ApplicationManagementException(msg, e),
Response.Status.BAD_REQUEST); Response.Status.BAD_REQUEST);
@ -305,6 +296,10 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
log.error("Error occured while handling the application artifact updating request. application release UUID: " log.error("Error occured while handling the application artifact updating request. application release UUID: "
+ applicationUuid); + applicationUuid);
return APIUtil.getResponse(e, Response.Status.BAD_REQUEST); return APIUtil.getResponse(e, Response.Status.BAD_REQUEST);
} catch (DeviceManagementException e) {
log.error("Error occurred while updating the image artifacts of the application with the uuid "
+ applicationUuid, e);
return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR);
} }
} }
@ -369,9 +364,9 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
} }
} }
applicationRelease = applicationStorageManager // applicationRelease = applicationStorageManager
.updateImageArtifacts(applicationRelease, iconFileStream, bannerFileStream, attachments); // .updateImageArtifacts(applicationRelease, iconFileStream, bannerFileStream, attachments);
applicationRelease = applicationManager.updateRelease(applicationId, applicationRelease); // applicationRelease = applicationManager.updateRelease(applicationId, applicationRelease);
return Response.status(Response.Status.OK).entity(applicationRelease).build(); return Response.status(Response.Status.OK).entity(applicationRelease).build();
} catch (ApplicationManagementException e) { } catch (ApplicationManagementException e) {

Loading…
Cancel
Save