| Line 24... |
Line 24... |
| 24 |
import org.apache.http.impl.client.HttpClients;
|
24 |
import org.apache.http.impl.client.HttpClients;
|
| 25 |
import org.apache.http.message.BasicNameValuePair;
|
25 |
import org.apache.http.message.BasicNameValuePair;
|
| 26 |
import org.slf4j.Logger;
|
26 |
import org.slf4j.Logger;
|
| 27 |
import org.slf4j.LoggerFactory;
|
27 |
import org.slf4j.LoggerFactory;
|
| 28 |
import org.springframework.http.HttpStatus;
|
28 |
import org.springframework.http.HttpStatus;
|
| - |
|
29 |
import org.springframework.stereotype.Component;
|
| 29 |
import org.springframework.web.util.UriComponentsBuilder;
|
30 |
import org.springframework.web.util.UriComponentsBuilder;
|
| 30 |
|
31 |
|
| 31 |
import com.spice.profitmandi.common.ResponseCodeHolder;
|
32 |
import com.spice.profitmandi.common.ResponseCodeHolder;
|
| 32 |
import com.spice.profitmandi.common.enumuration.SchemeType;
|
33 |
import com.spice.profitmandi.common.enumuration.SchemeType;
|
| 33 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
34 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 34 |
import com.spice.profitmandi.common.util.StringUtils;
|
35 |
import com.spice.profitmandi.common.util.StringUtils;
|
| 35 |
|
36 |
|
| - |
|
37 |
@Component
|
| 36 |
public class RestClient {
|
38 |
public class RestClient {
|
| 37 |
|
39 |
|
| 38 |
private static final Logger LOGGER = LoggerFactory.getLogger(RestClient.class);
|
40 |
private static final Logger LOGGER = LoggerFactory.getLogger(RestClient.class);
|
| 39 |
|
41 |
|
| 40 |
private HttpClient httpClient;
|
42 |
private HttpClient httpClient;
|
| 41 |
private SchemeType scheme;
|
- |
|
| 42 |
private String hostName;
|
- |
|
| 43 |
private int port;
|
43 |
|
| 44 |
private final String url;
|
- |
|
| 45 |
//private HttpHeaders headers;
|
- |
|
| 46 |
public RestClient(SchemeType scheme, String hostName, int port){
|
- |
|
| 47 |
this.scheme = scheme;
|
- |
|
| 48 |
this.hostName = hostName;
|
44 |
public RestClient() {
|
| 49 |
this.port = port;
|
- |
|
| 50 |
this.url = scheme.getValue() == null ? SchemeType.HTTP.toString() : scheme.getValue() + hostName + ":" + port + "/";
|
- |
|
| 51 |
//this.headers = new HttpHeaders();
|
- |
|
| 52 |
this.httpClient = HttpClients.createDefault();
|
45 |
this.httpClient = HttpClients.createDefault();
|
| 53 |
//headers.add("Content-Type", MediaType.APPLICATION_JSON_VALUE);
|
- |
|
| 54 |
}
|
- |
|
| 55 |
public SchemeType getScheme() {
|
- |
|
| 56 |
return scheme;
|
- |
|
| 57 |
}
|
- |
|
| 58 |
public String getHostName() {
|
- |
|
| 59 |
return hostName;
|
- |
|
| 60 |
}
|
46 |
}
|
| 61 |
public int getPort() {
|
- |
|
| 62 |
return port;
|
- |
|
| 63 |
}
|
47 |
|
| 64 |
public String getUrl() {
|
- |
|
| 65 |
return url;
|
- |
|
| 66 |
}
|
- |
|
| 67 |
public String get(String uri, Map<String, String> params, Map<String, String> headers)
|
48 |
public String get(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params, Map<String, String> headers)
|
| 68 |
throws ProfitMandiBusinessException, HttpHostConnectException{
|
49 |
throws ProfitMandiBusinessException, HttpHostConnectException{
|
| - |
|
50 |
String url = scheme.getValue() == null ? SchemeType.HTTP.toString() : scheme.getValue() + hostName + ":" + port + "/" + uri;
|
| 69 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url + uri);
|
51 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
| 70 |
Set<String> keys = params.keySet();
|
52 |
Set<String> keys = params.keySet();
|
| 71 |
for(String key : keys){
|
53 |
for(String key : keys){
|
| 72 |
builder.queryParam(key, params.get(key));
|
54 |
builder.queryParam(key, params.get(key));
|
| 73 |
}
|
55 |
}
|
| 74 |
HttpGet request = new HttpGet(builder.build().encode().toUri());
|
56 |
HttpGet request = new HttpGet(builder.build().encode().toUri());
|
| Line 76... |
Line 58... |
| 76 |
request.setHeader(entry.getKey(), entry.getValue());
|
58 |
request.setHeader(entry.getKey(), entry.getValue());
|
| 77 |
}
|
59 |
}
|
| 78 |
return this.execute(request);
|
60 |
return this.execute(request);
|
| 79 |
}
|
61 |
}
|
| 80 |
|
62 |
|
| 81 |
public String get(String uri, Map<String, String> params)
|
63 |
public String get(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params)
|
| 82 |
throws ProfitMandiBusinessException, HttpHostConnectException{
|
64 |
throws ProfitMandiBusinessException, HttpHostConnectException{
|
| - |
|
65 |
String url = scheme.getValue() == null ? SchemeType.HTTP.toString() : scheme.getValue() + hostName + ":" + port + "/" + uri;
|
| 83 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url + uri);
|
66 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
| 84 |
if(params != null) {
|
67 |
if(params != null){
|
| 85 |
Set<String> keys = params.keySet();
|
68 |
Set<String> keys = params.keySet();
|
| 86 |
for(String key : keys){
|
69 |
for(String key : keys){
|
| 87 |
builder.queryParam(key, params.get(key));
|
70 |
builder.queryParam(key, params.get(key));
|
| 88 |
}
|
71 |
}
|
| 89 |
}
|
72 |
}
|
| Line 113... |
Line 96... |
| 113 |
LOGGER.error("IO Error : ", e);
|
96 |
LOGGER.error("IO Error : ", e);
|
| 114 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
97 |
throw new RuntimeException(ResponseCodeHolder.getMessage("GE_1006"));
|
| 115 |
}
|
98 |
}
|
| 116 |
}
|
99 |
}
|
| 117 |
|
100 |
|
| 118 |
public String post(String uri, Map<String, String> params, Map<String, String> headers)
|
101 |
public String post(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params, Map<String, String> headers)
|
| 119 |
throws ProfitMandiBusinessException, HttpHostConnectException{
|
102 |
throws ProfitMandiBusinessException, HttpHostConnectException{
|
| - |
|
103 |
String url = scheme.getValue() == null ? SchemeType.HTTP.toString() : scheme.getValue() + hostName + ":" + port + "/" + uri;
|
| 120 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url + uri);
|
104 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
| 121 |
List<NameValuePair> bodyParameters = new ArrayList<NameValuePair>();
|
105 |
List<NameValuePair> bodyParameters = new ArrayList<NameValuePair>();
|
| 122 |
for(Map.Entry<String, String> entry : params.entrySet()){
|
106 |
for(Map.Entry<String, String> entry : params.entrySet()){
|
| 123 |
bodyParameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
|
107 |
bodyParameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
|
| 124 |
}
|
108 |
}
|
| 125 |
|
109 |
|
| Line 139... |
Line 123... |
| 139 |
|
123 |
|
| 140 |
return this.execute(request);
|
124 |
return this.execute(request);
|
| 141 |
|
125 |
|
| 142 |
}
|
126 |
}
|
| 143 |
|
127 |
|
| 144 |
public String postJson(String uri, Map<String, String> params, Map<String, String> headers)
|
128 |
public String postJson(SchemeType scheme, String hostName, int port, String uri, Map<String, String> params, Map<String, String> headers)
|
| 145 |
throws ProfitMandiBusinessException, HttpHostConnectException{
|
129 |
throws ProfitMandiBusinessException, HttpHostConnectException{
|
| - |
|
130 |
String url = scheme.getValue() == null ? SchemeType.HTTP.toString() : scheme.getValue() + hostName + ":" + port + "/" + uri;
|
| 146 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url + uri);
|
131 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
|
| 147 |
List<NameValuePair> bodyParameters = new ArrayList<NameValuePair>();
|
132 |
List<NameValuePair> bodyParameters = new ArrayList<NameValuePair>();
|
| 148 |
for(Map.Entry<String, String> entry : params.entrySet()){
|
133 |
for(Map.Entry<String, String> entry : params.entrySet()){
|
| 149 |
bodyParameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
|
134 |
bodyParameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
|
| 150 |
}
|
135 |
}
|
| 151 |
|
136 |
|