Subversion Repositories SmartDukaan

Rev

Rev 17646 | Rev 20185 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 17646 Rev 20180
Line 1... Line 1...
1
package in.shop2020.serving.utils;
1
package in.shop2020.serving.utils;
2
 
2
 
3
import in.shop2020.config.ConfigException;
-
 
4
import in.shop2020.model.v1.catalog.ExclusiveAffiliateItemInfo;
-
 
5
import in.shop2020.model.v1.order.LineItem;
-
 
6
import in.shop2020.model.v1.order.Order;
-
 
7
import in.shop2020.model.v1.order.RechargeOrderStatus;
-
 
8
import in.shop2020.model.v1.order.RechargePlan;
-
 
9
import in.shop2020.model.v1.order.RechargeType;
-
 
10
import in.shop2020.model.v1.user.Cart;
-
 
11
import in.shop2020.model.v1.user.Line;
-
 
12
import in.shop2020.thrift.clients.CatalogClient;
-
 
13
import in.shop2020.thrift.clients.TransactionClient;
-
 
14
import in.shop2020.thrift.clients.config.ConfigClient;
-
 
15
 
-
 
16
import java.io.BufferedReader;
3
import java.io.BufferedReader;
-
 
4
import java.io.DataOutputStream;
17
import java.io.InputStreamReader;
5
import java.io.InputStreamReader;
-
 
6
import java.net.HttpURLConnection;
-
 
7
import java.net.URL;
18
import java.util.ArrayList;
8
import java.util.ArrayList;
19
import java.util.Arrays;
9
import java.util.Arrays;
20
import java.util.Collections;
10
import java.util.Collections;
21
import java.util.Date;
11
import java.util.Date;
22
import java.util.HashMap;
12
import java.util.HashMap;
23
import java.util.List;
13
import java.util.List;
24
import java.util.Map;
14
import java.util.Map;
-
 
15
import java.util.Map.Entry;
25
 
16
 
26
import javax.mail.internet.AddressException;
17
import javax.mail.internet.AddressException;
27
import javax.mail.internet.InternetAddress;
18
import javax.mail.internet.InternetAddress;
-
 
19
import javax.net.ssl.HttpsURLConnection;
28
 
20
 
29
import org.apache.commons.lang.StringUtils;
21
import org.apache.commons.lang.StringUtils;
30
import org.apache.http.HttpResponse;
22
import org.apache.http.HttpResponse;
31
import org.apache.http.client.HttpClient;
23
import org.apache.http.client.HttpClient;
32
import org.apache.http.client.methods.HttpGet;
24
import org.apache.http.client.methods.HttpGet;
33
import org.apache.http.impl.client.DefaultHttpClient;
25
import org.apache.http.impl.client.DefaultHttpClient;
34
import org.apache.log4j.Logger;
26
import org.apache.log4j.Logger;
35
 
27
 
-
 
28
import in.shop2020.config.ConfigException;
-
 
29
import in.shop2020.model.v1.catalog.ExclusiveAffiliateItemInfo;
-
 
30
import in.shop2020.model.v1.order.LineItem;
-
 
31
import in.shop2020.model.v1.order.Order;
-
 
32
import in.shop2020.model.v1.order.RechargeOrderStatus;
-
 
33
import in.shop2020.model.v1.order.RechargePlan;
-
 
34
import in.shop2020.model.v1.order.RechargeType;
-
 
35
import in.shop2020.model.v1.user.Cart;
-
 
36
import in.shop2020.model.v1.user.Line;
-
 
37
import in.shop2020.thrift.clients.CatalogClient;
-
 
38
import in.shop2020.thrift.clients.TransactionClient;
-
 
39
import in.shop2020.thrift.clients.config.ConfigClient;
-
 
40
 
