blob: cbf7ffcea071a87bdb6a8d3a7a88621c943f90b0 [file] [log] [blame]
margaretha6cd27f32019-01-24 14:47:47 +01001package de.ids_mannheim.korap.validator;
2
3import java.io.IOException;
4import java.util.HashMap;
5import java.util.List;
6import java.util.Map;
7import java.util.Properties;
8
9import org.apache.commons.validator.routines.DateValidator;
10import org.apache.commons.validator.routines.EmailValidator;
11import org.apache.commons.validator.routines.RegexValidator;
12import org.apache.logging.log4j.LogManager;
13import org.apache.logging.log4j.Logger;
Michael Hanle8031912016-06-24 14:36:17 +020014
15import de.ids_mannheim.korap.config.Attributes;
16import de.ids_mannheim.korap.config.ConfigLoader;
17import de.ids_mannheim.korap.exceptions.KustvaktException;
18import de.ids_mannheim.korap.exceptions.StatusCodes;
Michael Hanle8031912016-06-24 14:36:17 +020019import de.ids_mannheim.korap.web.utils.KustvaktMap;
Michael Hanle8031912016-06-24 14:36:17 +020020
21/**
22 * Created by hanl on 09.06.16.
margaretha35e1ca22023-11-16 22:00:01 +010023 *
Michael Hanle8031912016-06-24 14:36:17 +020024 */
margaretha6cd27f32019-01-24 14:47:47 +010025public class ApacheValidator implements Validator {
Michael Hanle8031912016-06-24 14:36:17 +020026
margaretha49cb6882018-07-04 04:19:54 +020027 private static Logger jlog = LogManager.getLogger(ApacheValidator.class);
Michael Hanle8031912016-06-24 14:36:17 +020028
margaretha7e16d6f2017-04-18 18:01:59 +020029 private static final String STRING_PATTERN = "^[\\.;:,&\\|@\\[\\]\\=\\*\\/\\/_()\\-0-9\\p{L}\\p{Space}]{0,1024}$";
Michael Hanle8031912016-06-24 14:36:17 +020030
margarethaa85965d2018-12-19 15:58:21 +010031 private static final boolean DEBUG = false;
32
Michael Hanle8031912016-06-24 14:36:17 +020033 private Map<String, RegexValidator> validators;
34
35 public ApacheValidator () throws IOException {
36 this.validators = load();
37 }
38
39 private static Map<String, RegexValidator> load () throws IOException {
40 Map<String, RegexValidator> validatorMap = new HashMap<>();
41 Properties p = ConfigLoader.loadProperties("validation.properties");
42
43 for (String property : p.stringPropertyNames()) {
44 if (property.startsWith("Validator")) {
45 String name = property.replace("Validator.", "");
margaretha7e16d6f2017-04-18 18:01:59 +020046 RegexValidator v = new RegexValidator(
47 p.get(property).toString());
Michael Hanle8031912016-06-24 14:36:17 +020048 validatorMap.put(name, v);
49 }
50 }
51 return validatorMap;
52 }
53
Michael Hanle8031912016-06-24 14:36:17 +020054 @Override
margaretha7e16d6f2017-04-18 18:01:59 +020055 public Map<String, Object> validateMap (Map<String, Object> map)
56 throws KustvaktException {
Michael Hanle8031912016-06-24 14:36:17 +020057 Map<String, Object> safeMap = new HashMap<>();
58 KustvaktMap kmap = new KustvaktMap(map);
59
60 if (map != null) {
margaretha7e16d6f2017-04-18 18:01:59 +020061 loop: for (String key : kmap.keySet()) {
62 Object value = kmap.getRaw(key);
63 if (value instanceof List) {
64 List list = (List) value;
65 for (int i = 0; i < list.size(); i++) {
66 if (!isValid(String.valueOf(list.get(i)), key)) {
67 // list.remove(i);
68 throw new KustvaktException(
69 StatusCodes.ILLEGAL_ARGUMENT,
70 "The value for the parameter " + key
71 + " is not valid or acceptable.");
Michael Hanle8031912016-06-24 14:36:17 +020072 }
Michael Hanle8031912016-06-24 14:36:17 +020073 }
margaretha7e16d6f2017-04-18 18:01:59 +020074
75 if (list.size() == 1)
76 value = list.get(0);
77 else
78 value = list;
Michael Hanle8031912016-06-24 14:36:17 +020079 }
margaretha7e16d6f2017-04-18 18:01:59 +020080 else {
81 if (!isValid(kmap.get(key), key))
82 continue loop;
83 }
84 safeMap.put(key, value);
Michael Hanle8031912016-06-24 14:36:17 +020085 }
margaretha7e16d6f2017-04-18 18:01:59 +020086 }
Michael Hanle8031912016-06-24 14:36:17 +020087 return safeMap;
88 }
89
Michael Hanle8031912016-06-24 14:36:17 +020090 @Override
91 public String validateEntry (String input, String type)
92 throws KustvaktException {
93 if (!isValid(input, type))
margaretha6cd27f32019-01-24 14:47:47 +010094 throw new KustvaktException(StatusCodes.INVALID_ARGUMENT,
Michael Hanle8031912016-06-24 14:36:17 +020095 "Entry did not validate for type '" + type + "'", input);
96 return input;
97 }
98
99 @Override
margaretha7e16d6f2017-04-18 18:01:59 +0200100 public boolean isValid (String input, String type) {
Michael Hanle8031912016-06-24 14:36:17 +0200101 boolean valid = false;
102 RegexValidator validator = this.validators.get(type);
103 if (validator != null) {
104 valid = validator.isValid(input);
margaretha7e16d6f2017-04-18 18:01:59 +0200105 }
106 else {
Michael Hanle8031912016-06-24 14:36:17 +0200107 if (Attributes.EMAIL.equals(type)) {
108 valid = EmailValidator.getInstance().isValid(input);
margaretha7e16d6f2017-04-18 18:01:59 +0200109 }
110 else if ("date".equals(type)) {
Michael Hanle8031912016-06-24 14:36:17 +0200111 valid = DateValidator.getInstance().isValid(input);
margaretha7e16d6f2017-04-18 18:01:59 +0200112 }
113 else if ("string".equals(type)
114 && !this.validators.containsKey("string")) {
Michael Hanle8031912016-06-24 14:36:17 +0200115 RegexValidator regex = new RegexValidator(STRING_PATTERN);
116 valid = regex.isValid(input);
117 }
118 else
119 return this.isValid(input, "string");
120 }
margaretha35e1ca22023-11-16 22:00:01 +0100121 if (DEBUG) {
122 jlog.debug("validating entry " + input + " of type " + type + ": "
123 + (valid ? "Is valid!" : "Is not valid!"));
margarethaa85965d2018-12-19 15:58:21 +0100124 }
margaretha35e1ca22023-11-16 22:00:01 +0100125
Michael Hanle8031912016-06-24 14:36:17 +0200126 return valid;
127 }
128}