1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.openehealth.ipf.platform.camel.core.closures;
17
18 import groovy.lang.Closure;
19
20 import org.openehealth.ipf.commons.core.modules.api.ValidationException;
21 import org.openehealth.ipf.commons.core.modules.api.Validator;
22
23
24
25
26
27
28 public class DelegatingValidator extends ClosureAdapter implements Validator<Object, Object> {
29
30 public DelegatingValidator(Closure closure) {
31 super(closure);
32 }
33
34 @Override
35 public void validate(Object message, Object profile) {
36 Object result;
37 result = validateInternal(message, profile);
38 if (!(result instanceof Boolean)) {
39 return;
40 }
41 if (!(Boolean)result) {
42 throw new ValidationException("validation closure returned false");
43 }
44 }
45
46 private Object validateInternal(Object message, Object profile) {
47 if (getClosure().getParameterTypes().length == 2) {
48 return call(message, profile);
49 } else {
50 return call(message);
51 }
52 }
53
54 }