Search in sources :

Example 1 with JsonMappingException

use of io.github.microcks.util.dispatcher.JsonMappingException in project microcks by microcks.

the class RestController method computeDispatchCriteria.

/**
 * Create a dispatchCriteria string from type, rules and request elements.
 */
private String computeDispatchCriteria(String dispatcher, String dispatcherRules, String uriPattern, String resourcePath, HttpServletRequest request, String body) {
    String dispatchCriteria = null;
    // Depending on dispatcher, evaluate request with rules.
    if (dispatcher != null) {
        switch(dispatcher) {
            case DispatchStyles.SEQUENCE:
                dispatchCriteria = DispatchCriteriaHelper.extractFromURIPattern(uriPattern, resourcePath);
                break;
            case DispatchStyles.SCRIPT:
                ScriptEngineManager sem = new ScriptEngineManager();
                try {
                    // Evaluating request with script coming from operation dispatcher rules.
                    ScriptEngine se = sem.getEngineByExtension("groovy");
                    SoapUIScriptEngineBinder.bindSoapUIEnvironment(se, body, request);
                    dispatchCriteria = (String) se.eval(dispatcherRules);
                } catch (Exception e) {
                    log.error("Error during Script evaluation", e);
                }
                break;
            case DispatchStyles.URI_PARAMS:
                String fullURI = request.getRequestURL() + "?" + request.getQueryString();
                dispatchCriteria = DispatchCriteriaHelper.extractFromURIParams(dispatcherRules, fullURI);
                break;
            case DispatchStyles.URI_PARTS:
                dispatchCriteria = DispatchCriteriaHelper.extractFromURIPattern(uriPattern, resourcePath);
                break;
            case DispatchStyles.URI_ELEMENTS:
                dispatchCriteria = DispatchCriteriaHelper.extractFromURIPattern(uriPattern, resourcePath);
                fullURI = request.getRequestURL() + "?" + request.getQueryString();
                dispatchCriteria += DispatchCriteriaHelper.extractFromURIParams(dispatcherRules, fullURI);
                break;
            case DispatchStyles.JSON_BODY:
                try {
                    JsonEvaluationSpecification specification = JsonEvaluationSpecification.buildFromJsonString(dispatcherRules);
                    dispatchCriteria = JsonExpressionEvaluator.evaluate(body, specification);
                } catch (JsonMappingException jme) {
                    log.error("Dispatching rules of operation cannot be interpreted as JsonEvaluationSpecification", jme);
                }
                break;
        }
    }
    return dispatchCriteria;
}
Also used : JsonMappingException(io.github.microcks.util.dispatcher.JsonMappingException) ScriptEngineManager(javax.script.ScriptEngineManager) JsonEvaluationSpecification(io.github.microcks.util.dispatcher.JsonEvaluationSpecification) ScriptEngine(javax.script.ScriptEngine) JsonMappingException(io.github.microcks.util.dispatcher.JsonMappingException)

Example 2 with JsonMappingException

use of io.github.microcks.util.dispatcher.JsonMappingException in project microcks by microcks.

the class GraphQLController method computeDispatchCriteria.

/**
 * Create a dispatchCriteria string from type, rules and request elements.
 */
private String computeDispatchCriteria(String dispatcher, String dispatcherRules, Selection graphqlSelection, JsonNode requestVariables, HttpServletRequest request, String body) {
    String dispatchCriteria = null;
    // Depending on dispatcher, evaluate request with rules.
    if (dispatcher != null) {
        switch(dispatcher) {
            case DispatchStyles.SCRIPT:
                ScriptEngineManager sem = new ScriptEngineManager();
                try {
                    // Evaluating request with script coming from operation dispatcher rules.
                    ScriptEngine se = sem.getEngineByExtension("groovy");
                    SoapUIScriptEngineBinder.bindSoapUIEnvironment(se, body, request);
                    dispatchCriteria = (String) se.eval(dispatcherRules);
                } catch (Exception e) {
                    log.error("Error during Script evaluation", e);
                }
                break;
            case DispatchStyles.QUERY_ARGS:
                Field field = (Field) graphqlSelection;
                Map<String, String> queryParams = new HashMap<>();
                for (Argument arg : field.getArguments()) {
                    if (arg.getValue() instanceof StringValue) {
                        queryParams.put(arg.getName(), ((StringValue) arg.getValue()).getValue());
                    } else if (arg.getValue() instanceof VariableReference && requestVariables != null) {
                        queryParams.put(arg.getName(), requestVariables.path(((VariableReference) arg.getValue()).getName()).asText(""));
                    }
                }
                dispatchCriteria = DispatchCriteriaHelper.extractFromParamMap(dispatcherRules, queryParams);
                break;
            case DispatchStyles.JSON_BODY:
                try {
                    JsonEvaluationSpecification specification = JsonEvaluationSpecification.buildFromJsonString(dispatcherRules);
                    dispatchCriteria = JsonExpressionEvaluator.evaluate(mapper.writeValueAsString(requestVariables), specification);
                } catch (JsonMappingException jme) {
                    log.error("Dispatching rules of operation cannot be interpreted as JsonEvaluationSpecification", jme);
                } catch (JsonProcessingException jpe) {
                    log.error("Request variables cannot be serialized as Json for evaluation", jpe);
                }
                break;
        }
    }
    return dispatchCriteria;
}
Also used : HashMap(java.util.HashMap) ScriptEngineManager(javax.script.ScriptEngineManager) JsonEvaluationSpecification(io.github.microcks.util.dispatcher.JsonEvaluationSpecification) ScriptEngine(javax.script.ScriptEngine) JsonMappingException(io.github.microcks.util.dispatcher.JsonMappingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonMappingException(io.github.microcks.util.dispatcher.JsonMappingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 3 with JsonMappingException

use of io.github.microcks.util.dispatcher.JsonMappingException in project microcks by microcks.

the class GrpcServerCallHandler method computeDispatchCriteria.

/**
 * Create a dispatchCriteria string from type, rules and request elements.
 */
private String computeDispatchCriteria(String dispatcher, String dispatcherRules, String jsonBody) {
    String dispatchCriteria = null;
    // Depending on dispatcher, evaluate request with rules.
    if (dispatcher != null) {
        switch(dispatcher) {
            case DispatchStyles.JSON_BODY:
                try {
                    JsonEvaluationSpecification specification = JsonEvaluationSpecification.buildFromJsonString(dispatcherRules);
                    dispatchCriteria = JsonExpressionEvaluator.evaluate(jsonBody, specification);
                } catch (JsonMappingException jme) {
                    log.error("Dispatching rules of operation cannot be interpreted as JsonEvaluationSpecification", jme);
                }
                break;
        }
    }
    return dispatchCriteria;
}
Also used : JsonMappingException(io.github.microcks.util.dispatcher.JsonMappingException) JsonEvaluationSpecification(io.github.microcks.util.dispatcher.JsonEvaluationSpecification)

Aggregations

JsonEvaluationSpecification (io.github.microcks.util.dispatcher.JsonEvaluationSpecification)3 JsonMappingException (io.github.microcks.util.dispatcher.JsonMappingException)3 ScriptEngine (javax.script.ScriptEngine)2 ScriptEngineManager (javax.script.ScriptEngineManager)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 HashMap (java.util.HashMap)1