[Users | Trunk]: 1. Http utils: Encode params. 2. Registry calls: Add name parameter for getting cous base on a name. 3. Return coudId on role creation

This commit is contained in:
Konstantinos Triantafyllou 2021-06-02 13:40:45 +00:00
parent 2cc14ea7d7
commit 6665403c55
3 changed files with 49 additions and 36 deletions

View File

@ -12,13 +12,11 @@ import eu.dnetlib.openaire.usermanagement.utils.JsonUtils;
import eu.dnetlib.openaire.usermanagement.utils.RegistryCalls; import eu.dnetlib.openaire.usermanagement.utils.RegistryCalls;
import eu.dnetlib.openaire.usermanagement.utils.VerificationUtils; import eu.dnetlib.openaire.usermanagement.utils.VerificationUtils;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.security.access.method.P; import org.springframework.security.access.method.P;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
@ -118,8 +116,11 @@ public class RegistryService {
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)") @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
public Response createRole(@RequestBody Role role) { public Response createRole(@RequestBody Role role) {
if (calls.getCouId(role.getName()) == null) { if (calls.getCouId(role.getName()) == null) {
calls.createRole(role); if(calls.createRole(role) != null) {
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been created").toString()).type(MediaType.APPLICATION_JSON).build(); return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been created").toString()).type(MediaType.APPLICATION_JSON).build();
} else {
return Response.status(HttpStatus.BAD_REQUEST.value()).entity(jsonUtils.createResponse("An error has occurred. Please try again later").toString()).type(MediaType.APPLICATION_JSON).build();
}
} else { } else {
return Response.status(HttpStatus.CONFLICT.value()).entity(jsonUtils.createResponse("Role has already existed").toString()).type(MediaType.APPLICATION_JSON).build(); return Response.status(HttpStatus.CONFLICT.value()).entity(jsonUtils.createResponse("Role has already existed").toString()).type(MediaType.APPLICATION_JSON).build();
} }

View File

@ -10,7 +10,10 @@ import org.springframework.http.*;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Map; import java.util.Map;
@Component @Component
@ -82,7 +85,13 @@ public class HttpUtils {
StringBuilder ret = new StringBuilder("?"); StringBuilder ret = new StringBuilder("?");
int count = 0; int count = 0;
for (Map.Entry<String, String> param : params.entrySet()) { for (Map.Entry<String, String> param : params.entrySet()) {
ret.append(param.getKey()).append("=").append(param.getValue()); ret.append(param.getKey()).append("=");
try {
ret.append(URLEncoder.encode(param.getValue(), StandardCharsets.UTF_8.toString()));
} catch (UnsupportedEncodingException e) {
logger.error("UnsupportedEncodingException on param " + param);
return null;
}
count++; count++;
if (count != params.entrySet().size()) { if (count != params.entrySet().size()) {
ret.append("&"); ret.append("&");

View File

@ -115,26 +115,36 @@ public class RegistryCalls {
} }
/** /**
* 3. Get all OpenAIRE cous * 3.1 Get OpenAIRE cous with a specific name(or substring)
*/ */
public JsonArray getCous() { public JsonArray getCous(String name) {
Map<String, String> params = new HashMap<>(); Map<String, String> params = new HashMap<>();
params.put("coid", coid); params.put("coid", coid);
if(name != null) {
params.put("name", name.toLowerCase());
}
JsonElement response = httpUtils.get("cous.json", params); JsonElement response = httpUtils.get("cous.json", params);
return (response != null) ? response.getAsJsonObject().get("Cous").getAsJsonArray() : new JsonArray(); return (response != null) ? response.getAsJsonObject().get("Cous").getAsJsonArray() : new JsonArray();
} }
/** /**
* 4. Get a couId by name * 3.2 Get all OpenAIRE cous
*/
public JsonArray getCous() {
return getCous(null);
}
/**
* 4.1 Get a couId by name
* *
* @param name * @param name
* @return * @return
*/ */
public Integer getCouId(String name) { public Integer getCouId(String name) {
JsonArray cous = getCous(); JsonArray cous = getCous(name);
Integer couId = null; Integer couId = null;
for (JsonElement cou : cous) { for (JsonElement cou : cous) {
if (cou.getAsJsonObject().get("Name").getAsString().equals(name)) { if (cou.getAsJsonObject().get("Name").getAsString().toLowerCase().equals(name.toLowerCase())) {
couId = cou.getAsJsonObject().get("Id").getAsInt(); couId = cou.getAsJsonObject().get("Id").getAsInt();
} }
} }
@ -142,7 +152,18 @@ public class RegistryCalls {
} }
/** /**
* 4. Get a couId by type.id * 4.2 Get a couId by type.id with/without mapping type
*
* @param type
* @param id
* @return
*/
public Integer getCouId(String type, String id, boolean communityMap) {
return getCouId(mapType(type, communityMap) + "." + id);
}
/**
* 4.3 Get a couId by type.id with mapping type
* *
* @param type * @param type
* @param id * @param id
@ -152,25 +173,6 @@ public class RegistryCalls {
return getCouId(type, id, true); return getCouId(type, id, true);
} }
/**
* 4. Get a couId by type.id without mapping type
*
* @param type
* @param id
* @return
*/
public Integer getCouId(String type, String id, boolean communityMap) {
JsonArray cous = getCous();
Integer couId = null;
for (JsonElement cou : cous) {
if (cou.getAsJsonObject().get("Name").getAsString().equals(mapType(type, communityMap) + "." + id)) {
couId = cou.getAsJsonObject().get("Id").getAsInt();
}
}
return couId;
}
/** /**
* 5. Get User non admin roles * 5. Get User non admin roles
*/ */
@ -354,8 +356,9 @@ public class RegistryCalls {
/** /**
* 17. Create a new role * 17. Create a new role
*/ */
public void createRole(Role role) { public Integer createRole(Role role) {
httpUtils.post("cous.json", jsonUtils.createNewCou(role)); JsonElement element = httpUtils.post("cous.json", jsonUtils.createNewCou(role));
return element.getAsJsonObject().get("Id").getAsInt();
} }
/** /**