Subversion Repositories SmartDukaan

Rev

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

Rev 25702 Rev 25726
Line 25... Line 25...
25
import org.apache.http.impl.client.HttpClientBuilder;
25
import org.apache.http.impl.client.HttpClientBuilder;
26
import org.apache.http.impl.client.HttpClients;
26
import org.apache.http.impl.client.HttpClients;
27
import org.apache.http.message.BasicNameValuePair;
27
import org.apache.http.message.BasicNameValuePair;
28
import org.apache.logging.log4j.LogManager;
28
import org.apache.logging.log4j.LogManager;
29
import org.apache.logging.log4j.Logger;
29
import org.apache.logging.log4j.Logger;
-
 
30
import org.springframework.beans.factory.annotation.Autowired;
30
import org.springframework.http.HttpStatus;
31
import org.springframework.http.HttpStatus;
31
import org.springframework.stereotype.Component;
32
import org.springframework.stereotype.Component;
32
import org.springframework.web.util.UriComponentsBuilder;
33
import org.springframework.web.util.UriComponentsBuilder;
33
 
34
 
-
 
35
import com.fasterxml.jackson.databind.ObjectMapper;
34
import com.spice.profitmandi.common.ResponseCodeHolder;
36
import com.spice.profitmandi.common.ResponseCodeHolder;
35
import com.spice.profitmandi.common.enumuration.SchemeType;
37
import com.spice.profitmandi.common.enumuration.SchemeType;
36
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
38
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
37
import com.spice.profitmandi.common.util.StringUtils;
39
import com.spice.profitmandi.common.util.StringUtils;
38
 
40
 
Line 41... Line 43...
41
 
43
 
42
	private static final Logger LOGGER = LogManager.getLogger(RestClient.class);
44
	private static final Logger LOGGER = LogManager.getLogger(RestClient.class);
43
 
45
 
44
	private HttpClient httpClient;
46
	private HttpClient httpClient;
45
 
47
 
-
 
48
	@Autowired
-
 
49
	ObjectMapper objectMapper;
-
 
50
 
46
	public RestClient() {
51
	public RestClient() {
47
		this.httpClient = HttpClients.createDefault();
52
		this.httpClient = HttpClients.createDefault();
48
 
53
 
49
	}
54
	}
50
 
55
 
51
	public RestClient(int connectionTimeoutMillis) {
56
	public RestClient(int connectionTimeoutMillis) {
52
		RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5* 1000).build();
57
		RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5 * 1000).build();
53
		this.httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
58
		this.httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
54
 
59
 
55
	}
60
	}
56
 
61
 
57
	public String get(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params,
62
	public String get(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params,
Line 129... Line 134...
129
			String responseString = this.toString(response.getEntity().getContent());
134
			String responseString = this.toString(response.getEntity().getContent());
130
			LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
135
			LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
131
			if (response.getStatusLine().getStatusCode() == HttpStatus.OK.value()) {
136
			if (response.getStatusLine().getStatusCode() == HttpStatus.OK.value()) {
132
				return responseString;
137
				return responseString;
133
			} else {
138
			} else {
-
 
139
				LOGGER.info("Response String {} ", responseString);
134
				throw new ProfitMandiBusinessException("", "", "GE_1005");
140
				throw new ProfitMandiBusinessException("", "", "GE_1005");
135
			}
141
			}
136
		} catch (HttpHostConnectException httpHostConnectException) {
142
		} catch (HttpHostConnectException httpHostConnectException) {
137
			LOGGER.error("Connection Timeout Exception", httpHostConnectException);
143
			LOGGER.error("Connection Timeout Exception", httpHostConnectException);
138
			throw httpHostConnectException;
144
			throw httpHostConnectException;
Line 182... Line 188...
182
		String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
188
		String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
183
				: scheme.getValue() + hostName + ":" + port + "/" + uri;
189
				: scheme.getValue() + hostName + ":" + port + "/" + uri;
184
		return this.postJson(url, params, headers);
190
		return this.postJson(url, params, headers);
185
	}
191
	}
186
 
192
 
187
	public String postJson(String url, Map<String, String> params, Map<String, String> headers)
-
 
188
			throws ProfitMandiBusinessException, HttpHostConnectException {
-
 
189
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
-
 
190
		List<NameValuePair> bodyParameters = new ArrayList<NameValuePair>();
-
 
191
		for (Map.Entry<String, String> entry : params.entrySet()) {
-
 
192
			bodyParameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
-
 
193
		}
-
 
194
 
193
 
-
 
194
	public String postJson(String url, Object object, Map<String, String> headers)
-
 
195
			throws ProfitMandiBusinessException, HttpHostConnectException {
195
		String jsonString = null;
196
		String jsonString;
196
		try {
197
		try {
197
			jsonString = StringUtils.toString(params);
198
			jsonString = objectMapper.writeValueAsString(object);
-
 
199
			LOGGER.info("JSON String - {}", jsonString);
198
		} catch (Exception e) {
200
		} catch(Exception e) {
199
			e.printStackTrace();
201
			e.printStackTrace();
-
 
202
			throw new ProfitMandiBusinessException("Json Object", object.toString(), "Could not write as String");
200
		}
203
		}
201
 
-
 
202
		StringEntity requestEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
204
		StringEntity requestEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
203
 
205
 
204
		LOGGER.info("Body {}", jsonString);
206
		UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
-
 
207
 
205
		HttpPost request = new HttpPost(builder.build().encode().toUri());
208
		HttpPost request = new HttpPost(builder.build().encode().toUri());
206
		for (Map.Entry<String, String> entry : headers.entrySet()) {
209
		for (Map.Entry<String, String> entry : headers.entrySet()) {
207
			request.setHeader(entry.getKey(), entry.getValue());
210
			request.setHeader(entry.getKey(), entry.getValue());
208
		}
211
		}
209
		request.setEntity(requestEntity);
212
		request.setEntity(requestEntity);