1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.openehealth.ipf.platform.camel.core.adapter;
17
18 import static org.openehealth.ipf.platform.camel.core.util.Exchanges.prepareResult;
19
20 import java.io.IOException;
21
22 import org.apache.camel.Exchange;
23 import org.apache.camel.Expression;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.openehealth.ipf.commons.core.modules.api.Validator;
27
28
29
30
31
32
33 public class ValidatorAdapter extends ProcessorAdapter {
34 private static final transient Logger LOG = LoggerFactory.getLogger(ValidatorAdapter.class.getName());
35
36
37
38
39
40 public static final String NEED_VALIDATION_HEADER_NAME =
41 ValidatorAdapter.class.getName() + ".need.validation";
42
43 private final Validator validator;
44
45 private Object profile;
46
47 private Expression profileExpression;
48
49
50
51
52
53
54
55
56 public ValidatorAdapter(Validator validator) {
57 this.validator = validator;
58 }
59
60
61
62
63
64
65
66
67 public ValidatorAdapter staticProfile(Object profile) {
68 this.profile = profile;
69 return this;
70 }
71
72
73
74
75
76
77
78
79 @Deprecated
80 public ValidatorAdapter profile(Object profile) {
81 this.profile = profile;
82 return this;
83 }
84
85
86
87
88
89
90
91
92
93
94
95 public ValidatorAdapter profile(Expression profileExpression) {
96 this.profileExpression = profileExpression;
97 return this;
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 @Override
114 protected void doProcess(Exchange exchange, Object inputData, Object... inputParams) throws IOException {
115 prepareResult(exchange);
116 if (validationEnabled(exchange)) {
117 validator.validate(inputData, getProfile(exchange));
118 }
119 }
120
121 private Object getProfile(Exchange exchange) {
122 if (profileExpression != null) {
123 return profileExpression.evaluate(exchange, Object.class);
124 } else {
125 return profile;
126 }
127 }
128
129
130
131
132
133
134
135
136
137 public static boolean validationEnabled(Exchange exchange) {
138 if (Boolean.FALSE.equals(exchange.getIn().getHeader(NEED_VALIDATION_HEADER_NAME, Boolean.class))) {
139 LOG.debug("Validation disabled");
140 return false;
141 }
142 return true;
143 }
144 }