Subversion Repositories SmartDukaan

Rev

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

Rev 22877 Rev 23502
Line 16... Line 16...
16
import org.apache.http.client.HttpClient;
16
import org.apache.http.client.HttpClient;
17
import org.apache.http.client.entity.UrlEncodedFormEntity;
17
import org.apache.http.client.entity.UrlEncodedFormEntity;
18
import org.apache.http.client.methods.HttpGet;
18
import org.apache.http.client.methods.HttpGet;
19
import org.apache.http.client.methods.HttpPost;
19
import org.apache.http.client.methods.HttpPost;
20
import org.apache.http.client.methods.HttpUriRequest;
20
import org.apache.http.client.methods.HttpUriRequest;
-
 
21
import org.apache.http.conn.HttpHostConnectException;
-
 
22
import org.apache.http.entity.ContentType;
-
 
23
import org.apache.http.entity.StringEntity;
21
import org.apache.http.impl.client.HttpClients;
24
import org.apache.http.impl.client.HttpClients;
22
import org.apache.http.message.BasicNameValuePair;
25
import org.apache.http.message.BasicNameValuePair;
23
import org.slf4j.Logger;
26
import org.slf4j.Logger;
24
import org.slf4j.LoggerFactory;
27
import org.slf4j.LoggerFactory;
25
import org.springframework.http.HttpStatus;
28
import org.springframework.http.HttpStatus;
26
import org.springframework.web.util.UriComponentsBuilder;
29
import org.springframework.web.util.UriComponentsBuilder;
27
 
30
 
28
import com.spice.profitmandi.common.ResponseCodeHolder;
31
import com.spice.profitmandi.common.ResponseCodeHolder;
29
import com.spice.profitmandi.common.enumuration.SchemeType;
32
import com.spice.profitmandi.common.enumuration.SchemeType;
30
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
33
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
-
 
34
import com.spice.profitmandi.common.util.StringUtils;
31
 
35
 
32
public class RestClient {
36
public class RestClient {
33
	
37
	
34
	private static final Logger LOGGER = LoggerFactory.getLogger(RestClient.class);
38
	private static final Logger LOGGER = LoggerFactory.getLogger(RestClient.class);
35
	
39
	
Line 41... Line 45...
41
	//private HttpHeaders headers;
45
	//private HttpHeaders headers;
42
	public RestClient(SchemeType scheme, String hostName, int port){
46
	public RestClient(SchemeType scheme, String hostName, int port){
43
		this.scheme = scheme;
47
		this.scheme = scheme;
44
		this.hostName = hostName;
48
		this.hostName = hostName;
45
		this.port = port;
49
		this.port = port;
46
		this.url = scheme.getValue()==null ? SchemeType.HTTP.toString() : scheme.getValue()  + hostName + ":" + port + "/";
50
		this.url = scheme.getValue() == null ? SchemeType.HTTP.toString() : scheme.getValue()  + hostName + ":" + port + "/";
47
		//this.headers = new HttpHeaders();
51
		//this.headers = new HttpHeaders();
48
		this.httpClient = HttpClients.createDefault();
52
		this.httpClient = HttpClients.createDefault();
49
		//headers.add("Content-Type", MediaType.APPLICATION_JSON_VALUE);
53
		//headers.add("Content-Type", MediaType.APPLICATION_JSON_VALUE);
50
	}
54
	}
51
	public SchemeType getScheme() {
55
	public SchemeType getScheme() {
Line 59... Line 63...
59
	}
63
	}
60
	public String getUrl() {
64
	public String getUrl() {
61
		return url;
65
		return url;
62
	}
66
	}
63
	public String get(String uri, Map<String, String> params, Map<String, String> headers)
67
	public String get(String uri, Map<String, String> params, Map<String, String> headers)
64
		throws ProfitMandiBusinessException{
68
		throws ProfitMandiBusinessException, HttpHostConnectException{
65
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url + uri);
69
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url + uri);
66
		Set<String> keys = params.keySet();
70
		Set<String> keys = params.keySet();
67
		for(String key : keys){
71
		for(String key : keys){
68
			builder.queryParam(key, params.get(key));
72
			builder.queryParam(key, params.get(key));
69
		}
73
		}
Line 73... Line 77...
73
		}
77
		}
74
		return this.execute(request);
78
		return this.execute(request);
75
	}
79
	}
76
 
80
 
77
	public String get(String uri, Map<String, String> params)
81
	public String get(String uri, Map<String, String> params)
78
			throws ProfitMandiBusinessException{
82
			throws ProfitMandiBusinessException, HttpHostConnectException{
79
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url + uri);
83
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url + uri);
80
		Set<String> keys = params.keySet();
84
		Set<String> keys = params.keySet();
81
		for(String key : keys){
85
		for(String key : keys){
82
			builder.queryParam(key, params.get(key));
86
			builder.queryParam(key, params.get(key));
83
		}
87
		}
