Inform the developer about the possibility of a "RestClientException" being thrown by various methods of "HttpUtils" and "RegistryCalls" classes.

This commit is contained in:
Lampros Smyrnaios 2023-03-08 15:26:26 +02:00
parent a0fd5f67a7
commit d2973871a8
2 changed files with 27 additions and 25 deletions

View File

@ -15,6 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
@ -55,7 +56,7 @@ public class RegistryCalls implements AaiRegistryService {
} }
@Override @Override
public List<String> getUserIdentifiersByCoPersonId(Integer coPersonId) { public List<String> getUserIdentifiersByCoPersonId(Integer coPersonId) throws RestClientException {
List<String> ids = new ArrayList<>(); List<String> ids = new ArrayList<>();
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("copersonid", coPersonId.toString()); params.put("copersonid", coPersonId.toString());
@ -86,7 +87,7 @@ public class RegistryCalls implements AaiRegistryService {
} }
@Override @Override
public List<Integer> getCoPersonIdsByEmail(String email) { public List<Integer> getCoPersonIdsByEmail(String email) throws RestClientException {
List<Integer> coPersonIds = new ArrayList<>(); List<Integer> coPersonIds = new ArrayList<>();
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("mail", email); params.put("mail", email);
@ -113,7 +114,7 @@ public class RegistryCalls implements AaiRegistryService {
} }
} }
public Integer getCoPersonIdByIdentifier(String sub) { public Integer getCoPersonIdByIdentifier(String sub) throws RestClientException {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("search.identifier", sub); params.put("search.identifier", sub);
params.put("coid", coid); params.put("coid", coid);
@ -122,7 +123,7 @@ public class RegistryCalls implements AaiRegistryService {
} }
@Override @Override
public JsonArray getCous(String name) { public JsonArray getCous(String name) throws RestClientException {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
if (name != null) { if (name != null) {
try { try {
@ -163,7 +164,7 @@ public class RegistryCalls implements AaiRegistryService {
} }
@Override @Override
public JsonArray getRoles(Integer coPersonId) { public JsonArray getRoles(Integer coPersonId) throws RestClientException {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("coid", coid); params.put("coid", coid);
params.put("copersonid", coPersonId.toString()); params.put("copersonid", coPersonId.toString());
@ -204,7 +205,7 @@ public class RegistryCalls implements AaiRegistryService {
} }
@Override @Override
public JsonArray getUserGroups(Integer coPersonId) { public JsonArray getUserGroups(Integer coPersonId) throws RestClientException {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("copersonid", coPersonId.toString()); params.put("copersonid", coPersonId.toString());
JsonElement response = httpUtils.get("co_groups.json", params); JsonElement response = httpUtils.get("co_groups.json", params);
@ -212,7 +213,7 @@ public class RegistryCalls implements AaiRegistryService {
} }
@Override @Override
public JsonObject getUserAdminGroup(Integer coPersonId, Integer couId) { public JsonObject getUserAdminGroup(Integer coPersonId, Integer couId) throws RestClientException {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("copersonid", coPersonId.toString()); params.put("copersonid", coPersonId.toString());
JsonElement response = httpUtils.get("co_groups.json", params); JsonElement response = httpUtils.get("co_groups.json", params);
@ -229,7 +230,7 @@ public class RegistryCalls implements AaiRegistryService {
} }
@Override @Override
public JsonArray getCouGroups(Integer couId) { public JsonArray getCouGroups(Integer couId) throws RestClientException {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("couid", couId.toString()); params.put("couid", couId.toString());
JsonElement response = httpUtils.get("co_groups.json", params); JsonElement response = httpUtils.get("co_groups.json", params);
@ -248,7 +249,7 @@ public class RegistryCalls implements AaiRegistryService {
} }
@Override @Override
public JsonArray getGroupMembers(Integer coGroupId) { public JsonArray getGroupMembers(Integer coGroupId) throws RestClientException {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("cogroupid", coGroupId.toString()); params.put("cogroupid", coGroupId.toString());
JsonElement response = httpUtils.get("co_group_members.json", params); JsonElement response = httpUtils.get("co_group_members.json", params);
@ -257,7 +258,7 @@ public class RegistryCalls implements AaiRegistryService {
@Override @Override
public JsonArray getUserEmailByCouId(Integer couId, boolean admin) { public JsonArray getUserEmailByCouId(Integer couId, boolean admin) throws RestClientException {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
if (couId == null) { if (couId == null) {
throw new IllegalArgumentException("Provided 'couId' is null"); throw new IllegalArgumentException("Provided 'couId' is null");
@ -288,7 +289,7 @@ public class RegistryCalls implements AaiRegistryService {
} }
@Override @Override
public JsonArray getUsersByCouId(Integer couId) { public JsonArray getUsersByCouId(Integer couId) throws RestClientException {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("couid", couId.toString()); params.put("couid", couId.toString());
JsonElement response = httpUtils.get("co_person_roles.json", params); JsonElement response = httpUtils.get("co_person_roles.json", params);
@ -321,7 +322,7 @@ public class RegistryCalls implements AaiRegistryService {
} }
@Override @Override
public JsonArray getUserNamesByCouId(Integer couId, boolean admin) { public JsonArray getUserNamesByCouId(Integer couId, boolean admin) throws RestClientException {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("couid", couId.toString()); params.put("couid", couId.toString());
if (admin) { if (admin) {
@ -341,7 +342,7 @@ public class RegistryCalls implements AaiRegistryService {
} }
@Override @Override
public JsonArray getUserIdByCouId(Integer couId, boolean admin) { public JsonArray getUserIdByCouId(Integer couId, boolean admin) throws RestClientException {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("couid", couId.toString()); params.put("couid", couId.toString());
if (admin) { if (admin) {
@ -361,26 +362,26 @@ public class RegistryCalls implements AaiRegistryService {
} }
@Override @Override
public void assignMemberRole(Integer coPersonId, Integer couId) { public void assignMemberRole(Integer coPersonId, Integer couId) throws RestClientException {
httpUtils.post("co_person_roles.json", jsonUtils.coPersonRoles(coPersonId, couId, "Active")); httpUtils.post("co_person_roles.json", jsonUtils.coPersonRoles(coPersonId, couId, "Active"));
} }
@Override @Override
public void removeMemberRole(Integer coPersonId, Integer couId, Integer id) { public void removeMemberRole(Integer coPersonId, Integer couId, Integer id) throws RestClientException {
if (id != null) { if (id != null) {
httpUtils.put("co_person_roles/" + id + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Deleted")); httpUtils.put("co_person_roles/" + id + ".json", jsonUtils.coPersonRoles(coPersonId, couId, "Deleted"));
} }
} }
@Override @Override
public Integer createRole(Role role) { public Integer createRole(Role role) throws RestClientException {
JsonElement element = httpUtils.post("cous.json", jsonUtils.createNewCou(role)); JsonElement element = httpUtils.post("cous.json", jsonUtils.createNewCou(role));
return element.getAsJsonObject().get("Id").getAsInt(); return element.getAsJsonObject().get("Id").getAsInt();
} }
@Override @Override
public String getUserEmail(Integer coPersonId) { public String getUserEmail(Integer coPersonId) throws RestClientException {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("copersonid", coPersonId.toString()); params.put("copersonid", coPersonId.toString());
JsonElement response = httpUtils.get("email_addresses.json", params); JsonElement response = httpUtils.get("email_addresses.json", params);
@ -389,7 +390,7 @@ public class RegistryCalls implements AaiRegistryService {
} }
@Override @Override
public String getUserNames(Integer coPersonId) { public String getUserNames(Integer coPersonId) throws RestClientException {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("copersonid", coPersonId.toString()); params.put("copersonid", coPersonId.toString());
JsonElement response = httpUtils.get("names.json", params); JsonElement response = httpUtils.get("names.json", params);
@ -402,7 +403,7 @@ public class RegistryCalls implements AaiRegistryService {
} }
@Override @Override
public String getUserId(Integer coPersonId) { public String getUserId(Integer coPersonId) throws RestClientException {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("copersonid", coPersonId.toString()); params.put("copersonid", coPersonId.toString());
JsonElement response = httpUtils.get("identifiers.json", params); JsonElement response = httpUtils.get("identifiers.json", params);
@ -411,7 +412,7 @@ public class RegistryCalls implements AaiRegistryService {
} }
@Override @Override
public void assignAdminRole(Integer coPersonId, Integer couId) { public void assignAdminRole(Integer coPersonId, Integer couId) throws RestClientException {
JsonObject group = getCouAdminGroup(couId); JsonObject group = getCouAdminGroup(couId);
if (group != null) { if (group != null) {
httpUtils.post("co_group_members.json", jsonUtils.coGroupMembers(group.get("Id").getAsInt(), coPersonId, true)); httpUtils.post("co_group_members.json", jsonUtils.coGroupMembers(group.get("Id").getAsInt(), coPersonId, true));
@ -419,7 +420,7 @@ public class RegistryCalls implements AaiRegistryService {
} }
@Override @Override
public void removeAdminRole(Integer coPersonId, Integer couId) { public void removeAdminRole(Integer coPersonId, Integer couId) throws RestClientException {
JsonObject adminGroup = this.getCouAdminGroup(couId); JsonObject adminGroup = this.getCouAdminGroup(couId);
JsonArray admins = this.getGroupMembers(adminGroup.get("Id").getAsInt()); JsonArray admins = this.getGroupMembers(adminGroup.get("Id").getAsInt());
Integer id = null; Integer id = null;

View File

@ -10,6 +10,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*; import org.springframework.http.*;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents; import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder; import org.springframework.web.util.UriComponentsBuilder;
@ -32,7 +33,7 @@ public class HttpUtils {
@Value("${services.provide.aai.registry.password}") @Value("${services.provide.aai.registry.password}")
private String password; private String password;
public JsonElement post(String path, JsonObject body) { public JsonElement post(String path, JsonObject body) throws RestClientException {
RestTemplate restTemplate = new RestTemplate(); RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = createHeaders(user, password); HttpHeaders headers = createHeaders(user, password);
headers.setContentType(MediaType.APPLICATION_JSON); headers.setContentType(MediaType.APPLICATION_JSON);
@ -41,7 +42,7 @@ public class HttpUtils {
return getResponseEntityAsJsonElement(responseEntity); return getResponseEntityAsJsonElement(responseEntity);
} }
public JsonElement put(String path, JsonObject body) { public JsonElement put(String path, JsonObject body) throws RestClientException {
RestTemplate restTemplate = new RestTemplate(); RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = createHeaders(user, password); HttpHeaders headers = createHeaders(user, password);
headers.setContentType(MediaType.APPLICATION_JSON); headers.setContentType(MediaType.APPLICATION_JSON);
@ -50,7 +51,7 @@ public class HttpUtils {
return getResponseEntityAsJsonElement(responseEntity); return getResponseEntityAsJsonElement(responseEntity);
} }
public JsonElement get(String path, Map<String, String> params) { public JsonElement get(String path, Map<String, String> params) throws RestClientException {
RestTemplate restTemplate = new RestTemplate(); RestTemplate restTemplate = new RestTemplate();
String url = createUrl(registryUrl + path, params); String url = createUrl(registryUrl + path, params);
ResponseEntity<String> responseEntity = restTemplate.exchange ResponseEntity<String> responseEntity = restTemplate.exchange
@ -58,7 +59,7 @@ public class HttpUtils {
return getResponseEntityAsJsonElement(responseEntity); return getResponseEntityAsJsonElement(responseEntity);
} }
public JsonElement delete(String path) { public JsonElement delete(String path) throws RestClientException {
RestTemplate restTemplate = new RestTemplate(); RestTemplate restTemplate = new RestTemplate();
String url = registryUrl + path; String url = registryUrl + path;
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.DELETE, new HttpEntity<>(createHeaders(user, password)), String.class); ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.DELETE, new HttpEntity<>(createHeaders(user, password)), String.class);