1 |
59219
|
k.triantaf
|
package eu.dnetlib.openaire.usermanagement.utils;
|
2 |
|
|
|
3 |
|
|
import com.google.gson.JsonElement;
|
4 |
|
|
import com.google.gson.JsonObject;
|
5 |
|
|
import com.google.gson.JsonParser;
|
6 |
|
|
import org.apache.commons.codec.binary.Base64;
|
7 |
|
|
import org.apache.log4j.Logger;
|
8 |
|
|
import org.springframework.beans.factory.annotation.Value;
|
9 |
|
|
import org.springframework.http.*;
|
10 |
|
|
import org.springframework.stereotype.Component;
|
11 |
|
|
import org.springframework.web.client.RestTemplate;
|
12 |
|
|
|
13 |
61063
|
k.triantaf
|
import java.io.UnsupportedEncodingException;
|
14 |
61159
|
k.triantaf
|
import java.net.URLDecoder;
|
15 |
61063
|
k.triantaf
|
import java.net.URLEncoder;
|
16 |
59219
|
k.triantaf
|
import java.nio.charset.Charset;
|
17 |
61063
|
k.triantaf
|
import java.nio.charset.StandardCharsets;
|
18 |
59219
|
k.triantaf
|
import java.util.Map;
|
19 |
|
|
|
20 |
|
|
@Component
|
21 |
|
|
public class HttpUtils {
|
22 |
|
|
|
23 |
|
|
private static final Logger logger = Logger.getLogger(HttpUtils.class);
|
24 |
|
|
|
25 |
|
|
@Value("${registry.issuer}")
|
26 |
|
|
private String issuer;
|
27 |
|
|
|
28 |
|
|
@Value("${registry.user}")
|
29 |
|
|
private String user;
|
30 |
|
|
|
31 |
|
|
@Value("${registry.password}")
|
32 |
|
|
private String password;
|
33 |
|
|
|
34 |
|
|
public JsonElement post(String path, JsonObject body) {
|
35 |
|
|
RestTemplate restTemplate = new RestTemplate();
|
36 |
|
|
HttpHeaders headers = createHeaders(user, password);
|
37 |
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
38 |
|
|
HttpEntity<String> request = new HttpEntity<>(body.toString(), headers);
|
39 |
|
|
ResponseEntity<String> responseEntity = restTemplate.exchange(issuer + path, HttpMethod.POST, request, String.class);
|
40 |
61063
|
k.triantaf
|
if (responseEntity.getBody() != null) {
|
41 |
59219
|
k.triantaf
|
return new JsonParser().parse(responseEntity.getBody());
|
42 |
|
|
} else {
|
43 |
|
|
return null;
|
44 |
|
|
}
|
45 |
|
|
}
|
46 |
|
|
|
47 |
|
|
public JsonElement put(String path, JsonObject body) {
|
48 |
|
|
RestTemplate restTemplate = new RestTemplate();
|
49 |
|
|
HttpHeaders headers = createHeaders(user, password);
|
50 |
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
51 |
|
|
HttpEntity<String> request = new HttpEntity<>(body.toString(), headers);
|
52 |
|
|
ResponseEntity<String> responseEntity = restTemplate.exchange(issuer + path, HttpMethod.PUT, request, String.class);
|
53 |
61063
|
k.triantaf
|
if (responseEntity.getBody() != null) {
|
54 |
59219
|
k.triantaf
|
return new JsonParser().parse(responseEntity.getBody());
|
55 |
|
|
} else {
|
56 |
|
|
return null;
|
57 |
|
|
}
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
public JsonElement get(String path, Map<String, String> params) {
|
61 |
|
|
RestTemplate restTemplate = new RestTemplate();
|
62 |
|
|
String url = issuer + path + ((params != null) ? createParams(params) : null);
|
63 |
|
|
ResponseEntity<String> responseEntity = restTemplate.exchange
|
64 |
|
|
(url, HttpMethod.GET, new HttpEntity<>(createHeaders(user, password)), String.class);
|
65 |
61063
|
k.triantaf
|
if (responseEntity.getBody() != null) {
|
66 |
59219
|
k.triantaf
|
return new JsonParser().parse(responseEntity.getBody());
|
67 |
|
|
} else {
|
68 |
|
|
return null;
|
69 |
|
|
}
|
70 |
|
|
}
|
71 |
|
|
|
72 |
|
|
public JsonElement delete(String path) {
|
73 |
|
|
RestTemplate restTemplate = new RestTemplate();
|
74 |
|
|
String url = issuer + path;
|
75 |
|
|
ResponseEntity<String> responseEntity = restTemplate.exchange
|
76 |
|
|
(url, HttpMethod.DELETE, new HttpEntity<>(createHeaders(user, password)), String.class);
|
77 |
61063
|
k.triantaf
|
if (responseEntity.getBody() != null) {
|
78 |
59219
|
k.triantaf
|
return new JsonParser().parse(responseEntity.getBody());
|
79 |
|
|
} else {
|
80 |
|
|
return null;
|
81 |
|
|
}
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
|
85 |
|
|
private String createParams(Map<String, String> params) {
|
86 |
|
|
StringBuilder ret = new StringBuilder("?");
|
87 |
|
|
int count = 0;
|
88 |
|
|
for (Map.Entry<String, String> param : params.entrySet()) {
|
89 |
61063
|
k.triantaf
|
ret.append(param.getKey()).append("=");
|
90 |
61159
|
k.triantaf
|
ret.append(param.getValue());
|
91 |
59219
|
k.triantaf
|
count++;
|
92 |
|
|
if (count != params.entrySet().size()) {
|
93 |
|
|
ret.append("&");
|
94 |
|
|
}
|
95 |
|
|
}
|
96 |
|
|
return ret.toString();
|
97 |
|
|
}
|
98 |
|
|
|
99 |
|
|
private HttpHeaders createHeaders(String username, String password) {
|
100 |
|
|
return new HttpHeaders() {{
|
101 |
|
|
String auth = username + ":" + password;
|
102 |
|
|
byte[] encodedAuth = Base64.encodeBase64(
|
103 |
|
|
auth.getBytes(Charset.forName("US-ASCII")));
|
104 |
|
|
String authHeader = "Basic " + new String(encodedAuth);
|
105 |
|
|
set("Authorization", authHeader);
|
106 |
|
|
}};
|
107 |
|
|
}
|
108 |
|
|
}
|