| 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;
|
| 22215 |
ashik.ali |
20 |
import org.apache.http.client.methods.HttpPost;
|
|
|
21 |
import org.apache.http.client.methods.HttpUriRequest;
|
| 23502 |
ashik.ali |
22 |
import org.apache.http.conn.HttpHostConnectException;
|
|
|
23 |
import org.apache.http.entity.ContentType;
|
|
|
24 |
import org.apache.http.entity.StringEntity;
|
| 25244 |
amit.gupta |
25 |
import org.apache.http.impl.client.HttpClientBuilder;
|
| 21557 |
ashik.ali |
26 |
import org.apache.http.impl.client.HttpClients;
|
| 22215 |
ashik.ali |
27 |
import org.apache.http.message.BasicNameValuePair;
|
| 25011 |
amit.gupta |
28 |
import org.apache.logging.log4j.LogManager;
|
| 23568 |
govind |
29 |
import org.apache.logging.log4j.Logger;
|
| 21557 |
ashik.ali |
30 |
import org.springframework.http.HttpStatus;
|
| 23526 |
ashik.ali |
31 |
import org.springframework.stereotype.Component;
|
| 21557 |
ashik.ali |
32 |
import org.springframework.web.util.UriComponentsBuilder;
|
|
|
33 |
|
|
|
34 |
import com.spice.profitmandi.common.ResponseCodeHolder;
|
|
|
35 |
import com.spice.profitmandi.common.enumuration.SchemeType;
|
|
|
36 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 23502 |
ashik.ali |
37 |
import com.spice.profitmandi.common.util.StringUtils;
|
| 21557 |
ashik.ali |
38 |
|
| 23526 |
ashik.ali |
39 |
@Component
|
| 21557 |
ashik.ali |
40 |
public class RestClient {
|
| 25011 |
amit.gupta |
41 |
|
| 23568 |
govind |
42 |
private static final Logger LOGGER = LogManager.getLogger(RestClient.class);
|
| 25011 |
amit.gupta |
43 |
|
| 21557 |
ashik.ali |
44 |
private HttpClient httpClient;
|
| 25011 |
amit.gupta |
45 |
|
| 23526 |
ashik.ali |
46 |
public RestClient() {
|
| 21557 |
ashik.ali |
47 |
this.httpClient = HttpClients.createDefault();
|
| 25244 |
amit.gupta |
48 |
|
| 21557 |
ashik.ali |
49 |
}
|
| 25011 |
amit.gupta |
50 |
|
| 25244 |
amit.gupta |
51 |
public RestClient(int connectionTimeoutMillis) {
|
|
|
52 |
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5* 1000).build();
|
|
|
53 |
this.httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
|
|
|
54 |
|
|
|
55 |
}
|
|
|
56 |
|
| 25011 |
amit.gupta |
57 |
public String get(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params,
|
|
|
58 |
Map<String, String> headers) throws ProfitMandiBusinessException, HttpHostConnectException {
|
|
|
59 |
String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
|
|
|
60 |
: scheme.getValue() + hostName + ":" + port + "/" + uri;
|
| 23561 |
ashik.ali |
61 |
return this.get(url, params, headers);
|
|
|
62 |
}
|
| 25011 |
amit.gupta |
63 |
|
| 23561 |
ashik.ali |
64 |
public String get(String url, Map<String, String> params, Map<String, String> headers)
|
| 25011 |
amit.gupta |
65 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
| 23526 |
ashik.ali |
66 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
| 21557 |
ashik.ali |
67 |
Set<String> keys = params.keySet();
|
| 25011 |
amit.gupta |
68 |
for (String key : keys) {
|
| 22341 |
amit.gupta |
69 |
builder.queryParam(key, params.get(key));
|
| 21557 |
ashik.ali |
70 |
}
|
| 22345 |
amit.gupta |
71 |
HttpGet request = new HttpGet(builder.build().encode().toUri());
|
| 25011 |
amit.gupta |
72 |
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
| 22339 |
amit.gupta |
73 |
request.setHeader(entry.getKey(), entry.getValue());
|
| 22215 |
ashik.ali |
74 |
}
|
|
|
75 |
return this.execute(request);
|
|
|
76 |
}
|
| 22233 |
amit.gupta |
77 |
|
| 23526 |
ashik.ali |
78 |
public String get(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params)
|
| 25011 |
amit.gupta |
79 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
|
|
80 |
String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
|
|
|
81 |
: scheme.getValue() + hostName + ":" + port + "/" + uri;
|
| 23526 |
ashik.ali |
82 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
| 25011 |
amit.gupta |
83 |
if (params != null) {
|
| 23509 |
amit.gupta |
84 |
Set<String> keys = params.keySet();
|
| 25011 |
amit.gupta |
85 |
for (String key : keys) {
|
| 23509 |
amit.gupta |
86 |
builder.queryParam(key, params.get(key));
|
|
|
87 |
}
|
| 22233 |
amit.gupta |
88 |
}
|
| 22343 |
amit.gupta |
89 |
HttpGet request = new HttpGet(builder.build().encode().toUri());
|
| 22233 |
amit.gupta |
90 |
return this.execute(request);
|
|
|
91 |
}
|
| 25011 |
amit.gupta |
92 |
|
| 23612 |
amit.gupta |
93 |
public HttpResponse getResponse(String url, Map<String, String> params, Map<String, String> headers)
|
|
|
94 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
|
|
95 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
|
|
96 |
Set<String> keys = params.keySet();
|
| 25011 |
amit.gupta |
97 |
for (String key : keys) {
|
| 23612 |
amit.gupta |
98 |
builder.queryParam(key, params.get(key));
|
|
|
99 |
}
|
|
|
100 |
HttpGet request = new HttpGet(builder.build().encode().toUri());
|
| 25011 |
amit.gupta |
101 |
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
| 23612 |
amit.gupta |
102 |
request.setHeader(entry.getKey(), entry.getValue());
|
|
|
103 |
}
|
|
|
104 |
try {
|
|
|
105 |
LOGGER.info("Request uri is {}", request.getURI().toString());
|
|
|
106 |
HttpResponse response = httpClient.execute(request);
|
|
|
107 |
LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
|
| 25011 |
amit.gupta |
108 |
if (response.getStatusLine().getStatusCode() == HttpStatus.OK.value()) {
|
| 23612 |
amit.gupta |
109 |
return response;
|
| 25011 |
amit.gupta |
110 |
} else {
|
| 23612 |
amit.gupta |
111 |
throw new ProfitMandiBusinessException("", "", "GE_1005");
|
|
|
112 |
}
|
| 25011 |
amit.gupta |
113 |
} catch (HttpHostConnectException httpHostConnectException) {
|
| 23612 |
amit.gupta |
114 |
LOGGER.error("Connection Timeout Exception", httpHostConnectException);
|
|
|
115 |
throw httpHostConnectException;
|
|
|
116 |
} catch (ClientProtocolException e) {
|
| 25011 |
amit.gupta |
117 |
LOGGER.error("Client Error : ", e);
|
| 23612 |
amit.gupta |
118 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
|
|
119 |
} catch (IOException e) {
|
|
|
120 |
LOGGER.error("IO Error : ", e);
|
|
|
121 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
|
|
122 |
}
|
|
|
123 |
}
|
| 25011 |
amit.gupta |
124 |
|
|
|
125 |
public String execute(HttpUriRequest request) throws ProfitMandiBusinessException, HttpHostConnectException {
|
|
|
126 |
LOGGER.info("Connecting to server at url {}", request.getURI());
|
| 21557 |
ashik.ali |
127 |
try {
|
|
|
128 |
HttpResponse response = httpClient.execute(request);
|
|
|
129 |
String responseString = this.toString(response.getEntity().getContent());
|
| 22877 |
amit.gupta |
130 |
LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
|
| 25011 |
amit.gupta |
131 |
if (response.getStatusLine().getStatusCode() == HttpStatus.OK.value()) {
|
| 21557 |
ashik.ali |
132 |
return responseString;
|
| 25011 |
amit.gupta |
133 |
} else {
|
| 22215 |
ashik.ali |
134 |
throw new ProfitMandiBusinessException("", "", "GE_1005");
|
| 21557 |
ashik.ali |
135 |
}
|
| 25011 |
amit.gupta |
136 |
} catch (HttpHostConnectException httpHostConnectException) {
|
| 23502 |
ashik.ali |
137 |
LOGGER.error("Connection Timeout Exception", httpHostConnectException);
|
|
|
138 |
throw httpHostConnectException;
|
| 21557 |
ashik.ali |
139 |
} catch (ClientProtocolException e) {
|
| 25011 |
amit.gupta |
140 |
LOGGER.error("Client Error : ", e);
|
| 22215 |
ashik.ali |
141 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
| 21557 |
ashik.ali |
142 |
} catch (IOException e) {
|
| 22215 |
ashik.ali |
143 |
LOGGER.error("IO Error : ", e);
|
|
|
144 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
| 25011 |
amit.gupta |
145 |
}
|
| 21557 |
ashik.ali |
146 |
}
|
| 25011 |
amit.gupta |
147 |
|
|
|
148 |
public String post(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params,
|
|
|
149 |
Map<String, String> headers) throws ProfitMandiBusinessException, HttpHostConnectException {
|
|
|
150 |
String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
|
|
|
151 |
: scheme.getValue() + hostName + ":" + port + "/" + uri;
|
| 23561 |
ashik.ali |
152 |
return this.post(url, params, headers);
|
|
|
153 |
}
|
| 25011 |
amit.gupta |
154 |
|
| 23561 |
ashik.ali |
155 |
public String post(String url, Map<String, String> params, Map<String, String> headers)
|
| 25011 |
amit.gupta |
156 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
| 25244 |
amit.gupta |
157 |
// UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
| 22215 |
ashik.ali |
158 |
List<NameValuePair> bodyParameters = new ArrayList<NameValuePair>();
|
| 25011 |
amit.gupta |
159 |
for (Map.Entry<String, String> entry : params.entrySet()) {
|
| 22215 |
ashik.ali |
160 |
bodyParameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
|
|
|
161 |
}
|
| 25011 |
amit.gupta |
162 |
|
| 22215 |
ashik.ali |
163 |
LOGGER.info("Body Parameters {}", params);
|
| 25011 |
amit.gupta |
164 |
HttpPost request = new HttpPost(url);
|
|
|
165 |
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
| 22215 |
ashik.ali |
166 |
request.setHeader(entry.getKey(), entry.getValue());
|
|
|
167 |
}
|
| 25011 |
amit.gupta |
168 |
|
|
|
169 |
try {
|
| 22215 |
ashik.ali |
170 |
request.setEntity(new UrlEncodedFormEntity(bodyParameters));
|
| 25011 |
amit.gupta |
171 |
} catch (UnsupportedEncodingException unsupportedEncodingException) {
|
| 22215 |
ashik.ali |
172 |
LOGGER.error("Encoding error : ", unsupportedEncodingException);
|
|
|
173 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
|
|
174 |
}
|
| 25011 |
amit.gupta |
175 |
|
| 22215 |
ashik.ali |
176 |
return this.execute(request);
|
| 25011 |
amit.gupta |
177 |
|
| 22215 |
ashik.ali |
178 |
}
|
| 25011 |
amit.gupta |
179 |
|
|
|
180 |
public String postJson(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params,
|
|
|
181 |
Map<String, String> headers) throws ProfitMandiBusinessException, HttpHostConnectException {
|
|
|
182 |
String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
|
|
|
183 |
: scheme.getValue() + hostName + ":" + port + "/" + uri;
|
| 23561 |
ashik.ali |
184 |
return this.postJson(url, params, headers);
|
|
|
185 |
}
|
| 25011 |
amit.gupta |
186 |
|
| 23561 |
ashik.ali |
187 |
public String postJson(String url, Map<String, String> params, Map<String, String> headers)
|
| 25011 |
amit.gupta |
188 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
| 23526 |
ashik.ali |
189 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
| 23502 |
ashik.ali |
190 |
List<NameValuePair> bodyParameters = new ArrayList<NameValuePair>();
|
| 25011 |
amit.gupta |
191 |
for (Map.Entry<String, String> entry : params.entrySet()) {
|
| 23502 |
ashik.ali |
192 |
bodyParameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
|
|
|
193 |
}
|
| 25011 |
amit.gupta |
194 |
|
| 23502 |
ashik.ali |
195 |
String jsonString = null;
|
|
|
196 |
try {
|
|
|
197 |
jsonString = StringUtils.toString(params);
|
|
|
198 |
} catch (Exception e) {
|
|
|
199 |
e.printStackTrace();
|
|
|
200 |
}
|
| 25011 |
amit.gupta |
201 |
|
| 23561 |
ashik.ali |
202 |
StringEntity requestEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
|
| 25011 |
amit.gupta |
203 |
|
| 23502 |
ashik.ali |
204 |
LOGGER.info("Body {}", jsonString);
|
|
|
205 |
HttpPost request = new HttpPost(builder.build().encode().toUri());
|
| 25011 |
amit.gupta |
206 |
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
| 23502 |
ashik.ali |
207 |
request.setHeader(entry.getKey(), entry.getValue());
|
|
|
208 |
}
|
|
|
209 |
request.setEntity(requestEntity);
|
|
|
210 |
return this.execute(request);
|
|
|
211 |
}
|
| 25011 |
amit.gupta |
212 |
|
|
|
213 |
private String toString(InputStream inputStream) {
|
| 21557 |
ashik.ali |
214 |
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
|
|
215 |
StringBuilder responseString = new StringBuilder();
|
|
|
216 |
String line = null;
|
|
|
217 |
try {
|
| 25011 |
amit.gupta |
218 |
while ((line = reader.readLine()) != null) {
|
| 21557 |
ashik.ali |
219 |
responseString.append(line);
|
|
|
220 |
}
|
|
|
221 |
inputStream.close();
|
|
|
222 |
} catch (IOException e) {
|
|
|
223 |
throw new RuntimeException();
|
|
|
224 |
}
|
|
|
225 |
return responseString.toString();
|
|
|
226 |
}
|
|
|
227 |
}
|