Subversion Repositories SmartDukaan

Rev

Rev 22215 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
21557 ashik.ali 1
package com.spice.profitmandi.common.web.client;
2
 
3
import java.io.BufferedReader;
4
import java.io.IOException;
5
import java.io.InputStream;
6
import java.io.InputStreamReader;
7
import java.util.Map;
8
import java.util.Set;
9
 
10
import org.apache.http.HttpResponse;
11
import org.apache.http.client.ClientProtocolException;
12
import org.apache.http.client.HttpClient;
13
import org.apache.http.client.methods.HttpGet;
14
import org.apache.http.impl.client.HttpClients;
15
import org.slf4j.Logger;
16
import org.slf4j.LoggerFactory;
17
import org.springframework.http.HttpHeaders;
18
import org.springframework.http.HttpStatus;
19
import org.springframework.http.MediaType;
20
import org.springframework.web.util.UriComponentsBuilder;
21
 
22
import com.spice.profitmandi.common.ResponseCodeHolder;
23
import com.spice.profitmandi.common.enumuration.SchemeType;
24
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
25
import com.spice.profitmandi.common.model.ProfitMandiConstants;
26
 
27
public class RestClient {
28
 
29
	private static final Logger LOGGER = LoggerFactory.getLogger(RestClient.class);
30
 
31
	private HttpClient httpClient;
32
	private SchemeType scheme;
33
	private String hostName;
34
	private int port;
35
	private final String url;
36
	private HttpHeaders headers;
37
	public RestClient(SchemeType scheme, String hostName, int port){
38
		this.scheme = scheme;
39
		this.hostName = hostName;
40
		this.port = port;
41
		this.url = scheme.getValue()==null ? SchemeType.HTTP.toString() : scheme.getValue()  + hostName + ":" + port + "/";
42
		this.headers = new HttpHeaders();
43
		this.httpClient = HttpClients.createDefault();
44
		headers.add("Content-Type", MediaType.APPLICATION_JSON_VALUE);
45
	}
46
	public SchemeType getScheme() {
47
		return scheme;
48
	}
49
	public String getHostName() {
50
		return hostName;
51
	}
52
	public int getPort() {
53
		return port;
54
	}
55
	public String getUrl() {
56
		return url;
57
	}
58
	public String get(String uri, Map<String, String> params)
59
		throws ProfitMandiBusinessException{
60
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url + uri);
61
		Set<String> keys = params.keySet();
62
		for(String key : keys){
63
			builder.queryParam(key, params.get(key));
64
		}
65
		HttpGet request = new HttpGet(builder.build().toUri());
66
		request.setHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE);
67
		LOGGER.info("Connecting to google server for token validation at url {}",request.getURI());
68
		try {
69
			HttpResponse response = httpClient.execute(request);
70
			String responseString = this.toString(response.getEntity().getContent());
71
			LOGGER.info("Got response for token validation with responseCode {} and responseBody {}", response.getStatusLine().getStatusCode(), responseString);
72
			if(response.getStatusLine().getStatusCode() == HttpStatus.OK.value()){
73
				return responseString;
74
			}else{
75
				throw new ProfitMandiBusinessException(ProfitMandiConstants.TOKEN, params.get(ProfitMandiConstants.ACCESS_TOKEN), "USR_1005");
76
			}
77
		} catch (ClientProtocolException e) {
78
			throw new RuntimeException(ResponseCodeHolder.getMessage("USR_1006"));
79
		} catch (IOException e) {
80
			throw new RuntimeException(ResponseCodeHolder.getMessage("USR_1006"));
81
		}
82
	}
83
	private String toString(InputStream inputStream){
84
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
85
		StringBuilder responseString = new StringBuilder();
86
		String line = null;
87
		try {
88
			while((line = reader.readLine()) != null){
89
				responseString.append(line);
90
			}
91
			inputStream.close();
92
		} catch (IOException e) {
93
			throw new RuntimeException();
94
		}
95
		return responseString.toString();
96
	}
97
}