Commit 72eef90c15f8bada72e7a97852028eb6417e4eb7

Authored by Sumate Kongpui
1 parent 5d7692c4
Exists in master

update mockup

Showing 1 changed file with 113 additions and 17 deletions   Show diff stats
src/main/java/sourcecode/MockUp/MockUp.java
... ... @@ -4,8 +4,12 @@ import java.io.IOException;
4 4 import java.io.OutputStream;
5 5 import java.net.InetSocketAddress;
6 6 import java.net.URLDecoder;
  7 +import java.nio.charset.Charset;
  8 +import java.nio.charset.StandardCharsets;
  9 +import java.sql.Date;
7 10 import java.util.ArrayList;
8 11 import java.util.HashMap;
  12 +import java.util.Iterator;
9 13  
10 14 import org.apache.commons.io.IOUtils;
11 15 import org.apache.http.Consts;
... ... @@ -14,6 +18,7 @@ import org.json.JSONArray;
14 18 import org.json.JSONObject;
15 19  
16 20 import com.google.gson.Gson;
  21 +import com.google.gson.JsonObject;
17 22 import com.mongodb.BasicDBObject;
18 23 import com.mongodb.MongoClient;
19 24 import com.mongodb.MongoClientOptions;
... ... @@ -34,12 +39,15 @@ public class MockUp
34 39 private final static String POST = "POST";
35 40 private final static String PUT = "PUT";
36 41 private final static String DELETE = "DELETE";
37   - private static String DBNAME = "spw";
38   - private static String DBHOST = "10.1.2.155:27017";
  42 + private static int port = 6300;
  43 + private static String DBNAME = "coop_mockup";
  44 + private static String DBHOST = "10.1.2.144:27017";
  45 + private static String DBUSERNAME = "";
  46 + private static String DBPASSWORD = "";
  47 +
39 48  
40 49 public static void main(String[] args) throws Exception {
41 50  
42   - int port = 6300;
43 51 try {
44 52 if(args.length>0)
45 53 port = Integer.parseInt(args[0]);
... ... @@ -50,6 +58,12 @@ public class MockUp
50 58 if(args.length>2)
51 59 DBHOST = args[2];
52 60  
  61 + if(args.length>3)
  62 + DBUSERNAME = args[3];
  63 +
  64 + if(args.length>4)
  65 + DBPASSWORD = args[4];
  66 +
53 67 //connect mongo
54 68 MongoConnector(DBNAME);
55 69  
... ... @@ -65,6 +79,9 @@ public class MockUp
65 79 }
66 80  
67 81 static class MyHandler implements HttpHandler {
  82 + JSONObject resHeader = null;
  83 + int resEcode = 200;
  84 +
68 85 public void handle(HttpExchange t) throws IOException{
69 86 String response = "";
70 87 try
... ... @@ -217,6 +234,16 @@ public class MockUp
217 234 responsJSON.put("resultCode", "20000");
218 235 responsJSON.put("resultDescription", "Success");
219 236 }
  237 +
  238 + if(rowJSON.has("header"))
  239 + resHeader = rowJSON.getJSONObject("header");
  240 + else
  241 + resHeader = null;
  242 +
  243 + if(rowJSON.has("ecode"))
  244 + resEcode = rowJSON.getInt("ecode");
  245 + else
  246 + resEcode = 200;
220 247 }
221 248 }
222 249  
... ... @@ -246,12 +273,44 @@ public class MockUp
246 273 System.out.println("System error "+e.getMessage());
247 274  
248 275 }
  276 + if(resHeader != null)
  277 + {
  278 + Iterator<String> keys = resHeader.keys();
  279 + while(keys.hasNext()) {
  280 + String key = keys.next();
  281 + t.getResponseHeaders().set(key, resHeader.getString(key));
  282 + }
  283 +
  284 + }
  285 +
  286 + String ret = response.toString();
  287 + t.getResponseHeaders().set("Content-Type", "application/json");
  288 +
  289 + t.getResponseHeaders().set("Access-Control-Allow-Origin", "https://localhost:"+port);
  290 + t.getResponseHeaders().set("Access-Control-Allow-Credentials", "true");
  291 + t.getResponseHeaders().set("Access-Control-Allow-Headers", "content-Type");
  292 + t.getResponseHeaders().set("Access-Control-Allow-Methods", "POST, PUT, GET, DELETE, OPTIONS");
  293 +
  294 +
  295 +
  296 +
  297 + //ret= URLDecoder.decode(ret, "UTF-8");
  298 + byte[] bytes = ret.getBytes(StandardCharsets.UTF_8);
  299 + t.sendResponseHeaders(resEcode, bytes.length);
  300 +// System.out.println(ret);
  301 + OutputStream os = t.getResponseBody();
  302 + os.write(bytes);
  303 + os.close();
  304 +
