1 |
60749
|
akoulalis
|
package eu.dnetlib.repo.manager.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 |
|
|
import java.nio.charset.Charset;
|
14 |
|
|
import java.util.Map;
|
15 |
|
|
|
16 |
|
|
@Component
|
17 |
|
|
public class HttpUtils {
|
18 |
|
|
|
19 |
|
|
private static final Logger logger = Logger.getLogger(HttpUtils.class);
|
20 |
|
|
|
21 |
61318
|
spyroukon
|
//TODO: refactor
|
22 |
60749
|
akoulalis
|
// TODO: To be changed the values
|
23 |
|
|
// @Value("https://aai.openaire.eu/registry/")
|
24 |
|
|
@Value("https://openaire-dev.aai-dev.grnet.gr/registry/")
|
25 |
|
|
private String issuer;
|
26 |
|
|
|
27 |
|
|
@Value("kostis30fylloy")
|
28 |
|
|
private String user;
|
29 |
|
|
|
30 |
|
|
@Value("fynhWc7F*2y4me4U")
|
31 |
|
|
private String password;
|
32 |
|
|
|
33 |
|
|
public JsonElement post(String path, JsonObject body) {
|
34 |
|
|
RestTemplate restTemplate = new RestTemplate();
|
35 |
|
|
HttpHeaders headers = createHeaders(user, password);
|
36 |
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
37 |
|
|
HttpEntity<String> request = new HttpEntity<>(body.toString(), headers);
|
38 |
|
|
ResponseEntity<String> responseEntity = restTemplate.exchange(issuer + path, HttpMethod.POST, request, String.class);
|
39 |
61363
|
spyroukon
|
return getResponseEntityAsJsonElement(responseEntity);
|
40 |
60749
|
akoulalis
|
}
|
41 |
|
|
|
42 |
|
|
public JsonElement put(String path, JsonObject body) {
|
43 |
|
|
RestTemplate restTemplate = new RestTemplate();
|
44 |
|
|
HttpHeaders headers = createHeaders(user, password);
|
45 |
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
46 |
|
|
HttpEntity<String> request = new HttpEntity<>(body.toString(), headers);
|
47 |
|
|
ResponseEntity<String> responseEntity = restTemplate.exchange(issuer + path, HttpMethod.PUT, request, String.class);
|
48 |
61363
|
spyroukon
|
return getResponseEntityAsJsonElement(responseEntity);
|
49 |
60749
|
akoulalis
|
}
|
50 |
|
|
|
51 |
|
|
public JsonElement get(String path, Map<String, String> params) {
|
52 |
|
|
RestTemplate restTemplate = new RestTemplate();
|
53 |
|
|
String url = issuer + path + ((params != null) ? createParams(params) : null);
|
54 |
|
|
ResponseEntity<String> responseEntity = restTemplate.exchange
|
55 |
|
|
(url, HttpMethod.GET, new HttpEntity<>(createHeaders(user, password)), String.class);
|
56 |
61363
|
spyroukon
|
return getResponseEntityAsJsonElement(responseEntity);
|
57 |
60749
|
akoulalis
|
}
|
58 |
|
|
|
59 |
|
|
public JsonElement delete(String path) {
|
60 |
|
|
RestTemplate restTemplate = new RestTemplate();
|
61 |
|
|
String url = issuer + path;
|
62 |
|
|
ResponseEntity<String> responseEntity = restTemplate.exchange
|
63 |
|
|
(url, HttpMethod.DELETE, new HttpEntity<>(createHeaders(user, password)), String.class);
|
64 |
61363
|
spyroukon
|
return getResponseEntityAsJsonElement(responseEntity);
|
65 |
60749
|
akoulalis
|
}
|
66 |
|
|
|
67 |
|
|
|
68 |
|
|
private String createParams(Map<String, String> params) {
|
69 |
|
|
StringBuilder ret = new StringBuilder("?");
|
70 |
|
|
int count = 0;
|
71 |
|
|
for (Map.Entry<String, String> param : params.entrySet()) {
|
72 |
|
|
ret.append(param.getKey()).append("=").append(param.getValue());
|
73 |
|
|
count++;
|
74 |
|
|
if (count != params.entrySet().size()) {
|
75 |
|
|
ret.append("&");
|
76 |
|
|
}
|
77 |
|
|
}
|
78 |
|
|
return ret.toString();
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
private HttpHeaders createHeaders(String username, String password) {
|
82 |
|
|
return new HttpHeaders() {{
|
83 |
|
|
String auth = username + ":" + password;
|
84 |
|
|
byte[] encodedAuth = Base64.encodeBase64(
|
85 |
|
|
auth.getBytes(Charset.forName("US-ASCII")));
|
86 |
|
|
String authHeader = "Basic " + new String(encodedAuth);
|
87 |
|
|
set("Authorization", authHeader);
|
88 |
|
|
}};
|
89 |
|
|
}
|
90 |
61363
|
spyroukon
|
|
91 |
|
|
private JsonElement getResponseEntityAsJsonElement(ResponseEntity<String> responseEntity) {
|
92 |
|
|
if (responseEntity != null && responseEntity.getBody() != null) {
|
93 |
|
|
logger.info(responseEntity.getBody());
|
94 |
|
|
try {
|
95 |
|
|
return new JsonParser().parse(responseEntity.getBody());
|
96 |
|
|
} catch (Exception e) {
|
97 |
|
|
logger.warn("Could not parse response body", e);
|
98 |
|
|
}
|
99 |
|
|
}
|
100 |
|
|
return null;
|
101 |
|
|
}
|
102 |
60749
|
akoulalis
|
}
|