Commit 35ea4f23a2ad4f94a0d5be108007963c3cdda14c

Authored by sumatek
1 parent 2b37485b
Exists in master

update get

src/main/java/sourcecode/MockUp/MockUp.java
... ... @@ -9,8 +9,10 @@ import java.util.ArrayList;
9 9 import org.apache.commons.io.IOUtils;
10 10 import org.apache.http.Consts;
11 11 import org.bson.Document;
  12 +import org.json.JSONArray;
12 13 import org.json.JSONObject;
13 14  
  15 +import com.google.gson.Gson;
14 16 import com.mongodb.BasicDBObject;
15 17 import com.mongodb.MongoClient;
16 18 import com.mongodb.MongoClientOptions;
... ... @@ -25,7 +27,7 @@ import com.sun.net.httpserver.HttpServer;
25 27  
26 28 public class MockUp
27 29 {
28   -// private static Gson gson = new Gson();
  30 + private static Gson gson = new Gson();
29 31 private static MongoDatabase database;
30 32 private final static String GET = "GET";
31 33 private final static String POST = "POST";
... ... @@ -62,117 +64,186 @@ public class MockUp
62 64 }
63 65  
64 66 static class MyHandler implements HttpHandler {
65   - public void handle(HttpExchange t) throws IOException {
66   -
67   - String method = t.getRequestMethod();
68   - String url = t.getRequestURI().toString();
69   - url = URLDecoder.decode(url, "UTF-8");
  67 + public void handle(HttpExchange t) throws IOException{
70 68 String response = "";
71   - String keyBody ="";
  69 + try
  70 + {
  71 + String method = t.getRequestMethod();
  72 + String url = t.getRequestURI().toString();
  73 + url = URLDecoder.decode(url, "UTF-8");
  74 +
  75 + // System.out.println(t.getRequestMethod());
  76 + // if(!method.equals(GET))
  77 + // {
  78 + // String bodyData = IOUtils.toString(t.getRequestBody(),Consts.UTF_8);
  79 + // JSONObject body = new JSONObject(bodyData);
  80 + // String[] a = bodyData.split(",");
  81 + // // System.out.println(a[0]);
  82 + // String[] b = a[0].split(":");
  83 + // // System.out.println(b[0]);
  84 + // String c = b[0].replace("{", "").trim().replace("\"", "");
  85 + // // System.out.println(c);
  86 + // keyBody = (String) body.get(c);
  87 + // }
  88 + // System.out.println(body.get(c));
  89 +
  90 + // for(Entry<String, List<String>> row:t.getRequestHeaders().entrySet())
  91 + // {
  92 + // System.out.println(row.getKey());
  93 + // System.out.println(row.getValue());
  94 + // }
  95 + // System.out.println(t.getRequestHeaders());
  96 + // System.out.println(t.getRequestURI());
  97 +
  98 +
  99 + String keyData = findKey(method,url,IOUtils.toString(t.getRequestBody(),Consts.UTF_8));
  100 +
  101 + String collectionName = getSuffixTableName(url);
  102 + JSONObject responsJSON = new JSONObject();
  103 +
  104 + //find retry
  105 +
  106 + ArrayList<String> retryList = findRetry(method,keyData);
  107 + boolean notRetry = true;
  108 +
  109 + if(retryList.size() > 0)
  110 + {
  111 + JSONObject rowJSON = new JSONObject(retryList.get(0));
  112 + System.out.println("Count retry : "+rowJSON.get("count"));
  113 +
  114 + if(rowJSON.has("value"))
  115 + responsJSON = (JSONObject) rowJSON.get("value");
  116 + else
  117 + {
  118 + System.out.println("not found vale or resultCode");
  119 + responsJSON.put("resultCode", "50000");
  120 + responsJSON.put("resultDescription", "Retry");
  121 + }
  122 +
  123 + BasicDBObject find = new BasicDBObject();
  124 + find.put("key", rowJSON.get("key"));
  125 + // System.out.println(rowJSON.get("count"));
  126 + int newCount = (int) rowJSON.get("count");
  127 + rowJSON.remove("count");
  128 + if(newCount > 0)
  129 + {
  130 + rowJSON.put("count", newCount-1);
  131 + Document doc = Document.parse(rowJSON.toString());
  132 + setDB(find,doc,method.toLowerCase()+"_retry");
  133 + notRetry = false;
  134 + }
  135 + }
  136 +
  137 +
  138 + if(notRetry)
  139 + {
72 140  
73   -// System.out.println(t.getRequestMethod());
74   - if(!method.equals(GET))
75   - {
76   - String bodyData = IOUtils.toString(t.getRequestBody(),Consts.UTF_8);
77   - JSONObject body = new JSONObject(bodyData);
78   - String[] a = bodyData.split(",");
79   - // System.out.println(a[0]);
80   - String[] b = a[0].split(":");
81   - // System.out.println(b[0]);
82   - String c = b[0].replace("{", "").trim().replace("\"", "");
83   - // System.out.println(c);
84   - keyBody = (String) body.get(c);
85   - }
86   -// System.out.println(body.get(c));
87   -
88   -// for(Entry<String, List<String>> row:t.getRequestHeaders().entrySet())
89   -// {
90   -// System.out.println(row.getKey());
91   -// System.out.println(row.getValue());
92   -// }
93   -// System.out.println(t.getRequestHeaders());
94   -// System.out.println(t.getRequestURI());
95   -
96   -
97   - String collectionName = getSuffixTableName(url);
98   -
99   - BasicDBObject basicDBObject = new BasicDBObject();
100   -
101   - switch (method) {
102   - case GET:
103   - basicDBObject.put("url", url);
104   - break;
105   - case POST:
106   - case PUT:
107   - basicDBObject.put("key", keyBody);
108   - break;
109   - case DELETE:
110   - basicDBObject.put("key", url);
111   - break;
112   -
113   - default:
114   - break;
115   - }
116   -
117   - ArrayList<String> responseList = getDBData(basicDBObject,collectionName,method);
118   -
119   - if(responseList.size() == 0)
120   - {
121   - System.out.println("===> Go to Main flow");
122   -
123   - BasicDBObject basicDBObjectMain = new BasicDBObject();
124   - if(method.equals(GET))
125   - basicDBObjectMain.put("url", "");
126   - else
127   - basicDBObjectMain.put("key", "");
128   - responseList = getDBData(basicDBObjectMain,collectionName,method);
129   -
130   -
131   - }
132   -
133   - JSONObject responsJSON = new JSONObject();
134   - ArrayList<JSONObject> resultData = new ArrayList<JSONObject>();
135   - for(int i=0;i<responseList.size();i++)
136   - {
137   -
138   - JSONObject rowJSON = new JSONObject(responseList.get(i));
139   -
140   - if(responseList.size() > 1)
141   - {
142   - if(rowJSON.get("resultData")!=null)
143   - resultData.add((JSONObject) rowJSON.get("resultData"));
144   - else if(rowJSON.get("value")!=null)
145   - resultData.add((JSONObject) rowJSON.get("value"));
146   - }else
147   - resultData.add((JSONObject) rowJSON.get("resultData"));
148   - System.out.println("aa" + rowJSON);
149   -// resultData.add((JSONObject) rowJSON.get("resultData"));
150   - }
151   -
152   -
153   - responsJSON.put("resultCode", "20000");
154   - responsJSON.put("resultDescription", "Success");
155   - if(method.equals(GET))
156   - {
157   - responsJSON.put("resultData",resultData);
158   - responsJSON.put("rowCount", resultData.size());
159   - }
160   -
161   -// System.out.println(responsJSON.toString());
162   - response = responsJSON.toString();
163   -
  141 +
  142 + ArrayList<String> responseList = getDBData(keyData,collectionName,method);
  143 +
  144 +
  145 +
  146 + if(responseList.size() == 0)
  147 + {
  148 + System.out.println("not any Data");
  149 + responsJSON.put("resultCode", "80000");
  150 + responsJSON.put("resultDescription", "MockUp error : No data in "+method.toLowerCase()+"_"+collectionName+"!!!");
  151 + }else
  152 + {
  153 + if(method.equals(GET) && responseList.size() > 1)
  154 + {
  155 + ArrayList<Object> resultData = new ArrayList<Object>();
  156 + for(int i=0;i<responseList.size();i++)
  157 + {
  158 + JSONObject rowJSON = new JSONObject(responseList.get(i));
  159 +
  160 + if(rowJSON.has("value"))
  161 + {
  162 + JSONObject value = (JSONObject) rowJSON.get("value");
  163 + if(value.has("resultData"))
  164 + {
  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 + try{
  185 + JSONArray JSONArrayData = value.getJSONArray("resultData");
  186 +// System.out.println("bbbbbbbbbb"+JSONArrayData.length());
  187 + for(int n=0;n<JSONArrayData.length();n++)
  188 + {
  189 + resultData.add(JSONArrayData.get(n));
  190 +// System.out.println(JSONArrayData.get(n));
  191 + }
  192 + }catch(Exception e)
  193 + {
  194 +// e.printStackTrace();
  195 + resultData.add(value.get("resultData"));
  196 + }
  197 +
  198 + }
  199 + }
  200 +
  201 + }
  202 +
  203 + responsJSON.put("resultCode", "20000");
  204 + responsJSON.put("resultDescription", "Success");
  205 + responsJSON.put("resultData",resultData);
  206 + responsJSON.put("rowCount",resultData.size());
  207 +
  208 + }else
  209 + {
  210 + JSONObject rowJSON = new JSONObject(responseList.get(0));
  211 + if(rowJSON.has("value"))
  212 + responsJSON = (JSONObject) rowJSON.get("value");
  213 + else
  214 + {
  215 + System.out.println("not found vale or resultCode");
  216 + responsJSON.put("resultCode", "20000");
  217 + responsJSON.put("resultDescription", "Success");
  218 + }
  219 + }
  220 + }
  221 +
  222 + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  223 +
  224 +
  225 + }
  226 +
  227 +
  228 + response = responsJSON.toString();
  229 +
  230 + System.out.println("");
  231 + System.out.println("URL : " + url);
  232 + System.out.println("Method : " + method);
  233 + System.out.println("Response : " + response);
  234 + System.out.println("");
  235 +
  236 + }catch(Exception e) {
  237 + e.printStackTrace();
  238 + System.out.println("System error "+e.getMessage());
  239 +
  240 + }
164 241  
165 242 t.getResponseHeaders().set("Content-Type", "application/json");
166 243 t.sendResponseHeaders(200, response.length());
167 244 OutputStream os = t.getResponseBody();
168 245 os.write(response.getBytes());
169 246 os.close();
170   -
171   - System.out.println("");
172   - System.out.println("URL : " + url);
173   - System.out.println("Method : " + method);
174   - System.out.println("Response : " + response);
175   - System.out.println("");
176 247 }
177 248 }
178 249  
... ... @@ -226,67 +297,107 @@ public class MockUp
226 297 }
227 298 }
228 299  
229   - private static ArrayList<String> getDBData(BasicDBObject basicDBObject,String collectionName,String method)
  300 + private static ArrayList<String> getDBData(String keyData,String collectionName,String method)