36
public class Utils {
41
public class Utils {
37
    private static Logger logger = Logger.getLogger(Utils.class);
42
    private static Logger logger = Logger.getLogger(Utils.class);
38
    
43
    
39
	public static final String EXPORT_ENTITIES_PATH = getExportPath();
44
	public static final String EXPORT_ENTITIES_PATH = getExportPath();
40
	public static final String EXPORT_JAVASCRIPT_CONTENT_PATH = "/var/lib/tomcat6/webapps/export/javascripts/";
45
	public static final String EXPORT_JAVASCRIPT_CONTENT_PATH = "/var/lib/tomcat6/webapps/export/javascripts/";
Line 345... Line 350...
345
	}
350
	}
346
    
351
    
347
    public static Map<String, String> getOperatorByUri(String uri){
352
    public static Map<String, String> getOperatorByUri(String uri){
348
    	return uriOperatorMap.get(uri);
353
    	return uriOperatorMap.get(uri);
349
	}
354
	}
-
 
355
    
-
 
356
    private final String USER_AGENT = "Mozilla/5.0";
-
 
357
    public final static String METHOD_POST = "POST";
-
 
358
    public final static String METHOD_GET = "GET";
-
 
359
    
-
 
360
    
-
 
361
    public String callRestApi(String apiUrl,String method,Map<String, String> paramsMap, Map<String, String> requestProperty){
-
 
362
    	StringBuffer response = new StringBuffer();
-
 
363
    	StringBuffer urlParameters= new StringBuffer("?");
-
 
364
    	try{
-
 
365
 
-
 
366
			if (paramsMap != null) {
-
 
367
				for (Entry<String, String> entry : paramsMap.entrySet()) {
-
 
368
					urlParameters.append(entry.getKey() + "=" + entry.getValue() + "&");
-
 
369
				}
-
 
370
			}
-
 
371
			if (METHOD_GET.equalsIgnoreCase(method)) {
-
 
372
				apiUrl += "?"+urlParameters.toString();
-
 
373
			}
-
 
374
			URL obj = new URL(apiUrl);
-
 
375
			HttpURLConnection con = (HttpURLConnection) obj.openConnection();
-
 
376
			if (apiUrl.startsWith("https")) {
-
 
377
				con = (HttpsURLConnection) obj.openConnection();
-
 
378
			}
-
 
379
 
-
 
380
			// add reuqest header
-
 
381
			con.setRequestMethod(method);
-
 
382
			con.setRequestProperty("User-Agent", USER_AGENT);
-
 
383
			con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
-
 
384
 
-
 
385
			if (requestProperty != null) {
-
 
386
				for (Entry<String, String> entry : requestProperty.entrySet()) {
-
 
387
					con.setRequestProperty(entry.getKey(), entry.getValue());
-
 
388
				}
-
 
389
			}
-
 
390
			// Send Post request
-
 
391
			if (METHOD_POST.equalsIgnoreCase(method)) {
-
 
392
				con.setDoOutput(true);
-
 
393
				DataOutputStream wr = new DataOutputStream(con.getOutputStream());
-
 
394
				wr.writeBytes(urlParameters.toString());
-
 
395
				wr.flush();
-
 
396
				wr.close();
-
 
397
			}
-
 
398
 
-
 
399
			int responseCode = con.getResponseCode();
-
 
400
			System.out.println("\nSending  request to URL : " + apiUrl);
-
 
401
			System.out.println("Post parameters : " + urlParameters);
-
 
402
			System.out.println("Response Code : " + responseCode);
-
 
403
 
-
 
404
			if(responseCode==200){
-
 
405
			BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
-
 
406
			String inputLine;
-
 
407
 
-
 
408
			while ((inputLine = in.readLine()) != null) {
-
 
409
				response.append(inputLine);
-
 
410
			}
-
 
411
			in.close();
-
 
412
			
-
 
413
			}
-
 
414
 
-
 
415
			// print result
-
 
416
			System.out.println(response.toString());
-
 
417
		}catch(Exception e){
-
 
418
    		
-
 
419
    	}
-
 
420
    	return response.toString();
-
 
421
    }
-
 
422
    
-
 
423
    public static void main(String args[]){
-
 
424
 
-
 
425
		//new Utils().callRestApi("https://graph.facebook.com/debug_token", Utils.METHOD_GET, paramsMap, null);
-
 
426
	}
350
}
427
}
351
428