blob: 6cc2224c60584bf7299ccc54b0984158aac04b29 [file] [log] [blame]
Michael Hanle8031912016-06-24 14:36:17 +02001package de.ids_mannheim.korap.interfaces.defaults;
2
3import de.ids_mannheim.korap.config.Attributes;
4import de.ids_mannheim.korap.config.ConfigLoader;
5import de.ids_mannheim.korap.exceptions.KustvaktException;
6import de.ids_mannheim.korap.exceptions.StatusCodes;
7import de.ids_mannheim.korap.interfaces.ValidatorIface;
8import de.ids_mannheim.korap.web.utils.KustvaktMap;
9import org.apache.commons.validator.routines.*;
10import org.apache.commons.validator.routines.RegexValidator;
margaretha49cb6882018-07-04 04:19:54 +020011import org.apache.logging.log4j.LogManager;
12import org.apache.logging.log4j.Logger;
Michael Hanle8031912016-06-24 14:36:17 +020013
14import java.io.IOException;
15import java.util.*;
16
17/**
18 * Created by hanl on 09.06.16.
19 */
20public class ApacheValidator implements ValidatorIface {
21
margaretha49cb6882018-07-04 04:19:54 +020022 private static Logger jlog = LogManager.getLogger(ApacheValidator.class);
Michael Hanle8031912016-06-24 14:36:17 +020023
margaretha7e16d6f2017-04-18 18:01:59 +020024 private static final String STRING_PATTERN = "^[\\.;:,&\\|@\\[\\]\\=\\*\\/\\/_()\\-0-9\\p{L}\\p{Space}]{0,1024}$";
Michael Hanle8031912016-06-24 14:36:17 +020025
margarethaa85965d2018-12-19 15:58:21 +010026 private static final boolean DEBUG = false;
27
Michael Hanle8031912016-06-24 14:36:17 +020028 private Map<String, RegexValidator> validators;
29
margaretha7e16d6f2017-04-18 18:01:59 +020030
Michael Hanle8031912016-06-24 14:36:17 +020031 public ApacheValidator () throws IOException {
32 this.validators = load();
33 }
34
margaretha7e16d6f2017-04-18 18:01:59 +020035
Michael Hanle8031912016-06-24 14:36:17 +020036 private static Map<String, RegexValidator> load () throws IOException {
37 Map<String, RegexValidator> validatorMap = new HashMap<>();
38 Properties p = ConfigLoader.loadProperties("validation.properties");
39
40 for (String property : p.stringPropertyNames()) {
41 if (property.startsWith("Validator")) {
42 String name = property.replace("Validator.", "");
margaretha7e16d6f2017-04-18 18:01:59 +020043 RegexValidator v = new RegexValidator(
44 p.get(property).toString());
Michael Hanle8031912016-06-24 14:36:17 +020045 validatorMap.put(name, v);
46 }
47 }
48 return validatorMap;
49 }
50
51
52
53 @Override
margaretha7e16d6f2017-04-18 18:01:59 +020054 public Map<String, Object> validateMap (Map<String, Object> map)
55 throws KustvaktException {
Michael Hanle8031912016-06-24 14:36:17 +020056 Map<String, Object> safeMap = new HashMap<>();
57 KustvaktMap kmap = new KustvaktMap(map);
58
59 if (map != null) {
margaretha7e16d6f2017-04-18 18:01:59 +020060 loop: for (String key : kmap.keySet()) {
61 Object value = kmap.getRaw(key);
62 if (value instanceof List) {
63 List list = (List) value;
64 for (int i = 0; i < list.size(); i++) {
65 if (!isValid(String.valueOf(list.get(i)), key)) {
66 // list.remove(i);
67 throw new KustvaktException(
68 StatusCodes.ILLEGAL_ARGUMENT,
69 "The value for the parameter " + key
70 + " is not valid or acceptable.");
Michael Hanle8031912016-06-24 14:36:17 +020071 }
Michael Hanle8031912016-06-24 14:36:17 +020072 }
margaretha7e16d6f2017-04-18 18:01:59 +020073
74 if (list.size() == 1)
75 value = list.get(0);
76 else
77 value = list;
Michael Hanle8031912016-06-24 14:36:17 +020078 }
margaretha7e16d6f2017-04-18 18:01:59 +020079 else {
80 if (!isValid(kmap.get(key), key))
81 continue loop;
82 }
83 safeMap.put(key, value);
Michael Hanle8031912016-06-24 14:36:17 +020084 }
margaretha7e16d6f2017-04-18 18:01:59 +020085 }
Michael Hanle8031912016-06-24 14:36:17 +020086 return safeMap;
87 }
88
89
90 @Override
91 public String validateEntry (String input, String type)
92 throws KustvaktException {
93 if (!isValid(input, type))
94 throw new KustvaktException(StatusCodes.ILLEGAL_ARGUMENT,
95 "Entry did not validate for type '" + type + "'", input);
96 return input;
97 }
98
margaretha7e16d6f2017-04-18 18:01:59 +020099
Michael Hanle8031912016-06-24 14:36:17 +0200100 @Override
margaretha7e16d6f2017-04-18 18:01:59 +0200101 public boolean isValid (String input, String type) {
Michael Hanle8031912016-06-24 14:36:17 +0200102 boolean valid = false;
103 RegexValidator validator = this.validators.get(type);
104 if (validator != null) {
105 valid = validator.isValid(input);
margaretha7e16d6f2017-04-18 18:01:59 +0200106 }
107 else {
Michael Hanle8031912016-06-24 14:36:17 +0200108 if (Attributes.EMAIL.equals(type)) {
109 valid = EmailValidator.getInstance().isValid(input);
margaretha7e16d6f2017-04-18 18:01:59 +0200110 }
111 else if ("date".equals(type)) {
Michael Hanle8031912016-06-24 14:36:17 +0200112 valid = DateValidator.getInstance().isValid(input);
margaretha7e16d6f2017-04-18 18:01:59 +0200113 }
114 else if ("string".equals(type)
115 && !this.validators.containsKey("string")) {
Michael Hanle8031912016-06-24 14:36:17 +0200116 RegexValidator regex = new RegexValidator(STRING_PATTERN);
117 valid = regex.isValid(input);
118 }
119 else
120 return this.isValid(input, "string");
121 }
margarethaa85965d2018-12-19 15:58:21 +0100122 if (DEBUG){
123 jlog.debug("validating entry "+input+" of type "+type+": "+ (
124 valid ? "Is valid!" : "Is not valid!"));
125 }
126
Michael Hanle8031912016-06-24 14:36:17 +0200127 return valid;
128 }
129}