Commit 886ed8a48e568a68553af98fafc467a2b5542ce3

Authored by Supakit Jirasirichote
1 parent f58f29ec
Exists in master

- config db host

Showing 1 changed file with 35 additions and 28 deletions   Show diff stats
src/main/java/sourcecode/MockUp/MockUp.java
... ... @@ -2,19 +2,15 @@ package sourcecode.MockUp;
2 2  
3 3 import java.io.IOException;
4 4 import java.io.OutputStream;
5   -import java.io.UnsupportedEncodingException;
6 5 import java.net.InetSocketAddress;
  6 +import java.net.URLDecoder;
7 7 import java.util.ArrayList;
8   -import java.util.Iterator;
9   -import java.util.List;
10   -import java.util.Map.Entry;
11 8  
12 9 import org.apache.commons.io.IOUtils;
13 10 import org.apache.http.Consts;
14 11 import org.bson.Document;
15 12 import org.json.JSONObject;
16 13  
17   -import com.google.gson.Gson;
18 14 import com.mongodb.BasicDBObject;
19 15 import com.mongodb.MongoClient;
20 16 import com.mongodb.MongoClientOptions;
... ... @@ -27,43 +23,39 @@ import com.sun.net.httpserver.HttpExchange;
27 23 import com.sun.net.httpserver.HttpHandler;
28 24 import com.sun.net.httpserver.HttpServer;
29 25  
30   -
31   -
32   -
33   -
34 26 public class MockUp
35 27 {
36   - private static Gson gson = new Gson();
  28 +// private static Gson gson = new Gson();
37 29 private static MongoDatabase database;
38 30 private final static String GET = "GET";
39 31 private final static String POST = "POST";
40 32 private final static String PUT = "PUT";
41 33 private final static String DELETE = "DELETE";
42   -
43   -
  34 + private static String DBNAME = "spw";
  35 + private static String DBHOST = "10.1.2.155:27017";
44 36  
45 37 public static void main(String[] args) throws Exception {
46 38  
47   - int port = 8000;
48   -
49   -
50   - try{
51   -
52   -
  39 + int port = 6300;
  40 + try {
53 41 if(args.length>0)
54 42 port = Integer.parseInt(args[0]);
55 43  
  44 + if(args.length>1)
  45 + DBNAME = args[1];
  46 +
  47 + if(args.length>2)
  48 + DBHOST = args[2];
  49 +
56 50 //connect mongo
57   - MongoConnector("spw");
  51 + MongoConnector(DBNAME);
58 52  
59 53 HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
60 54 server.createContext("/", new MyHandler());
61 55 server.setExecutor(null); // creates a default executor
62 56 server.start();
63 57 System.out.println("Run in port : "+port);
64   -
65   - }catch(Exception e)
66   - {
  58 + } catch(Exception e) {
67 59 e.printStackTrace();
68 60 System.out.println("Fail to run in port : "+port);
69 61 }
... ... @@ -73,7 +65,8 @@ public class MockUp
73 65 public void handle(HttpExchange t) throws IOException {
74 66  
75 67 String method = t.getRequestMethod();
76   - String url = t.getRequestURI().toString();
  68 + String url = t.getRequestURI().toString();
  69 + url = URLDecoder.decode(url, "UTF-8");
77 70 String response = "";
78 71 String keyBody ="";
79 72  
... ... @@ -101,8 +94,7 @@ public class MockUp
101 94 // System.out.println(t.getRequestURI());
102 95  
103 96  
104   - String collectionName = "spwCustomerAccounts";
105   -
  97 + String collectionName = getSuffixTableName(url);
106 98  
107 99 BasicDBObject basicDBObject = new BasicDBObject();
108 100  
... ... @@ -152,8 +144,8 @@ public class MockUp
152 144 else if(rowJSON.get("value")!=null)
153 145 resultData.add((JSONObject) rowJSON.get("value"));
154 146 }else
155   - resultData.add(rowJSON);
156   -// System.out.println(rowJSON);
  147 + resultData.add((JSONObject) rowJSON.get("resultData"));
  148 + System.out.println("aa" + rowJSON);
157 149 // resultData.add((JSONObject) rowJSON.get("resultData"));
158 150 }
159 151  
... ... @@ -183,13 +175,27 @@ public class MockUp
183 175 System.out.println("");
184 176 }
185 177 }
  178 +
  179 + private static String getSuffixTableName(String url)
  180 + {
  181 + String value = null;
  182 + String[] arrData = url.split("/");
  183 + for (String data : arrData) {
  184 + if (data.contains(".json")) {
  185 + int lastIndex = data.indexOf(".json");
  186 + value = data.substring(0, lastIndex);
  187 + break;
  188 + }
  189 + }
  190 + return value;
  191 + }
186 192  
187 193  
188 194 private static void MongoConnector(String db) {
189 195  
190 196 String username = "";
191 197 String password = "";
192   - String address = "10.1.2.155:27017";
  198 + String address = DBHOST;
193 199 String dbname = db;
194 200 String authSource = db;
195 201 int timeoutMongoDB = 10000;
... ... @@ -206,6 +212,7 @@ public class MockUp
206 212 if(username.equals(""))
207 213 uri = new MongoClientURI("mongodb://"+address+"/?authSource="+authSource, optionsBuilder);
208 214 System.out.println("MongoDB Connecting to "+uri.toString()+"...");
  215 + @SuppressWarnings("resource")
209 216 MongoClient mongoClient = new MongoClient(uri);
210 217 database = mongoClient.getDatabase(dbname);
211 218 //test connect and list collections
... ...