1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.openehealth.ipf.platform.camel.hl7.validation;
17
18 import ca.uhn.hl7v2.DefaultHapiContext;
19 import ca.uhn.hl7v2.HL7Exception;
20 import ca.uhn.hl7v2.HapiContext;
21 import ca.uhn.hl7v2.Severity;
22 import ca.uhn.hl7v2.model.Message;
23 import ca.uhn.hl7v2.parser.GenericParser;
24 import ca.uhn.hl7v2.validation.MessageRule;
25 import ca.uhn.hl7v2.validation.ValidationContext;
26 import ca.uhn.hl7v2.validation.impl.ValidationContextFactory;
27 import org.apache.camel.Exchange;
28 import org.apache.camel.Processor;
29 import org.openehealth.ipf.commons.core.modules.api.ValidationException;
30 import org.openehealth.ipf.gazelle.validation.core.CachingGazelleProfileRule;
31 import org.openehealth.ipf.gazelle.validation.profile.ConformanceProfile;
32 import org.openehealth.ipf.gazelle.validation.profile.HL7v2Transactions;
33 import org.openehealth.ipf.gazelle.validation.profile.store.GazelleProfileStore;
34 import org.openehealth.ipf.modules.hl7.parser.DefaultEscaping;
35 import org.openehealth.ipf.platform.camel.core.adapter.ValidatorAdapter;
36
37 import java.util.ArrayList;
38 import java.util.List;
39
40 import static java.util.Objects.requireNonNull;
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public final class ConformanceProfileValidators {
55
56 private static final HapiContext FALLBACK_HAPI_CONTEXT;
57
58 static {
59 FALLBACK_HAPI_CONTEXT = new DefaultHapiContext();
60 FALLBACK_HAPI_CONTEXT.setProfileStore(new GazelleProfileStore());
61 FALLBACK_HAPI_CONTEXT.setValidationContext((ValidationContext) ValidationContextFactory.noValidation());
62 FALLBACK_HAPI_CONTEXT.getParserConfiguration().setEscaping(DefaultEscaping.INSTANCE);
63 }
64
65 private ConformanceProfileValidators() {
66 }
67
68
69
70
71
72
73
74 public static Processor validatingProcessor(final ConformanceProfile conformanceProfile) {
75 return new Processor() {
76
77 private CachingGazelleProfileRule rule = new CachingGazelleProfileRule(conformanceProfile);
78
79 @Override
80 public void process(Exchange exchange) throws Exception {
81 if (ValidatorAdapter.validationEnabled(exchange)) {
82 doValidate(bodyMessage(exchange), rule);
83 }
84 }
85 };
86 }
87
88
89
90
91
92
93
94
95 public static Processor validatingProcessor(final HL7v2Transactions iheTransaction) {
96 return new Processor() {
97
98 private CachingGazelleProfileRule rule = new CachingGazelleProfileRule(iheTransaction);
99
100 @Override
101 public void process(Exchange exchange) throws Exception {
102 if (ValidatorAdapter.validationEnabled(exchange)) {
103 doValidate(bodyMessage(exchange), rule);
104 }
105 }
106 };
107 }
108
109 private static void doValidate(Message message, final MessageRule validator) {
110 throwIPFValidationException(validator.apply(message));
111 }
112
113 private static void throwIPFValidationException(ca.uhn.hl7v2.validation.ValidationException... exceptions) {
114 List<ca.uhn.hl7v2.validation.ValidationException> fatalExceptions = new ArrayList<>();
115 if (exceptions != null) {
116 for (ca.uhn.hl7v2.validation.ValidationException exception : exceptions) {
117 if (exception.getSeverity().equals(Severity.ERROR)) {
118 fatalExceptions.add(exception);
119 }
120 }
121 }
122 if (!fatalExceptions.isEmpty()) {
123 throw new ValidationException("Message validation failed", fatalExceptions);
124 }
125 }
126
127
128
129
130
131
132
133
134
135 private static Message bodyMessage(Exchange exchange) throws HL7Exception {
136 Object body = exchange.getIn().getBody();
137 Message message;
138
139 if (body instanceof Message) {
140 message = (Message) body;
141 } else if (body instanceof String) {
142 HapiContext context = exchange.getIn().getHeader("CamelHL7Context", HapiContext.class);
143 context = context != null ? context : FALLBACK_HAPI_CONTEXT;
144 message = new GenericParser(context).parse((String) body);
145 } else {
146
147 message = exchange.getIn().getBody(Message.class);
148 }
149 requireNonNull(message, "Exchange does not contain or can be converted to the required 'ca.uhn.hl7v2.model.Message' type");
150 return message;
151 }
152
153 }