summaryrefslogtreecommitdiffstats
path: root/verigraph/src/main/java/it/polito/escape/verify/service/JsonValidationService.java
diff options
context:
space:
mode:
Diffstat (limited to 'verigraph/src/main/java/it/polito/escape/verify/service/JsonValidationService.java')
-rw-r--r--verigraph/src/main/java/it/polito/escape/verify/service/JsonValidationService.java136
1 files changed, 0 insertions, 136 deletions
diff --git a/verigraph/src/main/java/it/polito/escape/verify/service/JsonValidationService.java b/verigraph/src/main/java/it/polito/escape/verify/service/JsonValidationService.java
deleted file mode 100644
index 1ac6c1f..0000000
--- a/verigraph/src/main/java/it/polito/escape/verify/service/JsonValidationService.java
+++ /dev/null
@@ -1,136 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2017 Politecnico di Torino and others.
- *
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Apache License, Version 2.0
- * which accompanies this distribution, and is available at
- * http://www.apache.org/licenses/LICENSE-2.0
- *******************************************************************************/
-
-package it.polito.escape.verify.service;
-
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.util.Iterator;
-import java.util.Map.Entry;
-
-import org.apache.commons.lang3.text.WordUtils;
-
-import com.fasterxml.jackson.databind.JsonNode;
-
-import it.polito.escape.verify.exception.BadRequestException;
-import it.polito.escape.verify.model.Graph;
-import it.polito.escape.verify.model.Node;
-
-public class JsonValidationService {
-
- private Graph graph = new Graph();
-
- private Node node = new Node();
-
- public JsonValidationService() {
-
- }
-
- public JsonValidationService(Graph graph, Node node) {
- this.graph = graph;
- this.node = node;
- }
-
- public boolean validateFieldAgainstNodeNames(String value) {
- for (Node node : this.graph.getNodes().values()) {
- if (node.getName().equals(value))
- return true;
- }
- return false;
- }
-
- public void validateFieldsAgainstNodeNames(JsonNode node) {
- if (node.isTextual()) {
- boolean isValid = validateFieldAgainstNodeNames(node.asText());
- if (!isValid) {
- System.out.println(node.asText() + " is not a valid string!");
- throw new BadRequestException("String '" + node.asText()
- + "' is not valid for the configuration of node '" + this.node.getName()
- + "'");
- }
- }
- if (node.isArray()) {
- for (JsonNode object : node) {
- validateFieldsAgainstNodeNames(object);
- }
- }
- if (node.isObject()) {
- Iterator<Entry<String, JsonNode>> iter = node.fields();
-
- while (iter.hasNext()) {
- Entry<String, JsonNode> item = iter.next();
- validateFieldsAgainstNodeNames(item.getValue());
- }
- }
-
- }
-
- public boolean validateNodeConfiguration() {
- String className = WordUtils.capitalize(node.getFunctional_type()) + "Validator";
-
- Class<?> validator;
- try {
- validator = Class.forName("it.polito.escape.verify.validation." + className);
- }
- catch (ClassNotFoundException e) {
- System.out.println(className + " not found, configuration properties of node '" + node.getName()
- + "' will be validated against node names");
- return false;
- }
-
- Class<?> graphClass;
- Class<?> nodeClass;
- Class<?> configurationClass;
- try {
- graphClass = Class.forName("it.polito.escape.verify.model.Graph");
- nodeClass = Class.forName("it.polito.escape.verify.model.Node");
- configurationClass = Class.forName("it.polito.escape.verify.model.Configuration");
- }
- catch (ClassNotFoundException e) {
- throw new RuntimeException("Model classes not found");
- }
-
- Class<?>[] paramTypes = new Class[3];
- paramTypes[0] = graphClass;
- paramTypes[1] = nodeClass;
- paramTypes[2] = configurationClass;
-
- String methodName = "validate";
-
- Object instance;
- try {
- instance = validator.newInstance();
- }
- catch (InstantiationException e) {
- throw new RuntimeException("'" + className + "' cannot be instantiated");
- }
- catch (IllegalAccessException e) {
- throw new RuntimeException("Illegal access to '" + className + "' instantiation");
- }
-
- Method myMethod;
- try {
- myMethod = validator.getDeclaredMethod(methodName, paramTypes);
- }
- catch (NoSuchMethodException e) {
- throw new RuntimeException("'" + methodName + "' method has to be implemented in " + className + " class");
- }
- try {
- myMethod.invoke(instance, graph, node, node.getConfiguration());
- }
- catch (IllegalAccessException e) {
- throw new RuntimeException("Illegal access to '" + methodName + "' method in " + className + " instance");
- }
- catch (InvocationTargetException e) {
- throw new BadRequestException("Validation failed for node '" + node.getName() + "': "
- + e.getTargetException().getMessage());
- }
- return true;
- }
-}