| Line 36... |
Line 36... |
| 36 |
import java.util.Set;
|
36 |
import java.util.Set;
|
| 37 |
|
37 |
|
| 38 |
@Component
|
38 |
@Component
|
| 39 |
public class RestClient {
|
39 |
public class RestClient {
|
| 40 |
|
40 |
|
| 41 |
private static final Logger LOGGER = LogManager.getLogger(RestClient.class);
|
41 |
private static final Logger LOGGER = LogManager.getLogger(RestClient.class);
|
| 42 |
|
42 |
|
| 43 |
private HttpClient httpClient;
|
43 |
private HttpClient httpClient;
|
| 44 |
|
44 |
|
| 45 |
|
45 |
|
| 46 |
@Autowired
|
46 |
@Autowired
|
| 47 |
ObjectMapper objectMapper;
|
47 |
ObjectMapper objectMapper;
|
| 48 |
|
48 |
|
| 49 |
public RestClient() {
|
49 |
public RestClient() {
|
| 50 |
PoolingHttpClientConnectionManager connManager
|
50 |
PoolingHttpClientConnectionManager connManager
|
| 51 |
= new PoolingHttpClientConnectionManager();
|
51 |
= new PoolingHttpClientConnectionManager();
|
| 52 |
connManager.setMaxTotal(5);
|
52 |
connManager.setMaxTotal(5);
|
| 53 |
connManager.setDefaultMaxPerRoute(4);
|
53 |
connManager.setDefaultMaxPerRoute(4);
|
| 54 |
httpClient = HttpClients.custom().disableCookieManagement().disableAuthCaching().disableConnectionState().
|
54 |
httpClient = HttpClients.custom().disableCookieManagement().disableAuthCaching().disableConnectionState().
|
| 55 |
disableAuthCaching().setConnectionManager(connManager).build();
|
55 |
disableAuthCaching().setConnectionManager(connManager).build();
|
| 56 |
|
56 |
|
| 57 |
}
|
57 |
}
|
| 58 |
|
58 |
|
| 59 |
public RestClient(int connectionTimeoutMillis) {
|
59 |
public RestClient(int connectionTimeoutMillis) {
|
| 60 |
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5 * 1000).build();
|
60 |
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5 * 1000).build();
|
| 61 |
this.httpClient = HttpClientBuilder.create().disableCookieManagement().setDefaultRequestConfig(requestConfig).build();
|
61 |
this.httpClient = HttpClientBuilder.create().disableCookieManagement().setDefaultRequestConfig(requestConfig).build();
|
| 62 |
|
62 |
|
| 63 |
}
|
63 |
}
|
| 64 |
|
64 |
|
| 65 |
public String get(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params,
|
65 |
public String get(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params,
|
| 66 |
Map<String, String> headers) throws ProfitMandiBusinessException, HttpHostConnectException {
|
66 |
Map<String, String> headers) throws ProfitMandiBusinessException, HttpHostConnectException {
|
| 67 |
String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
|
67 |
String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
|
| 68 |
: scheme.getValue() + hostName + ":" + port + "/" + uri;
|
68 |
: scheme.getValue() + hostName + ":" + port + "/" + uri;
|
| 69 |
return this.get(url, params, headers);
|
69 |
return this.get(url, params, headers);
|
| 70 |
}
|
70 |
}
|
| 71 |
|
71 |
|
| 72 |
public String get(String url, Map<String, String> params, Map<String, String> headers)
|
72 |
public String get(String url, Map<String, String> params, Map<String, String> headers)
|
| 73 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
73 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
| 74 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
74 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
| 75 |
if (params != null) {
|
75 |
if (params != null) {
|
| 76 |
Set<String> keys = params.keySet();
|
76 |
Set<String> keys = params.keySet();
|
| 77 |
for (String key : keys) {
|
77 |
for (String key : keys) {
|
| 78 |
builder.queryParam(key, params.get(key));
|
78 |
builder.queryParam(key, params.get(key));
|
| 79 |
}
|
79 |
}
|
| 80 |
}
|
80 |
}
|
| 81 |
HttpGet request = new HttpGet(builder.build().encode().toUri());
|
81 |
HttpGet request = new HttpGet(builder.build().encode().toUri());
|
| 82 |
if (headers != null) {
|
82 |
if (headers != null) {
|
| 83 |
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
83 |
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
| 84 |
request.setHeader(entry.getKey(), entry.getValue());
|
84 |
request.setHeader(entry.getKey(), entry.getValue());
|
| 85 |
}
|
85 |
}
|
| 86 |
}
|
86 |
}
|
| 87 |
return this.execute(request);
|
87 |
return this.execute(request);
|
| 88 |
}
|
88 |
}
|
| 89 |
|
89 |
|
| 90 |
public MandiiResponse getMandii(String url, Map<String, String> params, Map<String, String> headers)
|
90 |
public MandiiResponse getMandii(String url, Map<String, String> params, Map<String, String> headers)
|
| 91 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
91 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
| 92 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
92 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
| 93 |
if (params != null) {
|
93 |
if (params != null) {
|
| 94 |
Set<String> keys = params.keySet();
|
94 |
Set<String> keys = params.keySet();
|
| 95 |
for (String key : keys) {
|
95 |
for (String key : keys) {
|
| 96 |
builder.queryParam(key, params.get(key));
|
96 |
builder.queryParam(key, params.get(key));
|
| 97 |
}
|
97 |
}
|
| 98 |
}
|
98 |
}
|
| 99 |
HttpGet request = new HttpGet(builder.build().encode().toUri());
|
99 |
HttpGet request = new HttpGet(builder.build().encode().toUri());
|
| 100 |
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
100 |
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
| 101 |
request.setHeader(entry.getKey(), entry.getValue());
|
101 |
request.setHeader(entry.getKey(), entry.getValue());
|
| 102 |
}
|
102 |
}
|
| 103 |
return this.executeMandii(request);
|
103 |
return this.executeMandii(request);
|
| 104 |
}
|
104 |
}
|
| 105 |
|
105 |
|
| 106 |
public String get(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params)
|
106 |
public String get(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params)
|
| 107 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
107 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
| 108 |
String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
|
108 |
String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
|
| 109 |
: scheme.getValue() + hostName + ":" + port + "/" + uri;
|
109 |
: scheme.getValue() + hostName + ":" + port + "/" + uri;
|
| 110 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
110 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
| 111 |
if (params != null) {
|
111 |
if (params != null) {
|
| 112 |
Set<String> keys = params.keySet();
|
112 |
Set<String> keys = params.keySet();
|
| 113 |
for (String key : keys) {
|
113 |
for (String key : keys) {
|
| 114 |
builder.queryParam(key, params.get(key));
|
114 |
builder.queryParam(key, params.get(key));
|
| 115 |
}
|
115 |
}
|
| 116 |
}
|
116 |
}
|
| 117 |
HttpGet request = new HttpGet(builder.build().encode().toUri());
|
117 |
HttpGet request = new HttpGet(builder.build().encode().toUri());
|
| 118 |
return this.execute(request);
|
118 |
return this.execute(request);
|
| 119 |
}
|
119 |
}
|
| 120 |
|
120 |
|
| 121 |
public HttpResponse getResponse(String url, Map<String, String> params, Map<String, String> headers)
|
121 |
public HttpResponse getResponse(String url, Map<String, String> params, Map<String, String> headers)
|
| 122 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
122 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
| 123 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
123 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
| 124 |
if (params != null) {
|
124 |
if (params != null) {
|
| 125 |
for (Map.Entry<String, String> paramsEntry : params.entrySet()) {
|
125 |
for (Map.Entry<String, String> paramsEntry : params.entrySet()) {
|
| 126 |
builder.queryParam(paramsEntry.getKey(), paramsEntry.getValue());
|
126 |
builder.queryParam(paramsEntry.getKey(), paramsEntry.getValue());
|
| 127 |
}
|
127 |
}
|
| 128 |
|
128 |
|
| 129 |
}
|
129 |
}
|
| 130 |
HttpGet request = new HttpGet(builder.build().encode().toUri());
|
130 |
HttpGet request = new HttpGet(builder.build().encode().toUri());
|
| 131 |
if (headers != null) {
|
131 |
if (headers != null) {
|
| 132 |
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
132 |
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
| 133 |
request.setHeader(entry.getKey(), entry.getValue());
|
133 |
request.setHeader(entry.getKey(), entry.getValue());
|
| 134 |
}
|
134 |
}
|
| 135 |
}
|
135 |
}
|
| 136 |
try {
|
136 |
try {
|
| 137 |
LOGGER.info("Request uri is {}", request.getURI().toString());
|
137 |
LOGGER.info("Request uri is {}", request.getURI().toString());
|
| 138 |
HttpResponse response = httpClient.execute(request);
|
138 |
HttpResponse response = httpClient.execute(request);
|
| 139 |
LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
|
139 |
LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
|
| 140 |
if (response.getStatusLine().getStatusCode() == HttpStatus.OK.value()) {
|
140 |
if (response.getStatusLine().getStatusCode() == HttpStatus.OK.value()) {
|
| 141 |
return response;
|
141 |
return response;
|
| 142 |
} else {
|
142 |
} else {
|
| 143 |
throw new ProfitMandiBusinessException("", "", "GE_1005");
|
143 |
throw new ProfitMandiBusinessException("", "", "GE_1005");
|
| 144 |
}
|
144 |
}
|
| 145 |
} catch (HttpHostConnectException httpHostConnectException) {
|
145 |
} catch (HttpHostConnectException httpHostConnectException) {
|
| 146 |
LOGGER.error("Connection Timeout Exception", httpHostConnectException);
|
146 |
LOGGER.error("Connection Timeout Exception", httpHostConnectException);
|
| 147 |
throw httpHostConnectException;
|
147 |
throw httpHostConnectException;
|
| 148 |
} catch (ClientProtocolException e) {
|
148 |
} catch (ClientProtocolException e) {
|
| 149 |
LOGGER.error("Client Error : ", e);
|
149 |
LOGGER.error("Client Error : ", e);
|
| 150 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
150 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
| 151 |
} catch (IOException e) {
|
151 |
} catch (IOException e) {
|
| 152 |
LOGGER.error("IO Error : ", e);
|
152 |
LOGGER.error("IO Error : ", e);
|
| 153 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
153 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
| 154 |
}
|
154 |
}
|
| 155 |
}
|
155 |
}
|
| 156 |
|
156 |
|
| 157 |
public String execute(HttpUriRequest request) throws ProfitMandiBusinessException, HttpHostConnectException {
|
157 |
public String execute(HttpUriRequest request) throws ProfitMandiBusinessException, HttpHostConnectException {
|
| 158 |
LOGGER.info("Connecting to server at url {}", request.getURI());
|
158 |
LOGGER.info("Connecting to server at url {}", request.getURI());
|
| 159 |
try {
|
159 |
try {
|
| 160 |
HttpResponse response = httpClient.execute(request);
|
160 |
HttpResponse response = httpClient.execute(request);
|
| 161 |
String responseString = this.toString(response.getEntity().getContent());
|
161 |
String responseString = this.toString(response.getEntity().getContent());
|
| 162 |
LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
|
162 |
LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
|
| 163 |
LOGGER.info("Response String {}", responseString);
|
163 |
LOGGER.info("Response String {}", responseString);
|
| 164 |
if (response.getStatusLine().getStatusCode() == HttpStatus.OK.value()) {
|
164 |
if (response.getStatusLine().getStatusCode() == HttpStatus.OK.value()) {
|
| 165 |
return responseString;
|
165 |
return responseString;
|
| 166 |
} else {
|
166 |
} else {
|
| 167 |
LOGGER.info("Response String {} ", responseString);
|
167 |
LOGGER.info("Response String {} ", responseString);
|
| 168 |
throw new ProfitMandiBusinessException("", "", "GE_1005");
|
168 |
throw new ProfitMandiBusinessException("", "", "GE_1005");
|
| 169 |
}
|
169 |
}
|
| 170 |
} catch (HttpHostConnectException httpHostConnectException) {
|
170 |
} catch (HttpHostConnectException httpHostConnectException) {
|
| 171 |
LOGGER.error("Connection Timeout Exception", httpHostConnectException);
|
171 |
LOGGER.error("Connection Timeout Exception", httpHostConnectException);
|
| 172 |
throw httpHostConnectException;
|
172 |
throw httpHostConnectException;
|
| 173 |
} catch (ClientProtocolException e) {
|
173 |
} catch (ClientProtocolException e) {
|
| 174 |
LOGGER.error("Client Error : ", e);
|
174 |
LOGGER.error("Client Error : ", e);
|
| 175 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
175 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
| 176 |
} catch (IOException e) {
|
176 |
} catch (IOException e) {
|
| 177 |
LOGGER.error("IO Error : ", e);
|
177 |
LOGGER.error("IO Error : ", e);
|
| 178 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
178 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
| 179 |
}
|
179 |
}
|
| 180 |
}
|
180 |
}
|
| 181 |
|
181 |
|
| 182 |
public MandiiResponse executeMandii(HttpUriRequest request)
|
182 |
public MandiiResponse executeMandii(HttpUriRequest request)
|
| 183 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
183 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
| 184 |
LOGGER.info("Connecting to server at url {}", request.getURI());
|
184 |
LOGGER.info("Connecting to server at url {}", request.getURI());
|
| 185 |
try {
|
185 |
try {
|
| 186 |
HttpResponse response = httpClient.execute(request);
|
186 |
HttpResponse response = httpClient.execute(request);
|
| 187 |
String responseString = this.toString(response.getEntity().getContent());
|
187 |
String responseString = this.toString(response.getEntity().getContent());
|
| 188 |
LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
|
188 |
LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
|
| 189 |
|
189 |
|
| 190 |
MandiiResponse mandiiResponse = new MandiiResponse();
|
190 |
MandiiResponse mandiiResponse = new MandiiResponse();
|
| 191 |
mandiiResponse.setResponseString(responseString);
|
191 |
mandiiResponse.setResponseString(responseString);
|
| 192 |
mandiiResponse.setStatusCode(response.getStatusLine().getStatusCode());
|
192 |
mandiiResponse.setStatusCode(response.getStatusLine().getStatusCode());
|
| 193 |
|
193 |
|
| 194 |
return mandiiResponse;
|
194 |
return mandiiResponse;
|
| 195 |
} catch (HttpHostConnectException httpHostConnectException) {
|
195 |
} catch (HttpHostConnectException httpHostConnectException) {
|
| 196 |
LOGGER.error("Connection Timeout Exception", httpHostConnectException);
|
196 |
LOGGER.error("Connection Timeout Exception", httpHostConnectException);
|
| 197 |
throw httpHostConnectException;
|
197 |
throw httpHostConnectException;
|
| 198 |
} catch (ClientProtocolException e) {
|
198 |
} catch (ClientProtocolException e) {
|
| 199 |
LOGGER.error("Client Error : ", e);
|
199 |
LOGGER.error("Client Error : ", e);
|
| 200 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
200 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
| 201 |
} catch (IOException e) {
|
201 |
} catch (IOException e) {
|
| 202 |
LOGGER.error("IO Error : ", e);
|
202 |
LOGGER.error("IO Error : ", e);
|
| 203 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
203 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
| 204 |
}
|
204 |
}
|
| 205 |
}
|
205 |
}
|
| 206 |
|
206 |
|
| 207 |
public String executeJson(HttpUriRequest request) throws ProfitMandiBusinessException, HttpHostConnectException {
|
207 |
public String executeJson(HttpUriRequest request) throws ProfitMandiBusinessException, HttpHostConnectException {
|
| 208 |
LOGGER.info("Connecting to server at url {}", request.getURI());
|
208 |
LOGGER.info("Connecting to server at url {}", request.getURI());
|
| 209 |
try {
|
209 |
try {
|
| 210 |
HttpResponse response = httpClient.execute(request);
|
210 |
HttpResponse response = httpClient.execute(request);
|
| 211 |
String responseString = this.toString(response.getEntity().getContent());
|
211 |
String responseString = this.toString(response.getEntity().getContent());
|
| 212 |
LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
|
212 |
LOGGER.info("Got response from server with responseCode {}", response.getStatusLine().getStatusCode());
|
| 213 |
LOGGER.info("Response String {}", responseString);
|
213 |
LOGGER.info("Response String {}", responseString);
|
| 214 |
return responseString;
|
214 |
return responseString;
|
| 215 |
} catch (HttpHostConnectException httpHostConnectException) {
|
215 |
} catch (HttpHostConnectException httpHostConnectException) {
|
| 216 |
LOGGER.error("Connection Timeout Exception", httpHostConnectException);
|
216 |
LOGGER.error("Connection Timeout Exception", httpHostConnectException);
|
| 217 |
throw httpHostConnectException;
|
217 |
throw httpHostConnectException;
|
| 218 |
} catch (ClientProtocolException e) {
|
218 |
} catch (ClientProtocolException e) {
|
| 219 |
LOGGER.error("Client Error : ", e);
|
219 |
LOGGER.error("Client Error : ", e);
|
| 220 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
220 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
| 221 |
} catch (IOException e) {
|
221 |
} catch (IOException e) {
|
| 222 |
LOGGER.error("IO Error : ", e);
|
222 |
LOGGER.error("IO Error : ", e);
|
| 223 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
223 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
| 224 |
}
|
224 |
}
|
| 225 |
}
|
225 |
}
|
| 226 |
|
226 |
|
| 227 |
public String post(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params,
|
227 |
public String post(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params,
|
| 228 |
Map<String, String> headers) throws ProfitMandiBusinessException, HttpHostConnectException {
|
228 |
Map<String, String> headers) throws ProfitMandiBusinessException, HttpHostConnectException {
|
| 229 |
String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
|
229 |
String url = scheme.getValue() == null ? SchemeType.HTTP.toString()
|
| 230 |
: scheme.getValue() + hostName + ":" + port + "/" + uri;
|
230 |
: scheme.getValue() + hostName + ":" + port + "/" + uri;
|
| 231 |
return this.post(url, params, headers);
|
231 |
return this.post(url, params, headers);
|
| 232 |
}
|
232 |
}
|
| 233 |
|
233 |
|
| 234 |
public String post(String url, Map<String, String> params, Map<String, String> headers)
|
234 |
public String post(String url, Map<String, String> params, Map<String, String> headers)
|
| 235 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
235 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
| 236 |
// UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
236 |
// UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
| 237 |
List<NameValuePair> bodyParameters = new ArrayList<NameValuePair>();
|
237 |
List<NameValuePair> bodyParameters = new ArrayList<NameValuePair>();
|
| 238 |
for (Map.Entry<String, String> entry : params.entrySet()) {
|
238 |
for (Map.Entry<String, String> entry : params.entrySet()) {
|
| 239 |
bodyParameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
|
239 |
bodyParameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
|
| 240 |
}
|
240 |
}
|
| 241 |
|
241 |
|
| 242 |
LOGGER.info("Body Parameters {}", params);
|
242 |
LOGGER.info("Body Parameters {}", params);
|
| 243 |
HttpPost request = new HttpPost(url);
|
243 |
HttpPost request = new HttpPost(url);
|
| 244 |
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
244 |
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
| 245 |
request.setHeader(entry.getKey(), entry.getValue());
|
245 |
request.setHeader(entry.getKey(), entry.getValue());
|
| 246 |
}
|
246 |
}
|
| 247 |
|
247 |
|
| 248 |
try {
|
248 |
try {
|
| 249 |
request.setEntity(new UrlEncodedFormEntity(bodyParameters));
|
249 |
request.setEntity(new UrlEncodedFormEntity(bodyParameters));
|
| 250 |
} catch (UnsupportedEncodingException unsupportedEncodingException) {
|
250 |
} catch (UnsupportedEncodingException unsupportedEncodingException) {
|
| 251 |
LOGGER.error("Encoding error : ", unsupportedEncodingException);
|
251 |
LOGGER.error("Encoding error : ", unsupportedEncodingException);
|
| 252 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
252 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
| 253 |
}
|
253 |
}
|
| 254 |
|
254 |
|
| 255 |
return this.execute(request);
|
255 |
return this.execute(request);
|
| 256 |
|
256 |
|
| 257 |
}
|
257 |
}
|
| 258 |
|
258 |
|
| 259 |
public String post(String url, String body, Map<String, String> headers)
|
259 |
public String post(String url, Map<String, String> queryParams, Map<String, String> postParams, Map<String, String> headers)
|
| 260 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
260 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
| 261 |
HttpPost request = new HttpPost(url);
|
261 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
| 262 |
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
262 |
if (queryParams != null) {
|
| 263 |
request.setHeader(entry.getKey(), entry.getValue());
|
263 |
Set<String> keys = queryParams.keySet();
|
| 264 |
}
|
264 |
for (String key : keys) {
|
| 265 |
|
265 |
builder.queryParam(key, queryParams.get(key));
|
| 266 |
try {
|
266 |
}
|
| 267 |
request.setEntity(new StringEntity(body));
|
267 |
}
|
| 268 |
} catch (UnsupportedEncodingException unsupportedEncodingException) {
|
268 |
List<NameValuePair> bodyParameters = new ArrayList<>();
|
| 269 |
LOGGER.error("Encoding error : ", unsupportedEncodingException);
|
269 |
for (Map.Entry<String, String> entry : postParams.entrySet()) {
|
| 270 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
270 |
bodyParameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
|
| 271 |
}
|
271 |
}
|
| 272 |
|
272 |
|
| 273 |
return this.execute(request);
|
273 |
HttpPost request = new HttpPost(builder.build().encode().toUri());
|
| 274 |
|
274 |
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
| 275 |
}
|
275 |
request.setHeader(entry.getKey(), entry.getValue());
|
| 276 |
|
276 |
}
|
| 277 |
public String postJson(String url, Object object, Map<String, String> headers)
|
277 |
|
| 278 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
278 |
try {
|
| 279 |
String jsonString;
|
279 |
request.setEntity(new UrlEncodedFormEntity(bodyParameters));
|
| 280 |
try {
|
280 |
} catch (UnsupportedEncodingException unsupportedEncodingException) {
|
| 281 |
if (object.getClass().equals(String.class)) {
|
281 |
LOGGER.error("Encoding error : ", unsupportedEncodingException);
|
| 282 |
jsonString = (String) object;
|
282 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
| 283 |
} else {
|
283 |
}
|
| 284 |
jsonString = objectMapper.writeValueAsString(object);
|
284 |
|
| 285 |
}
|
285 |
return this.execute(request);
|
| 286 |
LOGGER.info("JSON String - {}", jsonString);
|
286 |
|
| 287 |
} catch (Exception e) {
|
287 |
}
|
| 288 |
e.printStackTrace();
|
288 |
|
| 289 |
throw new ProfitMandiBusinessException("Json Object", object.toString(), "Could not write as String");
|
289 |
public String post(String url, String body, Map<String, String> headers)
|
| 290 |
}
|
290 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
| 291 |
StringEntity requestEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
|
291 |
HttpPost request = new HttpPost(url);
|
| 292 |
|
292 |
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
| 293 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
293 |
request.setHeader(entry.getKey(), entry.getValue());
|
| 294 |
|
294 |
}
|
| 295 |
HttpPost request = new HttpPost(builder.build().encode().toUri());
|
295 |
|
| 296 |
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
296 |
try {
|
| 297 |
request.setHeader(entry.getKey(), entry.getValue());
|
297 |
request.setEntity(new StringEntity(body));
|
| 298 |
}
|
298 |
} catch (UnsupportedEncodingException unsupportedEncodingException) {
|
| 299 |
request.setEntity(requestEntity);
|
299 |
LOGGER.error("Encoding error : ", unsupportedEncodingException);
|
| 300 |
return this.executeJson(request);
|
300 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
| 301 |
}
|
301 |
}
|
| 302 |
|
302 |
|
| 303 |
public String patchJson(String url, Object object, Map<String, String> headers)
|
303 |
return this.execute(request);
|
| 304 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
304 |
|
| 305 |
String jsonString;
|
305 |
}
|
| 306 |
try {
|
306 |
|
| 307 |
if (object.getClass().equals(String.class)) {
|
307 |
public String postJson(String url, Object object, Map<String, String> headers)
|
| 308 |
jsonString = (String) object;
|
308 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
| 309 |
} else {
|
309 |
String jsonString;
|
| 310 |
jsonString = objectMapper.writeValueAsString(object);
|
310 |
try {
|
| 311 |
}
|
311 |
if (object.getClass().equals(String.class)) {
|
| 312 |
LOGGER.info("JSON String - {}", jsonString);
|
312 |
jsonString = (String) object;
|
| 313 |
} catch (Exception e) {
|
313 |
} else {
|
| 314 |
e.printStackTrace();
|
314 |
jsonString = objectMapper.writeValueAsString(object);
|
| 315 |
throw new ProfitMandiBusinessException("Json Object", object.toString(), "Could not write as String");
|
315 |
}
|
| 316 |
}
|
316 |
LOGGER.info("JSON String - {}", jsonString);
|
| 317 |
StringEntity requestEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
|
317 |
} catch (Exception e) {
|
| 318 |
|
318 |
e.printStackTrace();
|
| 319 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
319 |
throw new ProfitMandiBusinessException("Json Object", object.toString(), "Could not write as String");
|
| 320 |
|
320 |
}
|
| 321 |
HttpPatch request = new HttpPatch(builder.build().encode().toUri());
|
321 |
StringEntity requestEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
|
| 322 |
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
322 |
|
| 323 |
request.setHeader(entry.getKey(), entry.getValue());
|
323 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
| 324 |
}
|
324 |
|
| 325 |
request.setEntity(requestEntity);
|
325 |
HttpPost request = new HttpPost(builder.build().encode().toUri());
|
| 326 |
return this.executeJson(request);
|
326 |
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
| 327 |
}
|
327 |
request.setHeader(entry.getKey(), entry.getValue());
|
| 328 |
|
328 |
}
|
| 329 |
private String toString(InputStream inputStream) {
|
329 |
request.setEntity(requestEntity);
|
| 330 |
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
330 |
return this.executeJson(request);
|
| 331 |
StringBuilder responseString = new StringBuilder();
|
331 |
}
|
| 332 |
String line = null;
|
332 |
|
| 333 |
try {
|
333 |
public String patchJson(String url, Object object, Map<String, String> headers)
|
| 334 |
while ((line = reader.readLine()) != null) {
|
334 |
throws ProfitMandiBusinessException, HttpHostConnectException {
|
| 335 |
responseString.append(line);
|
335 |
String jsonString;
|
| 336 |
}
|
336 |
try {
|
| 337 |
inputStream.close();
|
337 |
if (object.getClass().equals(String.class)) {
|
| 338 |
} catch (IOException e) {
|
338 |
jsonString = (String) object;
|
| 339 |
throw new RuntimeException();
|
339 |
} else {
|
| 340 |
} finally {
|
340 |
jsonString = objectMapper.writeValueAsString(object);
|
| 341 |
|
341 |
}
|
| 342 |
}
|
342 |
LOGGER.info("JSON String - {}", jsonString);
|
| 343 |
return responseString.toString();
|
343 |
} catch (Exception e) {
|
| 344 |
}
|
344 |
e.printStackTrace();
|
| - |
|
345 |
throw new ProfitMandiBusinessException("Json Object", object.toString(), "Could not write as String");
|
| - |
|
346 |
}
|
| - |
|
347 |
StringEntity requestEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
|
| - |
|
348 |
|
| - |
|
349 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
| - |
|
350 |
|
| - |
|
351 |
HttpPatch request = new HttpPatch(builder.build().encode().toUri());
|
| - |
|
352 |
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
| - |
|
353 |
request.setHeader(entry.getKey(), entry.getValue());
|
| - |
|
354 |
}
|
| - |
|
355 |
request.setEntity(requestEntity);
|
| - |
|
356 |
return this.executeJson(request);
|
| - |
|
357 |
}
|
| - |
|
358 |
|
| - |
|
359 |
private String toString(InputStream inputStream) {
|
| - |
|
360 |
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
| - |
|
361 |
StringBuilder responseString = new StringBuilder();
|
| - |
|
362 |
String line = null;
|
| - |
|
363 |
try {
|
| - |
|
364 |
while ((line = reader.readLine()) != null) {
|
| - |
|
365 |
responseString.append(line);
|
| - |
|
366 |
}
|
| - |
|
367 |
inputStream.close();
|
| - |
|
368 |
} catch (IOException e) {
|
| - |
|
369 |
throw new RuntimeException();
|
| - |
|
370 |
} finally {
|
| - |
|
371 |
|
| - |
|
372 |
}
|
| - |
|
373 |
return responseString.toString();
|
| - |
|
374 |
}
|
| 345 |
}
|
375 |
}
|
| 346 |
|
376 |
|