mirror of https://github.com/apache/cloudstack.git
bug 9564: 1) For all list commands set pageSize() to default.page.size if not specified 2) When pageSize is specified in the request, and it's more than default.page.size - throw an exception
status 9564: resolved fixed Conflicts: server/src/com/cloud/api/ApiDBUtils.java server/src/com/cloud/api/ApiResponseHelper.java server/src/com/cloud/api/ApiServer.java server/src/com/cloud/server/ManagementServer.java server/src/com/cloud/server/ManagementServerImpl.java
This commit is contained in:
parent
9a983d3bd8
commit
1ef546e588
|
|
@ -3,28 +3,27 @@ package com.cloud.api;
|
|||
import com.cloud.async.AsyncJob;
|
||||
import com.cloud.exception.InvalidParameterValueException;
|
||||
|
||||
|
||||
public abstract class BaseListCmd extends BaseCmd {
|
||||
|
||||
private static Long MAX_PAGESIZE = 500L;
|
||||
|
||||
/////////////////////////////////////////////////////
|
||||
/////////// BaseList API parameters /////////////////
|
||||
/////////////////////////////////////////////////////
|
||||
// ///////////////////////////////////////////////////
|
||||
// ///////// BaseList API parameters /////////////////
|
||||
// ///////////////////////////////////////////////////
|
||||
|
||||
@Parameter(name="keyword", type=CommandType.STRING, description="List by keyword")
|
||||
@Parameter(name = "keyword", type = CommandType.STRING, description = "List by keyword")
|
||||
private String keyword;
|
||||
|
||||
// FIXME: Need to be able to specify next/prev/first/last, so Integer might not be right
|
||||
@Parameter(name=ApiConstants.PAGE, type=CommandType.INTEGER)
|
||||
// FIXME: Need to be able to specify next/prev/first/last, so Integer might not be right
|
||||
@Parameter(name = ApiConstants.PAGE, type = CommandType.INTEGER)
|
||||
private Integer page;
|
||||
|
||||
@Parameter(name=ApiConstants.PAGE_SIZE, type=CommandType.INTEGER)
|
||||
@Parameter(name = ApiConstants.PAGE_SIZE, type = CommandType.INTEGER)
|
||||
private Integer pageSize;
|
||||
|
||||
/////////////////////////////////////////////////////
|
||||
/////////////////// Accessors ///////////////////////
|
||||
/////////////////////////////////////////////////////
|
||||
// ///////////////////////////////////////////////////
|
||||
// ///////////////// Accessors ///////////////////////
|
||||
// ///////////////////////////////////////////////////
|
||||
|
||||
public String getKeyword() {
|
||||
return keyword;
|
||||
|
|
@ -35,51 +34,46 @@ public abstract class BaseListCmd extends BaseCmd {
|
|||
}
|
||||
|
||||
public Integer getPageSize() {
|
||||
if (pageSize != null && pageSize.longValue() > MAX_PAGESIZE.longValue()) {
|
||||
throw new InvalidParameterValueException("Page size can't exceed max allowed page size value: " + MAX_PAGESIZE.longValue());
|
||||
}
|
||||
|
||||
return pageSize;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void configure() {
|
||||
MAX_PAGESIZE = _configService.getDefaultPageSize();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public long getEntityOwnerId() {
|
||||
//no owner is needed for list command
|
||||
// no owner is needed for list command
|
||||
return 0;
|
||||
}
|
||||
|
||||
public Long getPageSizeVal() {
|
||||
Long pageSize = null;
|
||||
Long pageSize = MAX_PAGESIZE;
|
||||
Integer pageSizeInt = getPageSize();
|
||||
if (pageSizeInt != null) {
|
||||
pageSize = pageSizeInt.longValue();
|
||||
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);
|
||||
}
|
||||
}
|
||||
if (pageSizeInt != null && pageSizeInt.intValue() != -1) {
|
||||
pageSize = pageSizeInt.longValue();
|
||||
}
|
||||
return pageSize;
|
||||
}
|
||||
|
||||
public Long getStartIndex() {
|
||||
Long startIndex = Long.valueOf(0);
|
||||
Long pageSizeVal = getPageSizeVal();
|
||||
if (pageSizeVal == null) {
|
||||
return null; // there's no limit, so start index is irrelevant
|
||||
}
|
||||
|
||||
if (page != null) {
|
||||
int pageNum = page.intValue();
|
||||
if (pageNum > 0) {
|
||||
startIndex = Long.valueOf(pageSizeVal * (pageNum-1));
|
||||
startIndex = Long.valueOf(pageSizeVal * (pageNum - 1));
|
||||
}
|
||||
}
|
||||
return startIndex;
|
||||
}
|
||||
|
||||
|
||||
public AsyncJob.Type getInstanceType() {
|
||||
return AsyncJob.Type.None;
|
||||
return AsyncJob.Type.None;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,8 +15,8 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
package com.cloud.api.commands;
|
||||
|
||||
package com.cloud.api.commands;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
|
@ -27,50 +27,49 @@ import com.cloud.api.ApiConstants;
|
|||
import com.cloud.api.BaseListCmd;
|
||||
import com.cloud.api.Implementation;
|
||||
import com.cloud.api.Parameter;
|
||||
import com.cloud.api.BaseCmd.CommandType;
|
||||
import com.cloud.api.response.AlertResponse;
|
||||
import com.cloud.api.response.ListResponse;
|
||||
|
||||
@Implementation(description="Lists all alerts.", responseObject=AlertResponse.class)
|
||||
@Implementation(description = "Lists all alerts.", responseObject = AlertResponse.class)
|
||||
public class ListAlertsCmd extends BaseListCmd {
|
||||
|
||||
public static final Logger s_logger = Logger.getLogger(ListAlertsCmd.class.getName());
|
||||
|
||||
private static final String s_name = "listalertsresponse";
|
||||
|
||||
/////////////////////////////////////////////////////
|
||||
//////////////// API parameters /////////////////////
|
||||
/////////////////////////////////////////////////////
|
||||
// ///////////////////////////////////////////////////
|
||||
// ////////////// API parameters /////////////////////
|
||||
// ///////////////////////////////////////////////////
|
||||
|
||||
@Parameter(name=ApiConstants.ID, type=CommandType.LONG, description="the ID of the alert")
|
||||
@Parameter(name = ApiConstants.ID, type = CommandType.LONG, description = "the ID of the alert")
|
||||
private Long id;
|
||||
|
||||
@Parameter(name=ApiConstants.TYPE, type=CommandType.STRING, description="list by alert type")
|
||||
|
||||
@Parameter(name = ApiConstants.TYPE, type = CommandType.STRING, description = "list by alert type")
|
||||
private String type;
|
||||
|
||||
/////////////////////////////////////////////////////
|
||||
/////////////////// Accessors ///////////////////////
|
||||
/////////////////////////////////////////////////////
|
||||
// ///////////////////////////////////////////////////
|
||||
// ///////////////// Accessors ///////////////////////
|
||||
// ///////////////////////////////////////////////////
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////
|
||||
/////////////// API Implementation///////////////////
|
||||
/////////////////////////////////////////////////////
|
||||
// ///////////////////////////////////////////////////
|
||||
// ///////////// API Implementation///////////////////
|
||||
// ///////////////////////////////////////////////////
|
||||
|
||||
@Override
|
||||
public String getCommandName() {
|
||||
return s_name;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void execute(){
|
||||
public void execute() {
|
||||
List<? extends Alert> result = _mgr.searchForAlerts(this);
|
||||
ListResponse<AlertResponse> response = new ListResponse<AlertResponse>();
|
||||
List<AlertResponse> alertResponseList = new ArrayList<AlertResponse>();
|
||||
|
|
|
|||
|
|
@ -93,9 +93,7 @@ public class ListCapacityCmd extends BaseListCmd {
|
|||
pageSizeVal = pageSize.longValue();
|
||||
}
|
||||
return pageSizeVal;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(){
|
||||
|
|
|
|||
|
|
@ -431,20 +431,12 @@ 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<ResponseObject> 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<ResponseObject> responses = ((ListResponse)command.getResponseObject()).getResponses();
|
||||
|
|
|
|||
Loading…
Reference in New Issue