1 /*******************************************************************************
2 * Copyright (c) 2017 Politecnico di Torino and others.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Apache License, Version 2.0
6 * which accompanies this distribution, and is available at
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *******************************************************************************/
10 package it.polito.escape.verify.service;
12 import java.lang.reflect.InvocationTargetException;
13 import java.lang.reflect.Method;
14 import java.util.Iterator;
15 import java.util.Map.Entry;
17 import org.apache.commons.lang3.text.WordUtils;
19 import com.fasterxml.jackson.databind.JsonNode;
21 import it.polito.escape.verify.exception.BadRequestException;
22 import it.polito.escape.verify.model.Graph;
23 import it.polito.escape.verify.model.Node;
25 public class JsonValidationService {
27 private Graph graph = new Graph();
29 private Node node = new Node();
31 public JsonValidationService() {
35 public JsonValidationService(Graph graph, Node node) {
40 public boolean validateFieldAgainstNodeNames(String value) {
41 for (Node node : this.graph.getNodes().values()) {
42 if (node.getName().equals(value))
48 public void validateFieldsAgainstNodeNames(JsonNode node) {
49 if (node.isTextual()) {
50 boolean isValid = validateFieldAgainstNodeNames(node.asText());
52 System.out.println(node.asText() + " is not a valid string!");
53 throw new BadRequestException("String '" + node.asText()
54 + "' is not valid for the configuration of node '" + this.node.getName()
59 for (JsonNode object : node) {
60 validateFieldsAgainstNodeNames(object);
63 if (node.isObject()) {
64 Iterator<Entry<String, JsonNode>> iter = node.fields();
66 while (iter.hasNext()) {
67 Entry<String, JsonNode> item = iter.next();
68 validateFieldsAgainstNodeNames(item.getValue());
74 public boolean validateNodeConfiguration() {
75 String className = WordUtils.capitalize(node.getFunctional_type()) + "Validator";
79 validator = Class.forName("it.polito.escape.verify.validation." + className);
81 catch (ClassNotFoundException e) {
82 System.out.println(className + " not found, configuration properties of node '" + node.getName()
83 + "' will be validated against node names");
89 Class<?> configurationClass;
91 graphClass = Class.forName("it.polito.escape.verify.model.Graph");
92 nodeClass = Class.forName("it.polito.escape.verify.model.Node");
93 configurationClass = Class.forName("it.polito.escape.verify.model.Configuration");
95 catch (ClassNotFoundException e) {
96 throw new RuntimeException("Model classes not found");
99 Class<?>[] paramTypes = new Class[3];
100 paramTypes[0] = graphClass;
101 paramTypes[1] = nodeClass;
102 paramTypes[2] = configurationClass;
104 String methodName = "validate";
108 instance = validator.newInstance();
110 catch (InstantiationException e) {
111 throw new RuntimeException("'" + className + "' cannot be instantiated");
113 catch (IllegalAccessException e) {
114 throw new RuntimeException("Illegal access to '" + className + "' instantiation");
119 myMethod = validator.getDeclaredMethod(methodName, paramTypes);
121 catch (NoSuchMethodException e) {
122 throw new RuntimeException("'" + methodName + "' method has to be implemented in " + className + " class");
125 myMethod.invoke(instance, graph, node, node.getConfiguration());
127 catch (IllegalAccessException e) {
128 throw new RuntimeException("Illegal access to '" + methodName + "' method in " + className + " instance");
130 catch (InvocationTargetException e) {
131 throw new BadRequestException("Validation failed for node '" + node.getName() + "': "
132 + e.getTargetException().getMessage());