Search in sources :

Example 1 with HttpServiceRequest

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);
}
Also used : BatchSource(io.cdap.cdap.etl.api.batch.BatchSource) HttpURLConnection(java.net.HttpURLConnection) JsonObject(com.google.gson.JsonObject) RemoteExecutionException(io.cdap.cdap.api.service.worker.RemoteExecutionException) PathParam(javax.ws.rs.PathParam) TypeToken(com.google.gson.reflect.TypeToken) OAuthMacroEvaluator(io.cdap.cdap.etl.common.OAuthMacroEvaluator) SchemaTypeAdapter(io.cdap.cdap.internal.io.SchemaTypeAdapter) GET(javax.ws.rs.GET) AccessException(io.cdap.cdap.api.security.AccessException) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) Bytes(io.cdap.cdap.api.common.Bytes) Function(java.util.function.Function) GsonBuilder(com.google.gson.GsonBuilder) RunnableTaskRequest(io.cdap.cdap.api.service.worker.RunnableTaskRequest) Gson(com.google.gson.Gson) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) PluginSpec(io.cdap.cdap.etl.proto.v2.spec.PluginSpec) Map(java.util.Map) ConnectionMacroEvaluator(io.cdap.cdap.etl.common.ConnectionMacroEvaluator) MacroParserOptions(io.cdap.cdap.api.macro.MacroParserOptions) RemoteTaskException(io.cdap.cdap.api.service.worker.RemoteTaskException) StageValidationRequest(io.cdap.cdap.etl.proto.v2.validation.StageValidationRequest) PipelineSpec(io.cdap.cdap.etl.proto.v2.spec.PipelineSpec) Logger(org.slf4j.Logger) POST(javax.ws.rs.POST) ImmutableMap(com.google.common.collect.ImmutableMap) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) HttpServiceResponder(io.cdap.cdap.api.service.http.HttpServiceResponder) Schema(io.cdap.cdap.api.data.schema.Schema) StandardCharsets(java.nio.charset.StandardCharsets) DefaultMacroEvaluator(io.cdap.cdap.etl.common.DefaultMacroEvaluator) AbstractSystemHttpServiceHandler(io.cdap.cdap.api.service.http.AbstractSystemHttpServiceHandler) BasicArguments(io.cdap.cdap.etl.common.BasicArguments) Type(java.lang.reflect.Type) ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) HttpServiceRequest(io.cdap.cdap.api.service.http.HttpServiceRequest) BatchPipelineSpec(io.cdap.cdap.etl.batch.BatchPipelineSpec) SecureStoreMacroEvaluator(io.cdap.cdap.etl.common.SecureStoreMacroEvaluator) StageSpec(io.cdap.cdap.etl.proto.v2.spec.StageSpec) AppRequest(io.cdap.cdap.proto.artifact.AppRequest) Collections(java.util.Collections) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) ArtifactScope(io.cdap.cdap.api.artifact.ArtifactScope) MacroEvaluator(io.cdap.cdap.api.macro.MacroEvaluator) SecureStoreMacroEvaluator(io.cdap.cdap.etl.common.SecureStoreMacroEvaluator) OAuthMacroEvaluator(io.cdap.cdap.etl.common.OAuthMacroEvaluator) ConnectionMacroEvaluator(io.cdap.cdap.etl.common.ConnectionMacroEvaluator) DefaultMacroEvaluator(io.cdap.cdap.etl.common.DefaultMacroEvaluator) SecureStoreMacroEvaluator(io.cdap.cdap.etl.common.SecureStoreMacroEvaluator) MacroEvaluator(io.cdap.cdap.api.macro.MacroEvaluator) ConnectionMacroEvaluator(io.cdap.cdap.etl.common.ConnectionMacroEvaluator) OAuthMacroEvaluator(io.cdap.cdap.etl.common.OAuthMacroEvaluator) MacroParserOptions(io.cdap.cdap.api.macro.MacroParserOptions) StageValidationRequest(io.cdap.cdap.etl.proto.v2.validation.StageValidationRequest) JsonSyntaxException(com.google.gson.JsonSyntaxException) DefaultMacroEvaluator(io.cdap.cdap.etl.common.DefaultMacroEvaluator) BasicArguments(io.cdap.cdap.etl.common.BasicArguments) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 JsonObject (com.google.gson.JsonObject)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 TypeToken (com.google.gson.reflect.TypeToken)1 ArtifactId (io.cdap.cdap.api.artifact.ArtifactId)1 ArtifactScope (io.cdap.cdap.api.artifact.ArtifactScope)1 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)1 ArtifactVersion (io.cdap.cdap.api.artifact.ArtifactVersion)1 Bytes (io.cdap.cdap.api.common.Bytes)1 Schema (io.cdap.cdap.api.data.schema.Schema)1 MacroEvaluator (io.cdap.cdap.api.macro.MacroEvaluator)1 MacroParserOptions (io.cdap.cdap.api.macro.MacroParserOptions)1 AccessException (io.cdap.cdap.api.security.AccessException)1 AbstractSystemHttpServiceHandler (io.cdap.cdap.api.service.http.AbstractSystemHttpServiceHandler)1 HttpServiceRequest (io.cdap.cdap.api.service.http.HttpServiceRequest)1 HttpServiceResponder (io.cdap.cdap.api.service.http.HttpServiceResponder)1 RemoteExecutionException (io.cdap.cdap.api.service.worker.RemoteExecutionException)1 RemoteTaskException (io.cdap.cdap.api.service.worker.RemoteTaskException)1