Skip to content

Commit

Permalink
Remove unused code to improve coverage (#113)
Browse files Browse the repository at this point in the history
Signed-off-by: sBouzols <[email protected]>
  • Loading branch information
sBouzols authored Sep 25, 2024
1 parent c9e8cb2 commit f67b6c1
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand All @@ -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<String> getUncontrolledRootPaths() {
return Set.of();
}

default boolean isNotControlledRootPath(String rootPath) {
return getUncontrolledRootPaths().contains(rootPath);
}

@Override
default boolean hasElementsAccessControl() {
return true;
}

default Optional<AccessControlInfos> getAccessControlInfos(@NonNull ServerHttpRequest request) {
RequestPath path = Objects.requireNonNull(request.getPath());
UUID elementUuid = getElementUuidIfExist(path);

// /<elements>/{elementUuid} or /<elements>/**?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<String> ids = request.getQueryParams().get(QUERY_PARAM_IDS);
List<UUID> 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();
}
}
67 changes: 0 additions & 67 deletions src/main/java/org/gridsuite/gateway/endpoints/ExploreServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <slimane.amar at rte-france.com>
*/
@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";

Expand All @@ -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();
Expand All @@ -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<String> 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<AccessControlInfos> 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<UUID> 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();
}
}
}

0 comments on commit f67b6c1

Please sign in to comment.