Improve APPM application retrieving APIs

4.x.x
tcdlpds@gmail.com 5 years ago
parent c953c41872
commit 8557980395

@ -1153,49 +1153,50 @@ public class ApplicationManagerImpl implements ApplicationManager {
public Application getApplicationById(int appId, String state) throws ApplicationManagementException { public Application getApplicationById(int appId, String state) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
boolean isVisibleApp = false;
ApplicationDTO applicationDTO = getApplication(appId); ApplicationDTO applicationDTO = getApplication(appId);
try { try {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
List<ApplicationReleaseDTO> filteredApplicationReleaseDTOs = new ArrayList<>(); List<String> unrestrictedRoles = this.visibilityDAO.getUnrestrictedRoles(appId, tenantId);
for (ApplicationReleaseDTO applicationReleaseDTO : applicationDTO.getApplicationReleaseDTOs()) { if (!unrestrictedRoles.isEmpty() && !hasUserRole(unrestrictedRoles, userName)) {
if (!applicationReleaseDTO.getCurrentState().equals(lifecycleStateManager.getEndState()) && ( String msg = "You are trying to access visibility restricted application and you are not assigned"
state == null || applicationReleaseDTO.getCurrentState().equals(state))) { + " required roles to view this application,";
filteredApplicationReleaseDTOs.add(applicationReleaseDTO); log.error(msg);
} throw new ForbiddenException(msg);
} }
if (state != null && filteredApplicationReleaseDTOs.isEmpty()) { if (lifecycleStateManager.getEndState().equals(applicationDTO.getStatus())) {
return null; return null;
} }
applicationDTO.setApplicationReleaseDTOs(filteredApplicationReleaseDTOs);
List<String> tags = this.applicationDAO.getAppTags(appId, tenantId);
List<String> categories = this.applicationDAO.getAppCategories(appId, tenantId);
applicationDTO.setTags(tags);
if (!categories.isEmpty()){
applicationDTO.setAppCategories(categories);
}
List<String> unrestrictedRoles = this.visibilityDAO.getUnrestrictedRoles(appId, tenantId); List<ApplicationReleaseDTO> filteredApplicationReleaseDTOs = new ArrayList<>();
if (!unrestrictedRoles.isEmpty()) { AtomicBoolean isDeletableApp = new AtomicBoolean(true);
if (hasUserRole(unrestrictedRoles, userName)) { AtomicBoolean isHideableApp = new AtomicBoolean(true);
isVisibleApp = true; for (ApplicationReleaseDTO applicationReleaseDTO : applicationDTO.getApplicationReleaseDTOs()) {
if (!applicationReleaseDTO.getCurrentState().equals(lifecycleStateManager.getEndState())) {
if (isHideableApp.get()) {
isHideableApp.set(false);
}
if (isDeletableApp.get() && !lifecycleStateManager
.isDeletableState(applicationReleaseDTO.getCurrentState())) {
isDeletableApp.set(false);
}
if (state == null || state.equals(applicationReleaseDTO.getCurrentState())) {
filteredApplicationReleaseDTOs.add(applicationReleaseDTO);
}
} }
} else {
isVisibleApp = true;
} }
if (!isVisibleApp) { applicationDTO.setApplicationReleaseDTOs(filteredApplicationReleaseDTOs);
String msg = "You are trying to access visibility restricted application. You don't have required " applicationDTO.setTags(this.applicationDAO.getAppTags(appId, tenantId));
+ "roles to view this application,"; applicationDTO.setAppCategories(this.applicationDAO.getAppCategories(appId, tenantId));
log.error(msg);
throw new ForbiddenException(msg); Application application = APIUtil.appDtoToAppResponse(applicationDTO);
} application.setHideableApp(isHideableApp.get());
return APIUtil.appDtoToAppResponse(applicationDTO); application.setDeletableApp(isDeletableApp.get());
return application;
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the database connection to get application for application ID: " String msg =
+ appId; "Error occurred while obtaining the database connection to get application for application ID: "
+ appId;
log.error(msg, e); log.error(msg, e);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} catch (LifecycleManagementException e) { } catch (LifecycleManagementException e) {
@ -1219,7 +1220,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
public Application getApplicationByUuid(String releaseUuid) throws ApplicationManagementException{ public Application getApplicationByUuid(String releaseUuid) throws ApplicationManagementException{
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
boolean isVisibleApp = false;
try { try {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
@ -1230,34 +1230,36 @@ public class ApplicationManagerImpl implements ApplicationManager {
throw new NotFoundException(msg); throw new NotFoundException(msg);
} }
List<String> tags = this.applicationDAO.getAppTags(applicationDTO.getId(), tenantId); ApplicationReleaseDTO applicationReleaseDTO = applicationDTO.getApplicationReleaseDTOs().get(0);
List<String> categories = this.applicationDAO.getAppCategories(applicationDTO.getId(), tenantId); if (lifecycleStateManager.isEndState(applicationReleaseDTO.getCurrentState())) {
applicationDTO.setTags(tags); return null;
applicationDTO.setAppCategories(categories);
List<String> unrestrictedRoles = this.visibilityDAO.getUnrestrictedRoles(applicationDTO.getId(), tenantId);
if (!unrestrictedRoles.isEmpty()) {
if (hasUserRole(unrestrictedRoles, userName)) {
isVisibleApp = true;
}
} else {
isVisibleApp = true;
} }
if (!isVisibleApp) { List<String> unrestrictedRoles = this.visibilityDAO.getUnrestrictedRoles(applicationDTO.getId(), tenantId);
if (!unrestrictedRoles.isEmpty() && !hasUserRole(unrestrictedRoles, userName)) {
String msg = "You are trying to access visibility restricted application. You don't have required " String msg = "You are trying to access visibility restricted application. You don't have required "
+ "roles to view this application,"; + "roles to view this application,";
log.error(msg); log.error(msg);
throw new ForbiddenException(msg); throw new ForbiddenException(msg);
} }
return APIUtil.appDtoToAppResponse(applicationDTO);
applicationDTO.setTags(this.applicationDAO.getAppTags(applicationDTO.getId(), tenantId));
applicationDTO.setAppCategories(this.applicationDAO.getAppCategories(applicationDTO.getId(), tenantId));
Application application = APIUtil.appDtoToAppResponse(applicationDTO);
if (lifecycleStateManager.isDeletableState(applicationReleaseDTO.getCurrentState())) {
ApplicationDTO entireApplication = applicationDAO.getApplication(applicationDTO.getId(), tenantId);
application.setDeletableApp(isDeletableApp(entireApplication.getApplicationReleaseDTOs()));
}
return application;
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the database connection to get application for application " String msg = "Error occurred while obtaining the database connection to get application for application "
+ "release UUID: " + releaseUuid; + "release UUID: " + releaseUuid;
log.error(msg, e); log.error(msg, e);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} catch (UserStoreException e) { } catch (UserStoreException e) {
String msg = "User-store exception occurred while getting application for application release UUID " + releaseUuid; String msg = "User-store exception occurred while getting application for application release UUID "
+ releaseUuid;
log.error(msg, e); log.error(msg, e);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} catch (ApplicationManagementDAOException e) { } catch (ApplicationManagementDAOException e) {

@ -165,10 +165,13 @@ public interface ApplicationManagementPublisherAPI {
response = ApplicationDTO.class), response = ApplicationDTO.class),
@ApiResponse( @ApiResponse(
code = 403, code = 403,
message = "Don't have permission to access the application"), message = "Forbidden. \n Don't have permission to access the application"),
@ApiResponse( @ApiResponse(
code = 404, code = 404,
message = "Application not found"), message = "Not Found. \n Application not found"),
@ApiResponse(
code = 409,
message = "Conflict. \n Couldn't find an active application"),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server Error. \n Error occurred while getting relevant application.", message = "Internal Server Error. \n Error occurred while getting relevant application.",
@ -211,10 +214,13 @@ public interface ApplicationManagementPublisherAPI {
response = ApplicationDTO.class), response = ApplicationDTO.class),
@ApiResponse( @ApiResponse(
code = 403, code = 403,
message = "Don't have permission to access the application release"), message = "Forbidden. \n Don't have permission to access the application release"),
@ApiResponse( @ApiResponse(
code = 404, code = 404,
message = "Application release not found"), message = "Not Found. \n Application release not found"),
@ApiResponse(
code = 409,
message = "Conflict. \n Application release is in the end state of lifecycle flow"),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server Error. \n Error occurred while getting relevant application release.", message = "Internal Server Error. \n Error occurred while getting relevant application release.",

@ -313,8 +313,8 @@ public interface ApplicationManagementPublisherAdminAPI {
consumes = MediaType.APPLICATION_JSON, consumes = MediaType.APPLICATION_JSON,
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
httpMethod = "PUT", httpMethod = "PUT",
value = "Retire the application with the given UUID", value = "Retire the application with the given app Id",
notes = "This will retire the application with the given UUID", notes = "This will retire the application with the given app Id",
tags = "Application Management", tags = "Application Management",
extensions = { extensions = {
@Extension(properties = { @Extension(properties = {
@ -326,7 +326,7 @@ public interface ApplicationManagementPublisherAdminAPI {
value = { value = {
@ApiResponse( @ApiResponse(
code = 200, code = 200,
message = "OK. \n Successfully deleted the application identified by UUID.", message = "OK. \n Successfully deleted the application identified by app Id.",
response = List.class), response = List.class),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
@ -341,8 +341,8 @@ public interface ApplicationManagementPublisherAdminAPI {
}) })
Response retireApplication( Response retireApplication(
@ApiParam( @ApiParam(
name = "UUID", name = "appId",
value = "Unique identifier of the ApplicationDTO", value = "Application Id",
required = true) required = true)
@PathParam("appId") int applicationId @PathParam("appId") int applicationId
); );

@ -116,9 +116,9 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem
try { try {
Application application = applicationManager.getApplicationById(appId, state); Application application = applicationManager.getApplicationById(appId, state);
if (application == null){ if (application == null){
String msg = "Could not found an application release which is in " + state + " state."; String msg = "Could not found an active application which has Id: " + appId;
log.error(msg); log.error(msg);
return Response.status(Response.Status.OK).entity(msg).build(); return Response.status(Response.Status.CONFLICT).entity(msg).build();
} }
return Response.status(Response.Status.OK).entity(application).build(); return Response.status(Response.Status.OK).entity(application).build();
} catch (NotFoundException e) { } catch (NotFoundException e) {
@ -148,7 +148,7 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem
if (application == null){ if (application == null){
String msg = "Application release is in the end state of the application lifecycle flow."; String msg = "Application release is in the end state of the application lifecycle flow.";
log.error(msg); log.error(msg);
return Response.status(Response.Status.OK).entity(msg).build(); return Response.status(Response.Status.CONFLICT).entity(msg).build();
} }
return Response.status(Response.Status.OK).entity(application).build(); return Response.status(Response.Status.OK).entity(application).build();
} catch (NotFoundException e) { } catch (NotFoundException e) {

Loading…
Cancel
Save