| Line 1... |
Line 1... |
| 1 |
package com.spice.profitmandi.common.web.client;
|
1 |
package com.spice.profitmandi.common.web.client;
|
| 2 |
|
2 |
|
| 3 |
import java.io.BufferedReader;
|
3 |
import com.fasterxml.jackson.databind.ObjectMapper;
|
| 4 |
import java.io.IOException;
|
4 |
import com.spice.profitmandi.common.ResponseCodeHolder;
|
| 5 |
import java.io.InputStream;
|
- |
|
| 6 |
import java.io.InputStreamReader;
|
5 |
import com.spice.profitmandi.common.enumuration.SchemeType;
|
| 7 |
import java.io.UnsupportedEncodingException;
|
6 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 8 |
import java.util.ArrayList;
|
7 |
import com.spice.profitmandi.common.model.MandiiResponse;
|
| 9 |
import java.util.List;
|
- |
|
| 10 |
import java.util.Map;
|
- |
|
| 11 |
import java.util.Set;
|
- |
|
| 12 |
|
- |
|
| 13 |
import org.apache.http.HttpResponse;
|
8 |
import org.apache.http.HttpResponse;
|
| 14 |
import org.apache.http.NameValuePair;
|
9 |
import org.apache.http.NameValuePair;
|
| 15 |
import org.apache.http.client.ClientProtocolException;
|
10 |
import org.apache.http.client.ClientProtocolException;
|
| 16 |
import org.apache.http.client.HttpClient;
|
11 |
import org.apache.http.client.HttpClient;
|
| 17 |
import org.apache.http.client.config.RequestConfig;
|
12 |
import org.apache.http.client.config.RequestConfig;
|
| Line 31... |
Line 26... |
| 31 |
import org.springframework.beans.factory.annotation.Autowired;
|
26 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 32 |
import org.springframework.http.HttpStatus;
|
27 |
import org.springframework.http.HttpStatus;
|
| 33 |
import org.springframework.stereotype.Component;
|
28 |
import org.springframework.stereotype.Component;
|
| 34 |
import org.springframework.web.util.UriComponentsBuilder;
|
29 |
import org.springframework.web.util.UriComponentsBuilder;
|
| 35 |
|
30 |
|
| 36 |
import com.fasterxml.jackson.databind.ObjectMapper;
|
31 |
import java.io.*;
|
| 37 |
import com.spice.profitmandi.common.ResponseCodeHolder;
|
32 |
import java.util.ArrayList;
|
| 38 |
import com.spice.profitmandi.common.enumuration.SchemeType;
|
33 |
import java.util.List;
|
| 39 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
34 |
import java.util.Map;
|
| 40 |
|
- |
|
| 41 |
import com.spice.profitmandi.common.model.MandiiResponse;
|
35 |
import java.util.Set;
|
| 42 |
|
36 |
|
| 43 |
@Component
|
37 |
@Component
|
| 44 |
public class RestClient {
|
38 |
public class RestClient {
|
| 45 |
|
39 |
|
| 46 |
private static final Logger LOGGER = LogManager.getLogger(RestClient.class);
|
40 |
private static final Logger LOGGER = LogManager.getLogger(RestClient.class);
|
| 47 |
|
41 |
|
| 48 |
private HttpClient httpClient;
|
42 |
private HttpClient httpClient;
|
| 49 |
|
43 |
|
| - |
|
44 |
|
| 50 |
@Autowired
|
45 |
@Autowired
|
| 51 |
ObjectMapper objectMapper;
|
46 |
ObjectMapper objectMapper;
|
| 52 |
|
47 |
|
| 53 |
public RestClient() {
|
48 |
public RestClient() {
|
| 54 |
this.httpClient = HttpClients.createDefault();
|
49 |
this.httpClient = HttpClients.createDefault();
|
| Line 60... |
Line 55... |
| 60 |
this.httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
|
55 |
this.httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
|
| 61 |
|
56 |
|
| 62 |
}
|
57 |
}
|
| 63 |
|
58 |
|
| 64 |
public String get(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params,
|
59 |
public String get(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params,
|
| 65 |
Map<String, String> headers) throws ProfitMandiBusinessException, HttpHostConnectException {
|
60 |
Map<String, String> headers) throws ProfitMandiBusinessException, HttpHostConnectException {
|
| 66 |
String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
|
61 |
String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
|
| 67 |
: scheme.getValue() + hostName + ":" + port + "/" + uri;
|
62 |
: scheme.getValue() + hostName + ":" + port + "/" + uri;
|
| 68 |
return this.get(url, params, headers);
|
63 |
return this.get(url, params, headers);
|
| 69 |
}
|
64 |
}
|
| 70 |
|
65 |
|
| Line 76... |
Line 71... |
| 76 |
for (String key : keys) {
|
71 |
for (String key : keys) {
|
| 77 |
builder.queryParam(key, params.get(key));
|
72 |
builder.queryParam(key, params.get(key));
|
| 78 |
}
|
73 |
}
|
| 79 |
}
|
74 |
}
|
| 80 |
HttpGet request = new HttpGet(builder.build().encode().toUri());
|
75 |
HttpGet request = new HttpGet(builder.build().encode().toUri());
|
| - |
|
76 |
if (headers != null) {
|
| 81 |
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
77 |
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
| 82 |
request.setHeader(entry.getKey(), entry.getValue());
|
78 |
request.setHeader(entry.getKey(), entry.getValue());
|
| - |
|
79 |
}
|
| 83 |
}
|
80 |
}
|
| 84 |
return this.execute(request);
|
81 |
return this.execute(request);
|
| 85 |
}
|
82 |
}
|
| 86 |
|
83 |
|
| 87 |
public MandiiResponse getMandii(String url, Map<String, String> params, Map<String, String> headers)
|
84 |
public MandiiResponse getMandii(String url, Map<String, String> params, Map<String, String> headers)
|
| Line 116... |
Line 113... |
| 116 |
}
|
113 |
}
|
| 117 |
|
114 |
|
| 118 |
public HttpResponse getResponse(String url, Map<String, String> params, Map<String, String> headers)
|
115 |
public HttpResponse getResponse(String url, Map<String, String> params, Map<String, String> headers)
|
| 119 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
116 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
| 120 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
117 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
| 121 |
Set<String> keys = params.keySet();
|
118 |
if (params != null) {
|
| 122 |
for (String key : keys) {
|
119 |
for (Map.Entry<String, String> paramsEntry : params.entrySet()) {
|
| 123 |
builder.queryParam(key, params.get(key));
|
120 |
builder.queryParam(paramsEntry.getKey(), paramsEntry.getValue());
|
| - |
|
121 |
}
|
| - |
|
122 |
|
| 124 |
}
|
123 |
}
|
| 125 |
HttpGet request = new HttpGet(builder.build().encode().toUri());
|
124 |
HttpGet request = new HttpGet(builder.build().encode().toUri());
|
| 126 |
if (headers != null) {
|
125 |
if (headers != null) {
|
| 127 |
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
126 |
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
| 128 |
request.setHeader(entry.getKey(), entry.getValue());
|
127 |
request.setHeader(entry.getKey(), entry.getValue());
|
| Line 153... |
Line 152... |
| 153 |
LOGGER.info("Connecting to server at url {}", request.getURI());
|
152 |
LOGGER.info("Connecting to server at url {}", request.getURI());
|
| 154 |
try {
|
153 |
try {
|
| 155 |
HttpResponse response = httpClient.execute(request);
|
154 |
HttpResponse response = httpClient.execute(request);
|
| 156 |
String responseString = this.toString(response.getEntity().getContent());
|
155 |
String responseString = this.toString(response.getEntity().getContent());
|
| 157 |
LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
|
156 |
LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
|
| - |
|
157 |
LOGGER.info("Response String {}", responseString);
|
| 158 |
if (response.getStatusLine().getStatusCode() == HttpStatus.OK.value()) {
|
158 |
if (response.getStatusLine().getStatusCode() == HttpStatus.OK.value()) {
|
| 159 |
return responseString;
|
159 |
return responseString;
|
| 160 |
} else {
|
160 |
} else {
|
| 161 |
LOGGER.info("Response String {} ", responseString);
|
161 |
LOGGER.info("Response String {} ", responseString);
|
| 162 |
throw new ProfitMandiBusinessException("", "", "GE_1005");
|
162 |
throw new ProfitMandiBusinessException("", "", "GE_1005");
|
| Line 202... |
Line 202... |
| 202 |
LOGGER.info("Connecting to server at url {}", request.getURI());
|
202 |
LOGGER.info("Connecting to server at url {}", request.getURI());
|
| 203 |
try {
|
203 |
try {
|
| 204 |
HttpResponse response = httpClient.execute(request);
|
204 |
HttpResponse response = httpClient.execute(request);
|
| 205 |
String responseString = this.toString(response.getEntity().getContent());
|
205 |
String responseString = this.toString(response.getEntity().getContent());
|
| 206 |
LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
|
206 |
LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
|
| 207 |
LOGGER.info("Response String", responseString);
|
207 |
LOGGER.info("Response String {}", responseString);
|
| 208 |
return responseString;
|
208 |
return responseString;
|
| 209 |
} catch (HttpHostConnectException httpHostConnectException) {
|
209 |
} catch (HttpHostConnectException httpHostConnectException) {
|
| 210 |
LOGGER.error("Connection Timeout Exception", httpHostConnectException);
|
210 |
LOGGER.error("Connection Timeout Exception", httpHostConnectException);
|
| 211 |
throw httpHostConnectException;
|
211 |
throw httpHostConnectException;
|
| 212 |
} catch (ClientProtocolException e) {
|
212 |
} catch (ClientProtocolException e) {
|
| Line 217... |
Line 217... |
| 217 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
217 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
| 218 |
}
|
218 |
}
|
| 219 |
}
|
219 |
}
|
| 220 |
|
220 |
|
| 221 |
public String post(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params,
|
221 |
public String post(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params,
|
| 222 |
Map<String, String> headers) throws ProfitMandiBusinessException, HttpHostConnectException {
|
222 |
Map<String, String> headers) throws ProfitMandiBusinessException, HttpHostConnectException {
|
| 223 |
String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
|
223 |
String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
|
| 224 |
: scheme.getValue() + hostName + ":" + port + "/" + uri;
|
224 |
: scheme.getValue() + hostName + ":" + port + "/" + uri;
|
| 225 |
return this.post(url, params, headers);
|
225 |
return this.post(url, params, headers);
|
| 226 |
}
|
226 |
}
|
| 227 |
|
227 |
|