Skip to content

Commit

Permalink
RANGER-4517: updated REST APIs handling of sortType when sortBy is no…
Browse files Browse the repository at this point in the history
…t specified

Signed-off-by: Madhan Neethiraj <madhan@apache.org>
  • Loading branch information
suchnit authored and mneethiraj committed Nov 8, 2023
1 parent 279f41f commit 7c805ef
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ public enum ValidationUserProfile {
RangerConstants.ROLE_SYS_ADMIN, RangerConstants.ROLE_KEY_ADMIN, RangerConstants.ROLE_ADMIN_AUDITOR,
RangerConstants.ROLE_KEY_ADMIN_AUDITOR));

public static final String DEFAULT_SORT_ORDER = "asc";

public static enum RBAC_PERM {
ALLOW_NONE,
ALLOW_READ,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,25 +194,27 @@ public SearchFilter extractCommonCriteriasForFilter(HttpServletRequest request,
StringUtil.VALIDATION_ALPHA, "Invalid value for parameter sortBy", MessageEnums.INVALID_INPUT_DATA,
null, SearchFilter.SORT_BY);

boolean sortSet = false;
if (!StringUtils.isEmpty(sortBy)) {
boolean sortSet = false;

for (SortField sortField : sortFields) {
if (sortField.getParamName().equalsIgnoreCase(sortBy)) {
ret.setSortBy(sortField.getParamName());
String sortType = restErrorUtil.validateString(request.getParameter("sortType"),
StringUtil.VALIDATION_ALPHA, "Invalid value for parameter sortType",
MessageEnums.INVALID_INPUT_DATA, null, "sortType");
ret.setSortType(sortType);
sortSet = true;
break;
}
}
}

if (!sortSet && !StringUtils.isEmpty(sortBy)) {
logger.info("Invalid or unsupported sortBy field passed. sortBy=" + sortBy, new Throwable());
if (!sortSet) {
logger.info("Invalid or unsupported sortBy field passed. sortBy=" + sortBy, new Throwable());
}
}


String sortType = restErrorUtil.validateString(request.getParameter("sortType"),
StringUtil.VALIDATION_ALPHA, "Invalid value for parameter sortType",
MessageEnums.INVALID_INPUT_DATA, null, "sortType");
ret.setSortType(sortType);

if(ret.getParams() == null) {
ret.setParams(new HashMap<String, String>());
}
Expand Down Expand Up @@ -383,11 +385,14 @@ public void updateQueryPageSize(Query query, SearchFilter searchCriteria) {
}

public String constructSortClause(SearchFilter searchCriteria, List<SortField> sortFields) {
String sortBy = searchCriteria.getSortBy();
String ret = null;
String sortBy = searchCriteria.getSortBy();
String sortType = getSortType(searchCriteria);
String querySortBy = null;

if (!stringUtil.isEmpty(sortBy)) {
sortBy = sortBy.trim();

for (SortField sortField : sortFields) {
if (sortBy.equalsIgnoreCase(sortField.getParamName())) {
querySortBy = sortField.getFieldName();
Expand All @@ -404,30 +409,40 @@ public String constructSortClause(SearchFilter searchCriteria, List<SortField> s
querySortBy = sortField.getFieldName();
// Override the sortBy using the default value
searchCriteria.setSortBy(sortField.getParamName());
searchCriteria.setSortType(sortField.getDefaultOrder().name());

if(sortType == null) {
sortType = sortField.getDefaultOrder().name();
}

searchCriteria.setSortType(sortType);
break;
}
}
}

if (querySortBy != null) {
String sortType = searchCriteria.getSortType();
String querySortType = "asc";
if (sortType != null) {
if ("asc".equalsIgnoreCase(sortType) || "desc".equalsIgnoreCase(sortType)) {
querySortType = sortType;
} else {
logger.error("Invalid sortType. sortType=" + sortType);
}
}

if(querySortType!=null){
searchCriteria.setSortType(querySortType.toLowerCase());
}
String sortClause = " ORDER BY " + querySortBy + " " + querySortType;
String querySortType = stringUtil.isEmpty(sortType) ? RangerConstants.DEFAULT_SORT_ORDER : sortType;

searchCriteria.setSortType(querySortType.toLowerCase());

return sortClause;
ret = " ORDER BY " + querySortBy + " " + querySortType;
}
return null;

return ret;
}

private String getSortType(SearchFilter searchCriteria) {
String ret = null;
String sortType = searchCriteria.getSortType();

if (!stringUtil.isEmpty(sortType)) {
if ("asc".equalsIgnoreCase(sortType) || "desc".equalsIgnoreCase(sortType)) {
ret = sortType;
} else {
logger.error("Invalid sortType. sortType=" + sortType);
}
}

return ret;
}
}

0 comments on commit 7c805ef

Please sign in to comment.