Skip to content

Commit 6b432ff

Browse files
committed
Updated constructor in ServiceRequest to parse URL encoded form data. Also added support for array column queries in getWhereStatement().
1 parent ef8a5e4 commit 6b432ff

File tree

1 file changed

+88
-3
lines changed

1 file changed

+88
-3
lines changed

src/javaxt/express/ServiceRequest.java

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,37 @@ public ServiceRequest(String service, HttpServletRequest request){
6969

7070

7171

72+
//Parse payload if it contains URL encoded form data
73+
String contentType = request.getContentType();
74+
if (contentType!=null){
75+
if (contentType.equalsIgnoreCase("application/x-www-form-urlencoded")){
76+
byte[] b = getPayload();
77+
if (b!=null && b.length>0){
78+
try{
79+
LinkedHashMap<String, List<String>> params =
80+
javaxt.utils.URL.parseQueryString(new String(b, "UTF-8"));
81+
Iterator<String> it = params.keySet().iterator();
82+
while (it.hasNext()){
83+
String key = it.next();
84+
List<String> values = params.get(key);
85+
List<String> currValues = this.parameters.get(key);
86+
if (currValues==null){
87+
this.parameters.put(key, values);
88+
}
89+
else{
90+
for (String val : values){
91+
currValues.add(val);
92+
}
93+
}
94+
}
95+
}
96+
catch(Exception e){}
97+
}
98+
}
99+
}
100+
101+
102+
72103
//Parse path, excluding servlet and service path
73104
setPath(request.getPathInfo());
74105

@@ -505,7 +536,7 @@ public JSONObject getJson(){
505536
byte[] b = getPayload();
506537
if (b!=null && b.length>0){
507538
try{
508-
json = new JSONObject(new String(getPayload(), "UTF-8"));
539+
json = new JSONObject(new String(b, "UTF-8"));
509540
}
510541
catch(Exception e){}
511542
}
@@ -1505,6 +1536,7 @@ private String getWhereStatement(ArrayList<HashMap<String, Object>> tablesAndFie
15051536
String tableName = (String) map.get("tableName");
15061537
HashMap<String, String> fieldMap = (HashMap<String, String>) map.get("fieldMap");
15071538
HashSet<String> stringFields = (HashSet<String>) map.get("stringFields");
1539+
HashSet<String> arrayFields = (HashSet<String>) map.get("arrayFields");
15081540

15091541

15101542
Iterator<String> i2 = fieldMap.keySet().iterator();
@@ -1514,13 +1546,65 @@ private String getWhereStatement(ArrayList<HashMap<String, Object>> tablesAndFie
15141546
if (name.equalsIgnoreCase(fieldName) || name.equalsIgnoreCase(columnName)){
15151547
foundField = true;
15161548

1549+
//Wrap value is single quote as needed
15171550
if (v!=null && stringFields.contains(fieldName)){
15181551
if (!(v.startsWith("'") && v.endsWith("'"))){
1519-
v = "'" + v.replace("'","''") + "'";
1552+
if (op.equals("IN")){
1553+
//TODO: split by commas and add quotes
1554+
}
1555+
else{
1556+
v = "'" + v.replace("'","''") + "'";
1557+
}
15201558
}
15211559
}
15221560

1523-
arr.add("(" + tableName + "." + columnName + " " + op + " " + v + ")");
1561+
1562+
//Compile statement and update arr
1563+
if (arrayFields.contains(fieldName)){
1564+
1565+
//Special case for arrays
1566+
if (op.equals("=")){
1567+
arr.add("(" + v + " = ANY(" + tableName + "." + columnName + "))");
1568+
}
1569+
else if (op.equals("IN")){
1570+
if (v==null){
1571+
1572+
}
1573+
else{
1574+
1575+
//Split up "in" statement with a bunch of "or" statements
1576+
if (v.startsWith("(") && v.endsWith(")")){
1577+
v = v.substring(1, v.length()-1);
1578+
}
1579+
StringBuilder str = new StringBuilder("(");
1580+
String[] a = v.split(","); //very weak!
1581+
for (int i=0; i<a.length; i++){
1582+
if (i>0) str.append(" OR ");
1583+
String s = a[i];
1584+
if (stringFields.contains(fieldName)){
1585+
if (!(s.startsWith("'") && s.endsWith("'"))){
1586+
s = "'" + s.replace("'","''") + "'";
1587+
}
1588+
}
1589+
str.append("(" + s + " = ANY(" + tableName + "." + columnName + "))");
1590+
}
1591+
str.append(")");
1592+
arr.add(str.toString());
1593+
}
1594+
}
1595+
else{
1596+
//Not sure what other array operations we can support...
1597+
}
1598+
1599+
}
1600+
else{
1601+
1602+
//Most statements are generated here
1603+
arr.add("(" + tableName + "." + columnName + " " + op + " " + v + ")");
1604+
1605+
}
1606+
1607+
15241608
break;
15251609
}
15261610
}
@@ -1559,6 +1643,7 @@ private String getWhereStatement(ArrayList<HashMap<String, Object>> tablesAndFie
15591643
}
15601644
}
15611645

1646+
//console.log(where);
15621647
return where;
15631648
}
15641649

0 commit comments

Comments
 (0)