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