Subversion Repositories SmartDukaan

Rev

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

Rev 21557 Rev 22215
Line 2... Line 2...
2
 
2
 
3
import java.io.BufferedReader;
3
import java.io.BufferedReader;
4
import java.io.IOException;
4
import java.io.IOException;
5
import java.io.InputStream;
5
import java.io.InputStream;
6
import java.io.InputStreamReader;
6
import java.io.InputStreamReader;
-
 
7
import java.io.UnsupportedEncodingException;
-
 
8
import java.util.ArrayList;
-
 
9
import java.util.List;
7
import java.util.Map;
10
import java.util.Map;
8
import java.util.Set;
11
import java.util.Set;
9
 
12
 
10
import org.apache.http.HttpResponse;
13
import org.apache.http.HttpResponse;
-
 
14
import org.apache.http.NameValuePair;
11
import org.apache.http.client.ClientProtocolException;
15
import org.apache.http.client.ClientProtocolException;
12
import org.apache.http.client.HttpClient;
16
import org.apache.http.client.HttpClient;
-
 
17
import org.apache.http.client.entity.UrlEncodedFormEntity;
13
import org.apache.http.client.methods.HttpGet;
18
import org.apache.http.client.methods.HttpGet;
-
 
19
import org.apache.http.client.methods.HttpPost;
-
 
20
import org.apache.http.client.methods.HttpUriRequest;
14
import org.apache.http.impl.client.HttpClients;
21
import org.apache.http.impl.client.HttpClients;
-
 
22
import org.apache.http.message.BasicNameValuePair;
15
import org.slf4j.Logger;
23
import org.slf4j.Logger;
16
import org.slf4j.LoggerFactory;
24
import org.slf4j.LoggerFactory;
17
import org.springframework.http.HttpHeaders;
-
 
18
import org.springframework.http.HttpStatus;
25
import org.springframework.http.HttpStatus;
19
import org.springframework.http.MediaType;
-
 
20
import org.springframework.web.util.UriComponentsBuilder;
26
import org.springframework.web.util.UriComponentsBuilder;
21
 
27
 
22
import com.spice.profitmandi.common.ResponseCodeHolder;
28
import com.spice.profitmandi.common.ResponseCodeHolder;
23
import com.spice.profitmandi.common.enumuration.SchemeType;
29
import com.spice.profitmandi.common.enumuration.SchemeType;
24
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
30
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
25
import com.spice.profitmandi.common.model.ProfitMandiConstants;
-
 
26
 
31
 
27
public class RestClient {
32
public class RestClient {
28
	
33
	
29
	private static final Logger LOGGER = LoggerFactory.getLogger(RestClient.class);
34
	private static final Logger LOGGER = LoggerFactory.getLogger(RestClient.class);
30
	
35
	
31
	private HttpClient httpClient;
36
	private HttpClient httpClient;
32
	private SchemeType scheme;
37
	private SchemeType scheme;
33
	private String hostName;
38
	private String hostName;
34
	private int port;
39
	private int port;
35
	private final String url;
40
	private final String url;
36
	private HttpHeaders headers;
41
	//private HttpHeaders headers;
37
	public RestClient(SchemeType scheme, String hostName, int port){
42
	public RestClient(SchemeType scheme, String hostName, int port){
38
		this.scheme = scheme;
43
		this.scheme = scheme;
39
		this.hostName = hostName;
44
		this.hostName = hostName;
40
		this.port = port;
45
		this.port = port;
41
		this.url = scheme.getValue()==null ? SchemeType.HTTP.toString() : scheme.getValue()  + hostName + ":" + port + "/";
46
		this.url = scheme.getValue()==null ? SchemeType.HTTP.toString() : scheme.getValue()  + hostName + ":" + port + "/";
42
		this.headers = new HttpHeaders();
47
		//this.headers = new HttpHeaders();
43
		this.httpClient = HttpClients.createDefault();
48
		this.httpClient = HttpClients.createDefault();
44
		headers.add("Content-Type", MediaType.APPLICATION_JSON_VALUE);
49
		//headers.add("Content-Type", MediaType.APPLICATION_JSON_VALUE);
45
	}
50
	}
46
	public SchemeType getScheme() {
51
	public SchemeType getScheme() {
47
		return scheme;
52
		return scheme;
48
	}
53
	}
49
	public String getHostName() {
54
	public String getHostName() {
Line 53... Line 58...
53
		return port;
58
		return port;
54
	}
59
	}
55
	public String getUrl() {
60
	public String getUrl() {
56
		return url;
61
		return url;
57
	}
62
	}
58
	public String get(String uri, Map<String, String> params)
63
	public String get(String uri, Map<String, String> params, Map<String, String> headers)
59
		throws ProfitMandiBusinessException{
64
		throws ProfitMandiBusinessException{
60
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url + uri);
65
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url + uri);
61
		Set<String> keys = params.keySet();
66
		Set<String> keys = params.keySet();
62
		for(String key : keys){
67
		for(String key : keys){
63
			builder.queryParam(key, params.get(key));
68
			builder.queryParam(key, params.get(key));
64
		}
69
		}
65
		HttpGet request = new HttpGet(builder.build().toUri());
70
		HttpGet request = new HttpGet(builder.build().toUri());
-
 
71
		for(Map.Entry<String, String> entry : headers.entrySet()){
66
		request.setHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE);
72
			request.setHeader(entry.getKey(), entry.getValue());
-
 
73
		}
-
 
74
		return this.execute(request);
-
 
75
	}
