Revision 61176
Added by Konstantinos Triantafyllou almost 3 years ago
modules/dnet-openaire-users/branches/beta/src/main/java/eu/dnetlib/openaire/usermanagement/utils/HttpUtils.java | ||
---|---|---|
33 | 33 |
headers.setContentType(MediaType.APPLICATION_JSON); |
34 | 34 |
HttpEntity<String> request = new HttpEntity<>(body.toString(), headers); |
35 | 35 |
ResponseEntity<String> responseEntity = restTemplate.exchange(issuer + path, HttpMethod.POST, request, String.class); |
36 |
if(responseEntity.getBody() != null) { |
|
36 |
if (responseEntity.getBody() != null) {
|
|
37 | 37 |
return new JsonParser().parse(responseEntity.getBody()); |
38 | 38 |
} else { |
39 | 39 |
return null; |
... | ... | |
46 | 46 |
headers.setContentType(MediaType.APPLICATION_JSON); |
47 | 47 |
HttpEntity<String> request = new HttpEntity<>(body.toString(), headers); |
48 | 48 |
ResponseEntity<String> responseEntity = restTemplate.exchange(issuer + path, HttpMethod.PUT, request, String.class); |
49 |
if(responseEntity.getBody() != null) { |
|
49 |
if (responseEntity.getBody() != null) {
|
|
50 | 50 |
return new JsonParser().parse(responseEntity.getBody()); |
51 | 51 |
} else { |
52 | 52 |
return null; |
... | ... | |
58 | 58 |
String url = issuer + path + ((params != null) ? createParams(params) : null); |
59 | 59 |
ResponseEntity<String> responseEntity = restTemplate.exchange |
60 | 60 |
(url, HttpMethod.GET, new HttpEntity<>(createHeaders(user, password)), String.class); |
61 |
if(responseEntity.getBody() != null) { |
|
61 |
if (responseEntity.getBody() != null) {
|
|
62 | 62 |
return new JsonParser().parse(responseEntity.getBody()); |
63 | 63 |
} else { |
64 | 64 |
return null; |
... | ... | |
70 | 70 |
String url = issuer + path; |
71 | 71 |
ResponseEntity<String> responseEntity = restTemplate.exchange |
72 | 72 |
(url, HttpMethod.DELETE, new HttpEntity<>(createHeaders(user, password)), String.class); |
73 |
if(responseEntity.getBody() != null) { |
|
73 |
if (responseEntity.getBody() != null) {
|
|
74 | 74 |
return new JsonParser().parse(responseEntity.getBody()); |
75 | 75 |
} else { |
76 | 76 |
return null; |
... | ... | |
82 | 82 |
StringBuilder ret = new StringBuilder("?"); |
83 | 83 |
int count = 0; |
84 | 84 |
for (Map.Entry<String, String> param : params.entrySet()) { |
85 |
ret.append(param.getKey()).append("=").append(param.getValue()); |
|
85 |
ret.append(param.getKey()).append("="); |
|
86 |
ret.append(param.getValue()); |
|
86 | 87 |
count++; |
87 | 88 |
if (count != params.entrySet().size()) { |
88 | 89 |
ret.append("&"); |
modules/dnet-openaire-users/branches/beta/src/main/java/eu/dnetlib/openaire/usermanagement/utils/RegistryCalls.java | ||
---|---|---|
115 | 115 |
} |
116 | 116 |
|
117 | 117 |
/** |
118 |
* 3. Get all OpenAIRE cous
|
|
118 |
* 3.1 Get OpenAIRE cous with a specific name(or substring)
|
|
119 | 119 |
*/ |
120 |
public JsonArray getCous() { |
|
120 |
public JsonArray getCous(String name) {
|
|
121 | 121 |
Map<String, String> params = new HashMap<>(); |
122 | 122 |
params.put("coid", coid); |
123 |
if(name != null) { |
|
124 |
params.put("name", name.toLowerCase()); |
|
125 |
} |
|
123 | 126 |
JsonElement response = httpUtils.get("cous.json", params); |
124 | 127 |
return (response != null) ? response.getAsJsonObject().get("Cous").getAsJsonArray() : new JsonArray(); |
125 | 128 |
} |
126 | 129 |
|
127 | 130 |
/** |
128 |
* 4. Get a couId by name |
|
131 |
* 3.2 Get all OpenAIRE cous |
|
132 |
*/ |
|
133 |
public JsonArray getCous() { |
|
134 |
return getCous(null); |
|
135 |
} |
|
136 |
|
|
137 |
/** |
|
138 |
* 4.1 Get a couId by name |
|
129 | 139 |
* |
130 | 140 |
* @param name |
131 | 141 |
* @return |
132 | 142 |
*/ |
133 | 143 |
public Integer getCouId(String name) { |
134 |
JsonArray cous = getCous(); |
|
135 |
Integer couId = null; |
|
144 |
JsonArray cous = getCous(name); |
|
136 | 145 |
for (JsonElement cou : cous) { |
137 |
if (cou.getAsJsonObject().get("Name").getAsString().equals(name)) {
|
|
138 |
couId = cou.getAsJsonObject().get("Id").getAsInt();
|
|
146 |
if (cou.getAsJsonObject().get("Name").getAsString().toLowerCase().equals(name.toLowerCase())) {
|
|
147 |
return cou.getAsJsonObject().get("Id").getAsInt();
|
|
139 | 148 |
} |
140 | 149 |
} |
141 |
return couId;
|
|
150 |
return null;
|
|
142 | 151 |
} |
143 | 152 |
|
144 | 153 |
/** |
145 |
* 4. Get a couId by type.id
|
|
154 |
* 4.2 Get a couId by type.id with/without mapping type
|
|
146 | 155 |
* |
147 | 156 |
* @param type |
148 | 157 |
* @param id |
149 | 158 |
* @return |
150 | 159 |
*/ |
151 |
public Integer getCouId(String type, String id) { |
|
152 |
return getCouId(type, id, true);
|
|
160 |
public Integer getCouId(String type, String id, boolean communityMap) {
|
|
161 |
return getCouId(mapType(type, communityMap) + "." + id);
|
|
153 | 162 |
} |
154 | 163 |
|
155 |
|
|
156 | 164 |
/** |
157 |
* 4. Get a couId by type.id without mapping type
|
|
165 |
* 4.3 Get a couId by type.id with mapping type
|
|
158 | 166 |
* |
159 | 167 |
* @param type |
160 | 168 |
* @param id |
161 | 169 |
* @return |
162 | 170 |
*/ |
163 |
public Integer getCouId(String type, String id, boolean communityMap) { |
|
164 |
JsonArray cous = getCous(); |
|
165 |
Integer couId = null; |
|
166 |
for (JsonElement cou : cous) { |
|
167 |
if (cou.getAsJsonObject().get("Name").getAsString().equals(mapType(type, communityMap) + "." + id)) { |
|
168 |
couId = cou.getAsJsonObject().get("Id").getAsInt(); |
|
169 |
} |
|
170 |
} |
|
171 |
return couId; |
|
171 |
public Integer getCouId(String type, String id) { |
|
172 |
return getCouId(type, id, true); |
|
172 | 173 |
} |
173 | 174 |
|
174 | 175 |
/** |
... | ... | |
354 | 355 |
/** |
355 | 356 |
* 17. Create a new role |
356 | 357 |
*/ |
357 |
public void createRole(Role role) { |
|
358 |
httpUtils.post("cous.json", jsonUtils.createNewCou(role)); |
|
358 |
public Integer createRole(Role role) { |
|
359 |
JsonElement element = httpUtils.post("cous.json", jsonUtils.createNewCou(role)); |
|
360 |
return element.getAsJsonObject().get("Id").getAsInt(); |
|
359 | 361 |
} |
360 | 362 |
|
361 | 363 |
/** |
modules/dnet-openaire-users/branches/beta/src/main/java/eu/dnetlib/openaire/usermanagement/api/RegistryService.java | ||
---|---|---|
12 | 12 |
import eu.dnetlib.openaire.usermanagement.utils.RegistryCalls; |
13 | 13 |
import eu.dnetlib.openaire.usermanagement.utils.VerificationUtils; |
14 | 14 |
import org.apache.log4j.Logger; |
15 |
import org.mitre.openid.connect.model.OIDCAuthenticationToken; |
|
16 | 15 |
import org.springframework.beans.factory.annotation.Autowired; |
17 | 16 |
import org.springframework.http.HttpStatus; |
18 | 17 |
import org.springframework.security.access.method.P; |
19 | 18 |
import org.springframework.security.access.prepost.PreAuthorize; |
20 | 19 |
import org.springframework.security.core.authority.SimpleGrantedAuthority; |
21 |
import org.springframework.security.core.context.SecurityContextHolder; |
|
22 | 20 |
import org.springframework.stereotype.Component; |
23 | 21 |
import org.springframework.web.bind.annotation.RequestBody; |
24 | 22 |
|
... | ... | |
118 | 116 |
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)") |
119 | 117 |
public Response createRole(@RequestBody Role role) { |
120 | 118 |
if (calls.getCouId(role.getName()) == null) { |
121 |
calls.createRole(role); |
|
122 |
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been created").toString()).type(MediaType.APPLICATION_JSON).build(); |
|
119 |
if(calls.createRole(role) != null) { |
|
120 |
return Response.status(HttpStatus.OK.value()).entity(jsonUtils.createResponse("Role has been created").toString()).type(MediaType.APPLICATION_JSON).build(); |
|
121 |
} else { |
|
122 |
return Response.status(HttpStatus.BAD_REQUEST.value()).entity(jsonUtils.createResponse("An error has occurred. Please try again later").toString()).type(MediaType.APPLICATION_JSON).build(); |
|
123 |
} |
|
123 | 124 |
} else { |
124 | 125 |
return Response.status(HttpStatus.CONFLICT.value()).entity(jsonUtils.createResponse("Role has already existed").toString()).type(MediaType.APPLICATION_JSON).build(); |
125 | 126 |
} |
modules/dnet-openaire-users/branches/beta/pom.xml | ||
---|---|---|
25 | 25 |
<dependency> |
26 | 26 |
<groupId>eu.dnetlib</groupId> |
27 | 27 |
<artifactId>uoa-login-core</artifactId> |
28 |
<version>1.0.0</version>
|
|
28 |
<version>1.0.1</version>
|
|
29 | 29 |
</dependency> |
30 | 30 |
<dependency> |
31 | 31 |
<groupId>org.slf4j</groupId> |
Also available in: Unified diff
1. Upgrade login-core 2. Search couId by name added.