2021-07-21 13:51:18 +02:00
|
|
|
package eu.dnetlib.repo.manager.utils;
|
|
|
|
|
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import com.google.gson.JsonParser;
|
|
|
|
import org.apache.commons.codec.binary.Base64;
|
2023-01-11 17:50:31 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2021-07-21 13:51:18 +02:00
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
import org.springframework.http.*;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
2022-12-07 12:37:55 +01:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2021-07-21 13:51:18 +02:00
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
@Component
|
|
|
|
public class HttpUtils {
|
|
|
|
|
2023-01-11 17:50:31 +01:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);
|
2021-07-21 13:51:18 +02:00
|
|
|
|
2021-10-26 13:46:08 +02:00
|
|
|
@Value("${services.provide.aai.registry.url}")
|
2021-07-21 13:51:18 +02:00
|
|
|
private String registryUrl;
|
|
|
|
|
2021-10-26 13:46:08 +02:00
|
|
|
@Value("${services.provide.aai.registry.username}")
|
2021-07-21 13:51:18 +02:00
|
|
|
private String user;
|
|
|
|
|
2021-10-26 13:46:08 +02:00
|
|
|
@Value("${services.provide.aai.registry.password}")
|
2021-07-21 13:51:18 +02:00
|
|
|
private String password;
|
|
|
|
|
|
|
|
public JsonElement post(String path, JsonObject body) {
|
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
|
HttpHeaders headers = createHeaders(user, password);
|
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
HttpEntity<String> request = new HttpEntity<>(body.toString(), headers);
|
|
|
|
ResponseEntity<String> responseEntity = restTemplate.exchange(registryUrl + path, HttpMethod.POST, request, String.class);
|
|
|
|
return getResponseEntityAsJsonElement(responseEntity);
|
|
|
|
}
|
|
|
|
|
|
|
|
public JsonElement put(String path, JsonObject body) {
|
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
|
HttpHeaders headers = createHeaders(user, password);
|
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
HttpEntity<String> request = new HttpEntity<>(body.toString(), headers);
|
|
|
|
ResponseEntity<String> responseEntity = restTemplate.exchange(registryUrl + path, HttpMethod.PUT, request, String.class);
|
|
|
|
return getResponseEntityAsJsonElement(responseEntity);
|
|
|
|
}
|
|
|
|
|
|
|
|
public JsonElement get(String path, Map<String, String> params) {
|
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
|
String url = registryUrl + path + ((params != null) ? createParams(params) : null);
|
|
|
|
ResponseEntity<String> responseEntity = restTemplate.exchange
|
|
|
|
(url, HttpMethod.GET, new HttpEntity<>(createHeaders(user, password)), String.class);
|
|
|
|
return getResponseEntityAsJsonElement(responseEntity);
|
|
|
|
}
|
|
|
|
|
|
|
|
public JsonElement delete(String path) {
|
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
|
String url = registryUrl + path;
|
|
|
|
ResponseEntity<String> responseEntity = restTemplate.exchange
|
|
|
|
(url, HttpMethod.DELETE, new HttpEntity<>(createHeaders(user, password)), String.class);
|
|
|
|
return getResponseEntityAsJsonElement(responseEntity);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private String createParams(Map<String, String> params) {
|
|
|
|
StringBuilder ret = new StringBuilder("?");
|
|
|
|
int count = 0;
|
|
|
|
for (Map.Entry<String, String> param : params.entrySet()) {
|
|
|
|
ret.append(param.getKey()).append("=").append(param.getValue());
|
|
|
|
count++;
|
|
|
|
if (count != params.entrySet().size()) {
|
|
|
|
ret.append("&");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
private HttpHeaders createHeaders(String username, String password) {
|
|
|
|
return new HttpHeaders() {{
|
|
|
|
String auth = username + ":" + password;
|
2022-12-07 12:37:55 +01:00
|
|
|
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(StandardCharsets.US_ASCII));
|
2021-07-21 13:51:18 +02:00
|
|
|
String authHeader = "Basic " + new String(encodedAuth);
|
|
|
|
set("Authorization", authHeader);
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
private JsonElement getResponseEntityAsJsonElement(ResponseEntity<String> responseEntity) {
|
|
|
|
if (responseEntity != null && responseEntity.getBody() != null) {
|
2022-09-07 15:29:04 +02:00
|
|
|
logger.debug(responseEntity.getBody());
|
2021-07-21 13:51:18 +02:00
|
|
|
try {
|
|
|
|
return new JsonParser().parse(responseEntity.getBody());
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.warn("Could not parse response body", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|