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