f58f29ec
sumatek
add new project
|
1
2
3
4
|
package sourcecode.MockUp;
import java.io.IOException;
import java.io.OutputStream;
|
f58f29ec
sumatek
add new project
|
5
|
import java.io.UnsupportedEncodingException;
|
886ed8a4
Supakit Jirasirichote
- config db host
|
6
|
import java.net.InetSocketAddress;
|
72eef90c
Sumate Kongpui
update mockup
|
7
8
9
|
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
|
f58f29ec
sumatek
add new project
|
10
|
import java.util.Map.Entry;
|
666338c1
Sumate Kongpui
update offset and...
|
11
|
|
5d7692c4
sumatek
update limit and ...
|
12
|
import org.apache.commons.io.IOUtils;
|
72eef90c
Sumate Kongpui
update mockup
|
13
|
import org.apache.http.Consts;
|
f58f29ec
sumatek
add new project
|
14
15
16
17
|
import org.bson.Document;
import org.json.JSONObject;
import com.google.gson.Gson;
|
35ea4f23
sumatek
update get
|
18
|
import com.mongodb.BasicDBObject;
|
f58f29ec
sumatek
add new project
|
19
20
|
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
|
35ea4f23
sumatek
update get
|
21
|
import com.mongodb.MongoClientURI;
|
72eef90c
Sumate Kongpui
update mockup
|
22
|
import com.mongodb.client.FindIterable;
|
f58f29ec
sumatek
add new project
|
23
24
25
26
27
28
29
30
31
32
33
34
|
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
public class MockUp
|
f58f29ec
sumatek
add new project
|
35
36
|
{
private static Gson gson = new Gson();
|
35ea4f23
sumatek
update get
|
37
|
private static MongoDatabase database;
|
f58f29ec
sumatek
add new project
|
38
39
40
41
42
|
private final static String GET = "GET";
private final static String POST = "POST";
private final static String PUT = "PUT";
private final static String DELETE = "DELETE";
|
72eef90c
Sumate Kongpui
update mockup
|
43
|
|
666338c1
Sumate Kongpui
update offset and...
|
44
45
|
public static void main(String[] args) throws Exception {
|
72eef90c
Sumate Kongpui
update mockup
|
46
47
48
|
int port = 8000;
|
f58f29ec
sumatek
add new project
|
49
50
51
|
try{
|
886ed8a4
Supakit Jirasirichote
- config db host
|
52
|
|
f58f29ec
sumatek
add new project
|
53
54
55
|
if(args.length>0)
port = Integer.parseInt(args[0]);
|
886ed8a4
Supakit Jirasirichote
- config db host
|
56
57
58
59
60
61
|
//connect mongo
MongoConnector("spw");
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
server.createContext("/", new MyHandler());
server.setExecutor(null); // creates a default executor
|
72eef90c
Sumate Kongpui
update mockup
|
62
63
64
65
66
67
|
server.start();
System.out.println("Run in port : "+port);
}catch(Exception e)
{
e.printStackTrace();
|
f58f29ec
sumatek
add new project
|
68
|
System.out.println("Fail to run in port : "+port);
|
886ed8a4
Supakit Jirasirichote
- config db host
|
69
|
}
|
f58f29ec
sumatek
add new project
|
70
71
72
73
74
75
|
}
static class MyHandler implements HttpHandler {
public void handle(HttpExchange t) throws IOException {
String method = t.getRequestMethod();
|
886ed8a4
Supakit Jirasirichote
- config db host
|
76
|
String url = t.getRequestURI().toString();
|
f58f29ec
sumatek
add new project
|
77
78
79
80
81
82
|
String response = "";
String keyBody ="";
// System.out.println(t.getRequestMethod());
if(!method.equals(GET))
{
|
72eef90c
Sumate Kongpui
update mockup
|
83
84
85
|
String bodyData = IOUtils.toString(t.getRequestBody(),Consts.UTF_8);
JSONObject body = new JSONObject(bodyData);
String[] a = bodyData.split(",");
|
35ea4f23
sumatek
update get
|
86
|
// System.out.println(a[0]);
|
f58f29ec
sumatek
add new project
|
87
|
String[] b = a[0].split(":");
|
35ea4f23
sumatek
update get
|
88
89
90
91
92
|
// System.out.println(b[0]);
String c = b[0].replace("{", "").trim().replace("\"", "");
// System.out.println(c);
keyBody = (String) body.get(c);
}
|
5d7692c4
sumatek
update limit and ...
|
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
// System.out.println(body.get(c));
// for(Entry<String, List<String>> 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 = "spwCustomerAccounts";
|
666338c1
Sumate Kongpui
update offset and...
|
107
108
109
110
111
112
113
114
115
116
117
118
|
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);
|
5d7692c4
sumatek
update limit and ...
|
119
120
121
122
123
124
|
break;
default:
break;
}
|
35ea4f23
sumatek
update get
|
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
ArrayList<String> 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<JSONObject> resultData = new ArrayList<JSONObject>();
for(int i=0;i<responseList.size();i++)
{
JSONObject rowJSON = new JSONObject(responseList.get(i));
if(responseList.size() > 1)
{
|
5d7692c4
sumatek
update limit and ...
|
150
|
if(rowJSON.get("resultData")!=null)
|
35ea4f23
sumatek
update get
|
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
resultData.add((JSONObject) rowJSON.get("resultData"));
else if(rowJSON.get("value")!=null)
resultData.add((JSONObject) rowJSON.get("value"));
}else
resultData.add(rowJSON);
// System.out.println(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();
t.getResponseHeaders().set("Content-Type", "application/json");
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
System.out.println("");
System.out.println("URL : " + url);
System.out.println("Method : " + method);
System.out.println("Response : " + response);
System.out.println("");
}
}
private static void MongoConnector(String db) {
String username = "";
|
f58f29ec
sumatek
add new project
|
191
|
String password = "";
|
35ea4f23
sumatek
update get
|
192
|
String address = "10.1.2.155:27017";
|
5d7692c4
sumatek
update limit and ...
|
193
|
String dbname = db;
|
35ea4f23
sumatek
update get
|
194
195
196
197
198
199
200
201
202
203
204
205
|
String authSource = db;
int timeoutMongoDB = 10000;
MongoClientOptions.Builder optionsBuilder = MongoClientOptions.builder();
optionsBuilder.connectTimeout(timeoutMongoDB);
optionsBuilder.socketTimeout(timeoutMongoDB);
optionsBuilder.serverSelectionTimeout(timeoutMongoDB);
// optionsBuilder.connectionsPerHost(maxPoolSize);
// optionsBuilder.minConnectionsPerHost(minPoolSize);
MongoClientURI uri = new MongoClientURI("mongodb://"+username+":"+password+"@"+address+"/?authSource="+authSource, optionsBuilder);
|
5d7692c4
sumatek
update limit and ...
|
206
|
if(username.equals(""))
|
35ea4f23
sumatek
update get
|
207
208
209
210
211
212
213
214
215
216
|
uri = new MongoClientURI("mongodb://"+address+"/?authSource="+authSource, optionsBuilder);
System.out.println("MongoDB Connecting to "+uri.toString()+"...");
MongoClient mongoClient = new MongoClient(uri);
database = mongoClient.getDatabase(dbname);
//test connect and list collections
MongoCursor<String> collectionNames = database.listCollectionNames().iterator();
System.out.println("MongoDB Connect to "+uri.toString()+" Success");
System.out.println("MongoDB Collections in datebase");
while (collectionNames.hasNext()) {
String collectionName = collectionNames.next();
|
35ea4f23
sumatek
update get
|
217
218
|
System.out.println(" |_ "+collectionName);
|
35ea4f23
sumatek
update get
|
219
|
}
|
35ea4f23
sumatek
update get
|
220
|
}
|
5d7692c4
sumatek
update limit and ...
|
221
|
|
35ea4f23
sumatek
update get
|
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
private static ArrayList<String> getDBData(BasicDBObject basicDBObject,String collectionName,String method)
{
collectionName = method.toLowerCase()+"_"+collectionName;
System.out.println("CollectionName : " + collectionName);
System.out.println("Find : "+basicDBObject.toJson());
ArrayList<String> returnData = new ArrayList<String>();
String found = "";
MongoCollection<Document> collection = database.getCollection(collectionName);
FindIterable<Document> findData = collection.find(basicDBObject);
MongoCursor<Document> cursor = findData.iterator();
switch (method) {
case GET:
while(cursor.hasNext()){
Document rawRow = cursor.next();
System.out.println("found _id : "+rawRow.get("_id"));
|
5d7692c4
sumatek
update limit and ...
|
240
|
rawRow.remove("_id");
|
35ea4f23
sumatek
update get
|
241
242
243
244
245
246
247
248
249
|
rawRow.remove("url");
if(rawRow.get("value") != null)
{
rawRow.append("resultData", rawRow.get("value"));
rawRow.append("rowCount", "1");
rawRow.remove("value");
}
found = rawRow.toJson();
|
72eef90c
Sumate Kongpui
update mockup
|
250
251
252
253
254
255
256
257
258
259
|
returnData.add(found);
}
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");
|
35ea4f23
sumatek
update get
|
260
261
262
263
264
265
266
267
|
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);
|
5d7692c4
sumatek
update limit and ...
|
268
|
System.out.println("found : "+found);
|
666338c1
Sumate Kongpui
update offset and...
|
269
270
271
272
|
}else
{
found = rawRow.toJson();
returnData.add(found);
|
5d7692c4
sumatek
update limit and ...
|
273
274
275
276
277
|
}
}
break;
|
35ea4f23
sumatek
update get
|
278
279
280
281
282
283
284
285
|
default:
break;
}
return returnData;
}
}
|
72eef90c
Sumate Kongpui
update mockup
|
|
|
f58f29ec
sumatek
add new project
|
|
|
72eef90c
Sumate Kongpui
update mockup
|
|
|
f58f29ec
sumatek
add new project
|
|
|
886ed8a4
Supakit Jirasirichote
- config db host
|
|
|
72eef90c
Sumate Kongpui
update mockup
|
|
|
886ed8a4
Supakit Jirasirichote
- config db host
|
|
|
f58f29ec
sumatek
add new project
|
|
|
72eef90c
Sumate Kongpui
update mockup
|
|
|
886ed8a4
Supakit Jirasirichote
- config db host
|
|
|
f58f29ec
sumatek
add new project
|
|
|
72eef90c
Sumate Kongpui
update mockup
|
|
|
f58f29ec
sumatek
add new project
|
|
|
72eef90c
Sumate Kongpui
update mockup
|
|
|
f58f29ec
sumatek
add new project
|
|
|
886ed8a4
Supakit Jirasirichote
- config db host
|
|
|
f58f29ec
sumatek
add new project
|
|
|
5d7692c4
sumatek
update limit and ...
|
|
|
f58f29ec
sumatek
add new project
|
|
|
35ea4f23
sumatek
update get
|
|
|
f58f29ec
sumatek
add new project
|
|
|
5d7692c4
sumatek
update limit and ...
|
|
|
35ea4f23
sumatek
update get
|
|
|
72eef90c
Sumate Kongpui
update mockup
|
|
|
35ea4f23
sumatek
update get
|
|
|
5d7692c4
sumatek
update limit and ...
|
|
|
f58f29ec
sumatek
add new project
|
|
|
35ea4f23
sumatek
update get
|
|
|
5d7692c4
sumatek
update limit and ...
|
|
|
35ea4f23
sumatek
update get
|
|
|
f58f29ec
sumatek
add new project
|
|
|
35ea4f23
sumatek
update get
|
|
|
f58f29ec
sumatek
add new project
|
|
|
35ea4f23
sumatek
update get
|
|
|
f58f29ec
sumatek
add new project
|
|
|
72eef90c
Sumate Kongpui
update mockup
|
|
|
35ea4f23
sumatek
update get
|
|
|
f58f29ec
sumatek
add new project
|
|
|
f58f29ec
sumatek
add new project
|
|
|
35ea4f23
sumatek
update get
|
|
|
72eef90c
Sumate Kongpui
update mockup
|
|
|
35ea4f23
sumatek
update get
|
|
|
5d7692c4
sumatek
update limit and ...
|
|
|
666338c1
Sumate Kongpui
update offset and...
|
|
|
5d7692c4
sumatek
update limit and ...
|
|
|
666338c1
Sumate Kongpui
update offset and...
|
|
|
f58f29ec
sumatek
add new project
|
|
|