From 35ea4f23a2ad4f94a0d5be108007963c3cdda14c Mon Sep 17 00:00:00 2001 From: Sumate Kongpui Date: Thu, 30 Aug 2018 18:47:45 +0700 Subject: [PATCH] update get --- src/main/java/sourcecode/MockUp/MockUp.java | 413 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------------------------------------------------------------------------- target/classes/sourcecode/MockUp/MockUp$MyHandler.class | Bin 4785 -> 0 bytes target/classes/sourcecode/MockUp/MockUp.class | Bin 7488 -> 0 bytes 3 files changed, 262 insertions(+), 151 deletions(-) diff --git a/src/main/java/sourcecode/MockUp/MockUp.java b/src/main/java/sourcecode/MockUp/MockUp.java index da9d8a9..1793d31 100644 --- a/src/main/java/sourcecode/MockUp/MockUp.java +++ b/src/main/java/sourcecode/MockUp/MockUp.java @@ -9,8 +9,10 @@ import java.util.ArrayList; import org.apache.commons.io.IOUtils; import org.apache.http.Consts; import org.bson.Document; +import org.json.JSONArray; import org.json.JSONObject; +import com.google.gson.Gson; import com.mongodb.BasicDBObject; import com.mongodb.MongoClient; import com.mongodb.MongoClientOptions; @@ -25,7 +27,7 @@ import com.sun.net.httpserver.HttpServer; public class MockUp { -// private static Gson gson = new Gson(); + private static Gson gson = new Gson(); private static MongoDatabase database; private final static String GET = "GET"; private final static String POST = "POST"; @@ -62,117 +64,186 @@ public class MockUp } static class MyHandler implements HttpHandler { - public void handle(HttpExchange t) throws IOException { - - String method = t.getRequestMethod(); - String url = t.getRequestURI().toString(); - url = URLDecoder.decode(url, "UTF-8"); + public void handle(HttpExchange t) throws IOException{ String response = ""; - String keyBody =""; + try + { + String method = t.getRequestMethod(); + String url = t.getRequestURI().toString(); + url = URLDecoder.decode(url, "UTF-8"); + + // System.out.println(t.getRequestMethod()); + // if(!method.equals(GET)) + // { + // String bodyData = IOUtils.toString(t.getRequestBody(),Consts.UTF_8); + // JSONObject body = new JSONObject(bodyData); + // String[] a = bodyData.split(","); + // // System.out.println(a[0]); + // String[] b = a[0].split(":"); + // // System.out.println(b[0]); + // String c = b[0].replace("{", "").trim().replace("\"", ""); + // // System.out.println(c); + // keyBody = (String) body.get(c); + // } + // System.out.println(body.get(c)); + + // for(Entry> row:t.getRequestHeaders().entrySet()) + // { + // System.out.println(row.getKey()); + // System.out.println(row.getValue()); + // } + // System.out.println(t.getRequestHeaders()); + // System.out.println(t.getRequestURI()); + + + String keyData = findKey(method,url,IOUtils.toString(t.getRequestBody(),Consts.UTF_8)); + + String collectionName = getSuffixTableName(url); + JSONObject responsJSON = new JSONObject(); + + //find retry + + ArrayList retryList = findRetry(method,keyData); + boolean notRetry = true; + + if(retryList.size() > 0) + { + JSONObject rowJSON = new JSONObject(retryList.get(0)); + System.out.println("Count retry : "+rowJSON.get("count")); + + if(rowJSON.has("value")) + responsJSON = (JSONObject) rowJSON.get("value"); + else + { + System.out.println("not found vale or resultCode"); + responsJSON.put("resultCode", "50000"); + responsJSON.put("resultDescription", "Retry"); + } + + BasicDBObject find = new BasicDBObject(); + find.put("key", rowJSON.get("key")); + // System.out.println(rowJSON.get("count")); + int newCount = (int) rowJSON.get("count"); + rowJSON.remove("count"); + if(newCount > 0) + { + rowJSON.put("count", newCount-1); + Document doc = Document.parse(rowJSON.toString()); + setDB(find,doc,method.toLowerCase()+"_retry"); + notRetry = false; + } + } + + + if(notRetry) + { -// System.out.println(t.getRequestMethod()); - if(!method.equals(GET)) - { - String bodyData = IOUtils.toString(t.getRequestBody(),Consts.UTF_8); - JSONObject body = new JSONObject(bodyData); - String[] a = bodyData.split(","); - // System.out.println(a[0]); - String[] b = a[0].split(":"); - // System.out.println(b[0]); - String c = b[0].replace("{", "").trim().replace("\"", ""); - // System.out.println(c); - keyBody = (String) body.get(c); - } -// System.out.println(body.get(c)); - -// for(Entry> row:t.getRequestHeaders().entrySet()) -// { -// System.out.println(row.getKey()); -// System.out.println(row.getValue()); -// } -// System.out.println(t.getRequestHeaders()); -// System.out.println(t.getRequestURI()); - - - String collectionName = getSuffixTableName(url); - - BasicDBObject basicDBObject = new BasicDBObject(); - - switch (method) { - case GET: - basicDBObject.put("url", url); - break; - case POST: - case PUT: - basicDBObject.put("key", keyBody); - break; - case DELETE: - basicDBObject.put("key", url); - break; - - default: - break; - } - - ArrayList responseList = getDBData(basicDBObject,collectionName,method); - - if(responseList.size() == 0) - { - System.out.println("===> Go to Main flow"); - - BasicDBObject basicDBObjectMain = new BasicDBObject(); - if(method.equals(GET)) - basicDBObjectMain.put("url", ""); - else - basicDBObjectMain.put("key", ""); - responseList = getDBData(basicDBObjectMain,collectionName,method); - - - } - - JSONObject responsJSON = new JSONObject(); - ArrayList resultData = new ArrayList(); - for(int i=0;i 1) - { - if(rowJSON.get("resultData")!=null) - resultData.add((JSONObject) rowJSON.get("resultData")); - else if(rowJSON.get("value")!=null) - resultData.add((JSONObject) rowJSON.get("value")); - }else - resultData.add((JSONObject) rowJSON.get("resultData")); - System.out.println("aa" + rowJSON); -// resultData.add((JSONObject) rowJSON.get("resultData")); - } - - - responsJSON.put("resultCode", "20000"); - responsJSON.put("resultDescription", "Success"); - if(method.equals(GET)) - { - responsJSON.put("resultData",resultData); - responsJSON.put("rowCount", resultData.size()); - } - -// System.out.println(responsJSON.toString()); - response = responsJSON.toString(); - + + ArrayList responseList = getDBData(keyData,collectionName,method); + + + + if(responseList.size() == 0) + { + System.out.println("not any Data"); + responsJSON.put("resultCode", "80000"); + responsJSON.put("resultDescription", "MockUp error : No data in "+method.toLowerCase()+"_"+collectionName+"!!!"); + }else + { + if(method.equals(GET) && responseList.size() > 1) + { + ArrayList resultData = new ArrayList(); + for(int i=0;i 0) && (value.get("resultData").toString().substring(0,1).equals("["))) +// { +//// Object test = gson.fromJson(value.get("resultData").toString(), Object.class); +//// System.out.println(test); +// +//// value.get("resultData").toString() +// JSONObject resultlist = new JSONObject("{'test' : "+value.get("resultData").toString()+"}"); +//// System.out.println(resultlist.get("test")); +// JSONArray JSONArrayData = value.getJSONArray("resultData"); +// System.out.println("aaaaaaaaaaaaaaaaaaaa"+JSONArrayData.get(0)); +//// for(int n=0;n getDBData(BasicDBObject basicDBObject,String collectionName,String method) + private static ArrayList getDBData(String keyData,String collectionName,String method) { - collectionName = method.toLowerCase()+"_"+collectionName; - System.out.println("CollectionName : " + collectionName); - System.out.println("Find : "+basicDBObject.toJson()); + ArrayList returnData = new ArrayList(); - String found = ""; + collectionName = method.toLowerCase()+"_"+collectionName; + System.out.println("===> Find Normal"); + BasicDBObject basicDBObject = new BasicDBObject(); + basicDBObject.put("key", keyData); + returnData = getDB(basicDBObject,collectionName); - MongoCollection collection = database.getCollection(collectionName); - FindIterable findData = collection.find(basicDBObject); - MongoCursor cursor = findData.iterator(); + if(returnData.size() == 0) + { + System.out.println("===> Go to Main flow"); + BasicDBObject basicDBObject2 = new BasicDBObject(); + basicDBObject2.put("key", ""); + returnData = getDB(basicDBObject2,collectionName); + } + + + + return returnData; + } + + private static String findKey(String method,String url,String bodyData) + { + String returnData = ""; + switch (method) { case GET: - while(cursor.hasNext()){ - Document rawRow = cursor.next(); - System.out.println("found _id : "+rawRow.get("_id")); - rawRow.remove("_id"); - rawRow.remove("url"); - if(rawRow.get("value") != null) - { - - rawRow.append("resultData", rawRow.get("value")); - rawRow.append("rowCount", "1"); - rawRow.remove("value"); - } - found = rawRow.toJson(); - returnData.add(found); - } + case DELETE: + returnData = url; break; case POST: case PUT: - case DELETE: - while(cursor.hasNext()){ - Document rawRow = cursor.next(); - System.out.println("found _id : "+rawRow.get("_id")); - rawRow.remove("_id"); - rawRow.remove("key"); - if(rawRow.get("value") != null) - { - - JSONObject rowJSON = new JSONObject(rawRow.toJson()); -// System.out.println(rowJSON.get("value")); - found = rowJSON.get("value").toString(); - returnData.add(found); - System.out.println("found : "+found); - }else - { - found = rawRow.toJson(); - returnData.add(found); - } - - } - break; - + JSONObject body = new JSONObject(bodyData); + String[] a = bodyData.split(","); + String[] b = a[0].split(":"); + String c = b[0].replace("{", "").trim().replace("\"", ""); + returnData = (String) body.get(c); + break; + default: break; } - return returnData; } + + private static ArrayList findRetry(String method,String keyData) + { + ArrayList returnData = new ArrayList(); + + System.out.println("===>Find retry"); + BasicDBObject basicDBObject = new BasicDBObject(); + basicDBObject.put("key", keyData); + String collectionName = method.toLowerCase()+"_retry"; + returnData = getDB(basicDBObject,collectionName); + + if(returnData.size() == 0) + { + BasicDBObject basicDBObject2 = new BasicDBObject(); + basicDBObject2.put("key", ""); + returnData = getDB(basicDBObject2,collectionName); + } + + + return returnData; + } + + private static ArrayList getDB(BasicDBObject basicDBObject,String collectionName) + { + ArrayList returnData = new ArrayList(); + + System.out.println("CollectionName : " + collectionName); + System.out.println("Find : "+basicDBObject.toJson()); + + MongoCollection collection = database.getCollection(collectionName); + FindIterable findData = collection.find(basicDBObject); + MongoCursor cursor = findData.iterator(); + + while(cursor.hasNext()){ + Document rawRow = cursor.next(); + System.out.println("found retry _id : "+rawRow.get("_id")); +// System.out.println("count : "+rawRow.get("count")); + returnData.add(rawRow.toJson()); + } + + return returnData; + + } + + private static void setDB(BasicDBObject find,Document doc,String collectionName) + { +// System.out.println("Start Update Mongo"); + + MongoCollection collection = database.getCollection(collectionName); + Document updateDoc = collection.findOneAndReplace(find, doc); + + System.out.println("Update Mongo"); + } + } diff --git a/target/classes/sourcecode/MockUp/MockUp$MyHandler.class b/target/classes/sourcecode/MockUp/MockUp$MyHandler.class index 5a44256..ee87b25 100644 Binary files a/target/classes/sourcecode/MockUp/MockUp$MyHandler.class and b/target/classes/sourcecode/MockUp/MockUp$MyHandler.class differ diff --git a/target/classes/sourcecode/MockUp/MockUp.class b/target/classes/sourcecode/MockUp/MockUp.class index 75b8a8c..1afebb2 100644 Binary files a/target/classes/sourcecode/MockUp/MockUp.class and b/target/classes/sourcecode/MockUp/MockUp.class differ -- libgit2 0.21.2