Blame view

src/main/java/sourcecode/MockUp/MockUp.java 22.1 KB
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.net.InetSocketAddress;
886ed8a4   Supakit Jirasirichote   - config db host
6
import java.net.URLDecoder;
72eef90c   Sumate Kongpui   update mockup
7
8
9
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.sql.Date;
f58f29ec   sumatek   add new project
10
import java.util.ArrayList;
666338c1   Sumate Kongpui   update offset and...
11
import java.util.Arrays;
5d7692c4   sumatek   update limit and ...
12
import java.util.HashMap;
72eef90c   Sumate Kongpui   update mockup
13
import java.util.Iterator;
f58f29ec   sumatek   add new project
14
15
16
17

import org.apache.commons.io.IOUtils;
import org.apache.http.Consts;
import org.bson.Document;
35ea4f23   sumatek   update get
18
import org.json.JSONArray;
f58f29ec   sumatek   add new project
19
20
import org.json.JSONObject;

35ea4f23   sumatek   update get
21
import com.google.gson.Gson;
72eef90c   Sumate Kongpui   update mockup
22
import com.google.gson.JsonObject;
f58f29ec   sumatek   add new project
23
24
25
26
27
28
29
30
31
32
33
34
import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoClientURI;
import com.mongodb.client.FindIterable;
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;

f58f29ec   sumatek   add new project
35
36
public class MockUp 
{
35ea4f23   sumatek   update get
37
	private static Gson gson = new Gson();
f58f29ec   sumatek   add new project
38
39
40
41
42
	private static MongoDatabase database;
	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
	private static int port = 6300;
666338c1   Sumate Kongpui   update offset and...
44
45
	private static String DBNAME = "sdf";
	private static String DBHOST = "10.1.2.146:27017";
72eef90c   Sumate Kongpui   update mockup
46
47
48
	private static String DBUSERNAME = "";
	private static String DBPASSWORD = "";
	
f58f29ec   sumatek   add new project
49
50
51
	
