| 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;
|
| 22215 |
ashik.ali |
7 |
import java.io.UnsupportedEncodingException;
|
|
|
8 |
import java.util.ArrayList;
|
|
|
9 |
import java.util.List;
|
| 21557 |
ashik.ali |
10 |
import java.util.Map;
|
|
|
11 |
import java.util.Set;
|
|
|
12 |
|
|
|
13 |
import org.apache.http.HttpResponse;
|
| 22215 |
ashik.ali |
14 |
import org.apache.http.NameValuePair;
|
| 21557 |
ashik.ali |
15 |
import org.apache.http.client.ClientProtocolException;
|
|
|
16 |
import org.apache.http.client.HttpClient;
|
| 25244 |
amit.gupta |
17 |
import org.apache.http.client.config.RequestConfig;
|
| 22215 |
ashik.ali |
18 |
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
| 21557 |
ashik.ali |
19 |
import org.apache.http.client.methods.HttpGet;
|
| 29710 |
amit.gupta |
20 |
import org.apache.http.client.methods.HttpPatch;
|
| 22215 |
ashik.ali |
21 |
import org.apache.http.client.methods.HttpPost;
|
|
|
22 |
import org.apache.http.client.methods.HttpUriRequest;
|
| 23502 |
ashik.ali |
23 |
import org.apache.http.conn.HttpHostConnectException;
|
|
|
24 |
import org.apache.http.entity.ContentType;
|
|
|
25 |
import org.apache.http.entity.StringEntity;
|
| 25244 |
amit.gupta |
26 |
import org.apache.http.impl.client.HttpClientBuilder;
|
| 21557 |
ashik.ali |
27 |
import org.apache.http.impl.client.HttpClients;
|
| 22215 |
ashik.ali |
28 |
import org.apache.http.message.BasicNameValuePair;
|
| 25011 |
amit.gupta |
29 |
import org.apache.logging.log4j.LogManager;
|
| 23568 |
govind |
30 |
import org.apache.logging.log4j.Logger;
|
| 25726 |
amit.gupta |
31 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 21557 |
ashik.ali |
32 |
import org.springframework.http.HttpStatus;
|
| 23526 |
ashik.ali |
33 |
import org.springframework.stereotype.Component;
|
| 21557 |
ashik.ali |
34 |
import org.springframework.web.util.UriComponentsBuilder;
|
|
|
35 |
|
| 25726 |
amit.gupta |
36 |
import com.fasterxml.jackson.databind.ObjectMapper;
|
| 21557 |
ashik.ali |
37 |
import com.spice.profitmandi.common.ResponseCodeHolder;
|
|
|
38 |
import com.spice.profitmandi.common.enumuration.SchemeType;
|
|
|
39 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
40 |
|
| 23526 |
ashik.ali |
41 |
@Component
|
| 21557 |
ashik.ali |
42 |
public class RestClient {
|
| 25011 |
amit.gupta |
43 |
|
| 23568 |
govind |
44 |
private static final Logger LOGGER = LogManager.getLogger(RestClient.class);
|
| 25011 |
amit.gupta |
45 |
|
| 21557 |
ashik.ali |
46 |
private HttpClient httpClient;
|
| 25011 |
amit.gupta |
47 |
|
| 25726 |
amit.gupta |
48 |
@Autowired
|
|
|
49 |
ObjectMapper objectMapper;
|
|
|
50 |
|
| 23526 |
ashik.ali |
51 |
public RestClient() {
|
| 21557 |
ashik.ali |
52 |
this.httpClient = HttpClients.createDefault();
|
| 25244 |
amit.gupta |
53 |
|
| 21557 |
ashik.ali |
54 |
}
|
| 25011 |
amit.gupta |
55 |
|
| 25244 |
amit.gupta |
56 |
public RestClient(int connectionTimeoutMillis) {
|
| 25726 |
amit.gupta |
57 |
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5 * 1000).build();
|
| 25244 |
amit.gupta |
58 |
this.httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
|
|
|
59 |
|
|
|
60 |
}
|
|
|
61 |
|
| 25011 |
amit.gupta |
62 |
public String get(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params,
|
|
|
63 |
Map<String, String> headers) throws ProfitMandiBusinessException, HttpHostConnectException {
|
|
|
64 |
String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
|
|
|
65 |
: scheme.getValue() + hostName + ":" + port + "/" + uri;
|
| 23561 |
ashik.ali |
66 |
return this.get(url, params, headers);
|
|
|
67 |
}
|
| 25011 |
amit.gupta |
68 |
|
| 23561 |
ashik.ali |
69 |
public String get(String url, Map<String, String> params, Map<String, String> headers)
|
| 25011 |
amit.gupta |
70 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
| 23526 |
ashik.ali |
71 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
| 28653 |
amit.gupta |
72 |
if(params!=null) {
|
|
|
73 |
Set<String> keys = params.keySet();
|
|
|
74 |
for (String key : keys) {
|
|
|
75 |
builder.queryParam(key, params.get(key));
|
|
|
76 |
}
|
| 21557 |
ashik.ali |
77 |
}
|
| 22345 |
amit.gupta |
78 |
HttpGet request = new HttpGet(builder.build().encode().toUri());
|
| 25011 |
amit.gupta |
79 |
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
| 22339 |
amit.gupta |
80 |
request.setHeader(entry.getKey(), entry.getValue());
|
| 22215 |
ashik.ali |
81 |
}
|
|
|
82 |
return this.execute(request);
|
|
|
83 |
}
|
| 22233 |
amit.gupta |
84 |
|
| 23526 |
ashik.ali |
85 |
public String get(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params)
|
| 25011 |
amit.gupta |
86 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
|
|
87 |
String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
|
|
|
88 |
: scheme.getValue() + hostName + ":" + port + "/" + uri;
|
| 23526 |
ashik.ali |
89 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
| 25011 |
amit.gupta |
90 |
if (params != null) {
|
| 23509 |
amit.gupta |
91 |
Set<String> keys = params.keySet();
|
| 25011 |
amit.gupta |
92 |
for (String key : keys) {
|
| 23509 |
amit.gupta |
93 |
builder.queryParam(key, params.get(key));
|
|
|
94 |
}
|
| 22233 |
amit.gupta |
95 |
}
|
| 22343 |
amit.gupta |
96 |
HttpGet request = new HttpGet(builder.build().encode().toUri());
|
| 22233 |
amit.gupta |
97 |
return this.execute(request);
|
|
|
98 |
}
|
| 25011 |
amit.gupta |
99 |
|
| 23612 |
amit.gupta |
100 |
public HttpResponse getResponse(String url, Map<String, String> params, Map<String, String> headers)
|
|
|
101 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
|
|
102 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
|
|
103 |
Set<String> keys = params.keySet();
|
| 25011 |
amit.gupta |
104 |
for (String key : keys) {
|
| 23612 |
amit.gupta |
105 |
builder.queryParam(key, params.get(key));
|
|
|
106 |
}
|
|
|
107 |
HttpGet request = new HttpGet(builder.build().encode().toUri());
|
| 27179 |
amit.gupta |
108 |
if (headers != null) {
|
| 26078 |
amit.gupta |
109 |
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
|
|
110 |
request.setHeader(entry.getKey(), entry.getValue());
|
|
|
111 |
}
|
| 23612 |
amit.gupta |
112 |
}
|
|
|
113 |
try {
|
|
|
114 |
LOGGER.info("Request uri is {}", request.getURI().toString());
|
|
|
115 |
HttpResponse response = httpClient.execute(request);
|
|
|
116 |
LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
|
| 25011 |
amit.gupta |
117 |
if (response.getStatusLine().getStatusCode() == HttpStatus.OK.value()) {
|
| 23612 |
amit.gupta |
118 |
return response;
|
| 25011 |
amit.gupta |
119 |
} else {
|
| 23612 |
amit.gupta |
120 |
throw new ProfitMandiBusinessException("", "", "GE_1005");
|
|
|
121 |
}
|
| 25011 |
amit.gupta |
122 |
} catch (HttpHostConnectException httpHostConnectException) {
|
| 23612 |
amit.gupta |
123 |
LOGGER.error("Connection Timeout Exception", httpHostConnectException);
|
|
|
124 |
throw httpHostConnectException;
|
|
|
125 |
} catch (ClientProtocolException e) {
|
| 25011 |
amit.gupta |
126 |
LOGGER.error("Client Error : ", e);
|
| 23612 |
amit.gupta |
127 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
|
|
128 |
} catch (IOException e) {
|
|
|
129 |
LOGGER.error("IO Error : ", e);
|
|
|
130 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
|
|
131 |
}
|
|
|
132 |
}
|
| 25011 |
amit.gupta |
133 |
|
|
|
134 |
public String execute(HttpUriRequest request) throws ProfitMandiBusinessException, HttpHostConnectException {
|
|
|
135 |
LOGGER.info("Connecting to server at url {}", request.getURI());
|
| 21557 |
ashik.ali |
136 |
try {
|
|
|
137 |
HttpResponse response = httpClient.execute(request);
|
|
|
138 |
String responseString = this.toString(response.getEntity().getContent());
|
| 22877 |
amit.gupta |
139 |
LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
|
| 25011 |
amit.gupta |
140 |
if (response.getStatusLine().getStatusCode() == HttpStatus.OK.value()) {
|
| 21557 |
ashik.ali |
141 |
return responseString;
|
| 25011 |
amit.gupta |
142 |
} else {
|
| 25726 |
amit.gupta |
143 |
LOGGER.info("Response String {} ", responseString);
|
| 22215 |
ashik.ali |
144 |
throw new ProfitMandiBusinessException("", "", "GE_1005");
|
| 21557 |
ashik.ali |
145 |
}
|
| 25011 |
amit.gupta |
146 |
} catch (HttpHostConnectException httpHostConnectException) {
|
| 23502 |
ashik.ali |
147 |
LOGGER.error("Connection Timeout Exception", httpHostConnectException);
|
|
|
148 |
throw httpHostConnectException;
|
| 21557 |
ashik.ali |
149 |
} catch (ClientProtocolException e) {
|
| 25011 |
amit.gupta |
150 |
LOGGER.error("Client Error : ", e);
|
| 22215 |
ashik.ali |
151 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
| 21557 |
ashik.ali |
152 |
} catch (IOException e) {
|
| 22215 |
ashik.ali |
153 |
LOGGER.error("IO Error : ", e);
|
|
|
154 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
| 25011 |
amit.gupta |
155 |
}
|
| 21557 |
ashik.ali |
156 |
}
|
| 25011 |
amit.gupta |
157 |
|
| 27179 |
amit.gupta |
158 |
public String executeJson(HttpUriRequest request) throws ProfitMandiBusinessException, HttpHostConnectException {
|
|
|
159 |
LOGGER.info("Connecting to server at url {}", request.getURI());
|
|
|
160 |
try {
|
|
|
161 |
HttpResponse response = httpClient.execute(request);
|
|
|
162 |
String responseString = this.toString(response.getEntity().getContent());
|
|
|
163 |
LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
|
| 29496 |
amit.gupta |
164 |
LOGGER.info("Response String", responseString);
|
| 27179 |
amit.gupta |
165 |
return responseString;
|
|
|
166 |
} catch (HttpHostConnectException httpHostConnectException) {
|
|
|
167 |
LOGGER.error("Connection Timeout Exception", httpHostConnectException);
|
|
|
168 |
throw httpHostConnectException;
|
|
|
169 |
} catch (ClientProtocolException e) {
|
|
|
170 |
LOGGER.error("Client Error : ", e);
|
|
|
171 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
|
|
172 |
} catch (IOException e) {
|
|
|
173 |
LOGGER.error("IO Error : ", e);
|
|
|
174 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
|
|
175 |
}
|
|
|
176 |
}
|
|
|
177 |
|
| 25011 |
amit.gupta |
178 |
public String post(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params,
|
|
|
179 |
Map<String, String> headers) throws ProfitMandiBusinessException, HttpHostConnectException {
|
|
|
180 |
String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
|
|
|
181 |
: scheme.getValue() + hostName + ":" + port + "/" + uri;
|
| 23561 |
ashik.ali |
182 |
return this.post(url, params, headers);
|
|
|
183 |
}
|
| 25011 |
amit.gupta |
184 |
|
| 23561 |
ashik.ali |
185 |
public String post(String url, Map<String, String> params, Map<String, String> headers)
|
| 25011 |
amit.gupta |
186 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
| 25244 |
amit.gupta |
187 |
// UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
| 22215 |
ashik.ali |
188 |
List<NameValuePair> bodyParameters = new ArrayList<NameValuePair>();
|
| 25011 |
amit.gupta |
189 |
for (Map.Entry<String, String> entry : params.entrySet()) {
|
| 22215 |
ashik.ali |
190 |
bodyParameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
|
|
|
191 |
}
|
| 25011 |
amit.gupta |
192 |
|
| 22215 |
ashik.ali |
193 |
LOGGER.info("Body Parameters {}", params);
|
| 25011 |
amit.gupta |
194 |
HttpPost request = new HttpPost(url);
|
|
|
195 |
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
| 22215 |
ashik.ali |
196 |
request.setHeader(entry.getKey(), entry.getValue());
|
|
|
197 |
}
|
| 25011 |
amit.gupta |
198 |
|
|
|
199 |
try {
|
| 22215 |
ashik.ali |
200 |
request.setEntity(new UrlEncodedFormEntity(bodyParameters));
|
| 25011 |
amit.gupta |
201 |
} catch (UnsupportedEncodingException unsupportedEncodingException) {
|
| 22215 |
ashik.ali |
202 |
LOGGER.error("Encoding error : ", unsupportedEncodingException);
|
|
|
203 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
|
|
204 |
}
|
| 25011 |
amit.gupta |
205 |
|
| 22215 |
ashik.ali |
206 |
return this.execute(request);
|
| 25011 |
amit.gupta |
207 |
|
| 22215 |
ashik.ali |
208 |
}
|
| 25011 |
amit.gupta |
209 |
|
| 28653 |
amit.gupta |
210 |
public String post(String url, String body, Map<String, String> headers)
|
|
|
211 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
|
|
212 |
HttpPost request = new HttpPost(url);
|
|
|
213 |
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
|
|
214 |
request.setHeader(entry.getKey(), entry.getValue());
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
try {
|
|
|
218 |
request.setEntity(new StringEntity(body));
|
|
|
219 |
} catch (UnsupportedEncodingException unsupportedEncodingException) {
|
|
|
220 |
LOGGER.error("Encoding error : ", unsupportedEncodingException);
|
|
|
221 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
return this.execute(request);
|
|
|
225 |
|
|
|
226 |
}
|
|
|
227 |
|
| 25726 |
amit.gupta |
228 |
public String postJson(String url, Object object, Map<String, String> headers)
|
| 25011 |
amit.gupta |
229 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
| 25726 |
amit.gupta |
230 |
String jsonString;
|
| 23502 |
ashik.ali |
231 |
try {
|
| 28653 |
amit.gupta |
232 |
if (object.getClass().equals(String.class)) {
|
|
|
233 |
jsonString = (String) object;
|
|
|
234 |
} else {
|
|
|
235 |
jsonString = objectMapper.writeValueAsString(object);
|
|
|
236 |
}
|
| 25726 |
amit.gupta |
237 |
LOGGER.info("JSON String - {}", jsonString);
|
| 27179 |
amit.gupta |
238 |
} catch (Exception e) {
|
| 23502 |
ashik.ali |
239 |
e.printStackTrace();
|
| 25726 |
amit.gupta |
240 |
throw new ProfitMandiBusinessException("Json Object", object.toString(), "Could not write as String");
|
| 23502 |
ashik.ali |
241 |
}
|
| 23561 |
ashik.ali |
242 |
StringEntity requestEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
|
| 25011 |
amit.gupta |
243 |
|
| 25726 |
amit.gupta |
244 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
|
|
245 |
|
| 23502 |
ashik.ali |
246 |
HttpPost request = new HttpPost(builder.build().encode().toUri());
|
| 25011 |
amit.gupta |
247 |
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
| 23502 |
ashik.ali |
248 |
request.setHeader(entry.getKey(), entry.getValue());
|
|
|
249 |
}
|
|
|
250 |
request.setEntity(requestEntity);
|
| 27179 |
amit.gupta |
251 |
return this.executeJson(request);
|
| 23502 |
ashik.ali |
252 |
}
|
| 29710 |
amit.gupta |
253 |
|
|
|
254 |
public String patchJson(String url, Object object, Map<String, String> headers)
|
|
|
255 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
|
|
256 |
String jsonString;
|
|
|
257 |
try {
|
|
|
258 |
if (object.getClass().equals(String.class)) {
|
|
|
259 |
jsonString = (String) object;
|
|
|
260 |
} else {
|
|
|
261 |
jsonString = objectMapper.writeValueAsString(object);
|
|
|
262 |
}
|
|
|
263 |
LOGGER.info("JSON String - {}", jsonString);
|
|
|
264 |
} catch (Exception e) {
|
|
|
265 |
e.printStackTrace();
|
|
|
266 |
throw new ProfitMandiBusinessException("Json Object", object.toString(), "Could not write as String");
|
|
|
267 |
}
|
|
|
268 |
StringEntity requestEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
|
|
|
269 |
|
|
|
270 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
|
|
271 |
|
|
|
272 |
HttpPatch request = new HttpPatch(builder.build().encode().toUri());
|
|
|
273 |
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
|
|
274 |
request.setHeader(entry.getKey(), entry.getValue());
|
|
|
275 |
}
|
|
|
276 |
request.setEntity(requestEntity);
|
|
|
277 |
return this.executeJson(request);
|
|
|
278 |
}
|
| 25011 |
amit.gupta |
279 |
|
|
|
280 |
private String toString(InputStream inputStream) {
|
| 21557 |
ashik.ali |
281 |
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
|
|
282 |
StringBuilder responseString = new StringBuilder();
|
|
|
283 |
String line = null;
|
|
|
284 |
try {
|
| 25011 |
amit.gupta |
285 |
while ((line = reader.readLine()) != null) {
|
| 21557 |
ashik.ali |
286 |
responseString.append(line);
|
|
|
287 |
}
|
|
|
288 |
inputStream.close();
|
|
|
289 |
} catch (IOException e) {
|
|
|
290 |
throw new RuntimeException();
|
| 26656 |
amit.gupta |
291 |
} finally {
|
| 27179 |
amit.gupta |
292 |
|
| 21557 |
ashik.ali |
293 |
}
|
|
|
294 |
return responseString.toString();
|
|
|
295 |
}
|
|
|
296 |
}
|