Search in sources :

Example 6 with CruiseControlEndPoint

use of com.linkedin.kafka.cruisecontrol.servlet.CruiseControlEndPoint in project cruise-control by linkedin.

the class ParameterUtils method hasValidParameterNames.

/**
 * Check whether the request has valid parameter names. If not, populate the HTTP response with the corresponding
 * error message and return {@code false}, return {@code true} otherwise.
 *
 * @param request HTTP request received by Cruise Control.
 * @param response HTTP response of Cruise Control. Populated in case of an error.
 * @param config The configurations for Cruise Control.
 * @param parameters Request parameters
 * @return {@code true} if the request has valid parameter names, {@code false} otherwise (and response is populated).
 */
public static boolean hasValidParameterNames(HttpServletRequest request, HttpServletResponse response, KafkaCruiseControlConfig config, CruiseControlParameters parameters) throws IOException {
    CruiseControlEndPoint endPoint = endPoint(request);
    Set<String> validParamNames = parameters.caseInsensitiveParameterNames();
    Set<String> userParams = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
    userParams.addAll(request.getParameterMap().keySet());
    if (validParamNames != null) {
        userParams.removeAll(validParamNames);
    }
    if (!userParams.isEmpty()) {
        // User request specifies parameters that are not a subset of the valid parameters.
        String errorMessage = String.format("Unrecognized endpoint parameters in %s %s request: %s.", endPoint, request.getMethod(), userParams);
        writeErrorResponse(response, null, errorMessage, SC_BAD_REQUEST, wantJSON(request), wantResponseSchema(request), config);
        return false;
    }
    return true;
}
Also used : CruiseControlEndPoint(com.linkedin.kafka.cruisecontrol.servlet.CruiseControlEndPoint) TreeSet(java.util.TreeSet)

Example 7 with CruiseControlEndPoint

use of com.linkedin.kafka.cruisecontrol.servlet.CruiseControlEndPoint in project cruise-control by linkedin.

the class ParameterUtils method endPoints.

/**
 * Default: An empty set.
 * @param request Http servlet request.
 * @return Endpoints.
 */
public static Set<CruiseControlEndPoint> endPoints(HttpServletRequest request) throws UnsupportedEncodingException {
    Set<String> parsedEndPoints = parseParamToStringSet(request, ENDPOINTS_PARAM).stream().map(String::toUpperCase).collect(Collectors.toSet());
    Set<CruiseControlEndPoint> endPoints = new HashSet<>();
    for (CruiseControlEndPoint endPoint : CruiseControlEndPoint.cachedValues()) {
        if (parsedEndPoints.contains(endPoint.toString())) {
            endPoints.add(endPoint);
        }
    }
    return Collections.unmodifiableSet(endPoints);
}
Also used : CruiseControlEndPoint(com.linkedin.kafka.cruisecontrol.servlet.CruiseControlEndPoint) HashSet(java.util.HashSet)

Aggregations

CruiseControlEndPoint (com.linkedin.kafka.cruisecontrol.servlet.CruiseControlEndPoint)7 UserRequestException (com.linkedin.kafka.cruisecontrol.servlet.UserRequestException)2 CruiseControlParameters (com.linkedin.cruisecontrol.servlet.parameters.CruiseControlParameters)1 KafkaCruiseControlConfig (com.linkedin.kafka.cruisecontrol.config.KafkaCruiseControlConfig)1 HashSet (java.util.HashSet)1 TreeSet (java.util.TreeSet)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Before (org.junit.Before)1 Test (org.junit.Test)1