Skip to content

Commit

Permalink
Add validation to check if is valid parameters for search request
Browse files Browse the repository at this point in the history
Clear unused code.
  • Loading branch information
steniobhz committed Nov 14, 2024
1 parent 4a4fbff commit 27f561f
Showing 1 changed file with 13 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ public void registerListenerRegistrationHandler(Handler<Void> readyHandler) {
public boolean handle(final RoutingContext ctx) {
HttpServerRequest request = ctx.request();
boolean consumed = false;
var requestUri = request.uri().replaceAll("/+$", "");;
var requestUri = request.uri().replaceAll("/+$", "");
/*
* 1) Un- / Register Listener / Routes
*/
Expand Down Expand Up @@ -577,15 +577,10 @@ public boolean handle(final RoutingContext ctx) {

if (requestMethod == GET && !request.params().isEmpty()) {
String queryParam = request.getParam("q");
if (request.params().size() > 1 || (queryParam == null) || (queryParam.isEmpty())) {
request.response().setStatusCode(StatusCode.BAD_REQUEST.getStatusCode())
.end("Bad Request: Only the 'q' parameter is allowed and can't be empty or null");
return true;
}
if (requestUri.contains(normalizedListenerBase)) {
if (requestUri.contains(normalizedListenerBase) && checkValidParams(request,queryParam)) {
handleListenerSearch(queryParam, request.response());
return true;
} else if (requestUri.contains(normalizedRouteBase)) {
} else if (requestUri.contains(normalizedRouteBase) && checkValidParams(request,queryParam)) {
handleRouteSearch(queryParam, request.response());
return true;
}
Expand Down Expand Up @@ -614,6 +609,15 @@ public boolean handle(final RoutingContext ctx) {
}
}

private boolean checkValidParams (HttpServerRequest request, String queryParam){
if (request.params().size() > 1 || (queryParam == null) || (queryParam.isEmpty())) {
request.response().setStatusCode(StatusCode.BAD_REQUEST.getStatusCode())
.end("Bad Request: Only the 'q' parameter is allowed and can't be empty or null");
return true;
}
return false;
}

private void handleListenerSearch(String queryParam, HttpServerResponse response) {
handleSearch(
listenerRepository.getListeners().stream().collect(Collectors.toMap(Listener::getListenerId, listener -> listener)),
Expand Down Expand Up @@ -646,8 +650,8 @@ private void handleRouteSearch(String queryParam, HttpServerResponse response) {
* @param response The HTTP response to return the results. Must not be null.
*/
private <T> void handleSearch(Map<String, T> repository, Function<T, String> getDestination, String queryParam, String resultKey, HttpServerResponse response) {
JsonArray matchingResults = new JsonArray();

JsonArray matchingResults = new JsonArray();
repository.forEach((key, value) -> {
String destination = getDestination.apply(value);
if (destination != null && destination.contains(queryParam)) {
Expand Down Expand Up @@ -1691,25 +1695,6 @@ private void registerRoute(Buffer buffer) {
monitoringHandler.updateRoutesCount(routeRepository.getRoutes().size());
}

/**
* Extracts the route name segment from the given request URL after the HOOKS_ROUTE_URI_PART.
* Returns an empty string if there is no route segment to extract.
*
* @param requestUrl the full URL of the request
* @return the extracted route name or an empty string if no route name is present
*/
private String extractRouteName(String requestUrl) {
int startIdx = requestUrl.indexOf(HOOKS_ROUTE_URI_PART);
if (requestUrl.isEmpty() || startIdx == -1) {
return "";
}
startIdx += HOOKS_ROUTE_URI_PART.length();
if (startIdx < requestUrl.length()) {
return requestUrl.substring(startIdx);
}
return "";
}

/**
* check if an existing route must be thrown away because the new Hook does not match the config of the existing Route
*
Expand Down

0 comments on commit 27f561f

Please sign in to comment.