diff --git a/api/src/com/cloud/api/BaseCmd.java b/api/src/com/cloud/api/BaseCmd.java index 0c52cf718e4..b6b4ff57504 100755 --- a/api/src/com/cloud/api/BaseCmd.java +++ b/api/src/com/cloud/api/BaseCmd.java @@ -73,6 +73,7 @@ public abstract class BaseCmd { public static final int MALFORMED_PARAMETER_ERROR = 430; public static final int PARAM_ERROR = 431; public static final int UNSUPPORTED_ACTION_ERROR = 432; + public static final int PAGE_LIMIT_EXCEED = 433; // Server error codes public static final int INTERNAL_ERROR = 530; diff --git a/api/src/com/cloud/api/BaseListCmd.java b/api/src/com/cloud/api/BaseListCmd.java index 4f6e08deec7..bad05e55e68 100755 --- a/api/src/com/cloud/api/BaseListCmd.java +++ b/api/src/com/cloud/api/BaseListCmd.java @@ -6,7 +6,7 @@ import com.cloud.exception.InvalidParameterValueException; public abstract class BaseListCmd extends BaseCmd { - private static final Long MAX_PAGESIZE = 500L; + private static final Long MAX_PAGESIZE = _configService.getDefaultPageSize(); ///////////////////////////////////////////////////// /////////// BaseList API parameters ///////////////// @@ -52,9 +52,9 @@ public abstract class BaseListCmd extends BaseCmd { if (pageSize == -1) { pageSize = null; } else if (pageSize > MAX_PAGESIZE){//FIX ME - have a validator and do this. - throw new InvalidParameterValueException("The parameter " +ApiConstants.PAGE_SIZE+ " exceeded its max value - "+MAX_PAGESIZE); + throw new InvalidParameterValueException("The parameter " + ApiConstants.PAGE_SIZE + " exceeded its max value - " + MAX_PAGESIZE); } - } + } return pageSize; } diff --git a/api/src/com/cloud/configuration/ConfigurationService.java b/api/src/com/cloud/configuration/ConfigurationService.java index 57586af0aa4..f5b3c71aea5 100644 --- a/api/src/com/cloud/configuration/ConfigurationService.java +++ b/api/src/com/cloud/configuration/ConfigurationService.java @@ -191,5 +191,7 @@ public interface ConfigurationService { DataCenter getZone(long id); ServiceOffering getServiceOffering(long serviceOfferingId); + + Long getDefaultPageSize(); } diff --git a/server/src/com/cloud/api/ApiServer.java b/server/src/com/cloud/api/ApiServer.java index 18bb84c7ccd..5ed57bb25b2 100755 --- a/server/src/com/cloud/api/ApiServer.java +++ b/server/src/com/cloud/api/ApiServer.java @@ -131,7 +131,7 @@ public class ApiServer implements HttpRequestHandler { private static List s_allCommands = null; private static ExecutorService _executor = new ThreadPoolExecutor(10, 150, 60, TimeUnit.SECONDS, new LinkedBlockingQueue(), new NamedThreadFactory("ApiServer")); - + static { s_userCommands = new ArrayList(); s_resellerCommands = new ArrayList(); @@ -436,12 +436,21 @@ public class ApiServer implements HttpRequestHandler { // if the command is of the listXXXCommand, we will need to also return the // the job id and status if possible if (cmdObj instanceof BaseListCmd) { + validatePageSize((BaseListCmd)cmdObj); buildAsyncListResponse((BaseListCmd)cmdObj, account); } return ApiResponseSerializer.toSerializedString((ResponseObject)cmdObj.getResponseObject(), cmdObj.getResponseType()); } } + private void validatePageSize(BaseListCmd command) { + List responses = ((ListResponse)command.getResponseObject()).getResponses(); + int defaultPageLimit = BaseCmd._configService.getDefaultPageSize().intValue(); + if (responses != null && responses.size() > defaultPageLimit && command.getPage() == null && command.getPageSize() == null) { + throw new ServerApiException(BaseCmd.PAGE_LIMIT_EXCEED, "Number of returned objects per page exceed default page limit " + defaultPageLimit + "; please specify \"page\"/\"pagesize\" parameters"); + } + } + private void buildAsyncListResponse(BaseListCmd command, Account account) { List responses = ((ListResponse)command.getResponseObject()).getResponses(); if (responses != null && responses.size() > 0) { diff --git a/server/src/com/cloud/configuration/Config.java b/server/src/com/cloud/configuration/Config.java index 010220885f2..974e3321c5e 100755 --- a/server/src/com/cloud/configuration/Config.java +++ b/server/src/com/cloud/configuration/Config.java @@ -217,7 +217,9 @@ public enum Config { VmOpLockStateRetry("Advanced", ManagementServer.class, Integer.class, "vm.op.lock.state.retry", "5", "Times to retry locking the state of a VM for operations", "-1 means try forever"), VmOpCleanupInterval("Advanced", ManagementServer.class, Long.class, "vm.op.cleanup.interval", "86400", "Interval to run the thread that cleans up the vm operations (in seconds)", "Seconds"), VmOpCleanupWait("Advanced", ManagementServer.class, Long.class, "vm.op.cleanup.wait", "3600", "Time (in seconds) to wait before cleanuping up any vm work items", "Seconds"), - VmOpCancelInterval("Advanced", ManagementServer.class, Long.class, "vm.op.cancel.interval", "3600", "Time (in seconds) to wait before cancelling a operation", "Seconds"); + VmOpCancelInterval("Advanced", ManagementServer.class, Long.class, "vm.op.cancel.interval", "3600", "Time (in seconds) to wait before cancelling a operation", "Seconds"), + + DefaultPageSize("Advanced", ManagementServer.class, Long.class, "default.page.size", "500", "Default page size for API list* commands", null); private final String _category; diff --git a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java index a8bbafa6b2f..d54e00d583c 100755 --- a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java +++ b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java @@ -162,6 +162,7 @@ public class ConfigurationManagerImpl implements ConfigurationManager, Configura protected static final DataCenterLinkLocalIpAddressDaoImpl _LinkLocalIpAllocDao = ComponentLocator.inject(DataCenterLinkLocalIpAddressDaoImpl.class); private int _maxVolumeSizeInGb; + private long _defaultPageSize; protected Set configValuesForValidation; @Override @@ -170,6 +171,9 @@ public class ConfigurationManagerImpl implements ConfigurationManager, Configura String maxVolumeSizeInGbString = _configDao.getValue("storage.max.volume.size"); _maxVolumeSizeInGb = NumbersUtil.parseInt(maxVolumeSizeInGbString, 2000); + + String defaultPageSizeString = _configDao.getValue("default.page.size"); + _defaultPageSize = NumbersUtil.parseLong(defaultPageSizeString, 500L); populateConfigValuesForValidationSet(); return true; @@ -2787,4 +2791,9 @@ public class ConfigurationManagerImpl implements ConfigurationManager, Configura public ServiceOffering getServiceOffering(long serviceOfferingId) { return _serviceOfferingDao.findById(serviceOfferingId); } + + @Override + public Long getDefaultPageSize() { + return _defaultPageSize; + } }