	public static void main(String[] args) throws Exception {
		
886ed8a4   Supakit Jirasirichote   - config db host
52
		try {
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
			if(args.length>1)
				DBNAME = args[1];
			
			if(args.length>2)
				DBHOST = args[2];
			
72eef90c   Sumate Kongpui   update mockup
62
63
64
65
66
67
			if(args.length>3)
				DBUSERNAME = args[3];
			
			if(args.length>4)
				DBPASSWORD = args[4];
			
f58f29ec   sumatek   add new project
68
	        //connect mongo
886ed8a4   Supakit Jirasirichote   - config db host
69
			MongoConnector(DBNAME);	       
f58f29ec   sumatek   add new project
70
71
72
73
74
75
	        
			HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);	       
	        server.createContext("/", new MyHandler());
	        server.setExecutor(null); // creates a default executor
	        server.start();
			System.out.println("Run in port : "+port);
886ed8a4   Supakit Jirasirichote   - config db host
76
		} catch(Exception e) {
f58f29ec   sumatek   add new project
77
78
79
80
81
82
			e.printStackTrace();
			System.out.println("Fail to run in port : "+port);
		}
    }

    static class MyHandler implements HttpHandler {
72eef90c   Sumate Kongpui   update mockup
83
84
85
    	JSONObject resHeader = null;
    	int resEcode = 200;
    	
35ea4f23   sumatek   update get
86
        public void handle(HttpExchange t) throws IOException{
f58f29ec   sumatek   add new project
87
        	String response = "";
35ea4f23   sumatek   update get
88
89
90
91
92
        	try
        	{        	
	        	String method = t.getRequestMethod();        	
	        	String url = t.getRequestURI().toString();
	        	url = URLDecoder.decode(url, "UTF-8");
5d7692c4   sumatek   update limit and ...
93
94
95
96
97
98
99
100
101
102
103
104
105
106
	        	String bodyData = IOUtils.toString(t.getRequestBody(),Consts.UTF_8); 
	        	
	        	
	        	int limit = 0;
	        	
	        	if(!getKey("limit", url).equals(""))
	        	{
	        		try{
	        			limit = Integer.parseInt(getKey("limit", url));
	        		}catch(Exception e) 
	        		{
	        			
	        		}
	        	}
666338c1   Sumate Kongpui   update offset and...
107
108
109
110
111
112
113
114
115
116
117
118
	        	
	        	int offset = 0;
	        	
	        	if(!getKey("offset", url).equals(""))
	        	{
	        		try{
	        			offset = Integer.parseInt(getKey("offset", url));
	        		}catch(Exception e) 
	        		{
	        			
	        		}
	        	}
5d7692c4   sumatek   update limit and ...
119
120
121
122
123
124
	        		
	        	
	        	String[] fields = new String[0];
	        	if(!getKey("fields", url).equals(""))
	        		fields = getKey("fields", url).split(",");
	        	     	
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
	        	
	//        	System.out.println(t.getRequestMethod());
	//        	if(!method.equals(GET))
	//        	{
	//	        	String bodyData = IOUtils.toString(t.getRequestBody(),Consts.UTF_8); 
	//	        	JSONObject body = new JSONObject(bodyData);
	//	        	String[] a = bodyData.split(",");
	//	//        	System.out.println(a[0]);
	//	        	String[] b = a[0].split(":");
	//	//        	System.out.println(b[0]);
	//	        	String c = b[0].replace("{", "").trim().replace("\"", "");
	//	//        	System.out.println(c);
	//	        	keyBody = (String) body.get(c);
	//        	}
	//        	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());
	          
	        	
5d7692c4   sumatek   update limit and ...
150
	        	String keyData = findKey(method,url,bodyData);
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
	        	
	        	String collectionName = getSuffixTableName(url);     	
	        	JSONObject responsJSON = new JSONObject();
	        	
	        	//find retry
	        	
	        	ArrayList<String> retryList = findRetry(method,keyData);
	        	boolean notRetry = true;
	
	        	if(retryList.size() > 0)
	        	{
	        		JSONObject rowJSON = new JSONObject(retryList.get(0));
	        		System.out.println("Count retry : "+rowJSON.get("count"));
	        		
	        		if(rowJSON.has("value"))
	        			responsJSON = (JSONObject) rowJSON.get("value");
	        		else
	        		{
	        			System.out.println("not found vale or resultCode");
	        			responsJSON.put("resultCode", "50000");
			    		responsJSON.put("resultDescription", "Retry");
	        		}
	        		
	            	BasicDBObject find = new BasicDBObject();
	            	find.put("key", rowJSON.get("key"));
	//            	System.out.println(rowJSON.get("count"));
	            	int newCount = (int) rowJSON.get("count");
	            	rowJSON.remove("count");
	            	if(newCount > 0)
	            	{
	            		rowJSON.put("count", newCount-1);
	            		Document doc = Document.parse(rowJSON.toString());            	
	            		setDB(find,doc,method.toLowerCase()+"_retry");
	            		notRetry = false;
	            	}
	            }
	        		
	        	
	        	if(notRetry)
	        	{
f58f29ec   sumatek   add new project
191

35ea4f23   sumatek   update get
192
		        	
5d7692c4   sumatek   update limit and ...
193
		        	ArrayList<String> responseList = getDBData(keyData,collectionName,method,bodyData);      	
35ea4f23   sumatek   update get
194
195
196
197
198
199
200
201
202
203
204
205

		        	
		        	
		        	if(responseList.size() == 0)
		        	{
		        		System.out.println("not any Data");
		        		responsJSON.put("resultCode", "80000");
			    		responsJSON.put("resultDescription", "MockUp error : No data in "+method.toLowerCase()+"_"+collectionName+"!!!");
		        	}else
		        	{
		        		if(method.equals(GET) && responseList.size() > 1)
			    		{
5d7692c4   sumatek   update limit and ...
206
		        			System.out.println("Multi Data => Merge");
35ea4f23   sumatek   update get
207
208
209
210
211
212
213
214
215
216
		        			ArrayList<Object> resultData = new ArrayList<Object>();		    		
				        	for(int i=0;i<responseList.size();i++)
				        	{
				        		JSONObject rowJSON = new JSONObject(responseList.get(i));
				        		
				        		if(rowJSON.has("value"))
				        		{
				        			JSONObject value = (JSONObject) rowJSON.get("value");
				        			if(value.has("resultData"))
					        		{	
35ea4f23   sumatek   update get
217
218
				        				try{
				        					JSONArray JSONArrayData = value.getJSONArray("resultData");
35ea4f23   sumatek   update get
219
				        					for(int n=0;n<JSONArrayData.length();n++)
35ea4f23   sumatek   update get
220
				        						resultData.add(JSONArrayData.get(n));
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
				        				}catch(Exception e)
				        				{				
//				        					e.printStackTrace();
				        					resultData.add(value.get("resultData"));
				        				}
				        				
					        		}
				        		}
				        		
		            		}
				        	
				        	responsJSON.put("resultCode", "20000");
				    		responsJSON.put("resultDescription", "Success");
				    		responsJSON.put("resultData",resultData);
				    		responsJSON.put("rowCount",resultData.size());
		        			
			    		}else
			    		{
5d7692c4   sumatek   update limit and ...
240
			    			System.out.println("Single Data => Direct");
35ea4f23   sumatek   update get
241
242
243
244
245
246
247
248
249
			    			JSONObject rowJSON = new JSONObject(responseList.get(0));			    			
			    			if(rowJSON.has("value"))
			    				responsJSON = (JSONObject) rowJSON.get("value");
			    			else
			    			{
			    				System.out.println("not found vale or resultCode");
			    				responsJSON.put("resultCode", "20000");
					    		responsJSON.put("resultDescription", "Success");
			    			}
72eef90c   Sumate Kongpui   update mockup
250
251
252
253
254
255
256
257
258
259
			    			
			    			if(rowJSON.has("header"))
			    				resHeader = rowJSON.getJSONObject("header");
			    			else
			    				resHeader = null;
			    			
			    			if(rowJSON.has("ecode"))
			    				resEcode = rowJSON.getInt("ecode");
			    			else
			    				resEcode = 200;
35ea4f23   sumatek   update get
260
261
262
263
264
265
266
267
			    		}
		        	}
		        	
		        	///////////////////////////////////////////////////////////////////////////////////////////////////////////////		        	
		        	

	        	}
	        	
5d7692c4   sumatek   update limit and ...
268
	        	//limit
666338c1   Sumate Kongpui   update offset and...
269
270
271
272
	        	if(method.equals(GET) && responsJSON.has("resultCode") && responsJSON.get("resultCode").equals("20000")) {
	        		responsJSON = offsetData(responsJSON,offset);
	        		responsJSON = limitData(responsJSON,limit);
	        	}
5d7692c4   sumatek   update limit and ...
273
274
275
276
277
	        	
	        	//field
	        	responsJSON = FieldsData(responsJSON,fields); 
	        	
	    			
35ea4f23   sumatek   update get
278
279
280
281
282
283
284
285
286
287
288
289
290
291
	        	
	        	response = responsJSON.toString();
	        	
	        	System.out.println("");
	            System.out.println("URL : " + url);
	            System.out.println("Method : " + method);
	            System.out.println("Response : " + response);
	            System.out.println("");
	        	
	        	}catch(Exception e) {
	    			e.printStackTrace();
	    			System.out.println("System error "+e.getMessage());
	    			
	    		}
72eef90c   Sumate Kongpui   update mockup
292
293
294
295
296
297
298
299
300
        	if(resHeader != null)
        	{
        		Iterator<String> keys = resHeader.keys();
        		while(keys.hasNext()) {
        		    String key = keys.next();
        		    t.getResponseHeaders().set(key, resHeader.getString(key));        		    
        		}
        		
        	}
f58f29ec   sumatek   add new project
301
        	
72eef90c   Sumate Kongpui   update mockup
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
    	    String ret = response.toString();
    	    t.getResponseHeaders().set("Content-Type", "application/json");
    	    
    	    t.getResponseHeaders().set("Access-Control-Allow-Origin", "https://localhost:"+port);
    	    t.getResponseHeaders().set("Access-Control-Allow-Credentials", "true");
    	    t.getResponseHeaders().set("Access-Control-Allow-Headers", "content-Type");
    	    t.getResponseHeaders().set("Access-Control-Allow-Methods", "POST, PUT, GET, DELETE, OPTIONS");
    	    
    	    
    	    

    	    //ret= URLDecoder.decode(ret, "UTF-8");
    	    byte[] bytes = ret.getBytes(StandardCharsets.UTF_8);
    	    t.sendResponseHeaders(resEcode, bytes.length);
//    	    System.out.println(ret);
    	    OutputStream os = t.getResponseBody();
    	    os.write(bytes);
    	    os.close();
        	
        	
        	
        	
//        	t.getResponseHeaders().set("Content-Type", "application/json");
//        	t.sendResponseHeaders(200, response.length());
//        	OutputStream os = t.getResponseBody();  
//        	os.write(response.getBytes(Charset.forName("US-ASCII")));
////        	os.write(response.getBytes());        	
//            os.close();
f58f29ec   sumatek   add new project
330
331
        }
    }