249 305  
250   - t.getResponseHeaders().set("Content-Type", "application/json");
251   - t.sendResponseHeaders(200, response.length());
252   - OutputStream os = t.getResponseBody();
253   - os.write(response.getBytes());
254   - os.close();
  306 +
  307 +
  308 +// t.getResponseHeaders().set("Content-Type", "application/json");
  309 +// t.sendResponseHeaders(200, response.length());
  310 +// OutputStream os = t.getResponseBody();
  311 +// os.write(response.getBytes(Charset.forName("US-ASCII")));
  312 +//// os.write(response.getBytes());
  313 +// os.close();
255 314 }
256 315 }
257 316  
... ... @@ -266,16 +325,24 @@ public class MockUp
266 325 break;
267 326 }
268 327 }
  328 + if(value == null)
  329 + value = arrData[arrData.length-1];
  330 + if(value.contains("?"))
  331 + {
  332 + arrData = value.split("\\?");
  333 + value = arrData[0];
  334 + }
  335 +
269 336 return value;
270 337 }
271 338  
272 339 private static void MongoConnector(String db) {
273 340  
274   - String username = "";
275   - String password = "";
  341 + String username = DBUSERNAME;
  342 + String password = DBPASSWORD.replace("@", "%40");
276 343 String address = DBHOST;
277 344 String dbname = db;
278   - String authSource = db;
  345 + String authSource = "admin";
279 346 int timeoutMongoDB = 10000;
280 347  
281 348 MongoClientOptions.Builder optionsBuilder = MongoClientOptions.builder();
... ... @@ -285,7 +352,7 @@ public class MockUp
285 352  
286 353 // optionsBuilder.connectionsPerHost(maxPoolSize);
287 354 // optionsBuilder.minConnectionsPerHost(minPoolSize);
288   -
  355 +// mongodb://root:password123@198.174.21.23:27017,198.342.121.23:27017,142.32.32.21:3001/databasename?replicaSet=rs01&ssl=false&connectTimeoutMS=100000
289 356 MongoClientURI uri = new MongoClientURI("mongodb://"+username+":"+password+"@"+address+"/?authSource="+authSource, optionsBuilder);
290 357 if(username.equals(""))
291 358 uri = new MongoClientURI("mongodb://"+address+"/?authSource="+authSource, optionsBuilder);
... ... @@ -311,6 +378,7 @@ public class MockUp
311 378 // collectionName = method.toLowerCase()+"_"+collectionName;
312 379 System.out.println("===> Find Normal");
313 380 BasicDBObject basicDBObject = new BasicDBObject();
  381 +// System.out.println(keyData);
314 382 basicDBObject.put("key", keyData);
315 383 returnData = getDB(basicDBObject,method.toLowerCase()+"_"+collectionName);
316 384  
... ... @@ -352,11 +420,21 @@ public class MockUp
352 420 break;
353 421 case POST:
354 422 case PUT:
355   - JSONObject body = new JSONObject(bodyData);
356   - String[] a = bodyData.split(",");
357   - String[] b = a[0].split(":");
358   - String c = b[0].replace("{", "").trim().replace("\"", "");
359   - returnData = (String) body.get(c);
  423 + try
  424 + {
  425 + JSONObject body = new JSONObject(bodyData);
  426 + String[] a = bodyData.split(",");
  427 + String[] b = a[0].split(":");
  428 + String c = b[0].replace("{", "").trim().replace("\"", "");
  429 +
  430 + returnData = findKeyObject(body.get(c));
  431 + // returnData = (String) body.get(c);
  432 + }catch(Exception e)
  433 + {
  434 + returnData = "";
  435 + }
  436 +
  437 +
360 438 break;
361 439  
362 440 default:
... ... @@ -366,6 +444,24 @@ public class MockUp
366 444 return returnData;
367 445 }
368 446  
  447 + private static String findKeyObject(Object obj)
  448 + {
  449 + String returnData = "";
  450 + if(obj instanceof JSONObject)
  451 + {
  452 + returnData = obj.toString();
  453 +// JSONObject json = (JSONObject) obj;
  454 +// String bodyData = obj.toString();
  455 +// String[] a = bodyData.split(",");
  456 +// String[] b = a[0].split(":");
  457 +// String c = b[0].replace("{", "").trim().replace("\"", "");
  458 +// returnData = findKeyObject(json.get(c));
  459 + }else
  460 + returnData = (String) obj;
  461 +
  462 + return returnData;
  463 + }
  464 +
369 465 private static ArrayList<String> findRetry(String method,String keyData)
370 466 {
371 467 ArrayList<String> returnData = new ArrayList<String>();
... ...