Subversion Repositories SmartDukaan

Rev

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

Rev 29710 Rev 29834
Line 36... Line 36...
36
import com.fasterxml.jackson.databind.ObjectMapper;
36
import com.fasterxml.jackson.databind.ObjectMapper;
37
import com.spice.profitmandi.common.ResponseCodeHolder;
37
import com.spice.profitmandi.common.ResponseCodeHolder;
38
import com.spice.profitmandi.common.enumuration.SchemeType;
38
import com.spice.profitmandi.common.enumuration.SchemeType;
39
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
39
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
40
 
40
 
-
 
41
import com.spice.profitmandi.common.model.MandiiResponse;
-
 
42
 
41
@Component
43
@Component
42
public class RestClient {
44
public class RestClient {
43
 
45
 
44
	private static final Logger LOGGER = LogManager.getLogger(RestClient.class);
46
	private static final Logger LOGGER = LogManager.getLogger(RestClient.class);
45
 
47
 
Line 67... Line 69...
67
	}
69
	}
68
 
70
 
69
	public String get(String url, Map<String, String> params, Map<String, String> headers)
71
	public String get(String url, Map<String, String> params, Map<String, String> headers)
70
			throws ProfitMandiBusinessException, HttpHostConnectException {
72
			throws ProfitMandiBusinessException, HttpHostConnectException {
71
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
73
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
72
		if(params!=null) {
74
		if (params != null) {
73
			Set<String> keys = params.keySet();
75
			Set<String> keys = params.keySet();
74
			for (String key : keys) {
76
			for (String key : keys) {
75
				builder.queryParam(key, params.get(key));
77
				builder.queryParam(key, params.get(key));
76
			}
78
			}
77
		}
79
		}
Line 80... Line 82...
80
			request.setHeader(entry.getKey(), entry.getValue());
82
			request.setHeader(entry.getKey(), entry.getValue());
81
		}
83
		}
82
		return this.execute(request);
84
		return this.execute(request);
83
	}
85
	}
84
 
86
 
-
 
87
	public MandiiResponse getMandii(String url, Map<String, String> params, Map<String, String> headers)
-
 
88
			throws ProfitMandiBusinessException, HttpHostConnectException {
-
 
89
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
-
 
90
		if (params != null) {
-
 
91
			Set<String> keys = params.keySet();
-
 
92
			for (String key : keys) {
-
 
93
				builder.queryParam(key, params.get(key));
-
 
94
			}
-
 
95
		}
-
 
96
		HttpGet request = new HttpGet(builder.build().encode().toUri());
-
 
97
		for (Map.Entry<String, String> entry : headers.entrySet()) {
-
 
98
			request.setHeader(entry.getKey(), entry.getValue());
-
 
99
		}
-
 
100
		return this.executeMandii(request);
-
 
101
	}
-
 
102
 
85
	public String get(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params)
103
	public String get(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params)
86
			throws ProfitMandiBusinessException, HttpHostConnectException {
104
			throws ProfitMandiBusinessException, HttpHostConnectException {
87
		String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
105
		String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
88
				: scheme.getValue() + hostName + ":" + port + "/" + uri;
106
				: scheme.getValue() + hostName + ":" + port + "/" + uri;
89
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
107
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
Line 153... Line 171...
153
			LOGGER.error("IO Error : ", e);
171
			LOGGER.error("IO Error : ", e);
154
			throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
172
			throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
155
		}
173
		}
156
	}
174
	}
157
 
175
 
-
 
176
	public MandiiResponse executeMandii(HttpUriRequest request)
-
 
177
			throws ProfitMandiBusinessException, HttpHostConnectException {
-
 
178
		LOGGER.info("Connecting to server at url {}", request.getURI());
-
 
179
		try {
-
 
180
			HttpResponse response = httpClient.execute(request);
-
 
181
			String responseString = this.toString(response.getEntity().getContent());
-
 
182
			LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
-
 
183
 
-
 
184
			MandiiResponse mandiiResponse = new MandiiResponse();
-
 
185
			mandiiResponse.setResponseString(responseString);
-
 
186
			mandiiResponse.setStatusCode(response.getStatusLine().getStatusCode());
-
 
187
 
-
 
188
			return mandiiResponse;
-
 
189
		} catch (HttpHostConnectException httpHostConnectException) {
-
 
190
			LOGGER.error("Connection Timeout Exception", httpHostConnectException);
-
 
191
			throw httpHostConnectException;
-
 
192
		} catch (ClientProtocolException e) {
-
 
193
			LOGGER.error("Client Error : ", e);
-
 
194
			throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
-
 
195
		} catch (IOException e) {
-
 
196
			LOGGER.error("IO Error : ", e);
-
 
197
			throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
-
 
198
		}
-
 
199
	}
-
 
200
 
158
	public String executeJson(HttpUriRequest request) throws ProfitMandiBusinessException, HttpHostConnectException {
201
	public String executeJson(HttpUriRequest request) throws ProfitMandiBusinessException, HttpHostConnectException {
159
		LOGGER.info("Connecting to server at url {}", request.getURI());
202
		LOGGER.info("Connecting to server at url {}", request.getURI());
160
		try {
203
		try {
161
			HttpResponse response = httpClient.execute(request);
204
			HttpResponse response = httpClient.execute(request);
162
			String responseString = this.toString(response.getEntity().getContent());
205
			String responseString = this.toString(response.getEntity().getContent());
Line 248... Line 291...
248
			request.setHeader(entry.getKey(), entry.getValue());
291
			request.setHeader(entry.getKey(), entry.getValue());
249
		}
292
		}
250
		request.setEntity(requestEntity);
293
		request.setEntity(requestEntity);
251
		return this.executeJson(request);
294
		return this.executeJson(request);
252
	}
295
	}
253
	
296
 
254
	public String patchJson(String url, Object object, Map<String, String> headers)
297
	public String patchJson(String url, Object object, Map<String, String> headers)
255
			throws ProfitMandiBusinessException, HttpHostConnectException {
298
			throws ProfitMandiBusinessException, HttpHostConnectException {
256
		String jsonString;
299
		String jsonString;
257
		try {
300
		try {
258
			if (object.getClass().equals(String.class)) {
301
			if (object.getClass().equals(String.class)) {
Line 264... Line 307...
264
		} catch (Exception e) {
307
		} catch (Exception e) {
265
			e.printStackTrace();
308
			e.printStackTrace();
266
			throw new ProfitMandiBusinessException("Json Object", object.toString(), "Could not write as String");
309
			throw new ProfitMandiBusinessException("Json Object", object.toString(), "Could not write as String");
267
		}
310
		}
268
		StringEntity requestEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
311
		StringEntity requestEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
269
		
312
 
270
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
313
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
271
		
314
 
272
		HttpPatch request = new HttpPatch(builder.build().encode().toUri());
315
		HttpPatch request = new HttpPatch(builder.build().encode().toUri());
273
		for (Map.Entry<String, String> entry : headers.entrySet()) {
316
		for (Map.Entry<String, String> entry : headers.entrySet()) {
274
			request.setHeader(entry.getKey(), entry.getValue());
317
			request.setHeader(entry.getKey(), entry.getValue());
275
		}
318
		}
276
		request.setEntity(requestEntity);
319
		request.setEntity(requestEntity);