886ed8a4   Supakit Jirasirichote   - config db host
332
333
334
335
336
337
338
339
340
341
342
343
    
    private static String getSuffixTableName(String url)
	{
		String value = null;
		String[] arrData = url.split("/");
		for (String data : arrData) {
		    if (data.contains(".json")) {
		    	int lastIndex = data.indexOf(".json");
		    	value = data.substring(0, lastIndex);
		    	break;
		    }
		}
72eef90c   Sumate Kongpui   update mockup
344
345
346
347
348
349
350
351
		if(value == null)
			value = arrData[arrData.length-1];
		if(value.contains("?"))
		{
			arrData = value.split("\\?");
			value = arrData[0];
		}
		
886ed8a4   Supakit Jirasirichote   - config db host
352
353
		return value;
	}
f58f29ec   sumatek   add new project
354
355
356
   
    private static void MongoConnector(String db) {
    	
72eef90c   Sumate Kongpui   update mockup
357
358
    	String username = DBUSERNAME;
		String password = DBPASSWORD.replace("@", "%40");
886ed8a4   Supakit Jirasirichote   - config db host
359
		String address = DBHOST;
f58f29ec   sumatek   add new project
360
		String dbname = db;
72eef90c   Sumate Kongpui   update mockup
361
		String authSource = "admin";
f58f29ec   sumatek   add new project
362
363
364
365
366
367
368
369
370
		int timeoutMongoDB = 10000;		
		
		 MongoClientOptions.Builder optionsBuilder = MongoClientOptions.builder(); 
			optionsBuilder.connectTimeout(timeoutMongoDB); 
			optionsBuilder.socketTimeout(timeoutMongoDB); 
			optionsBuilder.serverSelectionTimeout(timeoutMongoDB);

//			optionsBuilder.connectionsPerHost(maxPoolSize);
//			optionsBuilder.minConnectionsPerHost(minPoolSize);
72eef90c   Sumate Kongpui   update mockup
371
//			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
f58f29ec   sumatek   add new project
372
373
374
375
	        MongoClientURI uri = new MongoClientURI("mongodb://"+username+":"+password+"@"+address+"/?authSource="+authSource, optionsBuilder);
			if(username.equals(""))
				uri = new MongoClientURI("mongodb://"+address+"/?authSource="+authSource, optionsBuilder);
			System.out.println("MongoDB Connecting to "+uri.toString()+"...");
886ed8a4   Supakit Jirasirichote   - config db host
376
			@SuppressWarnings("resource")
f58f29ec   sumatek   add new project
377
378
379
380
381
382
383
384
385
386
387
388
389
			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();
				System.out.println(" |_ "+collectionName);		
				
			}
    }
    