84
		HttpGet request = new HttpGet(builder.build().encode().toUri());
88
		HttpGet request = new HttpGet(builder.build().encode().toUri());
85
		return this.execute(request);
89
		return this.execute(request);
86
	}
90
	}
87
	
91
	
88
	public String execute(HttpUriRequest request)
92
	public String execute(HttpUriRequest request)
89
			throws ProfitMandiBusinessException{
93
			throws ProfitMandiBusinessException, HttpHostConnectException{
90
		LOGGER.info("Connecting to server at url {}",request.getURI());
94
		LOGGER.info("Connecting to server at url {}",request.getURI());
91
		try {
95
		try {
92
			HttpResponse response = httpClient.execute(request);
96
			HttpResponse response = httpClient.execute(request);
93
			String responseString = this.toString(response.getEntity().getContent());
97
			String responseString = this.toString(response.getEntity().getContent());
94
			LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
98
			LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
95
			if(response.getStatusLine().getStatusCode() == HttpStatus.OK.value()){
99
			if(response.getStatusLine().getStatusCode() == HttpStatus.OK.value()){
96
				return responseString;
100
				return responseString;
97
			}else{
101
			}else{
98
				throw new ProfitMandiBusinessException("", "", "GE_1005");
102
				throw new ProfitMandiBusinessException("", "", "GE_1005");
99
			}
103
			}
-
 
104
		}catch(HttpHostConnectException httpHostConnectException) {
-
 
105
			LOGGER.error("Connection Timeout Exception", httpHostConnectException);
-
 
106
			throw httpHostConnectException;
100
		} catch (ClientProtocolException e) {
107
		} catch (ClientProtocolException e) {
101
			LOGGER.error("Client Error : ",e);
108
			LOGGER.error("Client Error : ",e);
102
			throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
109
			throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
103
		} catch (IOException e) {
110
		} catch (IOException e) {
104
			LOGGER.error("IO Error : ", e);
111
			LOGGER.error("IO Error : ", e);
105
			throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
112
			throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
106
		}
113
		} 
107
	}
114
	}
108
	
115
	
109
	public String post(String uri, Map<String, String> params, Map<String, String> headers)
116
	public String post(String uri, Map<String, String> params, Map<String, String> headers)
110
			throws ProfitMandiBusinessException{
117
			throws ProfitMandiBusinessException, HttpHostConnectException{
111
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url + uri);
118
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url + uri);
112
		List<NameValuePair> bodyParameters = new ArrayList<NameValuePair>();
119
		List<NameValuePair> bodyParameters = new ArrayList<NameValuePair>();
113
		for(Map.Entry<String, String> entry : params.entrySet()){
120
		for(Map.Entry<String, String> entry : params.entrySet()){
114
			bodyParameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
121
			bodyParameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
115
		}
122
		}
Line 129... Line 136...
129
		
136
		
130
		
137
		
131
		return this.execute(request);
138
		return this.execute(request);
132
		
139
		
133
	}
140
	}
-
 
141
	
-
 
142
	public String postJson(String uri, Map<String, String> params, Map<String, String> headers)
-
 
143
			throws ProfitMandiBusinessException, HttpHostConnectException{
-
 
144
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url + uri);
-
 
145
		List<NameValuePair> bodyParameters = new ArrayList<NameValuePair>();
-
 
146
		for(Map.Entry<String, String> entry : params.entrySet()){
-
 
147
			bodyParameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
-
 
148
		}
-
 
149
		
-
 
150
		String jsonString = null;
-
 
151
		try {
-
 
152
			jsonString = StringUtils.toString(params);
-
 
153
		} catch (Exception e) {
-
 
154
			e.printStackTrace();
-
 
155
		}
-
 
156
		
-
 
157
		
-
 
158
		StringEntity requestEntity = new StringEntity(
-
 
159
			    jsonString,
-
 
160
			    ContentType.APPLICATION_JSON);
-
 
161
		
-
 
162
		LOGGER.info("Body {}", jsonString);
-
 
163
		HttpPost request = new HttpPost(builder.build().encode().toUri());
-
 
164
		for(Map.Entry<String, String> entry : headers.entrySet()){
-
 
165
			request.setHeader(entry.getKey(), entry.getValue());
-
 
166
		}
-
 
167
		
-
 
168
		request.setEntity(requestEntity);
-
 
169
		
-
 
170
		
-
 
171
		
-
 
172
		return this.execute(request);
-
 
173
		
-
 
174
	}
134
	
175
	
135
	private String toString(InputStream inputStream){
176
	private String toString(InputStream inputStream){
136
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
177
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
137
		StringBuilder responseString = new StringBuilder();
178
		StringBuilder responseString = new StringBuilder();
138
		String line = null;
179
		String line = null;