This is an automated email from the ASF dual-hosted git repository.
madhan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ranger.git
The following commit(s) were added to refs/heads/master by this push:
new 7c805efe8 RANGER-4517: updated REST APIs handling of sortType when
sortBy is not specified
7c805efe8 is described below
commit 7c805efe864f821d1f57d2cd296e0a2b8af02681
Author: Subhrat Chaudhary <[email protected]>
AuthorDate: Wed Nov 8 12:14:49 2023 -0800
RANGER-4517: updated REST APIs handling of sortType when sortBy is not
specified
Signed-off-by: Madhan Neethiraj <[email protected]>
---
.../org/apache/ranger/common/RangerConstants.java | 2 +
.../org/apache/ranger/common/RangerSearchUtil.java | 69 +++++++++++++---------
2 files changed, 44 insertions(+), 27 deletions(-)
diff --git
a/security-admin/src/main/java/org/apache/ranger/common/RangerConstants.java
b/security-admin/src/main/java/org/apache/ranger/common/RangerConstants.java
index f00ea05ca..4d03042fc 100644
--- a/security-admin/src/main/java/org/apache/ranger/common/RangerConstants.java
+++ b/security-admin/src/main/java/org/apache/ranger/common/RangerConstants.java
@@ -182,6 +182,8 @@ public class RangerConstants extends RangerCommonEnums {
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,
diff --git
a/security-admin/src/main/java/org/apache/ranger/common/RangerSearchUtil.java
b/security-admin/src/main/java/org/apache/ranger/common/RangerSearchUtil.java
index 5ddc965a4..62ff8e135 100644
---
a/security-admin/src/main/java/org/apache/ranger/common/RangerSearchUtil.java
+++
b/security-admin/src/main/java/org/apache/ranger/common/RangerSearchUtil.java
@@ -194,25 +194,27 @@ public class RangerSearchUtil extends SearchUtil {
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>());
}
@@ -383,11 +385,14 @@ public class RangerSearchUtil extends SearchUtil {
}
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();
@@ -404,30 +409,40 @@ public class RangerSearchUtil extends SearchUtil {
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;
}
}