5d7692c4   sumatek   update limit and ...
390
    private static ArrayList<String> getDBData(String keyData,String collectionName,String method,String bodyData)
f58f29ec   sumatek   add new project
391
    {
35ea4f23   sumatek   update get
392

f58f29ec   sumatek   add new project
393
    	ArrayList<String> returnData = new ArrayList<String>();
5d7692c4   sumatek   update limit and ...
394
//    	collectionName = method.toLowerCase()+"_"+collectionName;
35ea4f23   sumatek   update get
395
396
    	System.out.println("===> Find Normal");
    	BasicDBObject basicDBObject = new BasicDBObject();
72eef90c   Sumate Kongpui   update mockup
397
//    	System.out.println(keyData);
35ea4f23   sumatek   update get
398
    	basicDBObject.put("key", keyData);
5d7692c4   sumatek   update limit and ...
399
    	returnData = getDB(basicDBObject,method.toLowerCase()+"_"+collectionName);
f58f29ec   sumatek   add new project
400
    	
35ea4f23   sumatek   update get
401
402
403
404
405
    	if(returnData.size() == 0)
    	{
    		System.out.println("===> Go to Main flow");
    		BasicDBObject basicDBObject2 = new BasicDBObject();
        	basicDBObject2.put("key", "");
5d7692c4   sumatek   update limit and ...
406
407
408
409
410
411
412
413
414
415
416
417
418
    		returnData = getDB(basicDBObject2,method.toLowerCase()+"_"+collectionName);
    		
//    		if(method.equals(POST) && returnData.size()>0)
//    		{
//    			//post main flow
//    			JSONObject rowJSON = new JSONObject(returnData.get(0));
//    			rowJSON.remove("_id");
//    			JSONObject value = rowJSON.getJSONObject("value");
//    			value.put("resultData",  new JSONObject(bodyData));
//    			
//    			Document doc = Document.parse(rowJSON.toString());
//    			insertDB(doc,GET.toLowerCase()+"_"+collectionName);
//    		}
35ea4f23   sumatek   update get
419
420
421
422
    	}    	
    	
    	

f58f29ec   sumatek   add new project
423
424
    	
    	
35ea4f23   sumatek   update get
425
426
427
428
429
430
431
    	return returnData;
    }

    private static String findKey(String method,String url,String bodyData)
    {
    	String returnData = "";
    	
f58f29ec   sumatek   add new project
432
433
    	switch (method) {
		case GET:
35ea4f23   sumatek   update get
434
435
		case DELETE:
			returnData = url;
f58f29ec   sumatek   add new project
436
437
438
			break;
		case POST:
		case PUT:
72eef90c   Sumate Kongpui   update mockup
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
			try
			{
	        	JSONObject body = new JSONObject(bodyData);
	        	String[] a = bodyData.split(",");
	        	String[] b = a[0].split(":");
	        	String c = b[0].replace("{", "").trim().replace("\"", "");
	        	
	        	returnData = findKeyObject(body.get(c));
	//        	returnData = (String) body.get(c);
			}catch(Exception e)
			{
				returnData = "";
			}
        	
        	
35ea4f23   sumatek   update get
454
455
			break;	
		
f58f29ec   sumatek   add new project
456
457
458
459
		default:
			break;
		}
    	
f58f29ec   sumatek   add new project
460
461
    	return returnData;
    }
