Skip to content

Commit

Permalink
KAD-61: Allow use case "owner-is-null=true"
Browse files Browse the repository at this point in the history
  • Loading branch information
CRoberto1926 committed Jul 30, 2024
1 parent bda0c72 commit 0e1a9a6
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import pro.taskana.common.api.exceptions.InvalidArgumentException;

public class QueryParamsValidator {

Expand All @@ -35,38 +34,57 @@ public static void validateParams(HttpServletRequest request, Class<?>... filter
if (!providedParams.isEmpty()) {
throw new IllegalArgumentException("Unknown request parameters found: " + providedParams);
}
checkExactParam(request, "owner-is-null");
}

public static void checkExactParam(HttpServletRequest request, String queryParameter) {
public static boolean hasQueryParameterValues(HttpServletRequest request, String queryParameter) {

Map<String, String[]> queryParametersMap = request.getParameterMap();

if (queryParametersMap.isEmpty()) {
return;
return false;
}

String[] queryParameterValues = queryParametersMap.get(queryParameter);

if (queryParameterValues == null) {
return;
return false;
}

boolean hasQueryParameterNotEmptyValues =
Arrays.stream(queryParameterValues).anyMatch(value -> !value.isBlank());

if (hasQueryParameterNotEmptyValues) {
throw new InvalidArgumentException(
"It is prohibited to use the param " + queryParameter + " with values.");
/* Workaround to manage the case "query-param=".
It should be safe enough to use because we have checked all other possibilities before. */
boolean hasQueryParameterEmptyValues = request.getQueryString().contains(queryParameter + "=");

return hasQueryParameterNotEmptyValues || hasQueryParameterEmptyValues;
}

public static boolean hasQueryParameterValuesOrIsNotTrue(
HttpServletRequest request, String queryParameter) {

Map<String, String[]> queryParametersMap = request.getParameterMap();

if (queryParametersMap.isEmpty()) {
return false;
}

String[] queryParameterValues = queryParametersMap.get(queryParameter);

if (queryParameterValues == null) {
return false;
}

boolean hasQueryParameterProhibitedValues =
Arrays.stream(queryParameterValues)
.anyMatch(value -> !value.isBlank() && !Boolean.parseBoolean(value));

/* Workaround to manage the case "query-param=".
It should be safe enough to use because we have checked all other possibilities before. */
boolean hasQueryParameterAnyValue = request.getQueryString().contains(queryParameter + "=");
boolean hasQueryParameterEmptyValues =
Arrays.stream(queryParameterValues).allMatch(String::isBlank)
&& request.getQueryString().contains(queryParameter + "=");

if (hasQueryParameterAnyValue) {
throw new InvalidArgumentException(
"It is prohibited to use the param " + queryParameter + " with values.");
}
return hasQueryParameterProhibitedValues || hasQueryParameterEmptyValues;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ public ResponseEntity<TaskSummaryPagedRepresentationModel> getTasks(
TaskQueryGroupByParameter.class,
QuerySortParameter.class,
QueryPagingParameter.class);

if (QueryParamsValidator.hasQueryParameterValuesOrIsNotTrue(request, "owner-is-null")) {
throw new InvalidArgumentException(
"It is prohibited to use the param owner-is-null with values.");
}

TaskQuery query = taskService.createTaskQuery();

filterParameter.apply(query);
Expand Down
Loading

0 comments on commit 0e1a9a6

Please sign in to comment.