Merge branch 'dmp-refactoring' of https://code-repo.d4science.org/MaDgiK-CITE/argos into dmp-refactoring

This commit is contained in:
Sofia Papacharalampous 2024-04-05 11:39:57 +03:00
commit f82501344c
6 changed files with 52 additions and 14 deletions

View File

@ -20,14 +20,16 @@ public class AffiliationCacheService extends CacheService<AffiliationCacheServic
public AffiliationCacheValue() {
}
public AffiliationCacheValue(UUID userId, UUID entityId, String entityType, AffiliatedResource affiliatedResource) {
public AffiliationCacheValue(UUID tenantId, UUID userId, UUID entityId, String entityType, AffiliatedResource affiliatedResource) {
this.userId = userId;
this.tenantId = tenantId;
this.entityId = entityId;
this.entityType = entityType;
this.affiliatedResource = affiliatedResource;
}
private UUID userId;
private UUID tenantId;
private UUID entityId;
private String entityType;
private AffiliatedResource affiliatedResource;
@ -40,6 +42,14 @@ public class AffiliationCacheService extends CacheService<AffiliationCacheServic
this.userId = userId;
}
public UUID getTenantId() {
return tenantId;
}
public void setTenantId(UUID tenantId) {
this.tenantId = tenantId;
}
public UUID getEntityId() {
return entityId;
}
@ -79,17 +89,18 @@ public class AffiliationCacheService extends CacheService<AffiliationCacheServic
@Override
public String keyOf(AffiliationCacheValue value) {
return this.buildKey(value.getUserId(), value.getEntityId(), value.getEntityType());
return this.buildKey(value.getTenantId(), value.getUserId(), value.getEntityId(), value.getEntityType());
}
public String buildKey(UUID userId, UUID entityId, String entityType) {
public String buildKey(UUID tenantId, UUID userId, UUID entityId, String entityType) {
if (userId == null) throw new IllegalArgumentException("userId id is required");
if (entityId == null) throw new IllegalArgumentException("entityId id is required");
if (this.conventionService.isNullOrEmpty(entityType)) throw new IllegalArgumentException("entityType id is required");
HashMap<String, String> keyParts = new HashMap<>();
keyParts.put("$user$", userId.toString().replace("-", "").toLowerCase(Locale.ROOT));
keyParts.put("$tenant$", tenantId == null ? "" : userId.toString().replace("-", "").toLowerCase(Locale.ROOT));
keyParts.put("$entity$", entityId.toString().replace("-", "").toLowerCase(Locale.ROOT));
keyParts.put("$type$", entityType);
return this.generateKey(keyParts);

View File

@ -3,6 +3,7 @@ package eu.eudat.authorization.authorizationcontentresolver;
import eu.eudat.authorization.AffiliatedResource;
import eu.eudat.authorization.PermissionNameProvider;
import eu.eudat.commons.enums.IsActive;
import eu.eudat.commons.scope.tenant.TenantScope;
import eu.eudat.commons.scope.user.UserScope;
import eu.eudat.data.DescriptionEntity;
import eu.eudat.data.DmpDescriptionTemplateEntity;
@ -19,6 +20,7 @@ import gr.cite.tools.fieldset.BaseFieldSet;
import org.springframework.stereotype.Service;
import org.springframework.web.context.annotation.RequestScope;
import javax.management.InvalidApplicationException;
import java.util.*;
import java.util.stream.Collectors;
@ -27,11 +29,13 @@ import java.util.stream.Collectors;
public class AuthorizationContentResolverImpl implements AuthorizationContentResolver {
private final QueryFactory queryFactory;
private final UserScope userScope;
private final TenantScope tenantScope;
private final AffiliationCacheService affiliationCacheService;
private final PermissionNameProvider permissionNameProvider;
public AuthorizationContentResolverImpl(QueryFactory queryFactory, UserScope userScope, AffiliationCacheService affiliationCacheService, PermissionNameProvider permissionNameProvider) {
public AuthorizationContentResolverImpl(QueryFactory queryFactory, UserScope userScope, TenantScope tenantScope, AffiliationCacheService affiliationCacheService, PermissionNameProvider permissionNameProvider) {
this.queryFactory = queryFactory;
this.userScope = userScope;
this.tenantScope = tenantScope;
this.affiliationCacheService = affiliationCacheService;
this.permissionNameProvider = permissionNameProvider;
}
@ -141,7 +145,12 @@ public class AuthorizationContentResolverImpl implements AuthorizationContentRes
private List<UUID> getAffiliatedFromCache(List<UUID> ids, UUID userId, Map<UUID, AffiliatedResource> affiliatedResources, String entityType) {
List<UUID> idsToResolve = new ArrayList<>();
for (UUID id : ids){
AffiliationCacheService.AffiliationCacheValue cacheValue = this.affiliationCacheService.lookup(this.affiliationCacheService.buildKey(userId, id, entityType));
AffiliationCacheService.AffiliationCacheValue cacheValue = null;
try {
cacheValue = this.affiliationCacheService.lookup(this.affiliationCacheService.buildKey(this.tenantScope.isSet() ? this.tenantScope.getTenant(): null, userId, id, entityType));
} catch (InvalidApplicationException e) {
throw new RuntimeException(e);
}
if (cacheValue != null) affiliatedResources.put(id, cacheValue.getAffiliatedResource());
else idsToResolve.add(id);
}
@ -152,7 +161,12 @@ public class AuthorizationContentResolverImpl implements AuthorizationContentRes
for (UUID id : idsToResolve){
AffiliatedResource affiliatedResource = affiliatedResources.getOrDefault(id, null);
if (affiliatedResource != null) {
AffiliationCacheService.AffiliationCacheValue cacheValue = new AffiliationCacheService.AffiliationCacheValue(userId, id, entityType, affiliatedResource);
AffiliationCacheService.AffiliationCacheValue cacheValue = null;
try {
cacheValue = new AffiliationCacheService.AffiliationCacheValue(this.tenantScope.isSet() ? this.tenantScope.getTenant(): null, userId, id, entityType, affiliatedResource);
} catch (InvalidApplicationException e) {
throw new RuntimeException(e);
}
this.affiliationCacheService.put(cacheValue);
}
}

View File

@ -110,4 +110,4 @@ cache:
keyPattern: resolve_$keyhash$:v0
affiliation:
name: affiliation
keyPattern: affiliation_$entity$_$user$_$type$:v0
keyPattern: affiliation_$entity$_$user$_$tenant$_$type$:v0

View File

@ -10,11 +10,12 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.2</version>
<version>3.2.4</version>
</parent>
<modules>
<module>queryable</module>
<module>../dmp-backend/core</module>
<module>web</module>
<module>data</module>
<module>elastic</module>
@ -301,16 +302,26 @@
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
</dependency>
<dependency>
<groupId>org.jboss</groupId>
<artifactId>jandex</artifactId>
<version>3.1.2</version>
</dependency>
<!--CITE DEPENDENCIES-->
<dependency>
<groupId>gr.cite</groupId>
<artifactId>data-tools</artifactId>
<version>2.1.0</version>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>gr.cite</groupId>
<artifactId>exceptions</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>gr.cite</groupId>
<artifactId>oidc-authz</artifactId>
<version>2.1.0</version>
</dependency>

View File

@ -387,7 +387,8 @@ public class RemoteFetcher {
JsonNode jsonBody = new ObjectMapper().readTree(replacedBody);
entity = new HttpEntity<>(jsonBody, headers);
response = restTemplate.exchange(replacedPath, HttpMethod.resolve(requestType), entity, String.class);
// response = restTemplate.exchange(replacedPath, HttpMethod.resolve(requestType), entity, String.class);
response = null;
if (response.getStatusCode() == HttpStatus.OK) {
if (response.getHeaders().get("Content-Type").get(0).contains("json")) {
DocumentContext jsonContext = JsonPath.parse(response.getBody());
@ -423,7 +424,8 @@ public class RemoteFetcher {
JsonNode jsonBody = new ObjectMapper().readTree(requestBody);
entity = new HttpEntity<>(jsonBody, headers);
response = restTemplate.exchange(urlString, HttpMethod.resolve(requestType), entity, String.class);
// response = restTemplate.exchange(urlString, HttpMethod.resolve(requestType), entity, String.class);
response = null;
if (response.getStatusCode() == HttpStatus.OK) { // success
//do here all the parsing
Results results = new Results();

View File

@ -25,6 +25,7 @@ import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.exception.MyApplicationException;
import gr.cite.tools.logging.LoggerService;
import jakarta.persistence.EntityManager;
import jakarta.ws.rs.NotSupportedException;
import jakarta.xml.bind.JAXBException;
import org.jetbrains.annotations.NotNull;
import org.slf4j.LoggerFactory;
@ -34,7 +35,6 @@ import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import javax.management.InvalidApplicationException;
import javax.ws.rs.NotSupportedException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import java.io.IOException;