Skip to content

Commit

Permalink
np-47469: removed strict validation code
Browse files Browse the repository at this point in the history
  • Loading branch information
torarnet committed Sep 5, 2024
1 parent 8ebf828 commit 10c77f4
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,19 @@
import nva.commons.apigateway.exceptions.BadRequestException;
import nva.commons.core.Environment;

import java.util.List;
import java.util.Set;

import static no.unit.nva.cristin.common.ErrorMessages.ALPHANUMERIC_CHARACTERS_DASH_COMMA_PERIOD_AND_WHITESPACE;
import static no.unit.nva.cristin.common.ErrorMessages.ERROR_MESSAGE_INVALID_VALUE;
import static no.unit.nva.cristin.common.ErrorMessages.invalidQueryParametersMessage;
import static no.unit.nva.cristin.common.ErrorMessages.validQueryParameterNamesMessage;
import static no.unit.nva.cristin.model.Constants.DEFAULT_NUMBER_OF_RESULTS;
import static no.unit.nva.cristin.model.Constants.FIRST_PAGE;
import static no.unit.nva.cristin.model.JsonPropertyNames.NAME;
import static no.unit.nva.cristin.model.JsonPropertyNames.NUMBER_OF_RESULTS;
import static no.unit.nva.cristin.model.JsonPropertyNames.PAGE;
import static no.unit.nva.cristin.model.JsonPropertyNames.QUERY;

public abstract class CristinQueryHandler<I, O> extends CristinHandler<I, O> {

private static final Set<String> VALID_QUERY_PARAMETERS = Set.of(QUERY, PAGE, NUMBER_OF_RESULTS);
private static final List<Character> VALID_SPECIAL_CHARS = List.of('-', ',', '.');

public CristinQueryHandler(Class<I> iclass, Environment environment) {
super(iclass, environment);
Expand Down Expand Up @@ -50,30 +45,4 @@ protected String getValidNumberOfResults(RequestInfo requestInfo) throws BadRequ
throw new BadRequestException(String.format(ERROR_MESSAGE_INVALID_VALUE, NUMBER_OF_RESULTS));
}

protected String getValidQuery(RequestInfo requestInfo) throws BadRequestException {
return requestInfo.getQueryParameterOpt(QUERY)
.filter(this::isValidQueryString)
.orElseThrow(() -> new BadRequestException(invalidQueryParametersMessage(
QUERY, ALPHANUMERIC_CHARACTERS_DASH_COMMA_PERIOD_AND_WHITESPACE)));
}

protected String getValidName(RequestInfo requestInfo) throws BadRequestException {
return requestInfo.getQueryParameterOpt(NAME)
.filter(this::isValidQueryString)
.orElseThrow(() -> new BadRequestException(
invalidQueryParametersMessage(NAME, ALPHANUMERIC_CHARACTERS_DASH_COMMA_PERIOD_AND_WHITESPACE)));
}

protected boolean isValidQueryString(String str) {
for (Character c : str.toCharArray()) {
if (isUnsupportedCharacter(c)) {
return false;
}
}
return true;
}

private boolean isUnsupportedCharacter(Character c) {
return !Character.isLetterOrDigit(c) && !Character.isWhitespace(c) && !VALID_SPECIAL_CHARS.contains(c);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static no.unit.nva.cristin.model.JsonPropertyNames.NUMBER_OF_RESULTS;
import static no.unit.nva.cristin.model.JsonPropertyNames.PAGE;
import static no.unit.nva.cristin.model.JsonPropertyNames.QUERY;
import static nva.commons.core.attempt.Try.attempt;
import com.amazonaws.services.lambda.runtime.Context;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -65,7 +64,7 @@ private ConcurrentHashMap<String, String> parseQueryParams(RequestInfo requestIn

var queryParams = new ConcurrentHashMap<String, String>();

getValidQueryOpt(requestInfo).ifPresent(query -> queryParams.put(QUERY, query));
requestInfo.getQueryParameterOpt(QUERY).ifPresent(query -> queryParams.put(QUERY, query));

getValidPageOpt(requestInfo).ifPresentOrElse(
page -> queryParams.put(PAGE, page),
Expand All @@ -80,16 +79,14 @@ private ConcurrentHashMap<String, String> parseQueryParams(RequestInfo requestIn
return queryParams;
}

private Optional<String> getValidQueryOpt(RequestInfo requestInfo) {
return attempt(() -> super.getValidQuery(requestInfo)).toOptional();
}

private Optional<String> getValidPageOpt(RequestInfo requestInfo) {
return requestInfo.getQueryParameterOpt(PAGE).filter(Utils::isPositiveInteger);
return requestInfo.getQueryParameterOpt(PAGE)
.filter(Utils::isPositiveInteger);
}

private Optional<String> getValidResultsPerPageOpt(RequestInfo requestInfo) {
return requestInfo.getQueryParameterOpt(NUMBER_OF_RESULTS).filter(Utils::isPositiveInteger);
return requestInfo.getQueryParameterOpt(NUMBER_OF_RESULTS)
.filter(Utils::isPositiveInteger);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ private ConcurrentHashMap<String, String> extractQueryParameters(RequestInfo req
return requestQueryParams;
}

@Override
protected String getValidQuery(RequestInfo requestInfo) throws BadRequestException {
return requestInfo.getQueryParameterOpt(QUERY)
.orElseThrow(() -> new BadRequestException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ private Map<String, String> extractQueryParameters(RequestInfo requestInfo) thro
getSort(requestInfo));
}

@Override
protected String getValidName(RequestInfo requestInfo) {
return requestInfo.getQueryParameterOpt(NAME)
.map(UriUtils::decodeUri)
Expand Down

0 comments on commit 10c77f4

Please sign in to comment.