105 lines
3.9 KiB
Java
105 lines
3.9 KiB
Java
package eu.dnetlib.openaire.usermanagement.utils;
|
|
|
|
import com.google.gson.JsonElement;
|
|
import com.google.gson.JsonObject;
|
|
import com.google.gson.JsonParser;
|
|
import org.apache.commons.codec.binary.Base64;
|
|
import org.apache.log4j.Logger;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.http.*;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
import java.nio.charset.Charset;
|
|
import java.util.Map;
|
|
|
|
@Component
|
|
public class HttpUtils {
|
|
|
|
private static final Logger logger = Logger.getLogger(HttpUtils.class);
|
|
|
|
@Value("${registry.issuer}")
|
|
private String issuer;
|
|
|
|
@Value("${registry.user}")
|
|
private String user;
|
|
|
|
@Value("${registry.password}")
|
|
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(issuer + path, HttpMethod.POST, request, String.class);
|
|
if (responseEntity.getBody() != null) {
|
|
return new JsonParser().parse(responseEntity.getBody());
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
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(issuer + path, HttpMethod.PUT, request, String.class);
|
|
if (responseEntity.getBody() != null) {
|
|
return new JsonParser().parse(responseEntity.getBody());
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public JsonElement get(String path, Map<String, String> params) {
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
String url = issuer + path + ((params != null) ? createParams(params) : null);
|
|
ResponseEntity<String> responseEntity = restTemplate.exchange
|
|
(url, HttpMethod.GET, new HttpEntity<>(createHeaders(user, password)), String.class);
|
|
if (responseEntity.getBody() != null) {
|
|
return new JsonParser().parse(responseEntity.getBody());
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public JsonElement delete(String path) {
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
String url = issuer + path;
|
|
ResponseEntity<String> responseEntity = restTemplate.exchange
|
|
(url, HttpMethod.DELETE, new HttpEntity<>(createHeaders(user, password)), String.class);
|
|
if (responseEntity.getBody() != null) {
|
|
return new JsonParser().parse(responseEntity.getBody());
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
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("=");
|
|
ret.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;
|
|
byte[] encodedAuth = Base64.encodeBase64(
|
|
auth.getBytes(Charset.forName("US-ASCII")));
|
|
String authHeader = "Basic " + new String(encodedAuth);
|
|
set("Authorization", authHeader);
|
|
}};
|
|
}
|
|
}
|