35ea4f23   sumatek   update get
462
    
72eef90c   Sumate Kongpui   update mockup
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
    private static String findKeyObject(Object obj)
    {
    	String returnData = "";
    	if(obj instanceof JSONObject)
    	{
    		returnData = obj.toString();
//    		JSONObject json = (JSONObject) obj;
//    		String bodyData = obj.toString();
//    		String[] a = bodyData.split(",");
//        	String[] b = a[0].split(":");
//        	String c = b[0].replace("{", "").trim().replace("\"", "");
//    		returnData = findKeyObject(json.get(c));
    	}else
    		returnData = (String) obj;
    	
    	return returnData;
    }
    
35ea4f23   sumatek   update get
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
    private static ArrayList<String> findRetry(String method,String keyData)
    {
    	ArrayList<String> returnData = new ArrayList<String>();
    	
    	System.out.println("===>Find retry");
    	BasicDBObject basicDBObject = new BasicDBObject();
    	basicDBObject.put("key", keyData);
    	String collectionName = method.toLowerCase()+"_retry";
    	returnData = getDB(basicDBObject,collectionName);
    	
    	if(returnData.size() == 0)
    	{
    		BasicDBObject basicDBObject2 = new BasicDBObject();
        	basicDBObject2.put("key", "");
    		returnData = getDB(basicDBObject2,collectionName);
    	}    	
    	
    	
    	return returnData;    	
    }
    
    private static ArrayList<String> getDB(BasicDBObject basicDBObject,String collectionName)
    {
    	ArrayList<String> returnData = new ArrayList<String>();
    	
    	System.out.println("CollectionName : " + collectionName);
    	System.out.println("Find : "+basicDBObject.toJson());
    	
    	MongoCollection<Document> collection = database.getCollection(collectionName);
    	FindIterable<Document> findData = collection.find(basicDBObject);
    	MongoCursor<Document> cursor = findData.iterator();
    	
    	while(cursor.hasNext()){
			Document rawRow = cursor.next(); 
			System.out.println("found retry _id : "+rawRow.get("_id"));
//			System.out.println("count : "+rawRow.get("count"));
			returnData.add(rawRow.toJson());
		}
    	 
		return returnData;
    	
    }
    
    private static void setDB(BasicDBObject find,Document doc,String collectionName)
    {
//    	System.out.println("Start Update Mongo");

    	MongoCollection<Document> collection = database.getCollection(collectionName);
    	Document updateDoc = collection.findOneAndReplace(find, doc);		
		
    	System.out.println("Update Mongo");
    }
    
