[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:
parent
2cc14ea7d7
commit
6665403c55
|
@ -12,13 +12,11 @@ import eu.dnetlib.openaire.usermanagement.utils.JsonUtils;
|
|||
import eu.dnetlib.openaire.usermanagement.utils.RegistryCalls;
|
||||
import eu.dnetlib.openaire.usermanagement.utils.VerificationUtils;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.security.access.method.P;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
|
@ -118,8 +116,11 @@ public class RegistryService {
|
|||
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
|
||||
public Response createRole(@RequestBody Role role) {
|
||||
if (calls.getCouId(role.getName()) == null) {
|
||||
calls.createRole(role);
|
||||
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been created").toString()).type(MediaType.APPLICATION_JSON).build();
|
||||
if(calls.createRole(role) != null) {
|
||||
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 {
|
||||
return Response.status(HttpStatus.CONFLICT.value()).entity(jsonUtils.createResponse("Role has already existed").toString()).type(MediaType.APPLICATION_JSON).build();
|
||||
}
|
||||
|
|
|
@ -10,7 +10,10 @@ import org.springframework.http.*;
|
|||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
|
@ -33,7 +36,7 @@ public class HttpUtils {
|
|||
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) {
|
||||
if (responseEntity.getBody() != null) {
|
||||
return new JsonParser().parse(responseEntity.getBody());
|
||||
} else {
|
||||
return null;
|
||||
|
@ -46,7 +49,7 @@ public class HttpUtils {
|
|||
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) {
|
||||
if (responseEntity.getBody() != null) {
|
||||
return new JsonParser().parse(responseEntity.getBody());
|
||||
} else {
|
||||
return null;
|
||||
|
@ -58,7 +61,7 @@ public class HttpUtils {
|
|||
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) {
|
||||
if (responseEntity.getBody() != null) {
|
||||
return new JsonParser().parse(responseEntity.getBody());
|
||||
} else {
|
||||
return null;
|
||||
|
@ -70,7 +73,7 @@ public class HttpUtils {
|
|||
String url = issuer + path;
|
||||
ResponseEntity<String> responseEntity = restTemplate.exchange
|
||||
(url, HttpMethod.DELETE, new HttpEntity<>(createHeaders(user, password)), String.class);
|
||||
if(responseEntity.getBody() != null) {
|
||||
if (responseEntity.getBody() != null) {
|
||||
return new JsonParser().parse(responseEntity.getBody());
|
||||
} else {
|
||||
return null;
|
||||
|
@ -82,7 +85,13 @@ public class HttpUtils {
|
|||
StringBuilder ret = new StringBuilder("?");
|
||||
int count = 0;
|
||||
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++;
|
||||
if (count != params.entrySet().size()) {
|
||||
ret.append("&");
|
||||
|
|
|
@ -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<>();
|
||||
params.put("coid", coid);
|
||||
if(name != null) {
|
||||
params.put("name", name.toLowerCase());
|
||||
}
|
||||
JsonElement response = httpUtils.get("cous.json", params);
|
||||
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
|
||||
* @return
|
||||
*/
|
||||
public Integer getCouId(String name) {
|
||||
JsonArray cous = getCous();
|
||||
JsonArray cous = getCous(name);
|
||||
Integer couId = null;
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -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 id
|
||||
|
@ -152,25 +173,6 @@ public class RegistryCalls {
|
|||
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
|
||||
*/
|
||||
|
@ -354,8 +356,9 @@ public class RegistryCalls {
|
|||
/**
|
||||
* 17. Create a new role
|
||||
*/
|
||||
public void createRole(Role role) {
|
||||
httpUtils.post("cous.json", jsonUtils.createNewCou(role));
|
||||
public Integer createRole(Role role) {
|
||||
JsonElement element = httpUtils.post("cous.json", jsonUtils.createNewCou(role));
|
||||
return element.getAsJsonObject().get("Id").getAsInt();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue