Skip to content

Commit 09fc2e2

Browse files
committed
Made param keywords configurable in ServiceRequest
1 parent 0fa2964 commit 09fc2e2

File tree

1 file changed

+72
-8
lines changed

1 file changed

+72
-8
lines changed

src/javaxt/express/ServiceRequest.java

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ public class ServiceRequest {
5050
};
5151

5252

53+
private Map<String, String> keywords = Map.ofEntries(
54+
Map.entry("fields", "fields"),
55+
Map.entry("orderby", "orderby"),
56+
Map.entry("limit", "limit"),
57+
Map.entry("offset", "offset"),
58+
59+
//Legacy - may be removed in the future
60+
Map.entry("filter", "filter"),
61+
Map.entry("count", "count"),
62+
Map.entry("where", "where"),
63+
Map.entry("page", "page")
64+
);
65+
66+
5367
//**************************************************************************
5468
//** Constructor
5569
//**************************************************************************
@@ -453,9 +467,9 @@ private static void removeParameter(String key, HashMap<String, List<String>> pa
453467
//** updateOffsetLimit
454468
//**************************************************************************
455469
private void updateOffsetLimit(){
456-
offset = getParameter("offset").toLong();
457-
limit = getParameter("limit").toLong();
458-
Long page = getParameter("page").toLong();
470+
offset = getParameter(getKeyword("offset")).toLong();
471+
limit = getParameter(getKeyword("limit")).toLong();
472+
Long page = getParameter(getKeyword("page")).toLong();
459473
if (offset==null && page!=null){
460474
if (limit==null) limit = 25L;
461475
offset = (page*limit)-limit;
@@ -653,7 +667,7 @@ public boolean isCacheable(String eTag, String date){
653667
*/
654668
public Field[] getFields(){
655669
if (fields!=null) return fields;
656-
String fields = getParameter("fields").toString();
670+
String fields = getParameter(getKeyword("fields")).toString();
657671

658672

659673
//If fields are empty, simply return an ampty array
@@ -779,9 +793,9 @@ public Filter getFilter(){
779793

780794
//Parse querystring
781795
LinkedHashMap<String, javaxt.utils.Value> params = new LinkedHashMap<>();
782-
if (hasParameter("filter")){
796+
if (hasParameter(getKeyword("filter"))){
783797

784-
String str = getParameter("filter").toString();
798+
String str = getParameter(getKeyword("filter")).toString();
785799
if (str.startsWith("{") && str.endsWith("}")){
786800
JSONObject json = new JSONObject(str);
787801
for (String key : json.keySet()){
@@ -807,10 +821,13 @@ public Filter getFilter(){
807821

808822
//Create filter
809823
filter = new Filter();
824+
HashSet<String> reservedKeywords = getKeywords();
810825
Iterator<String> it = params.keySet().iterator();
811826
while (it.hasNext()){
812827
String key = it.next().trim();
813828
if (key.isEmpty()) continue;
829+
if (key.equals("_")) continue;
830+
if (reservedKeywords.contains(key.toLowerCase())) continue;
814831

815832

816833
//Parse val
@@ -992,7 +1009,7 @@ else if (val.toLowerCase().startsWith("contains(") && val.endsWith(")")){
9921009
/** Returns the value for the "where" parameter in the HTTP request.
9931010
*/
9941011
public String getWhere(){
995-
return getParameter("where").toString();
1012+
return getParameter(getKeyword("where")).toString();
9961013
}
9971014

9981015

@@ -1010,7 +1027,7 @@ public Sort getSort(){
10101027
if (sort!=null) return sort;
10111028

10121029
LinkedHashMap<String, String> fields = new LinkedHashMap<>();
1013-
String orderBy = getParameter("orderby").toString();
1030+
String orderBy = getParameter(getKeyword("orderby")).toString();
10141031

10151032

10161033
if (orderBy!=null){
@@ -1056,6 +1073,53 @@ public Sort getSort(){
10561073
}
10571074

10581075

1076+
//**************************************************************************
1077+
//** getKeywords
1078+
//**************************************************************************
1079+
/** Returns a copy of all the keywords used to identify parameters (e.g.
1080+
* "fields", "orderby", "offset", "limit", etc)
1081+
*/
1082+
public HashSet<String> getKeywords(){
1083+
HashSet<String> reservedKeywords = new HashSet<>();
1084+
for (String key : keywords.keySet()){
1085+
reservedKeywords.add(getKeyword(key).toLowerCase());
1086+
}
1087+
return reservedKeywords;
1088+
}
1089+
1090+
1091+
//**************************************************************************
1092+
//** setKeyword
1093+
//**************************************************************************
1094+
/** Used to add or update an entry in the keyword map. For example, the
1095+
* default keyword used to identify fields in the getFields() method is
1096+
* "fields" and the default keyword used to identify sorting in getSort()
1097+
* if "orderby". This method provides a mechanism for overridding these
1098+
* defaults. For example, suppose in your application you wish to use
1099+
* "columns" instead of "fields" for the getFields() method. You would set
1100+
* the keyword to "columns" and type to "fields".
1101+
* @param keyword A parameter keyword.
1102+
* @param type What the given parameter should map to (e.g. "fields")
1103+
*/
1104+
public void setKeyword(String keyword, String type){
1105+
if (keyword==null) return;
1106+
keyword = keyword.trim();
1107+
if (keyword.isEmpty()) return;
1108+
keywords.put(type.toLowerCase(), keyword.toLowerCase());
1109+
}
1110+
1111+
1112+
//**************************************************************************
1113+
//** getKeyword
1114+
//**************************************************************************
1115+
/** Returns a keyword for a given type (see setKeyword for more information)
1116+
*/
1117+
public String getKeyword(String type){
1118+
if (type==null) return null;
1119+
return keywords.get(type.toLowerCase());
1120+
}
1121+
1122+
10591123
//**************************************************************************
10601124
//** Sort Class
10611125
//**************************************************************************

0 commit comments

Comments
 (0)