Commit 5d7692c434a9d886d6ba0da9451a5d45b52064bf
1 parent
35ea4f23
Exists in
master
update limit and field
Showing
1 changed file
with
225 additions
and
30 deletions
Show diff stats
src/main/java/sourcecode/MockUp/MockUp.java
| ... | ... | @@ -5,6 +5,7 @@ import java.io.OutputStream; |
| 5 | 5 | import java.net.InetSocketAddress; |
| 6 | 6 | import java.net.URLDecoder; |
| 7 | 7 | import java.util.ArrayList; |
| 8 | +import java.util.HashMap; | |
| 8 | 9 | |
| 9 | 10 | import org.apache.commons.io.IOUtils; |
| 10 | 11 | import org.apache.http.Consts; |
| ... | ... | @@ -71,6 +72,26 @@ public class MockUp |
| 71 | 72 | String method = t.getRequestMethod(); |
| 72 | 73 | String url = t.getRequestURI().toString(); |
| 73 | 74 | url = URLDecoder.decode(url, "UTF-8"); |
| 75 | + String bodyData = IOUtils.toString(t.getRequestBody(),Consts.UTF_8); | |
| 76 | + | |
| 77 | + | |
| 78 | + int limit = 0; | |
| 79 | + | |
| 80 | + if(!getKey("limit", url).equals("")) | |
| 81 | + { | |
| 82 | + try{ | |
| 83 | + limit = Integer.parseInt(getKey("limit", url)); | |
| 84 | + }catch(Exception e) | |
| 85 | + { | |
| 86 | + | |
| 87 | + } | |
| 88 | + } | |
| 89 | + | |
| 90 | + | |
| 91 | + String[] fields = new String[0]; | |
| 92 | + if(!getKey("fields", url).equals("")) | |
| 93 | + fields = getKey("fields", url).split(","); | |
| 94 | + | |
| 74 | 95 | |
| 75 | 96 | // System.out.println(t.getRequestMethod()); |
| 76 | 97 | // if(!method.equals(GET)) |
| ... | ... | @@ -96,7 +117,7 @@ public class MockUp |
| 96 | 117 | // System.out.println(t.getRequestURI()); |
| 97 | 118 | |
| 98 | 119 | |
| 99 | - String keyData = findKey(method,url,IOUtils.toString(t.getRequestBody(),Consts.UTF_8)); | |
| 120 | + String keyData = findKey(method,url,bodyData); | |
| 100 | 121 | |
| 101 | 122 | String collectionName = getSuffixTableName(url); |
| 102 | 123 | JSONObject responsJSON = new JSONObject(); |
| ... | ... | @@ -139,7 +160,7 @@ public class MockUp |
| 139 | 160 | { |
| 140 | 161 | |
| 141 | 162 | |
| 142 | - ArrayList<String> responseList = getDBData(keyData,collectionName,method); | |
| 163 | + ArrayList<String> responseList = getDBData(keyData,collectionName,method,bodyData); | |
| 143 | 164 | |
| 144 | 165 | |
| 145 | 166 | |
| ... | ... | @@ -152,6 +173,7 @@ public class MockUp |
| 152 | 173 | { |
| 153 | 174 | if(method.equals(GET) && responseList.size() > 1) |
| 154 | 175 | { |
| 176 | + System.out.println("Multi Data => Merge"); | |
| 155 | 177 | ArrayList<Object> resultData = new ArrayList<Object>(); |
| 156 | 178 | for(int i=0;i<responseList.size();i++) |
| 157 | 179 | { |
| ... | ... | @@ -162,33 +184,11 @@ public class MockUp |
| 162 | 184 | JSONObject value = (JSONObject) rowJSON.get("value"); |
| 163 | 185 | if(value.has("resultData")) |
| 164 | 186 | { |
| 165 | - | |
| 166 | -// if((value.get("resultData").toString().length()> 0) && (value.get("resultData").toString().substring(0,1).equals("["))) | |
| 167 | -// { | |
| 168 | -//// Object test = gson.fromJson(value.get("resultData").toString(), Object.class); | |
| 169 | -//// System.out.println(test); | |
| 170 | -// | |
| 171 | -//// value.get("resultData").toString() | |
| 172 | -// JSONObject resultlist = new JSONObject("{'test' : "+value.get("resultData").toString()+"}"); | |
| 173 | -//// System.out.println(resultlist.get("test")); | |
| 174 | -// JSONArray JSONArrayData = value.getJSONArray("resultData"); | |
| 175 | -// System.out.println("aaaaaaaaaaaaaaaaaaaa"+JSONArrayData.get(0)); | |
| 176 | -//// for(int n=0;n<resultlist.getJSONObject("test").length();n++) | |
| 177 | -//// { | |
| 178 | -////// System.out.println(resultlist.get()); | |
| 179 | -//// } | |
| 180 | -//// System.out.println(resultlist.get("test")); | |
| 181 | -//// System.out.println("{'test' : "+value.get("resultData").toString()+"}"); | |
| 182 | -// } | |
| 183 | - | |
| 184 | 187 | try{ |
| 185 | 188 | JSONArray JSONArrayData = value.getJSONArray("resultData"); |
| 186 | -// System.out.println("bbbbbbbbbb"+JSONArrayData.length()); | |
| 187 | 189 | for(int n=0;n<JSONArrayData.length();n++) |
| 188 | - { | |
| 189 | 190 | resultData.add(JSONArrayData.get(n)); |
| 190 | -// System.out.println(JSONArrayData.get(n)); | |
| 191 | - } | |
| 191 | + | |
| 192 | 192 | }catch(Exception e) |
| 193 | 193 | { |
| 194 | 194 | // e.printStackTrace(); |
| ... | ... | @@ -207,6 +207,7 @@ public class MockUp |
| 207 | 207 | |
| 208 | 208 | }else |
| 209 | 209 | { |
| 210 | + System.out.println("Single Data => Direct"); | |
| 210 | 211 | JSONObject rowJSON = new JSONObject(responseList.get(0)); |
| 211 | 212 | if(rowJSON.has("value")) |
| 212 | 213 | responsJSON = (JSONObject) rowJSON.get("value"); |
| ... | ... | @@ -224,6 +225,13 @@ public class MockUp |
| 224 | 225 | |
| 225 | 226 | } |
| 226 | 227 | |
| 228 | + //limit | |
| 229 | + responsJSON = limitData(responsJSON,limit); | |
| 230 | + | |
| 231 | + //field | |
| 232 | + responsJSON = FieldsData(responsJSON,fields); | |
| 233 | + | |
| 234 | + | |
| 227 | 235 | |
| 228 | 236 | response = responsJSON.toString(); |
| 229 | 237 | |
| ... | ... | @@ -260,7 +268,6 @@ public class MockUp |
| 260 | 268 | } |
| 261 | 269 | return value; |
| 262 | 270 | } |
| 263 | - | |
| 264 | 271 | |
| 265 | 272 | private static void MongoConnector(String db) { |
| 266 | 273 | |
| ... | ... | @@ -297,22 +304,34 @@ public class MockUp |
| 297 | 304 | } |
| 298 | 305 | } |
| 299 | 306 | |
| 300 | - private static ArrayList<String> getDBData(String keyData,String collectionName,String method) | |
| 307 | + private static ArrayList<String> getDBData(String keyData,String collectionName,String method,String bodyData) | |
| 301 | 308 | { |
| 302 | 309 | |
| 303 | 310 | ArrayList<String> returnData = new ArrayList<String>(); |
| 304 | - collectionName = method.toLowerCase()+"_"+collectionName; | |
| 311 | +// collectionName = method.toLowerCase()+"_"+collectionName; | |
| 305 | 312 | System.out.println("===> Find Normal"); |
| 306 | 313 | BasicDBObject basicDBObject = new BasicDBObject(); |
| 307 | 314 | basicDBObject.put("key", keyData); |
| 308 | - returnData = getDB(basicDBObject,collectionName); | |
| 315 | + returnData = getDB(basicDBObject,method.toLowerCase()+"_"+collectionName); | |
| 309 | 316 | |
| 310 | 317 | if(returnData.size() == 0) |
| 311 | 318 | { |
| 312 | 319 | System.out.println("===> Go to Main flow"); |
| 313 | 320 | BasicDBObject basicDBObject2 = new BasicDBObject(); |
| 314 | 321 | basicDBObject2.put("key", ""); |
| 315 | - returnData = getDB(basicDBObject2,collectionName); | |
| 322 | + returnData = getDB(basicDBObject2,method.toLowerCase()+"_"+collectionName); | |
| 323 | + | |
| 324 | +// if(method.equals(POST) && returnData.size()>0) | |
| 325 | +// { | |
| 326 | +// //post main flow | |
| 327 | +// JSONObject rowJSON = new JSONObject(returnData.get(0)); | |
| 328 | +// rowJSON.remove("_id"); | |
| 329 | +// JSONObject value = rowJSON.getJSONObject("value"); | |
| 330 | +// value.put("resultData", new JSONObject(bodyData)); | |
| 331 | +// | |
| 332 | +// Document doc = Document.parse(rowJSON.toString()); | |
| 333 | +// insertDB(doc,GET.toLowerCase()+"_"+collectionName); | |
| 334 | +// } | |
| 316 | 335 | } |
| 317 | 336 | |
| 318 | 337 | |
| ... | ... | @@ -400,4 +419,180 @@ public class MockUp |
| 400 | 419 | System.out.println("Update Mongo"); |
| 401 | 420 | } |
| 402 | 421 | |
| 422 | + private static void insertDB(Document doc,String collectionName) | |
| 423 | + { | |
| 424 | +// System.out.println("Start Update Mongo"); | |
| 425 | + | |
| 426 | + MongoCollection<Document> collection = database.getCollection(collectionName); | |
| 427 | + collection.insertOne(doc); | |
| 428 | + | |
| 429 | + System.out.println("Insert Mongo"); | |
| 430 | + } | |
| 431 | + | |
| 432 | + private static JSONObject limitData(JSONObject responsJSON,int limit) | |
| 433 | + { | |
| 434 | + if(limit != 0) | |
| 435 | + { | |
| 436 | + int size = 0; | |
| 437 | + try{ | |
| 438 | + System.out.println("Limit : "+limit); | |
| 439 | + JSONArray JSONArrayData = responsJSON.getJSONArray("resultData"); | |
| 440 | + | |
| 441 | + ArrayList<Object> limitResultData = new ArrayList<Object>(); | |
| 442 | + size = JSONArrayData.length(); | |
| 443 | + for(int i=0;i<size;i++) | |
| 444 | + { | |
| 445 | + limitResultData.add(JSONArrayData.get(i)); | |
| 446 | + if(i == limit-1) | |
| 447 | + i=JSONArrayData.length(); | |
| 448 | + } | |
| 449 | + responsJSON.remove("resultData"); | |
| 450 | + responsJSON.put("resultData",limitResultData); | |
| 451 | + | |
| 452 | + }catch(Exception e) | |
| 453 | + { | |
| 454 | + //resultData not array | |
| 455 | + size = 1; | |
| 456 | + } | |
| 457 | + responsJSON.remove("rowCount"); | |
| 458 | + responsJSON.put("rowCount", size); | |
| 459 | + } | |
| 460 | + | |
| 461 | + return responsJSON; | |
| 462 | + | |
| 463 | + } | |
| 464 | + | |
| 465 | + private static JSONObject FieldsData(JSONObject responsJSON,String[] fileds) | |
| 466 | + { | |
| 467 | + if(fileds.length > 0 && responsJSON.has("resultData")) | |
| 468 | + { | |
| 469 | + String select = ""; | |
| 470 | + for(String row:fileds) | |
| 471 | + select += ","+row; | |
| 472 | + System.out.println("Select Fields => "+select.substring(1)); | |
| 473 | + | |
| 474 | + try{ | |
| 475 | + JSONArray jsonArray = responsJSON.getJSONArray("resultData"); | |
| 476 | + JSONArray newJsonArray = new JSONArray(); | |
| 477 | + for(int i=0;i<jsonArray.length();i++) | |
| 478 | + { | |
| 479 | + try{ | |
| 480 | + JSONObject json = jsonArray.getJSONObject(i); | |
| 481 | + JSONObject newResponse = FieldsObjectData(json,fileds); | |
| 482 | + newJsonArray.put(newResponse); | |
| 483 | + }catch(Exception e2) | |
| 484 | + { | |
| 485 | + newJsonArray.put(jsonArray.get(i)); | |
| 486 | + } | |
| 487 | + } | |
| 488 | + responsJSON.remove("resultData"); | |
| 489 | + responsJSON.put("resultData", newJsonArray); | |
| 490 | + | |
| 491 | + }catch(Exception e) | |
| 492 | + { | |
| 493 | + try{ | |
| 494 | + JSONObject json = responsJSON.getJSONObject("resultData"); | |
| 495 | + JSONObject newResponse = FieldsObjectData(json,fileds); | |
| 496 | + responsJSON.remove("resultData"); | |
| 497 | + responsJSON.put("resultData", newResponse); | |
| 498 | + }catch(Exception e2) | |
| 499 | + { | |
| 500 | + //resultData not JSONObject or JSONArray | |
| 501 | + } | |
| 502 | + | |
| 503 | + } | |
| 504 | + } | |
| 505 | + | |
| 506 | + return responsJSON; | |
| 507 | + | |
| 508 | + } | |
| 509 | + | |
| 510 | + private static JSONObject FieldsObjectData(JSONObject json,String[] fileds) | |
| 511 | + { | |
| 512 | + JSONObject returnData = null; | |
| 513 | + if(fileds.length > 0) | |
| 514 | + { | |
| 515 | + JSONObject newResponse = new JSONObject(); | |
| 516 | + | |
| 517 | + for(String row:fileds) | |
| 518 | + { | |
| 519 | + if(json.has(row)) | |
| 520 | + newResponse.put(row, json.get(row)); | |
| 521 | + | |
| 522 | + } | |
| 523 | + returnData = newResponse; | |
| 524 | + }else | |
| 525 | + { | |
| 526 | + returnData = json; | |
| 527 | + } | |
| 528 | + | |
| 529 | + | |
| 530 | + return returnData; | |
| 531 | + } | |
| 532 | + | |
| 533 | + public static ArrayList<String> getKeyUrl(String url) { | |
| 534 | + | |
| 535 | + ArrayList<String> arrData = new ArrayList<String>(); | |
| 536 | + | |
| 537 | + String[] arr = url.split("[/]"); | |
| 538 | + | |
| 539 | + for(int i = 0; i < arr.length; i++) { | |
| 540 | + | |
| 541 | + if(arr[i].contains("?")) | |
| 542 | + arr[i] = arr[i].substring(0, arr[i].indexOf("?")); | |
| 543 | + | |
| 544 | + if(!arr[i].equals("")) | |
| 545 | + arrData.add(arr[i]); | |
| 546 | + } | |
| 547 | + | |
| 548 | + return arrData; | |
| 549 | + | |
| 550 | + } | |
| 551 | + | |
| 552 | + public static HashMap<String, ArrayList<String>> getKeyFilter(String url) { | |
| 553 | + | |
| 554 | + String key = "filter="; | |
| 555 | + String value = null; | |
| 556 | + HashMap<String,ArrayList<String>> hashMap = new HashMap<String,ArrayList<String>>(); | |
| 557 | + | |
| 558 | + value = url.substring(url.indexOf(key)+key.length()); | |
| 559 | + value = value.substring(0, value.indexOf(")&")+1); | |
| 560 | + String[] array = value.split("[()]"); | |
| 561 | + | |
| 562 | + for(int i = 0; i < array.length; i++) { | |
| 563 | + | |
| 564 | + String[] temp = array[i].split("="); | |
| 565 | + | |
| 566 | + if(temp.length > 1) { | |
| 567 | +// System.out.println(temp[0]+" "+temp[1]); | |
| 568 | + | |
| 569 | + if(hashMap.containsKey(temp[0])) { | |
| 570 | + hashMap.get(temp[0]).add(temp[1]); | |
| 571 | + } else { | |
| 572 | + ArrayList<String> tempArr = new ArrayList<>(); | |
| 573 | + tempArr.add(temp[1]); | |
| 574 | + hashMap.put(temp[0], tempArr); | |
| 575 | + } | |
| 576 | + } | |
| 577 | + } | |
| 578 | + | |
| 579 | + return hashMap; | |
| 580 | + | |
| 581 | + } | |
| 582 | + | |
| 583 | + public static String getKey(String key, String url) { | |
| 584 | + | |
| 585 | + key = key + "="; | |
| 586 | + String value = ""; | |
| 587 | + | |
| 588 | + if(url.contains(key)) { | |
| 589 | + value = url.substring(url.indexOf(key)+key.length()); | |
| 590 | + if(value.indexOf("&") != -1) | |
| 591 | + value = value.substring(0, value.indexOf("&")); | |
| 592 | + } | |
| 593 | + | |
| 594 | + return value; | |
| 595 | + } | |
| 596 | + | |
| 597 | + | |
| 403 | 598 | } | ... | ... |