5d7692c4   sumatek   update limit and ...
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
    private static void insertDB(Document doc,String collectionName)
    {
//    	System.out.println("Start Update Mongo");

    	MongoCollection<Document> collection = database.getCollection(collectionName);
    	collection.insertOne(doc);	
		
    	System.out.println("Insert Mongo");
    }
   
    private static JSONObject limitData(JSONObject responsJSON,int limit)
    {
    	if(limit != 0)
		{
    		int size = 0;
        	try{
        		System.out.println("Limit : "+limit);
        		JSONArray JSONArrayData = responsJSON.getJSONArray("resultData");	        		
        		
	    		ArrayList<Object> limitResultData = new ArrayList<Object>();	
	    		size = JSONArrayData.length();
	    		for(int i=0;i<size;i++)
		        {				    				
	    			limitResultData.add(JSONArrayData.get(i));
	    			if(i == limit-1)
	    				i=JSONArrayData.length();
	    		}
	    		responsJSON.remove("resultData");
	    		responsJSON.put("resultData",limitResultData);		    		
        		
        	}catch(Exception e)
        	{
        		//resultData not array 
        		size = 1;
        	}
666338c1   Sumate Kongpui   update offset and...
569
570
571
572
//        	responsJSON.remove("rowCount");
        	if(!responsJSON.has("rowCount")) {
            	responsJSON.put("rowCount", size);
        	}
5d7692c4   sumatek   update limit and ...
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
        }
		
    	return responsJSON;
    	
    }
    
    private static JSONObject FieldsData(JSONObject responsJSON,String[] fileds)
    {
    	if(fileds.length > 0 && responsJSON.has("resultData"))
    	{
    		String select = "";
    		for(String row:fileds)
    			select += ","+row;
    		System.out.println("Select Fields => "+select.substring(1));
    		
	    	try{
	    		JSONArray jsonArray = responsJSON.getJSONArray("resultData");
	    		JSONArray newJsonArray = new  JSONArray();
	    		for(int i=0;i<jsonArray.length();i++)
	    		{
	    			try{
		    			JSONObject json = jsonArray.getJSONObject(i);
		    			JSONObject newResponse = FieldsObjectData(json,fileds);
		    			newJsonArray.put(newResponse);
		    		}catch(Exception e2)
		    		{
		    			newJsonArray.put(jsonArray.get(i));
		    		}
	    		}
	    		responsJSON.remove("resultData");
    			responsJSON.put("resultData", newJsonArray);
	    		
	    	}catch(Exception e)
	    	{
	    		try{
	    			JSONObject json = responsJSON.getJSONObject("resultData");
	    			JSONObject newResponse = FieldsObjectData(json,fileds);
	    			responsJSON.remove("resultData");
	    			responsJSON.put("resultData", newResponse);
	    		}catch(Exception e2)
	    		{
	    			//resultData not JSONObject or JSONArray
	    		}
	    		
	    	}
    	}
    	
		return responsJSON;
    	
    }    
   
    private static JSONObject FieldsObjectData(JSONObject json,String[] fileds)
    {
    	JSONObject returnData = null; 
    	if(fileds.length > 0)
    	{
    		JSONObject newResponse = new JSONObject();
        	
        	for(String row:fileds)
    		{
    			if(json.has(row))
    				newResponse.put(row, json.get(row));
    			
    		}
        	returnData = newResponse;
    	}else
    	{
    		returnData = json;
    	}
    		
    	
    	return returnData;
    }

    public static ArrayList<String> getKeyUrl(String url) {
		
		ArrayList<String> arrData = new ArrayList<String>();
		
		String[] arr = url.split("[/]");
		
		for(int i = 0; i < arr.length; i++) {
			
			if(arr[i].contains("?"))
				arr[i] = arr[i].substring(0, arr[i].indexOf("?"));
			
			if(!arr[i].equals(""))
				arrData.add(arr[i]);
		}
		
		return arrData;
		
	}
	
	public static HashMap<String, ArrayList<String>> getKeyFilter(String url) {
		
		String key = "filter=";
		String value = null;
		HashMap<String,ArrayList<String>> hashMap = new HashMap<String,ArrayList<String>>();
		
		value = url.substring(url.indexOf(key)+key.length());
		value = value.substring(0, value.indexOf(")&")+1);
		String[] array = value.split("[()]");
		
		for(int i = 0; i < array.length; i++) {
			
			String[] temp = array[i].split("=");
			
			if(temp.length > 1) {
//				System.out.println(temp[0]+" "+temp[1]);
				
				if(hashMap.containsKey(temp[0])) {
					hashMap.get(temp[0]).add(temp[1]);
				} else {
					ArrayList<String> tempArr = new ArrayList<>();
					tempArr.add(temp[1]);
					hashMap.put(temp[0], tempArr);
				}
			}
		}
		
		return hashMap;
		
	}
	
	public static String getKey(String key, String url) {
		
		key = key + "=";
		String value = "";
		
		if(url.contains(key)) {
			value = url.substring(url.indexOf(key)+key.length());
			if(value.indexOf("&") != -1)
				value = value.substring(0, value.indexOf("&"));
		}
		
		return value;
	}
	
666338c1   Sumate Kongpui   update offset and...
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
    private static JSONObject offsetData(JSONObject responsJSON, int offset) {
    	
    	try {
    		System.out.println("Offset : " + offset);
    		JSONArray JSONArrayData = responsJSON.getJSONArray("resultData");
    		
    		int n = JSONArrayData.length();
    		
    		ArrayList<Object> offsetResultData = new ArrayList<Object>();	

    		for (int i = 0; i < n; i++)
    		{
    			if (i >= offset)
    				offsetResultData.add(JSONArrayData.get(i));
    		}
    		
    		responsJSON.remove("resultData");
    		responsJSON.put("resultData",offsetResultData);		    
    		
    	} catch (Exception e) {
			// TODO: handle exception
		}
    	
    	
    	return responsJSON;
    }
f58f29ec   sumatek   add new project
737
}