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