use of io.cdap.cdap.api.service.http.HttpServiceRequest in project cdap by caskdata.
the class ValidationHandler method validateLocally.
private void validateLocally(HttpServiceRequest request, HttpServiceResponder responder, String namespace) throws IOException {
StageValidationRequest validationRequest;
try {
validationRequest = GSON.fromJson(StandardCharsets.UTF_8.decode(request.getContent()).toString(), StageValidationRequest.class);
validationRequest.validate();
} catch (JsonSyntaxException e) {
responder.sendError(HttpURLConnection.HTTP_BAD_REQUEST, "Unable to decode request body: " + e.getMessage());
return;
} catch (IllegalArgumentException e) {
responder.sendError(HttpURLConnection.HTTP_BAD_REQUEST, "Invalid stage config: " + e.getMessage());
return;
}
Map<String, String> arguments = Collections.emptyMap();
// this option.
if (validationRequest.getResolveMacrosFromPreferences()) {
try {
arguments = getContext().getPreferencesForNamespace(namespace, true);
} catch (IllegalArgumentException iae) {
// If this method returns IllegalArgumentException, it means the namespace doesn't exist.
// If this is the case, we return a 404 error.
responder.sendError(HttpURLConnection.HTTP_NOT_FOUND, String.format("Namespace '%s' does not exist", namespace));
return;
}
}
Map<String, MacroEvaluator> evaluators = ImmutableMap.of(SecureStoreMacroEvaluator.FUNCTION_NAME, new SecureStoreMacroEvaluator(namespace, getContext()), OAuthMacroEvaluator.FUNCTION_NAME, new OAuthMacroEvaluator(getContext()), ConnectionMacroEvaluator.FUNCTION_NAME, new ConnectionMacroEvaluator(namespace, getContext()));
MacroEvaluator macroEvaluator = new DefaultMacroEvaluator(new BasicArguments(arguments), evaluators, DefaultMacroEvaluator.MAP_FUNCTIONS);
MacroParserOptions macroParserOptions = MacroParserOptions.builder().skipInvalidMacros().setEscaping(false).setFunctionWhitelist(evaluators.keySet()).build();
Function<Map<String, String>, Map<String, String>> macroFn = macroProperties -> getContext().evaluateMacros(namespace, macroProperties, macroEvaluator, macroParserOptions);
String validationResponse = GSON.toJson(ValidationUtils.validate(namespace, validationRequest, getContext().createServicePluginConfigurer(namespace), macroFn, getContext()));
responder.sendString(validationResponse);
}
Aggregations