Added validation for app name length when creating an app

Co-authored-by: Ravindu Madhubhashana <ravindu.kp@gmail.com>
Co-committed-by: Ravindu Madhubhashana <ravindu.kp@gmail.com>
remotes/1729253769841084517/master
parent c03e1359d3
commit e28c71d4ae

@ -3434,6 +3434,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
int deviceTypeId = -1; int deviceTypeId = -1;
String appName; String appName;
int appNameLength = 20;
List<String> appCategories; List<String> appCategories;
List<String> unrestrictedRoles; List<String> unrestrictedRoles;
@ -3445,6 +3446,11 @@ public class ApplicationManagerImpl implements ApplicationManager {
log.error(msg); log.error(msg);
throw new BadRequestException(msg); throw new BadRequestException(msg);
} }
if (appName.length() > appNameLength) {
String msg = "Application name must be less than or equal to 20 characters in length.";
log.error(msg);
throw new BadRequestException(msg);
}
appCategories = applicationWrapper.getCategories(); appCategories = applicationWrapper.getCategories();
if (appCategories == null) { if (appCategories == null) {
String msg = "Application category can't be null."; String msg = "Application category can't be null.";
@ -3477,6 +3483,11 @@ public class ApplicationManagerImpl implements ApplicationManager {
log.error(msg); log.error(msg);
throw new BadRequestException(msg); throw new BadRequestException(msg);
} }
if (appName.length() > appNameLength) {
String msg = "Application name must be less than or equal to 20 characters in length.";
log.error(msg);
throw new BadRequestException(msg);
}
appCategories = webAppWrapper.getCategories(); appCategories = webAppWrapper.getCategories();
if (appCategories == null) { if (appCategories == null) {
String msg = "Web Clip category can't be null."; String msg = "Web Clip category can't be null.";
@ -3510,6 +3521,11 @@ public class ApplicationManagerImpl implements ApplicationManager {
log.error(msg); log.error(msg);
throw new BadRequestException(msg); throw new BadRequestException(msg);
} }
if (appName.length() > appNameLength) {
String msg = "Application name must be less than or equal to 20 characters in length.";
log.error(msg);
throw new BadRequestException(msg);
}
appCategories = publicAppWrapper.getCategories(); appCategories = publicAppWrapper.getCategories();
if (appCategories == null) { if (appCategories == null) {
String msg = "Application category can't be null."; String msg = "Application category can't be null.";
@ -3542,6 +3558,11 @@ public class ApplicationManagerImpl implements ApplicationManager {
log.error(msg); log.error(msg);
throw new BadRequestException(msg); throw new BadRequestException(msg);
} }
if (appName.length() > appNameLength) {
String msg = "Application name must be less than or equal to 20 characters in length.";
log.error(msg);
throw new BadRequestException(msg);
}
appCategories = customAppWrapper.getCategories(); appCategories = customAppWrapper.getCategories();
if (appCategories == null) { if (appCategories == null) {
String msg = "Application category can't be null."; String msg = "Application category can't be null.";

@ -371,19 +371,28 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem
if (appName == null) { if (appName == null) {
String msg = "Invalid app name, appName query param cannot be empty/null."; String msg = "Invalid app name, appName query param cannot be empty/null.";
log.error(msg); log.error(msg);
return Response.status(Response.Status.BAD_REQUEST).build(); return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}
if (appName.length() > 20) {
String msg = "Invalid app name, maximum length of the application name should be 20 characters.";
log.error(msg);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
} }
ApplicationManager applicationManager = APIUtil.getApplicationManager(); ApplicationManager applicationManager = APIUtil.getApplicationManager();
if (applicationManager.isExistingAppName(appName, deviceType)) { if (applicationManager.isExistingAppName(appName, deviceType)) {
return Response.status(Response.Status.CONFLICT).build(); String msg = "Invalid app name, app name already exists.";
log.error(msg);
return Response.status(Response.Status.CONFLICT).entity(msg).build();
} }
return Response.status(Response.Status.OK).build(); return Response.status(Response.Status.OK).build();
} catch (BadRequestException e) { } catch (BadRequestException e) {
log.error("Found invalid device type to check application existence.", e); String msg = "Found invalid device type to check application existence.";
return Response.status(Response.Status.BAD_REQUEST).build(); log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
} catch (ApplicationManagementException e) { } catch (ApplicationManagementException e) {
log.error("Internal Error occurred while checking the application existence.", e); String msg = "Internal Error occurred while checking the application existence.";
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
} }
} }

Loading…
Cancel
Save