diff --git a/src/main/java/org/gridsuite/gateway/endpoints/EndPointElementServer.java b/src/main/java/org/gridsuite/gateway/endpoints/EndPointElementServer.java index 367c932..0e44a20 100644 --- a/src/main/java/org/gridsuite/gateway/endpoints/EndPointElementServer.java +++ b/src/main/java/org/gridsuite/gateway/endpoints/EndPointElementServer.java @@ -6,14 +6,9 @@ */ package org.gridsuite.gateway.endpoints; -import lombok.NonNull; -import org.gridsuite.gateway.dto.AccessControlInfos; import org.springframework.http.HttpMethod; -import org.springframework.http.server.RequestPath; -import org.springframework.http.server.reactive.ServerHttpRequest; import java.util.*; -import java.util.stream.Collectors; import static org.springframework.http.HttpMethod.*; @@ -28,56 +23,7 @@ public interface EndPointElementServer extends EndPointServer { PUT, POST, DELETE ); - static UUID getUuid(String uuid) { - try { - return UUID.fromString(uuid); - } catch (IllegalArgumentException e) { - return null; - } - } - - default UUID getElementUuidIfExist(@NonNull RequestPath path) { - return (path.elements().size() > 5) ? getUuid(path.elements().get(5).value()) : null; - } - - default boolean isAllowedMethod(HttpMethod httpMethod) { - return ALLOWED_HTTP_METHODS.contains(httpMethod); - } - default Set getUncontrolledRootPaths() { return Set.of(); } - - default boolean isNotControlledRootPath(String rootPath) { - return getUncontrolledRootPaths().contains(rootPath); - } - - @Override - default boolean hasElementsAccessControl() { - return true; - } - - default Optional getAccessControlInfos(@NonNull ServerHttpRequest request) { - RequestPath path = Objects.requireNonNull(request.getPath()); - UUID elementUuid = getElementUuidIfExist(path); - - // //{elementUuid} or //**?id= - HttpMethod httpMethod = Objects.requireNonNull(request.getMethod()); - if (httpMethod.equals(HEAD) || httpMethod.equals(GET)) { - if (elementUuid != null) { - return Optional.of(AccessControlInfos.create(List.of(elementUuid))); - } else { - if (request.getQueryParams().get(QUERY_PARAM_IDS) == null) { - return Optional.empty(); - } else { - List ids = request.getQueryParams().get(QUERY_PARAM_IDS); - List elementUuids = ids.stream().map(EndPointElementServer::getUuid).filter(Objects::nonNull).collect(Collectors.toList()); - return elementUuids.size() == ids.size() ? Optional.of(AccessControlInfos.create(elementUuids)) : Optional.empty(); - } - } - } else if (httpMethod.equals(POST) || httpMethod.equals(PUT) || httpMethod.equals(DELETE)) { - return elementUuid == null ? Optional.empty() : Optional.of(AccessControlInfos.create(List.of(elementUuid))); - } - return Optional.empty(); - } } diff --git a/src/main/java/org/gridsuite/gateway/endpoints/ExploreServer.java b/src/main/java/org/gridsuite/gateway/endpoints/ExploreServer.java index 148eeb9..569c438 100644 --- a/src/main/java/org/gridsuite/gateway/endpoints/ExploreServer.java +++ b/src/main/java/org/gridsuite/gateway/endpoints/ExploreServer.java @@ -6,24 +6,15 @@ */ package org.gridsuite.gateway.endpoints; -import lombok.NonNull; import org.gridsuite.gateway.ServiceURIsConfig; -import org.gridsuite.gateway.dto.AccessControlInfos; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.http.HttpMethod; -import org.springframework.http.server.RequestPath; -import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.stereotype.Component; -import java.util.*; /** * @author Slimane Amar */ @Component(value = ExploreServer.ENDPOINT_NAME) public class ExploreServer implements EndPointElementServer { - private static final Logger LOGGER = LoggerFactory.getLogger(ExploreServer.class); public static final String ENDPOINT_NAME = "explore"; @@ -36,11 +27,6 @@ public ExploreServer(ServiceURIsConfig servicesURIsConfig) { this.servicesURIsConfig = servicesURIsConfig; } - @Override - public UUID getElementUuidIfExist(@NonNull RequestPath path) { - return (path.elements().size() > 7) ? EndPointElementServer.getUuid(path.elements().get(7).value()) : null; - } - @Override public String getEndpointBaseUri() { return servicesURIsConfig.getExploreServerBaseUri(); @@ -50,57 +36,4 @@ public String getEndpointBaseUri() { public String getEndpointName() { return ENDPOINT_NAME; } - - @Override - public boolean hasElementsAccessControl() { - return true; - } - - private UUID getUniqueOptionalUuidFromParam(@NonNull ServerHttpRequest request, @NonNull String queryParamName) { - List ids = request.getQueryParams().get(queryParamName); - if (ids != null && ids.size() != 1) { - throw new IllegalArgumentException("There must be only one " + queryParamName); - } - if (ids != null) { - UUID uuid = EndPointElementServer.getUuid(ids.get(0)); - if (uuid == null) { - throw new IllegalArgumentException(queryParamName + " must be an UUID"); - } - return uuid; - } - return null; - } - - @Override - public Optional getAccessControlInfos(@NonNull ServerHttpRequest request) { - RequestPath path = Objects.requireNonNull(request.getPath()); - UUID elementUuid = getElementUuidIfExist(path); - if (Objects.requireNonNull(request.getMethod()) != HttpMethod.POST) { - return EndPointElementServer.super.getAccessControlInfos(request); - } - // Elements creation - if (elementUuid != null) { - return Optional.of(AccessControlInfos.create(List.of(elementUuid))); - } - try { - List uuidsToControl = new ArrayList<>(); - UUID duplicateFromUuid = getUniqueOptionalUuidFromParam(request, QUERY_PARAM_DUPLICATE_FROM_ID); - if (duplicateFromUuid != null) { - uuidsToControl.add(duplicateFromUuid); - } - UUID parentDirectoryUuid = getUniqueOptionalUuidFromParam(request, QUERY_PARAM_PARENT_DIRECTORY_ID); - if (parentDirectoryUuid != null) { - uuidsToControl.add(parentDirectoryUuid); - } - if (uuidsToControl.isEmpty()) { - // At least one of the param is required - return Optional.empty(); - } - // Check resources access - return Optional.of(AccessControlInfos.create(uuidsToControl)); - } catch (IllegalArgumentException e) { - LOGGER.error(e.getMessage()); - return Optional.empty(); - } - } }