-
 
76
	
-
 
77
	public String execute(HttpUriRequest request)
-
 
78
			throws ProfitMandiBusinessException{
67
		LOGGER.info("Connecting to google server for token validation at url {}",request.getURI());
79
		LOGGER.info("Connecting to server at url {}",request.getURI());
68
		try {
80
		try {
69
			HttpResponse response = httpClient.execute(request);
81
			HttpResponse response = httpClient.execute(request);
70
			String responseString = this.toString(response.getEntity().getContent());
82
			String responseString = this.toString(response.getEntity().getContent());
71
			LOGGER.info("Got response for token validation with responseCode {} and responseBody {}", response.getStatusLine().getStatusCode(), responseString);
83
			LOGGER.info("Got response from server with responseCode {} and responseBody {}", response.getStatusLine().getStatusCode(), responseString);
72
			if(response.getStatusLine().getStatusCode() == HttpStatus.OK.value()){
84
			if(response.getStatusLine().getStatusCode() == HttpStatus.OK.value()){
73
				return responseString;
85
				return responseString;
74
			}else{
86
			}else{
75
				throw new ProfitMandiBusinessException(ProfitMandiConstants.TOKEN, params.get(ProfitMandiConstants.ACCESS_TOKEN), "USR_1005");
87
				throw new ProfitMandiBusinessException("", "", "GE_1005");
76
			}
88
			}
77
		} catch (ClientProtocolException e) {
89
		} catch (ClientProtocolException e) {
-
 
90
			LOGGER.error("Client Error : ",e);
78
			throw new RuntimeException(ResponseCodeHolder.getMessage("USR_1006"));
91
			throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
79
		} catch (IOException e) {
92
		} catch (IOException e) {
-
 
93
			LOGGER.error("IO Error : ", e);
80
			throw new RuntimeException(ResponseCodeHolder.getMessage("USR_1006"));
94
			throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
-
 
95
		}
-
 
96
	}
-
 
97
	
-
 
98
	public String post(String uri, Map<String, String> params, Map<String, String> headers)
-
 
99
			throws ProfitMandiBusinessException{
-
 
100
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url + uri);
-
 
101
		List<NameValuePair> bodyParameters = new ArrayList<NameValuePair>();
-
 
102
		for(Map.Entry<String, String> entry : params.entrySet()){
-
 
103
			bodyParameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
-
 
104
		}
-
 
105
		
-
 
106
		LOGGER.info("Body Parameters {}", params);
-
 
107
		HttpPost request = new HttpPost(builder.build().toUri());
-
 
108
		for(Map.Entry<String, String> entry : headers.entrySet()){
-
 
109
			request.setHeader(entry.getKey(), entry.getValue());
81
		}
110
		}
-
 
111
		
-
 
112
		try{
-
 
113
			request.setEntity(new UrlEncodedFormEntity(bodyParameters));
-
 
114
		}catch (UnsupportedEncodingException unsupportedEncodingException) {
-
 
115
			LOGGER.error("Encoding error : ", unsupportedEncodingException);
-
 
116
			throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
-
 
117
		}
-
 
118
		
-
 
119
		
-
 
120
		return this.execute(request);
-
 
121
		
82
	}
122
	}
-
 
123
	
83
	private String toString(InputStream inputStream){
124
	private String toString(InputStream inputStream){
84
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
125
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
85
		StringBuilder responseString = new StringBuilder();
126
		StringBuilder responseString = new StringBuilder();
86
		String line = null;
127
		String line = null;
87
		try {
128
		try {