230 301 {
231   - collectionName = method.toLowerCase()+"_"+collectionName;
232   - System.out.println("CollectionName : " + collectionName);
233   - System.out.println("Find : "+basicDBObject.toJson());
  302 +
234 303 ArrayList<String> returnData = new ArrayList<String>();
235   - String found = "";
  304 + collectionName = method.toLowerCase()+"_"+collectionName;
  305 + System.out.println("===> Find Normal");
  306 + BasicDBObject basicDBObject = new BasicDBObject();
  307 + basicDBObject.put("key", keyData);
  308 + returnData = getDB(basicDBObject,collectionName);
236 309  
237   - MongoCollection<Document> collection = database.getCollection(collectionName);
238   - FindIterable<Document> findData = collection.find(basicDBObject);
239   - MongoCursor<Document> cursor = findData.iterator();
  310 + if(returnData.size() == 0)
  311 + {
  312 + System.out.println("===> Go to Main flow");
  313 + BasicDBObject basicDBObject2 = new BasicDBObject();
  314 + basicDBObject2.put("key", "");
  315 + returnData = getDB(basicDBObject2,collectionName);
  316 + }
  317 +
  318 +
  319 +
240 320  
241 321  
  322 + return returnData;
  323 + }
  324 +
  325 + private static String findKey(String method,String url,String bodyData)
  326 + {
  327 + String returnData = "";
  328 +
242 329 switch (method) {
243 330 case GET:
244   - while(cursor.hasNext()){
245   - Document rawRow = cursor.next();
246   - System.out.println("found _id : "+rawRow.get("_id"));
247   - rawRow.remove("_id");
248   - rawRow.remove("url");
249   - if(rawRow.get("value") != null)
250   - {
251   -
252   - rawRow.append("resultData", rawRow.get("value"));
253   - rawRow.append("rowCount", "1");
254   - rawRow.remove("value");
255   - }
256   - found = rawRow.toJson();
257   - returnData.add(found);
258   - }
  331 + case DELETE:
  332 + returnData = url;
259 333 break;
260 334 case POST:
261 335 case PUT:
262   - case DELETE:
263   - while(cursor.hasNext()){
264   - Document rawRow = cursor.next();
265   - System.out.println("found _id : "+rawRow.get("_id"));
266   - rawRow.remove("_id");
267   - rawRow.remove("key");
268   - if(rawRow.get("value") != null)
269   - {
270   -
271   - JSONObject rowJSON = new JSONObject(rawRow.toJson());
272   -// System.out.println(rowJSON.get("value"));
273   - found = rowJSON.get("value").toString();
274   - returnData.add(found);
275   - System.out.println("found : "+found);
276   - }else
277   - {
278   - found = rawRow.toJson();
279   - returnData.add(found);
280   - }
281   -
282   - }
283   - break;
284   -
  336 + JSONObject body = new JSONObject(bodyData);
  337 + String[] a = bodyData.split(",");
  338 + String[] b = a[0].split(":");
  339 + String c = b[0].replace("{", "").trim().replace("\"", "");
  340 + returnData = (String) body.get(c);
  341 + break;
  342 +
285 343 default:
286 344 break;
287 345 }
288 346  
289   -
290 347 return returnData;
291 348 }
  349 +
  350 + private static ArrayList<String> findRetry(String method,String keyData)
  351 + {
  352 + ArrayList<String> returnData = new ArrayList<String>();
  353 +
  354 + System.out.println("===>Find retry");
  355 + BasicDBObject basicDBObject = new BasicDBObject();
  356 + basicDBObject.put("key", keyData);
  357 + String collectionName = method.toLowerCase()+"_retry";
  358 + returnData = getDB(basicDBObject,collectionName);
  359 +
  360 + if(returnData.size() == 0)
  361 + {
  362 + BasicDBObject basicDBObject2 = new BasicDBObject();
  363 + basicDBObject2.put("key", "");
  364 + returnData = getDB(basicDBObject2,collectionName);
  365 + }
  366 +
  367 +
  368 + return returnData;
  369 + }
  370 +
  371 + private static ArrayList<String> getDB(BasicDBObject basicDBObject,String collectionName)
  372 + {
  373 + ArrayList<String> returnData = new ArrayList<String>();
  374 +
  375 + System.out.println("CollectionName : " + collectionName);
  376 + System.out.println("Find : "+basicDBObject.toJson());
  377 +
  378 + MongoCollection<Document> collection = database.getCollection(collectionName);
  379 + FindIterable<Document> findData = collection.find(basicDBObject);
  380 + MongoCursor<Document> cursor = findData.iterator();
  381 +
  382 + while(cursor.hasNext()){
  383 + Document rawRow = cursor.next();
  384 + System.out.println("found retry _id : "+rawRow.get("_id"));
  385 +// System.out.println("count : "+rawRow.get("count"));
  386 + returnData.add(rawRow.toJson());
  387 + }
  388 +
  389 + return returnData;
  390 +
  391 + }
  392 +
  393 + private static void setDB(BasicDBObject find,Document doc,String collectionName)
  394 + {
  395 +// System.out.println("Start Update Mongo");
  396 +
  397 + MongoCollection<Document> collection = database.getCollection(collectionName);
  398 + Document updateDoc = collection.findOneAndReplace(find, doc);
  399 +
  400 + System.out.println("Update Mongo");
  401 + }
  402 +
292 403 }
... ...
target/classes/sourcecode/MockUp/MockUp$MyHandler.class
No preview for this file type
target/classes/sourcecode/MockUp/MockUp.class
No preview for this file type